-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathParsedSplit.java
More file actions
286 lines (254 loc) · 9.81 KB
/
ParsedSplit.java
File metadata and controls
286 lines (254 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package io.split.engine.experiments;
import com.google.common.collect.ImmutableList;
import io.split.engine.matchers.AttributeMatcher;
import io.split.engine.matchers.PrerequisitesMatcher;
import io.split.engine.matchers.RuleBasedSegmentMatcher;
import io.split.engine.matchers.UserDefinedSegmentMatcher;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
* a value class representing an io.codigo.dtos.Experiment. Why are we not using
* that class? Because it does not have the logic of matching. ParsedExperiment
* has the matchers that also encapsulate the logic of matching. We
* can easily cache this object.
*
* @author adil
*/
public class ParsedSplit {
private final String _split;
private final int _seed;
private final boolean _killed;
private final String _defaultTreatment;
private final ImmutableList<ParsedCondition> _parsedCondition;
private final String _trafficTypeName;
private final long _changeNumber;
private final int _trafficAllocation;
private final int _trafficAllocationSeed;
private final int _algo;
private final Map<String, String> _configurations;
private final HashSet<String> _flagSets;
private final boolean _impressionsDisabled;
private PrerequisitesMatcher _prerequisitesMatcher;
public static ParsedSplit createParsedSplitForTests(
String feature,
int seed,
boolean killed,
String defaultTreatment,
List<ParsedCondition> matcherAndSplits,
String trafficTypeName,
long changeNumber,
int algo,
HashSet<String> flagSets,
boolean impressionsDisabled,
PrerequisitesMatcher prerequisitesMatcher
) {
return new ParsedSplit(
feature,
seed,
killed,
defaultTreatment,
matcherAndSplits,
trafficTypeName,
changeNumber,
100,
seed,
algo,
null,
flagSets,
impressionsDisabled,
prerequisitesMatcher
);
}
public static ParsedSplit createParsedSplitForTests(
String feature,
int seed,
boolean killed,
String defaultTreatment,
List<ParsedCondition> matcherAndSplits,
String trafficTypeName,
long changeNumber,
int algo,
Map<String, String> configurations,
HashSet<String> flagSets,
boolean impressionsDisabled,
PrerequisitesMatcher prerequisitesMatcher
) {
return new ParsedSplit(
feature,
seed,
killed,
defaultTreatment,
matcherAndSplits,
trafficTypeName,
changeNumber,
100,
seed,
algo,
configurations,
flagSets,
impressionsDisabled,
prerequisitesMatcher
);
}
public ParsedSplit(
String feature,
int seed,
boolean killed,
String defaultTreatment,
List<ParsedCondition> matcherAndSplits,
String trafficTypeName,
long changeNumber,
int trafficAllocation,
int trafficAllocationSeed,
int algo,
Map<String, String> configurations,
HashSet<String> flagSets,
boolean impressionsDisabled,
PrerequisitesMatcher prerequisitesMatcher
) {
_split = feature;
_seed = seed;
_killed = killed;
_defaultTreatment = defaultTreatment;
_parsedCondition = ImmutableList.copyOf(matcherAndSplits);
_trafficTypeName = trafficTypeName;
_changeNumber = changeNumber;
_algo = algo;
if (_defaultTreatment == null) {
throw new IllegalArgumentException("DefaultTreatment is null");
}
_trafficAllocation = trafficAllocation;
_trafficAllocationSeed = trafficAllocationSeed;
_configurations = configurations;
_flagSets = flagSets;
_impressionsDisabled = impressionsDisabled;
_prerequisitesMatcher = prerequisitesMatcher;
}
public String feature() {
return _split;
}
public int trafficAllocation() {
return _trafficAllocation;
}
public int trafficAllocationSeed() {
return _trafficAllocationSeed;
}
public int seed() {
return _seed;
}
public boolean killed() {
return _killed;
}
public String defaultTreatment() {
return _defaultTreatment;
}
public List<ParsedCondition> parsedConditions() {
return _parsedCondition;
}
public String trafficTypeName() {return _trafficTypeName;}
public long changeNumber() {return _changeNumber;}
public int algo() {return _algo;}
public HashSet<String> flagSets() {
return _flagSets;
}
public Map<String, String> configurations() {
return _configurations;
}
public boolean impressionsDisabled() {
return _impressionsDisabled;
}
public PrerequisitesMatcher prerequisitesMatcher() { return _prerequisitesMatcher; }
@Override
public int hashCode() {
int result = 17;
result = 31 * result + _split.hashCode();
result = 31 * result + (int)(_seed ^ (_seed >>> 32));
result = 31 * result + (_killed ? 1 : 0);
result = 31 * result + _defaultTreatment.hashCode();
result = 31 * result + _parsedCondition.hashCode();
result = 31 * result + (_trafficTypeName == null ? 0 : _trafficTypeName.hashCode());
result = 31 * result + (int)(_changeNumber ^ (_changeNumber >>> 32));
result = 31 * result + (_algo ^ (_algo >>> 32));
result = 31 * result + (_configurations == null? 0 : _configurations.hashCode());
result = 31 * result + (_impressionsDisabled ? 1 : 0);
return result;
}
@Override
public boolean equals(Object obj) {
if (obj == null) return false;
if (this == obj) return true;
if (!(obj instanceof ParsedSplit)) return false;
ParsedSplit other = (ParsedSplit) obj;
boolean trafficTypeCond = _trafficTypeName == null ? other._trafficTypeName == null : _trafficTypeName.equals(other._trafficTypeName);
boolean configCond = _configurations == null ? other._configurations == null : _configurations.equals(other._configurations);
return _split.equals(other._split)
&& _seed == other._seed
&& _killed == other._killed
&& _defaultTreatment.equals(other._defaultTreatment)
&& _parsedCondition.equals(other._parsedCondition)
&& trafficTypeCond
&& _changeNumber == other._changeNumber
&& _algo == other._algo
&& configCond
&& _impressionsDisabled == other._impressionsDisabled
&& _prerequisitesMatcher == other._prerequisitesMatcher;
}
@Override
public String toString() {
StringBuilder bldr = new StringBuilder();
bldr.append("name:");
bldr.append(_split);
bldr.append(", seed:");
bldr.append(_seed);
bldr.append(", killed:");
bldr.append(_killed);
bldr.append(", default treatment:");
bldr.append(_defaultTreatment);
bldr.append(", parsedConditions:");
bldr.append(_parsedCondition);
bldr.append(", trafficTypeName:");
bldr.append(_trafficTypeName);
bldr.append(", changeNumber:");
bldr.append(_changeNumber);
bldr.append(", algo:");
bldr.append(_algo);
bldr.append(", config:");
bldr.append(_configurations);
bldr.append(", impressionsDisabled:");
bldr.append(_impressionsDisabled);
bldr.append(", prerequisites:");
bldr.append(_prerequisitesMatcher);
return bldr.toString();
}
public Set<String> getSegmentsNames() {
return parsedConditions().stream()
.flatMap(parsedCondition -> parsedCondition.matcher().attributeMatchers().stream())
.filter(ParsedSplit::isSegmentMatcher)
.map(ParsedSplit::asSegmentMatcherForEach)
.map(UserDefinedSegmentMatcher::getSegmentName)
.collect(Collectors.toSet());
}
public Set<String> getRuleBasedSegmentsNames() {
return parsedConditions().stream()
.flatMap(parsedCondition -> parsedCondition.matcher().attributeMatchers().stream())
.filter(ParsedSplit::isRuleBasedSegmentMatcher)
.map(ParsedSplit::asRuleBasedSegmentMatcherForEach)
.map(RuleBasedSegmentMatcher::getSegmentName)
.collect(Collectors.toSet());
}
private static boolean isSegmentMatcher(AttributeMatcher attributeMatcher) {
return ((AttributeMatcher.NegatableMatcher) attributeMatcher.matcher()).delegate() instanceof UserDefinedSegmentMatcher;
}
private static UserDefinedSegmentMatcher asSegmentMatcherForEach(AttributeMatcher attributeMatcher) {
return (UserDefinedSegmentMatcher) ((AttributeMatcher.NegatableMatcher) attributeMatcher.matcher()).delegate();
}
private static boolean isRuleBasedSegmentMatcher(AttributeMatcher attributeMatcher) {
return ((AttributeMatcher.NegatableMatcher) attributeMatcher.matcher()).delegate() instanceof RuleBasedSegmentMatcher;
}
private static RuleBasedSegmentMatcher asRuleBasedSegmentMatcherForEach(AttributeMatcher attributeMatcher) {
return (RuleBasedSegmentMatcher) ((AttributeMatcher.NegatableMatcher) attributeMatcher.matcher()).delegate();
}
}