|
| 1 | +package dev.openfeature.api; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | + |
| 5 | +/** |
| 6 | + * Contains information about how the provider resolved a flag, including the |
| 7 | + * resolved value. |
| 8 | + * |
| 9 | + * @param <T> the type of the flag being evaluated. |
| 10 | + */ |
| 11 | +class DefaultFlagEvaluationDetails<T> implements FlagEvaluationDetails<T> { |
| 12 | + |
| 13 | + private final String flagKey; |
| 14 | + private final T value; |
| 15 | + private final String variant; |
| 16 | + private final String reason; |
| 17 | + private final ErrorCode errorCode; |
| 18 | + private final String errorMessage; |
| 19 | + private final Metadata flagMetadata; |
| 20 | + |
| 21 | + /** |
| 22 | + * Private constructor for builder pattern only. |
| 23 | + */ |
| 24 | + DefaultFlagEvaluationDetails() { |
| 25 | + this(null, null, null, null, null, null, null); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Private constructor for immutable FlagEvaluationDetails. |
| 30 | + * |
| 31 | + * @param flagKey the flag key |
| 32 | + * @param value the resolved value |
| 33 | + * @param variant the variant identifier |
| 34 | + * @param reason the reason for the evaluation result |
| 35 | + * @param errorCode the error code if applicable |
| 36 | + * @param errorMessage the error message if applicable |
| 37 | + * @param flagMetadata metadata associated with the flag |
| 38 | + */ |
| 39 | + DefaultFlagEvaluationDetails( |
| 40 | + String flagKey, |
| 41 | + T value, |
| 42 | + String variant, |
| 43 | + String reason, |
| 44 | + ErrorCode errorCode, |
| 45 | + String errorMessage, |
| 46 | + Metadata flagMetadata) { |
| 47 | + this.flagKey = flagKey; |
| 48 | + this.value = value; |
| 49 | + this.variant = variant; |
| 50 | + this.reason = reason; |
| 51 | + this.errorCode = errorCode; |
| 52 | + this.errorMessage = errorMessage; |
| 53 | + this.flagMetadata = flagMetadata != null ? flagMetadata : Metadata.EMPTY; |
| 54 | + } |
| 55 | + |
| 56 | + public String getFlagKey() { |
| 57 | + return flagKey; |
| 58 | + } |
| 59 | + |
| 60 | + public T getValue() { |
| 61 | + return value; |
| 62 | + } |
| 63 | + |
| 64 | + public String getVariant() { |
| 65 | + return variant; |
| 66 | + } |
| 67 | + |
| 68 | + public String getReason() { |
| 69 | + return reason; |
| 70 | + } |
| 71 | + |
| 72 | + public ErrorCode getErrorCode() { |
| 73 | + return errorCode; |
| 74 | + } |
| 75 | + |
| 76 | + public String getErrorMessage() { |
| 77 | + return errorMessage; |
| 78 | + } |
| 79 | + |
| 80 | + public Metadata getFlagMetadata() { |
| 81 | + return flagMetadata; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public boolean equals(Object obj) { |
| 86 | + if (this == obj) { |
| 87 | + return true; |
| 88 | + } |
| 89 | + if (obj == null || getClass() != obj.getClass()) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + FlagEvaluationDetails<?> that = (FlagEvaluationDetails<?>) obj; |
| 93 | + return Objects.equals(flagKey, that.getFlagKey()) |
| 94 | + && Objects.equals(value, that.getValue()) |
| 95 | + && Objects.equals(variant, that.getVariant()) |
| 96 | + && Objects.equals(reason, that.getReason()) |
| 97 | + && errorCode == that.getErrorCode() |
| 98 | + && Objects.equals(errorMessage, that.getErrorMessage()) |
| 99 | + && Objects.equals(flagMetadata, that.getFlagMetadata()); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public int hashCode() { |
| 104 | + return Objects.hash(flagKey, value, variant, reason, errorCode, errorMessage, flagMetadata); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public String toString() { |
| 109 | + return "FlagEvaluationDetails{" + "flagKey='" |
| 110 | + + flagKey + '\'' + ", value=" |
| 111 | + + value + ", variant='" |
| 112 | + + variant + '\'' + ", reason='" |
| 113 | + + reason + '\'' + ", errorCode=" |
| 114 | + + errorCode + ", errorMessage='" |
| 115 | + + errorMessage + '\'' + ", flagMetadata=" |
| 116 | + + flagMetadata + '}'; |
| 117 | + } |
| 118 | +} |
0 commit comments