|
| 1 | +package dev.openfeature.sdk; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import lombok.Getter; |
| 7 | +import lombok.ToString; |
| 8 | +import lombok.experimental.Delegate; |
| 9 | + |
| 10 | +/** |
| 11 | + * The EvaluationContext is a container for arbitrary contextual data |
| 12 | + * that can be used as a basis for dynamic evaluation. |
| 13 | + * The ImmutableContext is an EvaluationContext implementation which is threadsafe, and whose attributes can |
| 14 | + * not be modified after instantiation. |
| 15 | + */ |
| 16 | +@ToString |
| 17 | +@SuppressWarnings("PMD.BeanMembersShouldSerialize") |
| 18 | +public final class ImmutableContext implements EvaluationContext { |
| 19 | + |
| 20 | + @Getter |
| 21 | + private final String targetingKey; |
| 22 | + @Delegate |
| 23 | + private final Structure structure; |
| 24 | + |
| 25 | + /** |
| 26 | + * Create an immutable context with an empty targeting_key and attributes provided. |
| 27 | + */ |
| 28 | + public ImmutableContext() { |
| 29 | + this("", new HashMap<>()); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Create an immutable context with given targeting_key provided. |
| 34 | + * |
| 35 | + * @param targetingKey targeting key |
| 36 | + */ |
| 37 | + public ImmutableContext(String targetingKey) { |
| 38 | + this(targetingKey, new HashMap<>()); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Create an immutable context with an attributes provided. |
| 43 | + * |
| 44 | + * @param attributes evaluation context attributes |
| 45 | + */ |
| 46 | + public ImmutableContext(Map<String, Value> attributes) { |
| 47 | + this("", attributes); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Create an immutable context with given targetingKey and attributes provided. |
| 52 | + * |
| 53 | + * @param targetingKey targeting key |
| 54 | + * @param attributes evaluation context attributes |
| 55 | + */ |
| 56 | + public ImmutableContext(String targetingKey, Map<String, Value> attributes) { |
| 57 | + this.structure = new ImmutableStructure(attributes); |
| 58 | + this.targetingKey = targetingKey; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Mutating targeting key is not supported in ImmutableContext and will be removed. |
| 63 | + */ |
| 64 | + @Override |
| 65 | + @Deprecated |
| 66 | + public void setTargetingKey(String targetingKey) { |
| 67 | + throw new UnsupportedOperationException("changing of targeting key is not allowed"); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Merges this EvaluationContext object with the passed EvaluationContext, overriding in case of conflict. |
| 72 | + * |
| 73 | + * @param overridingContext overriding context |
| 74 | + * @return resulting merged context |
| 75 | + */ |
| 76 | + @Override |
| 77 | + public EvaluationContext merge(EvaluationContext overridingContext) { |
| 78 | + if (overridingContext == null) { |
| 79 | + return new ImmutableContext(this.targetingKey, this.asMap()); |
| 80 | + } |
| 81 | + String newTargetingKey = ""; |
| 82 | + if (this.getTargetingKey() != null && !this.getTargetingKey().trim().equals("")) { |
| 83 | + newTargetingKey = this.getTargetingKey(); |
| 84 | + } |
| 85 | + |
| 86 | + if (overridingContext.getTargetingKey() != null && !overridingContext.getTargetingKey().trim().equals("")) { |
| 87 | + newTargetingKey = overridingContext.getTargetingKey(); |
| 88 | + } |
| 89 | + Map<String, Value> merged = new HashMap<>(); |
| 90 | + |
| 91 | + merged.putAll(this.asMap()); |
| 92 | + merged.putAll(overridingContext.asMap()); |
| 93 | + return new ImmutableContext(newTargetingKey, merged); |
| 94 | + } |
| 95 | +} |
0 commit comments