Skip to content

Commit 55f1cea

Browse files
authored
Update classpath, add regex-with-strings test class (#31)
1 parent 437d3b5 commit 55f1cea

File tree

6 files changed

+302
-12
lines changed

6 files changed

+302
-12
lines changed

UnicodeJspsTest/.classpath

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
33
<classpathentry kind="src" path="src"/>
4-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
5+
<attributes>
6+
<attribute name="module" value="true"/>
7+
</attributes>
8+
</classpathentry>
59
<classpathentry combineaccessrules="false" kind="src" path="/UnicodeJsps"/>
6-
<classpathentry combineaccessrules="false" kind="src" path="/cldr-tools"/>
710
<classpathentry kind="lib" path="/UnicodeJsps/WebContent/WEB-INF/lib/cldr.jar"/>
811
<classpathentry kind="lib" path="/UnicodeJsps/WebContent/WEB-INF/lib/guava-sources.jar"/>
912
<classpathentry kind="lib" path="/UnicodeJsps/WebContent/WEB-INF/lib/guava.jar"/>

UnicodeJspsTest/src/org/unicode/jsptest/TestUnicodeSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public void TestPretty() {
136136
public void TestU60 () {
137137
logln("ICU Version: " + VersionInfo.ICU_VERSION.toString());
138138
logln("Unicode Data Version: " + UCharacter.getUnicodeVersion().toString());
139-
logln("Java Version: " + java.lang.Runtime.version());
139+
logln("Java Version: " + System.getProperty("java.version"));
140140
logln("CLDR Data Version: " + LocaleData.getCLDRVersion().toString());
141141
logln("Time Zone Data Version: " + TimeZone.getTZDataVersion());
142142

unicodetools/.classpath

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
3-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
4-
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/cldr-tools"/>
5-
<classpathentry exported="true" kind="lib" path="/cldr-tools/libs/xercesImpl.jar"/>
6-
<classpathentry exported="true" kind="lib" path="/cldr-tools/libs/icu4j.jar" sourcepath="/cldr-tools/libs/icu4j-src.jar"/>
7-
<classpathentry exported="true" kind="lib" path="/cldr-tools/libs/utilities.jar" sourcepath="/cldr-tools/libs/utilities-src.jar"/>
8-
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/cldr-unittest"/>
93
<classpathentry kind="src" path=""/>
10-
<classpathentry kind="lib" path="/cldr-tools/libs/guava-sources.jar"/>
11-
<classpathentry kind="lib" path="/cldr-tools/libs/guava.jar" sourcepath="/cldr-tools/libs/guava-sources.jar"/>
4+
<classpathentry combineaccessrules="false" kind="src" path="/cldr-code"/>
5+
<classpathentry kind="lib" path="/cldr-all/cldr-code/target/test-classes"/>
6+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
7+
<attributes>
8+
<attribute name="module" value="true"/>
9+
</attributes>
10+
</classpathentry>
1211
<classpathentry kind="output" path="classes"/>
1312
</classpath>

unicodetools/org/unicode/jsptest/TestUnicodeSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void TestAEncodings() {
4646
public void TestU60 () {
4747
logln("ICU Version: " + VersionInfo.ICU_VERSION.toString());
4848
logln("Unicode Data Version: " + UCharacter.getUnicodeVersion().toString());
49-
logln("Java Version: " + java.lang.Runtime.version());
49+
logln("Java Version: " + System.getProperty("version"));
5050
logln("CLDR Data Version: " + LocaleData.getCLDRVersion().toString());
5151
logln("Time Zone Data Version: " + TimeZone.getTZDataVersion());
5252

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
package org.unicode.utilities;
2+
3+
import java.util.Collection;
4+
import java.util.Collections;
5+
import java.util.HashSet;
6+
import java.util.LinkedHashSet;
7+
import java.util.Set;
8+
import java.util.TreeSet;
9+
10+
import com.google.common.base.Joiner;
11+
import com.google.common.base.Splitter;
12+
13+
public class PolaritySet<T> {
14+
public enum Operation {
15+
NEGATION("¬"),
16+
UNION("∪"),
17+
INTERSECT("∩"),
18+
SUBTRACT("∖"),
19+
XOR("⊕"),
20+
;
21+
public final String display;
22+
private Operation(String display) {
23+
this.display = display;
24+
}
25+
public static Operation fromDisplay(String op) {
26+
for (Operation item : values()) {
27+
if (item.display.equals(op)) {
28+
return item;
29+
}
30+
}
31+
return null;
32+
}
33+
}
34+
private static final Joiner SPACE_JOINER = Joiner.on(' ');
35+
private boolean isPositive;
36+
private Set<T> source;
37+
38+
public static <T> PolaritySet<T> of(PolaritySet<T> left) {
39+
return new PolaritySet<T>(left.source, left.isPositive);
40+
}
41+
42+
public static <T> PolaritySet<T> of(Set<T> other, boolean isPositive) {
43+
return new PolaritySet<T>(other, isPositive);
44+
}
45+
46+
private PolaritySet(Set<T> source, boolean isPositive) {
47+
this.source = new LinkedHashSet<T>(source);
48+
this.isPositive = isPositive;
49+
}
50+
51+
public int size() {
52+
return source.size();
53+
}
54+
55+
public boolean isEmpty() {
56+
return isPositive && source.isEmpty();
57+
}
58+
59+
public boolean isFull() {
60+
return !isPositive && source.isEmpty();
61+
}
62+
63+
public boolean contains(Object o) {
64+
return source.contains(o) == isPositive;
65+
}
66+
67+
public boolean add(T e) {
68+
return isPositive ? source.add(e) : source.remove(e);
69+
}
70+
71+
public boolean remove(Object o) {
72+
return isPositive ? source.remove(o) : source.add((T)o);
73+
}
74+
75+
public boolean containsAll(Collection<?> c) {
76+
return isPositive ? source.containsAll(c) : Collections.disjoint(source, c);
77+
}
78+
79+
public boolean containsNone(Collection<?> c) {
80+
return isPositive ? Collections.disjoint(source, c) : source.containsAll(c);
81+
}
82+
83+
// see https://unicode.org/reports/tr18/#Resolving_Character_Ranges_with_Strings
84+
85+
public boolean addAll(PolaritySet<T> other) {
86+
if (isPositive && other.isPositive) {
87+
return source.addAll(other.source); // A ∪ B
88+
} else if (isPositive && !other.isPositive) {
89+
isPositive = false;
90+
return otherRemoveAllMe(other.source); // B ∖ A
91+
} else if (!isPositive && other.isPositive) {
92+
return source.removeAll(other.source); // A ∖ B
93+
} else /* if (!isPositive && !other.isPositive) */ {
94+
return source.retainAll(other.source); // A ∩ B
95+
}
96+
}
97+
98+
public boolean retainAll(PolaritySet<T> other) {
99+
if (isPositive && other.isPositive) {
100+
return source.retainAll(other.source); // A ∩ B
101+
} else if (isPositive && !other.isPositive) {
102+
return source.removeAll(other.source); // A ∖ B
103+
} else if (!isPositive && other.isPositive) {
104+
isPositive = true;
105+
return otherRemoveAllMe(other.source); // B ∖ A
106+
} else /* if (!isPositive && !other.isPositive) */ {
107+
return source.addAll(other.source); // A ∪ B
108+
}
109+
}
110+
111+
public boolean removeAll(PolaritySet<T> other) {
112+
if (isPositive && other.isPositive) {
113+
return source.removeAll(other.source); // A ∖ B
114+
} else if (isPositive && !other.isPositive) {
115+
return source.retainAll(other.source); // A ∩ B
116+
} else if (!isPositive && other.isPositive) {
117+
return source.addAll(other.source); // A ∪ B
118+
} else /* if (!isPositive && !other.isPositive) */ {
119+
isPositive = true;
120+
return otherRemoveAllMe(other.source); // B ∖ A
121+
}
122+
}
123+
124+
public void retainDifferences(PolaritySet<T> other) {
125+
PolaritySet<T> temp = PolaritySet.of(other);
126+
temp.removeAll(this);
127+
removeAll(other);
128+
addAll(temp);
129+
}
130+
131+
132+
private boolean otherRemoveAllMe(Set<T> other) {
133+
HashSet<T> temp = new HashSet<>(source);
134+
source.clear();
135+
source.addAll(other);
136+
return source.removeAll(temp);
137+
}
138+
139+
public PolaritySet<T> negate() {
140+
isPositive = !isPositive;
141+
return this;
142+
}
143+
144+
public void clear() {
145+
source.clear();
146+
isPositive = true;
147+
}
148+
149+
public boolean equals(Object o) {
150+
PolaritySet<T> other = (PolaritySet<T>) o;
151+
return source.equals(o) && (isPositive == other.isPositive);
152+
}
153+
154+
public int hashCode() {
155+
return isPositive ? source.hashCode() : source.hashCode() ^ 1;
156+
}
157+
158+
@Override
159+
public String toString() {
160+
if (source.isEmpty()) {
161+
return (isPositive ? "∅" : "Ω");
162+
}
163+
T item = source.iterator().next();
164+
Set<?> temp = source;
165+
if (item instanceof Comparable) {
166+
temp = new TreeSet(); // put in defined order for debugging
167+
temp.addAll((Set)source);
168+
}
169+
return (isPositive ? "" : Operation.NEGATION.display) + "{" + SPACE_JOINER.join(temp) + "}";
170+
}
171+
172+
private static final Splitter SPACE_SPLITTER = Splitter.on(' ');
173+
174+
public static PolaritySet<String> fromTestString(String source) {
175+
source = source.trim();
176+
boolean isNegated = source.startsWith(Operation.NEGATION.display);
177+
if (isNegated) {
178+
source = source.substring(1);
179+
}
180+
TreeSet<String> set = new TreeSet<>();
181+
switch(source) {
182+
case "∅":
183+
break;
184+
case "Ω":
185+
isNegated = !isNegated;
186+
break;
187+
default:
188+
if (!source.startsWith("{") || !source.endsWith("}")) {
189+
return null;
190+
}
191+
set.addAll(SPACE_SPLITTER.splitToList(source.substring(1, source.length()-1)));
192+
break;
193+
}
194+
return PolaritySet.of(set, !isNegated);
195+
}
196+
197+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package org.unicode.utilities;
2+
3+
import java.util.Set;
4+
import java.util.TreeSet;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
8+
import org.unicode.utilities.PolaritySet.Operation;
9+
10+
import com.google.common.base.Splitter;
11+
import com.google.common.collect.ImmutableSet;
12+
import com.ibm.icu.dev.test.TestFmwk;
13+
14+
public class TestPolaritySet extends TestFmwk {
15+
private static final Splitter SPACE_SPLITTER = Splitter.on(' ');
16+
17+
public static void main(String[] args) {
18+
new TestPolaritySet().run(args);
19+
}
20+
21+
public void testBasic() {
22+
Matcher mainOp = Pattern.compile("[∪∩∖⊕]").matcher("");
23+
System.out.println(ImmutableSet.of("a", "b"));
24+
25+
String[][] tests = {
26+
{"¬{a b}", "¬{a b}"},
27+
28+
{"¬Ω", "∅"},
29+
{"¬∅", "Ω"},
30+
31+
{"{a b} ∪ Ω", "Ω"},
32+
{"{a b} ∪ ∅", "{a b}"},
33+
34+
{"{a b} ∩ Ω", "{a b}"},
35+
{"{a b} ∩ ∅", "∅"},
36+
37+
{"{a b} ∪ {b c}", "{a b c}"},
38+
{"{a b} ∪ ¬{b c}", "¬{c}"},
39+
{"¬{a b} ∪ {b c}", "¬{a}"},
40+
{"¬{a b} ∪ ¬{b c}", "¬{b}"},
41+
42+
{"{a b} ∩ {b c}", "{b}"},
43+
{"{a b} ∩ ¬{b c}", "{a}"},
44+
{"¬{a b} ∩ {b c}", "{c}"},
45+
{"¬{a b} ∩ ¬{b c}", "¬{a b c}"},
46+
47+
{"{a b} ∖ {b c}", "{a}"},
48+
{"{a b} ∖ ¬{b c}", "{b}"},
49+
{"¬{a b} ∖ {b c}", "¬{a b c}"},
50+
{"¬{a b} ∖ ¬{b c}", "{c}"},
51+
52+
{"{a b} ⊕ {b c}", "{a c}"},
53+
{"{a b} ⊕ ¬{b c}", "¬{a c}"},
54+
{"¬{a b} ⊕ {b c}", "¬{a c}"},
55+
{"¬{a b} ⊕ ¬{b c}", "{a c}"},
56+
};
57+
for (String[] row : tests) {
58+
String source = row[0];
59+
String expected = row[1];
60+
PolaritySet<String> result;
61+
String modSource;
62+
if (!mainOp.reset(source).find()) {
63+
// no main op, check !
64+
result = PolaritySet.fromTestString(source);
65+
modSource = result.toString();
66+
} else {
67+
PolaritySet<String> left = PolaritySet.fromTestString(source.substring(0, mainOp.start()));
68+
PolaritySet<String> right = PolaritySet.fromTestString(source.substring(mainOp.end()));
69+
modSource = left + " " + mainOp.group() + " " + right;
70+
result = PolaritySet.of(left);
71+
Operation operation = PolaritySet.Operation.fromDisplay(mainOp.group());
72+
switch(operation) {
73+
case UNION:
74+
result.addAll(right);
75+
break;
76+
case INTERSECT:
77+
result.retainAll(right);
78+
break;
79+
case SUBTRACT:
80+
result.removeAll(right);
81+
break;
82+
case XOR:
83+
result.retainDifferences(right);
84+
break;
85+
}
86+
}
87+
String actual = result == null ? null : result.toString();
88+
assertEquals(source + (modSource.equals(source) ? "" : "\t" + "(" + modSource + ")"), expected, actual);
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)