1 11 package com.ibm.icu.impl; 12 13 import java.util.SortedSet ; 14 import java.util.Iterator ; 15 import java.util.TreeSet ; 16 17 21 public class SortedSetRelation { 22 23 33 public static final int 34 A_NOT_B = 4, 35 A_AND_B = 2, 36 B_NOT_A = 1; 37 38 43 public static final int 44 ANY = A_NOT_B | A_AND_B | B_NOT_A, CONTAINS = A_NOT_B | A_AND_B, DISJOINT = A_NOT_B | B_NOT_A, ISCONTAINED = A_AND_B | B_NOT_A, NO_B = A_NOT_B, EQUALS = A_AND_B, NO_A = B_NOT_A, NONE = 0, 53 ADDALL = ANY, A = CONTAINS, COMPLEMENTALL = DISJOINT, B = ISCONTAINED, REMOVEALL = NO_B, RETAINALL = EQUALS, B_REMOVEALL = NO_A; 61 62 70 public static boolean hasRelation(SortedSet a, int allow, SortedSet b) { 71 if (allow < NONE || allow > ANY) { 72 throw new IllegalArgumentException ("Relation " + allow + " out of range"); 73 } 74 75 78 boolean anb = (allow & A_NOT_B) != 0; 79 boolean ab = (allow & A_AND_B) != 0; 80 boolean bna = (allow & B_NOT_A) != 0; 81 82 switch(allow) { 84 case CONTAINS: if (a.size() < b.size()) return false; break; 85 case ISCONTAINED: if (a.size() > b.size()) return false; break; 86 case EQUALS: if (a.size() != b.size()) return false; break; 87 } 88 89 if (a.size() == 0) { 91 if (b.size() == 0) return true; 92 return bna; 93 } else if (b.size() == 0) { 94 return anb; 95 } 96 97 Iterator ait = a.iterator(); 99 Iterator bit = b.iterator(); 100 101 Comparable aa = (Comparable ) ait.next(); 102 Comparable bb = (Comparable ) bit.next(); 103 104 while (true) { 105 int comp = aa.compareTo(bb); 106 if (comp == 0) { 107 if (!ab) return false; 108 if (!ait.hasNext()) { 109 if (!bit.hasNext()) return true; 110 return bna; 111 } else if (!bit.hasNext()) { 112 return anb; 113 } 114 aa = (Comparable ) ait.next(); 115 bb = (Comparable ) bit.next(); 116 } else if (comp < 0) { 117 if (!anb) return false; 118 if (!ait.hasNext()) { 119 return bna; 120 } 121 aa = (Comparable ) ait.next(); 122 } else { 123 if (!bna) return false; 124 if (!bit.hasNext()) { 125 return anb; 126 } 127 bb = (Comparable ) bit.next(); 128 } 129 } 130 } 131 132 140 public static SortedSet doOperation(SortedSet a, int relation, SortedSet b) { 141 TreeSet temp; 143 switch (relation) { 144 case ADDALL: 145 a.addAll(b); 146 return a; 147 case A: 148 return a; case B: 150 a.clear(); 151 a.addAll(b); 152 return a; 153 case REMOVEALL: 154 a.removeAll(b); 155 return a; 156 case RETAINALL: 157 a.retainAll(b); 158 return a; 159 case COMPLEMENTALL: 162 temp = new TreeSet (b); 163 temp.removeAll(a); 164 a.removeAll(b); 165 a.addAll(temp); 166 return a; 167 case B_REMOVEALL: 168 temp = new TreeSet (b); 169 temp.removeAll(a); 170 a.clear(); 171 a.addAll(temp); 172 return a; 173 case NONE: 174 a.clear(); 175 return a; 176 default: 177 throw new IllegalArgumentException ("Relation " + relation + " out of range"); 178 } 179 } 180 } 181 | Popular Tags |