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

Code before:
  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 metatadaExtractor: MetadataExtractor = config.metatadaExtractor
  private var computeRetainedHeapSize: Boolean = config.computeRetainedHeapSize
  private var maxStoredHeapDumps: Int = config.maxStoredHeapDumps
  private var requestWriteExternalStoragePermission: Boolean = config.requestWriteExternalStoragePermission
  private var leakingObjectFinder: LeakingObjectFinder = config.leakingObjectFinder
  private var useExperimentalLeakFinders: Boolean = config.useExperimentalLeakFinders


  /**
   * Whether LeakCanary should dump the heap when enough retained instances are found. This needs
   * to be true for LeakCanary to work, but sometimes you may want to temporarily disable
   * LeakCanary (e.g. for a product demo).
   *
   * Defaults to true.
   */
  fun dumpHeap(value: Boolean) = apply { dumpHeap = value }

  /**
   * If [dumpHeapWhenDebugging] is false then LeakCanary will not dump the heap
   * when the debugger is attached. The debugger can create temporary memory leaks (for instance
   * if a thread is blocked on a breakpoint).
   *
   * Defaults to false.
   */
  fun dumpHeapWhenDebugging(value: Boolean) = apply { dumpHeapWhenDebugging = value }

  /**
   * When the app is visible, LeakCanary will wait for at least
   * [retainedVisibleThreshold] retained instances before dumping the heap. Dumping the heap
   * freezes the UI and can be frustrating for developers who are trying to work. This is
   * especially frustrating as the Android Framework has a number of leaks that cannot easily
   * be fixed.
   *
   * When the app becomes invisible, LeakCanary dumps the heap after
   * [AppWatcher.Config.watchDurationMillis] ms.
   *
   * The app is considered visible if it has at least one activity in started state.
   *
   * A higher threshold means LeakCanary will dump the heap less often, therefore it won't be
   * bothering developers as much but it could miss some leaks.
   *
   * Defaults to 5.
   */
  fun retainedVisibleThreshold(value: Int) = apply { retainedVisibleThreshold = value }


Review comment (suggestion):
Remove all javadoc and replace with `@see`, so that we don't have to update two places when we update javadoc.

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 }