|
| 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 | +} |
0 commit comments