Skip to content

Commit 6e4f77e

Browse files
committed
manager refactor
1 parent 25b6872 commit 6e4f77e

File tree

2 files changed

+41
-34
lines changed

2 files changed

+41
-34
lines changed

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

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@
22

33
import com.google.common.base.Preconditions;
44
import io.split.client.api.SplitView;
5-
import io.split.client.dtos.Partition;
65
import io.split.engine.SDKReadinessGates;
76
import io.split.cache.SplitCache;
8-
import io.split.engine.experiments.ParsedCondition;
97
import io.split.engine.experiments.ParsedSplit;
8+
import io.split.inputValidation.SplitNameValidator;
109
import org.slf4j.Logger;
1110
import org.slf4j.LoggerFactory;
1211

1312
import java.util.ArrayList;
1413
import java.util.Collection;
15-
import java.util.Collections;
16-
import java.util.HashSet;
1714
import java.util.List;
18-
import java.util.Set;
1915

16+
import java.util.Optional;
2017
import java.util.concurrent.TimeoutException;
2118

2219
/**
@@ -44,21 +41,20 @@ public List<SplitView> splits() {
4441
List<SplitView> result = new ArrayList<>();
4542
Collection<ParsedSplit> parsedSplits = _splitCache.getAll();
4643
for (ParsedSplit split : parsedSplits) {
47-
result.add(toSplitView(split));
44+
result.add(SplitView.fromParsedSplit(split));
4845
}
46+
4947
return result;
5048
}
5149

5250
@Override
5351
public SplitView split(String featureName) {
54-
if (featureName == null) {
55-
_log.error("split: you passed a null split name, split name must be a non-empty string");
56-
return null;
57-
}
58-
if (featureName.isEmpty()) {
59-
_log.error("split: you passed an empty split name, split name must be a non-empty string");
52+
Optional<String> result = SplitNameValidator.isValid(featureName, "split");
53+
if (!result.isPresent()) {
6054
return null;
6155
}
56+
featureName = result.get();
57+
6258
ParsedSplit parsedSplit = _splitCache.get(featureName);
6359
if (parsedSplit == null) {
6460
if (_gates.isSDKReadyNow()) {
@@ -67,7 +63,8 @@ public SplitView split(String featureName) {
6763
}
6864
return null;
6965
}
70-
return toSplitView(parsedSplit);
66+
67+
return SplitView.fromParsedSplit(parsedSplit);
7168
}
7269

7370
@Override
@@ -77,6 +74,7 @@ public List<String> splitNames() {
7774
for (ParsedSplit split : parsedSplits) {
7875
result.add(split.feature());
7976
}
77+
8078
return result;
8179
}
8280

@@ -89,25 +87,4 @@ public void blockUntilReady() throws TimeoutException, InterruptedException {
8987
throw new TimeoutException("SDK was not ready in " + _config.blockUntilReady()+ " milliseconds");
9088
}
9189
}
92-
93-
private SplitView toSplitView(ParsedSplit parsedSplit) {
94-
SplitView splitView = new SplitView();
95-
splitView.name = parsedSplit.feature();
96-
splitView.trafficType = parsedSplit.trafficTypeName();
97-
splitView.killed = parsedSplit.killed();
98-
splitView.changeNumber = parsedSplit.changeNumber();
99-
100-
Set<String> treatments = new HashSet<String>();
101-
for (ParsedCondition condition : parsedSplit.parsedConditions()) {
102-
for (Partition partition : condition.partitions()) {
103-
treatments.add(partition.treatment);
104-
}
105-
}
106-
treatments.add(parsedSplit.defaultTreatment());
107-
108-
splitView.treatments = new ArrayList<String>(treatments);
109-
splitView.configs = parsedSplit.configurations() == null? Collections.<String, String>emptyMap() : parsedSplit.configurations() ;
110-
111-
return splitView;
112-
}
11390
}

client/src/main/java/io/split/client/api/SplitView.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
package io.split.client.api;
22

3+
import io.split.client.dtos.Partition;
4+
import io.split.engine.experiments.ParsedCondition;
5+
import io.split.engine.experiments.ParsedSplit;
6+
7+
import java.util.ArrayList;
8+
import java.util.Collections;
9+
import java.util.HashSet;
310
import java.util.List;
411
import java.util.Map;
12+
import java.util.Set;
13+
514

615
/**
716
* A view of a Split meant for consumption through SplitManager interface.
@@ -15,4 +24,25 @@ public class SplitView {
1524
public List<String> treatments;
1625
public long changeNumber;
1726
public Map<String, String> configs;
27+
28+
public static SplitView fromParsedSplit(ParsedSplit parsedSplit) {
29+
SplitView splitView = new SplitView();
30+
splitView.name = parsedSplit.feature();
31+
splitView.trafficType = parsedSplit.trafficTypeName();
32+
splitView.killed = parsedSplit.killed();
33+
splitView.changeNumber = parsedSplit.changeNumber();
34+
35+
Set<String> treatments = new HashSet<String>();
36+
for (ParsedCondition condition : parsedSplit.parsedConditions()) {
37+
for (Partition partition : condition.partitions()) {
38+
treatments.add(partition.treatment);
39+
}
40+
}
41+
treatments.add(parsedSplit.defaultTreatment());
42+
43+
splitView.treatments = new ArrayList<String>(treatments);
44+
splitView.configs = parsedSplit.configurations() == null? Collections.<String, String>emptyMap() : parsedSplit.configurations() ;
45+
46+
return splitView;
47+
}
1848
}

0 commit comments

Comments
 (0)