@@ -26,11 +26,11 @@ public enum Comparator {
26
26
/**
27
27
* @param v1
28
28
* @param v2
29
- * it convert string v1 and v2 into integer when Comparator is GreatEqual, SamallEqual ,Great and Small
29
+ * it converts string v1 and v2 into integer when Comparator is GreatEqual, SmallEqual ,Great and Small
30
30
* then do integer comparison for v1 and v2. Otherwise it straightly does String comparison ignoring Case
31
31
* for Comparator Equal and NotEqual
32
32
* @return true if v1 is GreatEqual than v2 for Comparator GreatEqual;
33
- * @return true if v1 is Great than v2 for Comparator Great;
33
+ * @return true if v1 is Greater than v2 for Comparator Great;
34
34
* @return true if v1 is SmallEqual than v2 for Comparator SmallEqual;
35
35
* @return true if v1 is Small than v2 for Comparator Small;
36
36
* @return true if v1 and v2 are same string for Comparator Equal
@@ -44,21 +44,21 @@ public enum Comparator {
44
44
*/
45
45
public boolean eval (String v1 , String v2 ) {
46
46
switch (this ){
47
- case GreatEqual : return Integer .valueOf (v1 ) >= Integer .valueOf (v2 );
48
- case SmallEqual : return Integer .valueOf (v1 ) <= Integer .valueOf (v2 );
49
- case Great : return Integer .valueOf (v1 ) > Integer .valueOf (v2 );
50
- case Small : return Integer .valueOf (v1 ) < Integer .valueOf (v2 );
47
+ case GreatEqual : return Integer .parseInt (v1 ) >= Integer .parseInt (v2 );
48
+ case SmallEqual : return Integer .parseInt (v1 ) <= Integer .parseInt (v2 );
49
+ case Great : return Integer .parseInt (v1 ) > Integer .parseInt (v2 );
50
+ case Small : return Integer .parseInt (v1 ) < Integer .parseInt (v2 );
51
51
case Equal : return v1 .equalsIgnoreCase (v2 );
52
- case NotEqual : return !( v1 .equalsIgnoreCase (v2 ) );
52
+ case NotEqual : return ! v1 .equalsIgnoreCase (v2 );
53
53
case StartWith : return v1 .toLowerCase ().startsWith (v2 .toLowerCase ());
54
- case NotStartWith : return !v1 .toLowerCase ().startsWith (v2 .toLowerCase ());
54
+ case NotStartWith : return ! v1 .toLowerCase ().startsWith (v2 .toLowerCase ());
55
55
case EndWith : return v1 .toLowerCase ().endsWith (v2 .toLowerCase ());
56
56
case NotEndWith : return ! v1 .toLowerCase ().endsWith (v2 .toLowerCase ());
57
57
case Contain : return v1 .toLowerCase ().contains (v2 .toLowerCase ());
58
- case NotContain : return !v1 .toLowerCase ().contains (v2 .toLowerCase ());
58
+ case NotContain : return ! v1 .toLowerCase ().contains (v2 .toLowerCase ());
59
59
}
60
60
61
- throw new AssertionError ("Unknow comparator mark:" + this );
61
+ throw new AssertionError ("Unknown comparator mark:" + this );
62
62
}
63
63
64
64
@@ -74,15 +74,27 @@ public boolean eval(String v1, String v2) {
74
74
* @return true if v1 is not equal to v2 for Comparator NotEqual;
75
75
*/
76
76
public boolean eval (int v1 , int v2 ){
77
- switch (this ){
78
- case GreatEqual : return (v1 >= v2 );
79
- case SmallEqual : return (v1 <= v2 );
80
- case Great : return (v1 > v2 );
81
- case Small : return (v1 < v2 );
82
- case Equal : return (v1 == v2 );
83
- case NotEqual : return (v1 != v2 );
77
+ switch (this ) {
78
+ case GreatEqual -> {
79
+ return (v1 >= v2 );
80
+ }
81
+ case SmallEqual -> {
82
+ return (v1 <= v2 );
83
+ }
84
+ case Great -> {
85
+ return (v1 > v2 );
86
+ }
87
+ case Small -> {
88
+ return (v1 < v2 );
89
+ }
90
+ case Equal -> {
91
+ return (v1 == v2 );
92
+ }
93
+ case NotEqual -> {
94
+ return (v1 != v2 );
95
+ }
84
96
}
85
- throw new AssertionError ("Unknow comparator mark:" + this );
97
+ throw new AssertionError ("Unknown comparator mark:" + this );
86
98
}
87
99
88
100
/**
@@ -92,15 +104,27 @@ public boolean eval(int v1, int v2){
92
104
* see detail return value on documents of eval(int v1, int v2);
93
105
*/
94
106
public boolean eval (float v1 , float v2 ){
95
- switch (this ){
96
- case GreatEqual : return (v1 >= v2 );
97
- case SmallEqual : return (v1 <= v2 );
98
- case Great : return (v1 > v2 );
99
- case Small : return (v1 < v2 );
100
- case Equal : return (v1 == v2 );
101
- case NotEqual : return (v1 != v2 );
107
+ switch (this ) {
108
+ case GreatEqual -> {
109
+ return (v1 >= v2 );
110
+ }
111
+ case SmallEqual -> {
112
+ return (v1 <= v2 );
113
+ }
114
+ case Great -> {
115
+ return (v1 > v2 );
116
+ }
117
+ case Small -> {
118
+ return (v1 < v2 );
119
+ }
120
+ case Equal -> {
121
+ return (v1 == v2 );
122
+ }
123
+ case NotEqual -> {
124
+ return (v1 != v2 );
125
+ }
102
126
}
103
- throw new AssertionError ("Unknow comparator mark:" + this );
127
+ throw new AssertionError ("Unknown comparator mark:" + this );
104
128
}
105
129
106
130
/**
@@ -111,30 +135,34 @@ public boolean eval(float v1, float v2){
111
135
* Throw Exception for another type of Comparator;
112
136
*/
113
137
public boolean eval (boolean v1 , boolean v2 ){
114
- switch (this ){
115
- case Equal : return (v1 == v2 );
116
- case NotEqual : return (v1 != v2 );
138
+ switch (this ) {
139
+ case Equal -> {
140
+ return (v1 == v2 );
141
+ }
142
+ case NotEqual -> {
143
+ return (v1 != v2 );
144
+ }
117
145
}
118
- throw new AssertionError ("Unknow op:" + this );
146
+ throw new AssertionError ("Unknown op:" + this );
119
147
}
120
148
121
149
/**
122
150
*
123
151
* @param comp: valid string parameter must belong to [">=", ">", "<=", "<", "==", "!="]
124
- * @return one of the six Comparators based onthe parameter string comp
125
- * @throws Exception if the parameter comp is not valid.
152
+ * @return one of the six Comparators based on the parameter string comp
126
153
*/
127
- public static final Comparator GetComparator (String comp , String value ) {
128
-
129
- if (comp .equals (">=" )){ return GreatEqual ;}
130
- else if ( comp .equals ("<=" )){return SmallEqual ; }
131
- else if ( comp .equals (">" )){return Great ; }
132
- else if ( comp .equals ("<" )){return Small ; }
133
- else if ( comp .equals ("==" )){return Equal ; }
134
- else if ( comp .equals ("!=" )){return NotEqual ;}
135
- else if ( comp .equals ("=~" ) || comp .equals ("!~" )){
136
- return GetWildCaseComparator (comp , value );}
137
- else return null ;
154
+ public static Comparator GetComparator (String comp , String value ) {
155
+
156
+ return switch (comp ) {
157
+ case ">=" -> GreatEqual ;
158
+ case "<=" -> SmallEqual ;
159
+ case ">" -> Great ;
160
+ case "<" -> Small ;
161
+ case "==" -> Equal ;
162
+ case "!=" -> NotEqual ;
163
+ case "=~" , "!~" -> getWildCaseComparator (comp , value );
164
+ default -> null ;
165
+ };
138
166
139
167
}
140
168
@@ -143,53 +171,62 @@ else if( comp.equals("=~") || comp.equals("!~")){
143
171
* @param comp must be '=~' or '!~'
144
172
* @param value: String contain single or non '*'
145
173
* @return
146
- * @throws Exception unless the single '*' appear at the begin or end of value string
147
174
*/
148
- public static Comparator GetWildCaseComparator (String comp , String value ) {
149
- String subStr = GetWildCaseValue (value );
175
+ public static Comparator getWildCaseComparator (String comp , String value ) {
176
+ String subStr = getWildCaseValue (value );
150
177
151
- if ( !comp .equals ("=~" ) && !comp .equals ("!~" ) )
152
- return null ;
178
+ if ( ! comp .equals ("=~" ) && ! comp .equals ("!~" )) {
179
+ return null ;
180
+ }
153
181
154
- // System.out.println( String.format("GetWildCaseComparator( String %s, String %s )", comp, value) );
155
-
156
- //only allow single or none '*'
157
- if ( value .contains ("*" ) && subStr .length () != (value .length () - 1 ) )
158
- return null ;
182
+ //only allow single or none '*'
183
+ if ( value .contains ("*" ) && subStr .length () != (value .length () - 1 )) {
184
+ return null ;
185
+ }
159
186
160
- if (value .startsWith ("*" ))
161
- return comp .equals ("=~" )? EndWith : NotEndWith ;
162
- else if (value .endsWith ("*" )){
187
+ if (value .startsWith ("*" )) {
188
+ return comp .equals ("=~" ) ? EndWith : NotEndWith ;
189
+ } else if (value .endsWith ("*" )) {
163
190
return comp .equals ("=~" )? StartWith : NotStartWith ;
164
- }else if ( ! value .contains ("*" )){
191
+ } else if ( ! value .contains ("*" )) {
165
192
return comp .equals ("=~" )? Contain : NotContain ;
166
193
}
167
194
168
195
return null ;
169
196
}
170
197
171
198
172
- public static String GetWildCaseValue (String value ) {
199
+ public static String getWildCaseValue (String value ) {
173
200
//remove all '*'
174
201
return value .replace ("*" , "" );
175
-
176
-
177
- }
202
+ }
178
203
179
204
/**
180
205
* @return comparator string. eg.
181
206
* return ">=" for Comparator.GreatEqual.GetString().
182
207
* return "==" for Comparator.Equal.GetString().
183
208
*/
184
- public String GetString (){
185
- switch (this ){
186
- case GreatEqual : return ">=" ;
187
- case SmallEqual : return "<=" ;
188
- case Great : return ">" ;
189
- case Small : return "<" ;
190
- case Equal : return "==" ;
191
- case NotEqual : return "!=" ;
209
+ public String getString () {
210
+ switch (this ) {
211
+ case GreatEqual -> {
212
+ return ">=" ;
213
+ }
214
+ case SmallEqual -> {
215
+ return "<=" ;
216
+ }
217
+ case Great -> {
218
+ return ">" ;
219
+ }
220
+ case Small -> {
221
+ return "<" ;
222
+ }
223
+ case Equal -> {
224
+ return "==" ;
225
+ }
226
+ case NotEqual -> {
227
+ return "!=" ;
228
+ }
192
229
}
193
- throw new AssertionError ("Unknow comparator mark:" + this );
230
+ throw new AssertionError ("Unknown comparator mark:" + this );
194
231
}
195
232
}
0 commit comments