Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 8 additions & 47 deletions UnicodeJsps/src/main/java/org/unicode/jsp/UnicodeSetUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import java.util.List;
import java.util.regex.Pattern;
import org.unicode.cldr.util.MultiComparator;
import org.unicode.jsp.UnicodeSetUtilities.ComparisonMatcher.Relation;
import org.unicode.props.UnicodeProperty;
import org.unicode.props.UnicodeProperty.PatternMatcher;
import org.unicode.props.UnicodePropertySymbolTable;

public class UnicodeSetUtilities {

Expand Down Expand Up @@ -292,7 +292,13 @@ private boolean applyPropertyAlias0(
+ prop.getValueAliases());
}
if (isAge) {
set = prop.getSet(new ComparisonMatcher(propertyValue, Relation.geq));
set =
prop.getSet(
new UnicodePropertySymbolTable.ComparisonMatcher(
propertyValue,
UnicodePropertySymbolTable.Relation.geq,
UnicodePropertySymbolTable
.VERSION_STRING_COMPARATOR));
} else {
if (prop.getName().equals("General_Category")) {
for (String[] coarseValue : COARSE_GENERAL_CATEGORIES) {
Expand Down Expand Up @@ -344,49 +350,4 @@ private boolean isValid(UnicodeProperty prop, String propertyValue) {
return prop.isValidValue(propertyValue);
}
}
;

public static class ComparisonMatcher implements PatternMatcher {
Relation relation;

enum Relation {
less,
leq,
equal,
geq,
greater
}

static Comparator comparator = new UTF16.StringComparator(true, false, 0);

String pattern;

public ComparisonMatcher(String pattern, Relation comparator) {
this.relation = comparator;
this.pattern = pattern;
}

@Override
public boolean test(String value) {
int comp = comparator.compare(pattern, value);
switch (relation) {
case less:
return comp < 0;
case leq:
return comp <= 0;
default:
return comp == 0;
case geq:
return comp >= 0;
case greater:
return comp > 0;
}
}

@Override
public PatternMatcher set(String pattern) {
this.pattern = pattern;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
package org.unicode.props;

import com.ibm.icu.impl.UnicodeRegex;
import com.ibm.icu.text.UTF16;
import com.ibm.icu.text.UnicodeSet;
import com.ibm.icu.util.VersionInfo;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -202,7 +202,9 @@ public boolean applyPropertyAlias0(
set =
prop.getSet(
new ComparisonMatcher(
propertyValue, Relation.geq, DOUBLE_STRING_COMPARATOR));
propertyValue,
Relation.geq,
VERSION_STRING_COMPARATOR));
} else {
set = prop.getSet(propertyValue);
}
Expand Down Expand Up @@ -247,10 +249,6 @@ public static class ComparisonMatcher implements PatternMatcher {
final Comparator<String> comparator;
String pattern;

public ComparisonMatcher(String pattern, Relation relation) {
this(pattern, relation, new UTF16.StringComparator(true, false, 0));
}

public ComparisonMatcher(String pattern, Relation relation, Comparator<String> comparator) {
this.relation = relation;
this.pattern = pattern;
Expand Down Expand Up @@ -281,8 +279,11 @@ public PatternMatcher set(String pattern) {
}
}

/** Special parser for doubles. Anything not parsable is higher than everything else. */
public static final Comparator<String> DOUBLE_STRING_COMPARATOR =
/**
* Special parser for version strings. Anything not parsable is higher than everything
* parseable.
*/
public static final Comparator<String> VERSION_STRING_COMPARATOR =
new Comparator<String>() {

@Override
Expand All @@ -294,22 +295,25 @@ public int compare(String o1, String o2) {
} else if (o2 == null) {
return 1;
} else {
int f1 = o1.codePointAt(0);
int f2 = o2.codePointAt(0);
boolean n1 = f1 < '0' || f1 > '9';
boolean n2 = f2 < '0' || f2 > '9';
if (n1) {
return n2 ? o1.compareTo(o2) : 1;
} else if (n2) {
return -1;
boolean o1Valid = true;
boolean o2Valid = true;
VersionInfo v1 = null;
VersionInfo v2 = null;
try {
v1 = VersionInfo.getInstance(o1);
} catch (IllegalArgumentException e) {
o1Valid = false;
}
double d1 = Double.parseDouble(o1);
double d2 = Double.parseDouble(o2);
if (Double.isNaN(d1) || Double.isNaN(d2)) {
throw new IllegalArgumentException();
try {
v2 = VersionInfo.getInstance(o2);
} catch (IllegalArgumentException e) {
o2Valid = false;
}
if (o1Valid && o2Valid) {
return v1.compareTo(v2);
} else {
return o2Valid ? 1 : o1Valid ? -1 : o1.compareTo(o2);
}

return d1 > d2 ? 1 : d1 < d2 ? -1 : 0;
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ private void checkOrder(String d1, String d2, int expected) {
assertEquals(
d1 + " ?< " + d2,
expected,
UnicodePropertySymbolTable.DOUBLE_STRING_COMPARATOR.compare(d1, d2));
UnicodePropertySymbolTable.VERSION_STRING_COMPARATOR.compare(d1, d2));
assertEquals(
d2 + " ?< " + d1,
-expected,
UnicodePropertySymbolTable.DOUBLE_STRING_COMPARATOR.compare(d2, d1));
UnicodePropertySymbolTable.VERSION_STRING_COMPARATOR.compare(d2, d1));
}

@Test
Expand Down
Loading