|
| 1 | +package dev.openfeature.sdk; |
| 2 | + |
| 3 | +import lombok.Getter; |
| 4 | +import lombok.Setter; |
| 5 | +import lombok.ToString; |
| 6 | +import lombok.experimental.Delegate; |
| 7 | + |
| 8 | +import java.time.Instant; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +/** |
| 14 | + * The EvaluationContext is a container for arbitrary contextual data |
| 15 | + * that can be used as a basis for dynamic evaluation. |
| 16 | + * The MutableContext is an EvaluationContext implementation which is not threadsafe, and whose attributes can |
| 17 | + * be modified after instantiation. |
| 18 | + */ |
| 19 | +@ToString |
| 20 | +@SuppressWarnings("PMD.BeanMembersShouldSerialize") |
| 21 | +public class ImmutableContext implements EvaluationContext { |
| 22 | + |
| 23 | + @Getter |
| 24 | + private String targetingKey; |
| 25 | + @Delegate(excludes = HideDelegateAddMethods.class) |
| 26 | + private final MutableStructure structure; |
| 27 | + |
| 28 | + public ImmutableContext() { |
| 29 | + this.structure = new MutableStructure(); |
| 30 | + this.targetingKey = ""; |
| 31 | + } |
| 32 | + |
| 33 | + public ImmutableContext(Map<String, Value> attributes) { |
| 34 | + HashMap<String, Value> copy = new HashMap<>(attributes); |
| 35 | + this.structure = new MutableStructure(copy); |
| 36 | + this.targetingKey = ""; |
| 37 | + } |
| 38 | + |
| 39 | + public ImmutableContext(String targetingKey, Map<String, Value> attributes) { |
| 40 | + this(attributes); |
| 41 | + this.targetingKey = targetingKey; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void setTargetingKey(String targetingKey) { |
| 46 | + throw new UnsupportedOperationException("changing of targeting key is not allowed"); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Merges this EvaluationContext objects with the second overriding the this in |
| 51 | + * case of conflict. |
| 52 | + * |
| 53 | + * @param overridingContext overriding context |
| 54 | + * @return resulting merged context |
| 55 | + */ |
| 56 | + @Override |
| 57 | + public EvaluationContext merge(EvaluationContext overridingContext) { |
| 58 | + if (overridingContext == null) { |
| 59 | + return new ImmutableContext(this.asMap()); |
| 60 | + } |
| 61 | + String targetingKey = ""; |
| 62 | + if (this.getTargetingKey() != null && !this.getTargetingKey().trim().equals("")) { |
| 63 | + targetingKey = this.getTargetingKey(); |
| 64 | + } |
| 65 | + |
| 66 | + if (overridingContext.getTargetingKey() != null && !overridingContext.getTargetingKey().trim().equals("")) { |
| 67 | + targetingKey = overridingContext.getTargetingKey(); |
| 68 | + } |
| 69 | + Map<String, Value> merged = new HashMap<>(); |
| 70 | + |
| 71 | + merged.putAll(this.asMap()); |
| 72 | + merged.putAll(overridingContext.asMap()); |
| 73 | + return new ImmutableContext(targetingKey, merged); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Hidden class to tell Lombok not to copy these methods over via delegation. |
| 78 | + */ |
| 79 | + private static class HideDelegateAddMethods { |
| 80 | + public MutableStructure add(String ignoredKey, Boolean ignoredValue) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + public MutableStructure add(String ignoredKey, Double ignoredValue) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + public MutableStructure add(String ignoredKey, String ignoredValue) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + public MutableStructure add(String ignoredKey, Value ignoredValue) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + public MutableStructure add(String ignoredKey, Integer ignoredValue) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + public MutableStructure add(String ignoredKey, List<Value> ignoredValue) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + public MutableStructure add(String ignoredKey, MutableStructure ignoredValue) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + public MutableStructure add(String ignoredKey, Instant ignoredValue) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments