Repository: square/leakcanary
File: leakcanary-android-core/src/main/java/leakcanary/ConfigBuilder.kt (Kotlin)

Code before:
 *    .build();
 * LeakCanary.setConfig(config);
 * ```
 *
 * Kotlin callers should be using `copy()` method instead:
 * ```
 * LeakCanary.config = LeakCanary.config.copy(dumpHeap = false)
 * ```
 */
@Suppress("TooManyFunctions")
class ConfigBuilder(config: LeakCanary.Config) {
  private var dumpHeap: Boolean = config.dumpHeap
  private var dumpHeapWhenDebugging: Boolean = config.dumpHeapWhenDebugging
  private var retainedVisibleThreshold: Int = config.retainedVisibleThreshold
  private var referenceMatchers: List<ReferenceMatcher> = config.referenceMatchers
  private var objectInspectors: List<ObjectInspector> = config.objectInspectors
  private var onHeapAnalyzedListener: OnHeapAnalyzedListener = config.onHeapAnalyzedListener
  private var metadataExtractor: MetadataExtractor = config.metadataExtractor
  private var computeRetainedHeapSize: Boolean = config.computeRetainedHeapSize
  private var maxStoredHeapDumps: Int = config.maxStoredHeapDumps
  private var requestWriteExternalStoragePermission: Boolean =
    config.requestWriteExternalStoragePermission
  private var leakingObjectFinder: LeakingObjectFinder = config.leakingObjectFinder

  /** @see [LeakCanary.Config.dumpHeap] */
  fun dumpHeap(value: Boolean) = apply { dumpHeap = value }

  /** @see [LeakCanary.Config.dumpHeapWhenDebugging] */
  fun dumpHeapWhenDebugging(value: Boolean) = apply { dumpHeapWhenDebugging = value }

  /** @see [LeakCanary.Config.retainedVisibleThreshold] */
  fun retainedVisibleThreshold(value: Int) = apply { retainedVisibleThreshold = value }

  /** @see [LeakCanary.Config.referenceMatchers] */
  fun referenceMatchers(value: List<ReferenceMatcher>) = apply { referenceMatchers = value }

  /** @see [LeakCanary.Config.objectInspectors] */
  fun objectInspectors(value: List<ObjectInspector>) = apply { objectInspectors = value }

  /** @see [LeakCanary.Config.onHeapAnalyzedListener] */
  fun onHeapAnalyzedListener(value: OnHeapAnalyzedListener) =
    apply { onHeapAnalyzedListener = value }

  /** @see [LeakCanary.Config.metadataExtractor] */
  fun metadataExtractor(value: MetadataExtractor) = apply { metadataExtractor = value }

  /** @see [LeakCanary.Config.computeRetainedHeapSize] */
  fun computeRetainedHeapSize(value: Boolean) = apply { computeRetainedHeapSize = value }

  /** @see [LeakCanary.Config.maxStoredHeapDumps] */
  fun maxStoredHeapDumps(value: Int) = apply { maxStoredHeapDumps = value }

Review comment (bug):
Still not sure about the parameter name here and below. `value` is short and coupled with method name is more that enough, but still feels wrong to me..

Code after:
 *    .build();
 * LeakCanary.setConfig(config);
 * ```
 *
 * For idiomatic Kotlin use `copy()` method instead:
 * ```
 * LeakCanary.config = LeakCanary.config.copy(dumpHeap = false)
 * ```
 */
@Suppress("TooManyFunctions")
class ConfigBuilder(config: LeakCanary.Config) {
  private var dumpHeap: Boolean = config.dumpHeap
  private var dumpHeapWhenDebugging: Boolean = config.dumpHeapWhenDebugging
  private var retainedVisibleThreshold: Int = config.retainedVisibleThreshold
  private var referenceMatchers: List<ReferenceMatcher> = config.referenceMatchers
  private var objectInspectors: List<ObjectInspector> = config.objectInspectors
  private var onHeapAnalyzedListener: OnHeapAnalyzedListener = config.onHeapAnalyzedListener
  private var metadataExtractor: MetadataExtractor = config.metadataExtractor
  private var computeRetainedHeapSize: Boolean = config.computeRetainedHeapSize
  private var maxStoredHeapDumps: Int = config.maxStoredHeapDumps
  private var requestWriteExternalStoragePermission: Boolean =
    config.requestWriteExternalStoragePermission
  private var leakingObjectFinder: LeakingObjectFinder = config.leakingObjectFinder

  /** @see [LeakCanary.Config.dumpHeap] */
  fun dumpHeap(dumpHeap: Boolean) =
    apply { this.dumpHeap = dumpHeap }

  /** @see [LeakCanary.Config.dumpHeapWhenDebugging] */
  fun dumpHeapWhenDebugging(dumpHeapWhenDebugging: Boolean) =
    apply { this.dumpHeapWhenDebugging = dumpHeapWhenDebugging }

  /** @see [LeakCanary.Config.retainedVisibleThreshold] */
  fun retainedVisibleThreshold(retainedVisibleThreshold: Int) =
    apply { this.retainedVisibleThreshold = retainedVisibleThreshold }

  /** @see [LeakCanary.Config.referenceMatchers] */
  fun referenceMatchers(referenceMatchers: List<ReferenceMatcher>) =
    apply { this.referenceMatchers = referenceMatchers }

  /** @see [LeakCanary.Config.objectInspectors] */
  fun objectInspectors(objectInspectors: List<ObjectInspector>) =
    apply { this.objectInspectors = objectInspectors }

  /** @see [LeakCanary.Config.onHeapAnalyzedListener] */
  fun onHeapAnalyzedListener(onHeapAnalyzedListener: OnHeapAnalyzedListener) =
    apply { this.onHeapAnalyzedListener = onHeapAnalyzedListener }

  /** @see [LeakCanary.Config.metadataExtractor] */
  fun metadataExtractor(metadataExtractor: MetadataExtractor) =
    apply { this.metadataExtractor = metadataExtractor }