1
2
3
4 package com.github.mkolisnyk.aerial.expressions.value;
5
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import org.junit.Assert;
10
11 import com.github.mkolisnyk.aerial.document.InputRecord;
12 import com.github.mkolisnyk.aerial.expressions.ValueExpression;
13 import com.github.mkolisnyk.aerial.expressions.ValueExpressionType;
14
15
16
17
18
19 public class NumericRangeValueExpression extends ValueExpression {
20
21 private static final String NUMBER_PATTERN = "(([-0-9](\\d*)))";
22 private static final String OPEN_RANGE_BRACKET_PATTERN = "(\\[|\\()";
23 private static final String CLOSE_RANGE_BRACKET_PATTERN = "(\\]|\\))";
24
25 private boolean includeLower = false;
26 private boolean includeUpper = false;
27 private int lower = 0;
28 private int upper = 0;
29
30
31
32
33 public NumericRangeValueExpression(InputRecord inputValue) {
34 super(inputValue);
35
36 }
37
38
39
40
41 @Override
42 public List<InputRecord> generate() throws Exception {
43 List<InputRecord> result = new ArrayList<InputRecord>();
44 InputRecord testRecord = (InputRecord) this.getInput().clone();
45 this.validate();
46 result.add(
47 new InputRecord(
48 testRecord.getName(),
49 testRecord.getType(),
50 "" + (lower + upper) / 2,
51 testRecord.getCondition(),
52 true));
53 if (Math.abs(upper - lower) > 1) {
54 result.add(
55 new InputRecord(
56 testRecord.getName(),
57 testRecord.getType(),
58 "" + ((lower + upper) / 2 + 1),
59 testRecord.getCondition(),
60 true));
61 }
62 result.add(
63 new InputRecord(
64 testRecord.getName(),
65 testRecord.getType(),
66 "" + lower,
67 testRecord.getCondition(),
68 includeLower));
69 result.add(
70 new InputRecord(
71 testRecord.getName(),
72 testRecord.getType(),
73 "" + upper,
74 testRecord.getCondition(),
75 includeUpper));
76 result.add(
77 new InputRecord(
78 testRecord.getName(),
79 testRecord.getType(),
80 "" + (lower - 1),
81 testRecord.getCondition(),
82 false));
83 result.add(
84 new InputRecord(
85 testRecord.getName(),
86 testRecord.getType(),
87 "" + (upper + 1),
88 testRecord.getCondition(),
89 false));
90 return result;
91 }
92
93
94
95
96 @Override
97 public ValueExpressionType getValueType() {
98 return ValueExpressionType.INTEGER;
99 }
100
101
102
103
104 @Override
105 public void validate() throws Exception {
106 super.validate();
107 this.parse();
108 Assert.assertTrue(
109 String.format(
110 "Lower bound %d should be less than upper bound %d",
111 lower,
112 upper),
113 lower <= upper);
114 Assert.assertFalse(
115 "The range is empty",
116 !this.includeLower
117 && !this.includeUpper
118 && (this.upper - this.lower) <= 1);
119 Assert.assertFalse(
120 "The range is empty",
121 !(this.includeLower && this.includeUpper)
122 && (this.upper - this.lower) < 1);
123 }
124
125
126
127
128 @Override
129 public String getMatchPattern() {
130 return String.format("%s%s;%s%s",
131 OPEN_RANGE_BRACKET_PATTERN,
132 NUMBER_PATTERN,
133 NUMBER_PATTERN,
134 CLOSE_RANGE_BRACKET_PATTERN);
135 }
136
137 public void parse() {
138 String input = this.getInput().getValue();
139 String pattern = this.getMatchPattern();
140 this.includeLower = input.replaceFirst(pattern, "$1").equals("[");
141 this.includeUpper = input.replaceFirst(pattern, "$8").equals("]");
142 this.lower = new Integer(input.replaceFirst(pattern, "$2"));
143 this.upper = new Integer(input.replaceFirst(pattern, "$5"));
144 }
145
146
147
148
149 @Override
150 public String toString() {
151 String start = "(";
152 String end = ")";
153 if (includeLower) {
154 start = "[";
155 }
156 if (includeUpper) {
157 end = "]";
158 }
159 return String.format("%s%d;%d%s",
160 start,
161 lower,
162 upper,
163 end);
164 }
165 }