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

Code before:
 * Builder for [LeakCanary.Config] intended to use only by Java callers.
 *
 * Usage:
 * ```
 * LeakCanary.Config config = LeakCanary.getConfig().newBuilder()
 *    .dumpHeap(false)
 *    .retainedVisibleThreshold(3)
 *    .maxStoredHeapDumps(10)
 *    .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 }


Review comment (suggestion):
Guess what caused build errors after rebasing on master 😄 

Code after:
 * Builder for [LeakCanary.Config] intended to be used only from Java code.
 *
 * Usage:
 * ```
 * LeakCanary.Config config = LeakCanary.getConfig().newBuilder()
 *    .dumpHeap(false)
 *    .retainedVisibleThreshold(3)
 *    .maxStoredHeapDumps(10)
 *    .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 }