|
3 | 3 | import io.split.client.api.Key; |
4 | 4 | import io.split.client.api.SplitResult; |
5 | 5 | import io.split.grammar.Treatments; |
| 6 | +import io.split.inputValidation.KeyValidator; |
| 7 | +import io.split.inputValidation.SplitNameValidator; |
6 | 8 | import org.slf4j.Logger; |
7 | 9 | import org.slf4j.LoggerFactory; |
8 | 10 |
|
9 | 11 | import java.util.Map; |
| 12 | +import java.util.Optional; |
10 | 13 | import java.util.concurrent.TimeoutException; |
11 | 14 |
|
12 | 15 | import static com.google.common.base.Preconditions.checkNotNull; |
@@ -60,43 +63,6 @@ public SplitResult getTreatmentWithConfig(Key key, String split, Map<String, Obj |
60 | 63 | return getTreatmentAndConfigInternal(key.matchingKey(), split, attributes); |
61 | 64 | } |
62 | 65 |
|
63 | | - private SplitResult getTreatmentAndConfigInternal(String key, String split) { |
64 | | - return getTreatmentAndConfigInternal(key, split, null); |
65 | | - } |
66 | | - |
67 | | - private SplitResult getTreatmentAndConfigInternal(String key, String split, Map<String, Object> attributes) { |
68 | | - if (key == null || split == null) { |
69 | | - return SPLIT_RESULT_CONTROL; |
70 | | - } |
71 | | - |
72 | | - SplitAndKey override = SplitAndKey.of(split, key); |
73 | | - if (_map.containsKey(override)) { |
74 | | - return toSplitResult(_map.get(override)); |
75 | | - } |
76 | | - |
77 | | - SplitAndKey splitDefaultTreatment = SplitAndKey.of(split); |
78 | | - |
79 | | - LocalhostSplit localhostSplit = _map.get(splitDefaultTreatment); |
80 | | - |
81 | | - if (localhostSplit == null) { |
82 | | - return SPLIT_RESULT_CONTROL; |
83 | | - } |
84 | | - |
85 | | - return toSplitResult(localhostSplit); |
86 | | - } |
87 | | - |
88 | | - private SplitResult toSplitResult(LocalhostSplit localhostSplit) { |
89 | | - return new SplitResult(localhostSplit.treatment,localhostSplit.config); |
90 | | - } |
91 | | - |
92 | | - public void updateFeatureToTreatmentMap(Map<SplitAndKey, LocalhostSplit> map) { |
93 | | - if (map == null) { |
94 | | - _log.warn("A null map was passed as an update. Ignoring this update."); |
95 | | - return; |
96 | | - } |
97 | | - _map = map; |
98 | | - } |
99 | | - |
100 | 66 | @Override |
101 | 67 | public void destroy() { |
102 | 68 | _map.clear(); |
@@ -127,4 +93,50 @@ public void blockUntilReady() throws TimeoutException, InterruptedException { |
127 | 93 | // LocalhostSplitClient is always ready |
128 | 94 | } |
129 | 95 |
|
| 96 | + public void updateFeatureToTreatmentMap(Map<SplitAndKey, LocalhostSplit> map) { |
| 97 | + if (map == null) { |
| 98 | + _log.warn("A null map was passed as an update. Ignoring this update."); |
| 99 | + return; |
| 100 | + } |
| 101 | + _map = map; |
| 102 | + } |
| 103 | + |
| 104 | + private SplitResult getTreatmentAndConfigInternal(String key, String split, Map<String, Object> attributes) { |
| 105 | + boolean keyIsValid = KeyValidator.isValid(key, "matchingKey", "getTreatment"); |
| 106 | + |
| 107 | + if (!keyIsValid) { |
| 108 | + return SPLIT_RESULT_CONTROL; |
| 109 | + } |
| 110 | + |
| 111 | + Optional<String> splitName = SplitNameValidator.isValid(split, "getTreatment"); |
| 112 | + |
| 113 | + if (!splitName.isPresent()) { |
| 114 | + return SPLIT_RESULT_CONTROL; |
| 115 | + } |
| 116 | + |
| 117 | + split = splitName.get(); |
| 118 | + |
| 119 | + SplitAndKey override = SplitAndKey.of(split, key); |
| 120 | + if (_map.containsKey(override)) { |
| 121 | + return toSplitResult(_map.get(override)); |
| 122 | + } |
| 123 | + |
| 124 | + SplitAndKey splitDefaultTreatment = SplitAndKey.of(split); |
| 125 | + |
| 126 | + LocalhostSplit localhostSplit = _map.get(splitDefaultTreatment); |
| 127 | + |
| 128 | + if (localhostSplit == null) { |
| 129 | + return SPLIT_RESULT_CONTROL; |
| 130 | + } |
| 131 | + |
| 132 | + return toSplitResult(localhostSplit); |
| 133 | + } |
| 134 | + |
| 135 | + private SplitResult toSplitResult(LocalhostSplit localhostSplit) { |
| 136 | + return new SplitResult(localhostSplit.treatment,localhostSplit.config); |
| 137 | + } |
| 138 | + |
| 139 | + private SplitResult getTreatmentAndConfigInternal(String key, String split) { |
| 140 | + return getTreatmentAndConfigInternal(key, split, null); |
| 141 | + } |
130 | 142 | } |
0 commit comments