Skip to content

Commit 6efce1b

Browse files
committed
Factory code cleanup
1 parent f192608 commit 6efce1b

File tree

3 files changed

+60
-43
lines changed

3 files changed

+60
-43
lines changed

client/src/main/java/io/split/client/LocalhostSplitClient.java

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import io.split.client.api.Key;
44
import io.split.client.api.SplitResult;
55
import io.split.grammar.Treatments;
6+
import io.split.inputValidation.KeyValidator;
7+
import io.split.inputValidation.SplitNameValidator;
68
import org.slf4j.Logger;
79
import org.slf4j.LoggerFactory;
810

911
import java.util.Map;
12+
import java.util.Optional;
1013
import java.util.concurrent.TimeoutException;
1114

1215
import static com.google.common.base.Preconditions.checkNotNull;
@@ -60,43 +63,6 @@ public SplitResult getTreatmentWithConfig(Key key, String split, Map<String, Obj
6063
return getTreatmentAndConfigInternal(key.matchingKey(), split, attributes);
6164
}
6265

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-
10066
@Override
10167
public void destroy() {
10268
_map.clear();
@@ -127,4 +93,50 @@ public void blockUntilReady() throws TimeoutException, InterruptedException {
12793
// LocalhostSplitClient is always ready
12894
}
12995

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+
}
130142
}

client/src/main/java/io/split/client/LocalhostSplitClientAndFactory.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@ public final class LocalhostSplitClientAndFactory implements SplitClient {
2525
private LocalhostSplitClient _splitClient;
2626

2727
public LocalhostSplitClientAndFactory(LocalhostSplitFactory container, LocalhostSplitClient client) {
28-
_factory = container;
29-
_splitClient = client;
30-
31-
checkNotNull(_factory);
32-
checkNotNull(_splitClient);
28+
_factory = checkNotNull(container);
29+
_splitClient = checkNotNull(client);
3330
}
3431

3532
@Override

client/src/main/java/io/split/inputValidation/KeyValidator.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public class KeyValidator {
77
private static final Logger _log = LoggerFactory.getLogger(KeyValidator.class);
88

9-
public static boolean isValid(String key, String propertyName, int maxStringLength, String method) {
9+
public static boolean isValid(String key, String propertyName, String method) {
1010
if (key == null) {
1111
_log.error(String.format("%s: you passed a null %s, %s must be a non-empty string", method, propertyName, propertyName));
1212
return false;
@@ -17,6 +17,14 @@ public static boolean isValid(String key, String propertyName, int maxStringLeng
1717
return false;
1818
}
1919

20+
return true;
21+
}
22+
23+
public static boolean isValid(String key, String propertyName, int maxStringLength, String method) {
24+
if (!isValid(key, propertyName, method)) {
25+
return false;
26+
}
27+
2028
if (key.length() > maxStringLength) {
2129
_log.error(String.format("%s: %s too long - must be %s characters or less", method, propertyName, maxStringLength));
2230
return false;

0 commit comments

Comments
 (0)