All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.launchdarkly.client.EvaluationDetail Maven / Gradle / Ivy

There is a newer version: 4.61
Show newest version
package com.launchdarkly.client;

import com.google.common.base.Objects;

/**
 * An object returned by the "variation detail" methods such as {@link LDClientInterface#boolVariationDetail(String, LDUser, boolean)},
 * combining the result of a flag evaluation with an explanation of how it was calculated.
 * @since 4.3.0
 */
public class EvaluationDetail {

  private final EvaluationReason reason;
  private final Integer variationIndex;
  private final T value;
  
  public EvaluationDetail(EvaluationReason reason, Integer variationIndex, T value) {
    this.reason = reason;
    this.variationIndex = variationIndex;
    this.value = value;
  }
  
  static  EvaluationDetail error(EvaluationReason.ErrorKind errorKind, T defaultValue) {
    return new EvaluationDetail<>(EvaluationReason.error(errorKind), null, defaultValue);
  }
  
  /**
   * An object describing the main factor that influenced the flag evaluation value.
   * @return an {@link EvaluationReason}
   */
  public EvaluationReason getReason() {
    return reason;
  }

  /**
   * The index of the returned value within the flag's list of variations, e.g. 0 for the first variation -
   * or {@code null} if the default value was returned.
   * @return the variation index or null
   */
  public Integer getVariationIndex() {
    return variationIndex;
  }

  /**
   * The result of the flag evaluation. This will be either one of the flag's variations or the default
   * value that was passed to the {@code variation} method.
   * @return the flag value
   */
  public T getValue() {
    return value;
  }
  
  /**
   * Returns true if the flag evaluation returned the default value, rather than one of the flag's
   * variations.
   * @return true if this is the default value
   */
  public boolean isDefaultValue() {
    return variationIndex == null;
  }
  
  @Override
  public boolean equals(Object other) {
    if (other instanceof EvaluationDetail) {
      @SuppressWarnings("unchecked")
      EvaluationDetail o = (EvaluationDetail)other;
      return Objects.equal(reason, o.reason) && Objects.equal(variationIndex, o.variationIndex) && Objects.equal(value, o.value);
    }
    return false;
  }
  
  @Override
  public int hashCode() {
    return Objects.hashCode(reason, variationIndex, value);
  }
  
  @Override
  public String toString() {
    return "{" + reason + "," + variationIndex + "," + value + "}";
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy