main

square/leakcanary

Last updated at: 29/12/2023 09:39

ConditionalInterceptor.kt

TLDR

This file defines the ConditionalInterceptor class, which is an interceptor that runs only if a certain condition is met.

Classes

ConditionalInterceptor

This class implements the HeapAnalysisInterceptor interface and provides a way to conditionally intercept heap analysis. It takes two parameters:

  • delegate: the delegate interceptor that will be run when the condition is met.
  • evaluateCondition: a function that determines whether the condition is met or not, based on the HeapAnalysisJob.

END

package leakcanary

import leakcanary.HeapAnalysisInterceptor.Chain
import leakcanary.HeapAnalysisJob.Result

/**
 * An interceptor that runs only when [evaluateCondition] returns true.
 */
class ConditionalInterceptor(
  private val delegate: HeapAnalysisInterceptor,
  private val evaluateCondition: (HeapAnalysisJob) -> Boolean
) : HeapAnalysisInterceptor {
  override fun intercept(chain: Chain): Result {
    if (evaluateCondition(chain.job)) {
      return delegate.intercept(object : Chain {
        override val job = chain.job

        override fun proceed(): Result {
          return chain.proceed()
        }
      })
    } else {
      return chain.proceed()
    }
  }
}