-
-
Notifications
You must be signed in to change notification settings - Fork 63
Centralize and fix age comparison #1043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,11 +7,12 @@ | |
| 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; | ||
| import java.util.function.Function; | ||
| import org.unicode.props.UnicodeProperty.PatternMatcher; | ||
|
|
||
| /** | ||
|
|
@@ -201,8 +202,14 @@ public boolean applyPropertyAlias0( | |
| if (isAge) { | ||
| set = | ||
| prop.getSet( | ||
| new ComparisonMatcher( | ||
| propertyValue, Relation.geq, DOUBLE_STRING_COMPARATOR)); | ||
| new ComparisonMatcher<VersionInfo>( | ||
| UnicodePropertySymbolTable.parseVersionInfoOrMax( | ||
| propertyValue), | ||
| Relation.geq, | ||
| Comparator.nullsFirst(Comparator.naturalOrder()), | ||
| (s) -> | ||
| UnicodePropertySymbolTable | ||
| .parseVersionInfoOrMax(s))); | ||
eggrobin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| set = prop.getSet(propertyValue); | ||
| } | ||
|
|
@@ -242,24 +249,26 @@ public enum Relation { | |
| greater | ||
| } | ||
|
|
||
| public static class ComparisonMatcher implements PatternMatcher { | ||
| public static class ComparisonMatcher<T> implements PatternMatcher { | ||
| final Relation relation; | ||
| final Comparator<String> comparator; | ||
| String pattern; | ||
| final Comparator<T> comparator; | ||
| final Function<String, T> parser; | ||
| T expected; | ||
|
Comment on lines
+250
to
+254
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: nice! |
||
|
|
||
| public ComparisonMatcher(String pattern, Relation relation) { | ||
| this(pattern, relation, new UTF16.StringComparator(true, false, 0)); | ||
| } | ||
|
|
||
| public ComparisonMatcher(String pattern, Relation relation, Comparator<String> comparator) { | ||
| public ComparisonMatcher( | ||
| T expected, | ||
| Relation relation, | ||
| Comparator<T> comparator, | ||
| Function<String, T> parser) { | ||
| this.relation = relation; | ||
| this.pattern = pattern; | ||
| this.expected = expected; | ||
| this.comparator = comparator; | ||
| this.parser = parser; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean test(String value) { | ||
| int comp = comparator.compare(pattern, value); | ||
| int comp = comparator.compare(expected, parser.apply(value)); | ||
| switch (relation) { | ||
| case less: | ||
| return comp < 0; | ||
|
|
@@ -276,41 +285,19 @@ public boolean test(String value) { | |
|
|
||
| @Override | ||
| public PatternMatcher set(String pattern) { | ||
| this.pattern = pattern; | ||
| this.expected = parser.apply(pattern); | ||
| return this; | ||
| } | ||
| } | ||
|
|
||
| /** Special parser for doubles. Anything not parsable is higher than everything else. */ | ||
| public static final Comparator<String> DOUBLE_STRING_COMPARATOR = | ||
| new Comparator<String>() { | ||
|
|
||
| @Override | ||
| public int compare(String o1, String o2) { | ||
| if (o1 == o2) { | ||
| return 0; | ||
| } else if (o1 == null) { | ||
| return -1; | ||
| } 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; | ||
| } | ||
| double d1 = Double.parseDouble(o1); | ||
| double d2 = Double.parseDouble(o2); | ||
| if (Double.isNaN(d1) || Double.isNaN(d2)) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| return d1 > d2 ? 1 : d1 < d2 ? -1 : 0; | ||
| } | ||
| } | ||
| }; | ||
| public static VersionInfo parseVersionInfoOrMax(String s) { | ||
| if (s == null) { | ||
| return null; | ||
| } | ||
| try { | ||
| return VersionInfo.getInstance(s); | ||
| } catch (IllegalArgumentException e) { | ||
| return VersionInfo.getInstance(255, 255, 255, 255); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.