|
| 1 | +package io.split.engine.experiments; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableList; |
| 4 | +import io.split.engine.matchers.AttributeMatcher; |
| 5 | +import io.split.engine.matchers.UserDefinedSegmentMatcher; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | +import java.util.Set; |
| 9 | +import java.util.stream.Collectors; |
| 10 | + |
| 11 | +public class ParsedRuleBasedSegment { |
| 12 | + |
| 13 | + private final String _ruleBasedSegment; |
| 14 | + private final ImmutableList<ParsedCondition> _parsedCondition; |
| 15 | + private final String _trafficTypeName; |
| 16 | + private final long _changeNumber; |
| 17 | + private final List<String> _excludedKeys; |
| 18 | + private final List<String> _excludedSegments; |
| 19 | + |
| 20 | + public static ParsedRuleBasedSegment createParsedRuleBasedSegmentForTests( |
| 21 | + String ruleBasedSegment, |
| 22 | + List<ParsedCondition> matcherAndSplits, |
| 23 | + String trafficTypeName, |
| 24 | + long changeNumber, |
| 25 | + List<String> excludedKeys, |
| 26 | + List<String> excludedSegments |
| 27 | + ) { |
| 28 | + return new ParsedRuleBasedSegment( |
| 29 | + ruleBasedSegment, |
| 30 | + matcherAndSplits, |
| 31 | + trafficTypeName, |
| 32 | + changeNumber, |
| 33 | + excludedKeys, |
| 34 | + excludedSegments |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + public ParsedRuleBasedSegment( |
| 39 | + String ruleBasedSegment, |
| 40 | + List<ParsedCondition> matcherAndSplits, |
| 41 | + String trafficTypeName, |
| 42 | + long changeNumber, |
| 43 | + List<String> excludedKeys, |
| 44 | + List<String> excludedSegments |
| 45 | + ) { |
| 46 | + _ruleBasedSegment = ruleBasedSegment; |
| 47 | + _parsedCondition = ImmutableList.copyOf(matcherAndSplits); |
| 48 | + _trafficTypeName = trafficTypeName; |
| 49 | + _changeNumber = changeNumber; |
| 50 | + _excludedKeys = excludedKeys; |
| 51 | + _excludedSegments = excludedSegments; |
| 52 | + } |
| 53 | + |
| 54 | + public String ruleBasedSegment() { |
| 55 | + return _ruleBasedSegment; |
| 56 | + } |
| 57 | + |
| 58 | + public List<ParsedCondition> parsedConditions() { |
| 59 | + return _parsedCondition; |
| 60 | + } |
| 61 | + |
| 62 | + public String trafficTypeName() {return _trafficTypeName;} |
| 63 | + |
| 64 | + public long changeNumber() {return _changeNumber;} |
| 65 | + |
| 66 | + public List<String> excludedKeys() {return _excludedKeys;} |
| 67 | + public List<String> excludedSegments() {return _excludedSegments;} |
| 68 | + |
| 69 | + @Override |
| 70 | + public int hashCode() { |
| 71 | + int result = 17; |
| 72 | + result = 31 * result + _ruleBasedSegment.hashCode(); |
| 73 | + result = 31 * result + _parsedCondition.hashCode(); |
| 74 | + result = 31 * result + (_trafficTypeName == null ? 0 : _trafficTypeName.hashCode()); |
| 75 | + result = 31 * result + (int)(_changeNumber ^ (_changeNumber >>> 32)); |
| 76 | + return result; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public boolean equals(Object obj) { |
| 81 | + if (obj == null) return false; |
| 82 | + if (this == obj) return true; |
| 83 | + if (!(obj instanceof ParsedRuleBasedSegment)) return false; |
| 84 | + |
| 85 | + ParsedRuleBasedSegment other = (ParsedRuleBasedSegment) obj; |
| 86 | + |
| 87 | + return _ruleBasedSegment.equals(other._ruleBasedSegment) |
| 88 | + && _parsedCondition.equals(other._parsedCondition) |
| 89 | + && _trafficTypeName == null ? other._trafficTypeName == null : _trafficTypeName.equals(other._trafficTypeName) |
| 90 | + && _changeNumber == other._changeNumber; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public String toString() { |
| 95 | + StringBuilder bldr = new StringBuilder(); |
| 96 | + bldr.append("name:"); |
| 97 | + bldr.append(_ruleBasedSegment); |
| 98 | + bldr.append(", parsedConditions:"); |
| 99 | + bldr.append(_parsedCondition); |
| 100 | + bldr.append(", trafficTypeName:"); |
| 101 | + bldr.append(_trafficTypeName); |
| 102 | + bldr.append(", changeNumber:"); |
| 103 | + bldr.append(_changeNumber); |
| 104 | + return bldr.toString(); |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + public Set<String> getSegmentsNames() { |
| 109 | + return parsedConditions().stream() |
| 110 | + .flatMap(parsedCondition -> parsedCondition.matcher().attributeMatchers().stream()) |
| 111 | + .filter(ParsedRuleBasedSegment::isSegmentMatcher) |
| 112 | + .map(ParsedRuleBasedSegment::asSegmentMatcherForEach) |
| 113 | + .map(UserDefinedSegmentMatcher::getSegmentName) |
| 114 | + .collect(Collectors.toSet()); |
| 115 | + } |
| 116 | + |
| 117 | + private static boolean isSegmentMatcher(AttributeMatcher attributeMatcher) { |
| 118 | + return ((AttributeMatcher.NegatableMatcher) attributeMatcher.matcher()).delegate() instanceof UserDefinedSegmentMatcher; |
| 119 | + } |
| 120 | + |
| 121 | + private static UserDefinedSegmentMatcher asSegmentMatcherForEach(AttributeMatcher attributeMatcher) { |
| 122 | + return (UserDefinedSegmentMatcher) ((AttributeMatcher.NegatableMatcher) attributeMatcher.matcher()).delegate(); |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments