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

Code before:

import shark.LeakingObjectFinder
import shark.MetadataExtractor
import shark.ObjectInspector
import shark.ReferenceMatcher

/**
 * 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 }


Review comment (suggestion):
I wanted to hide this class from Kotlin users as well, but then the `LeakCanary.config.newBuilder()` won't work as it won't be able to find this class. Choosing between the two, I would rather have `newBuilder()` method hidden

Code after:

import shark.LeakingObjectFinder
import shark.MetadataExtractor
import shark.ObjectInspector
import shark.ReferenceMatcher

/**
 * 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 }