|
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; |
8 | 6 | import org.slf4j.Logger; |
9 | 7 | import org.slf4j.LoggerFactory; |
10 | 8 |
|
11 | 9 | import java.util.Map; |
12 | | -import java.util.Optional; |
13 | 10 | import java.util.concurrent.TimeoutException; |
14 | 11 |
|
15 | 12 | import static com.google.common.base.Preconditions.checkNotNull; |
@@ -63,6 +60,43 @@ public SplitResult getTreatmentWithConfig(Key key, String split, Map<String, Obj |
63 | 60 | return getTreatmentAndConfigInternal(key.matchingKey(), split, attributes); |
64 | 61 | } |
65 | 62 |
|
| 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 | + |
66 | 100 | @Override |
67 | 101 | public void destroy() { |
68 | 102 | _map.clear(); |
@@ -93,50 +127,4 @@ public void blockUntilReady() throws TimeoutException, InterruptedException { |
93 | 127 | // LocalhostSplitClient is always ready |
94 | 128 | } |
95 | 129 |
|
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 | | - } |
142 | 130 | } |
0 commit comments