Skip to content

Commit 2804ceb

Browse files
committed
Centralize and fix age comparison
1 parent ed8b6dc commit 2804ceb

File tree

3 files changed

+36
-71
lines changed

3 files changed

+36
-71
lines changed

UnicodeJsps/src/main/java/org/unicode/jsp/UnicodeSetUtilities.java

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
import java.util.List;
1313
import java.util.regex.Pattern;
1414
import org.unicode.cldr.util.MultiComparator;
15-
import org.unicode.jsp.UnicodeSetUtilities.ComparisonMatcher.Relation;
1615
import org.unicode.props.UnicodeProperty;
1716
import org.unicode.props.UnicodeProperty.PatternMatcher;
17+
import org.unicode.props.UnicodePropertySymbolTable;
1818

1919
public class UnicodeSetUtilities {
2020

@@ -292,7 +292,13 @@ private boolean applyPropertyAlias0(
292292
+ prop.getValueAliases());
293293
}
294294
if (isAge) {
295-
set = prop.getSet(new ComparisonMatcher(propertyValue, Relation.geq));
295+
set =
296+
prop.getSet(
297+
new UnicodePropertySymbolTable.ComparisonMatcher(
298+
propertyValue,
299+
UnicodePropertySymbolTable.Relation.geq,
300+
UnicodePropertySymbolTable
301+
.VERSION_STRING_COMPARATOR));
296302
} else {
297303
if (prop.getName().equals("General_Category")) {
298304
for (String[] coarseValue : COARSE_GENERAL_CATEGORIES) {
@@ -344,49 +350,4 @@ private boolean isValid(UnicodeProperty prop, String propertyValue) {
344350
return prop.isValidValue(propertyValue);
345351
}
346352
}
347-
;
348-
349-
public static class ComparisonMatcher implements PatternMatcher {
350-
Relation relation;
351-
352-
enum Relation {
353-
less,
354-
leq,
355-
equal,
356-
geq,
357-
greater
358-
}
359-
360-
static Comparator comparator = new UTF16.StringComparator(true, false, 0);
361-
362-
String pattern;
363-
364-
public ComparisonMatcher(String pattern, Relation comparator) {
365-
this.relation = comparator;
366-
this.pattern = pattern;
367-
}
368-
369-
@Override
370-
public boolean test(String value) {
371-
int comp = comparator.compare(pattern, value);
372-
switch (relation) {
373-
case less:
374-
return comp < 0;
375-
case leq:
376-
return comp <= 0;
377-
default:
378-
return comp == 0;
379-
case geq:
380-
return comp >= 0;
381-
case greater:
382-
return comp > 0;
383-
}
384-
}
385-
386-
@Override
387-
public PatternMatcher set(String pattern) {
388-
this.pattern = pattern;
389-
return this;
390-
}
391-
}
392353
}

unicodetools/src/main/java/org/unicode/props/UnicodePropertySymbolTable.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
package org.unicode.props;
88

99
import com.ibm.icu.impl.UnicodeRegex;
10-
import com.ibm.icu.text.UTF16;
1110
import com.ibm.icu.text.UnicodeSet;
11+
import com.ibm.icu.util.VersionInfo;
1212
import java.util.Comparator;
1313
import java.util.HashMap;
1414
import java.util.List;
@@ -202,7 +202,9 @@ public boolean applyPropertyAlias0(
202202
set =
203203
prop.getSet(
204204
new ComparisonMatcher(
205-
propertyValue, Relation.geq, DOUBLE_STRING_COMPARATOR));
205+
propertyValue,
206+
Relation.geq,
207+
VERSION_STRING_COMPARATOR));
206208
} else {
207209
set = prop.getSet(propertyValue);
208210
}
@@ -247,10 +249,6 @@ public static class ComparisonMatcher implements PatternMatcher {
247249
final Comparator<String> comparator;
248250
String pattern;
249251

250-
public ComparisonMatcher(String pattern, Relation relation) {
251-
this(pattern, relation, new UTF16.StringComparator(true, false, 0));
252-
}
253-
254252
public ComparisonMatcher(String pattern, Relation relation, Comparator<String> comparator) {
255253
this.relation = relation;
256254
this.pattern = pattern;
@@ -281,8 +279,11 @@ public PatternMatcher set(String pattern) {
281279
}
282280
}
283281

284-
/** Special parser for doubles. Anything not parsable is higher than everything else. */
285-
public static final Comparator<String> DOUBLE_STRING_COMPARATOR =
282+
/**
283+
* Special parser for version strings. Anything not parsable is higher than everything
284+
* parseable.
285+
*/
286+
public static final Comparator<String> VERSION_STRING_COMPARATOR =
286287
new Comparator<String>() {
287288

288289
@Override
@@ -294,22 +295,25 @@ public int compare(String o1, String o2) {
294295
} else if (o2 == null) {
295296
return 1;
296297
} else {
297-
int f1 = o1.codePointAt(0);
298-
int f2 = o2.codePointAt(0);
299-
boolean n1 = f1 < '0' || f1 > '9';
300-
boolean n2 = f2 < '0' || f2 > '9';
301-
if (n1) {
302-
return n2 ? o1.compareTo(o2) : 1;
303-
} else if (n2) {
304-
return -1;
298+
boolean o1Valid = true;
299+
boolean o2Valid = true;
300+
VersionInfo v1 = null;
301+
VersionInfo v2 = null;
302+
try {
303+
v1 = VersionInfo.getInstance(o1);
304+
} catch (IllegalArgumentException e) {
305+
o1Valid = false;
305306
}
306-
double d1 = Double.parseDouble(o1);
307-
double d2 = Double.parseDouble(o2);
308-
if (Double.isNaN(d1) || Double.isNaN(d2)) {
309-
throw new IllegalArgumentException();
307+
try {
308+
v2 = VersionInfo.getInstance(o2);
309+
} catch (IllegalArgumentException e) {
310+
o2Valid = false;
311+
}
312+
if (o1Valid && o2Valid) {
313+
return v1.compareTo(v2);
314+
} else {
315+
return o2Valid ? 1 : o1Valid ? -1 : o1.compareTo(o2);
310316
}
311-
312-
return d1 > d2 ? 1 : d1 < d2 ? -1 : 0;
313317
}
314318
}
315319
};

unicodetools/src/test/java/org/unicode/unittest/TestUnicodeSet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ private void checkOrder(String d1, String d2, int expected) {
3333
assertEquals(
3434
d1 + " ?< " + d2,
3535
expected,
36-
UnicodePropertySymbolTable.DOUBLE_STRING_COMPARATOR.compare(d1, d2));
36+
UnicodePropertySymbolTable.VERSION_STRING_COMPARATOR.compare(d1, d2));
3737
assertEquals(
3838
d2 + " ?< " + d1,
3939
-expected,
40-
UnicodePropertySymbolTable.DOUBLE_STRING_COMPARATOR.compare(d2, d1));
40+
UnicodePropertySymbolTable.VERSION_STRING_COMPARATOR.compare(d2, d1));
4141
}
4242

4343
@Test

0 commit comments

Comments
 (0)