2 **********************************************************************
3 * Copyright (c) 2002-2010, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
7 * Created: December 2002 (moved from UnicodeSet)
9 **********************************************************************
11 package com.ibm.icu.impl;
13 import java.util.Iterator;
14 import java.util.SortedSet;
15 import java.util.TreeSet;
18 * Computationally efficient determination of the relationship between
21 public class SortedSetRelation {
24 * The relationship between two sets A and B can be determined by looking at:
26 * A & B (intersection)
28 * These are represented by a set of bits.
29 * Bit 2 is true if A - B is not empty
30 * Bit 1 is true if A & B is not empty
31 * BIT 0 is true if B - A is not empty
33 public static final int
39 * There are 8 combinations of the relationship bits. These correspond to
40 * the filters (combinations of allowed bits) in hasRelation. They also
41 * correspond to the modification functions, listed in comments.
43 public static final int
44 ANY = A_NOT_B | A_AND_B | B_NOT_A, // union, addAll
45 CONTAINS = A_NOT_B | A_AND_B, // A (unnecessary)
46 DISJOINT = A_NOT_B | B_NOT_A, // A xor B, missing Java function
47 ISCONTAINED = A_AND_B | B_NOT_A, // B (unnecessary)
48 NO_B = A_NOT_B, // A setDiff B, removeAll
49 EQUALS = A_AND_B, // A intersect B, retainAll
50 NO_A = B_NOT_A, // B setDiff A, removeAll
51 NONE = 0, // null (unnecessary)
53 ADDALL = ANY, // union, addAll
54 A = CONTAINS, // A (unnecessary)
55 COMPLEMENTALL = DISJOINT, // A xor B, missing Java function
56 B = ISCONTAINED, // B (unnecessary)
57 REMOVEALL = NO_B, // A setDiff B, removeAll
58 RETAINALL = EQUALS, // A intersect B, retainAll
59 B_REMOVEALL = NO_A; // B setDiff A, removeAll
63 * Utility that could be on SortedSet. Faster implementation than
64 * what is in Java for doing contains, equals, etc.
66 * @param allow filter, using ANY, CONTAINS, etc.
68 * @return whether the filter relationship is true or not.
70 public static <T extends Object & Comparable<? super T>> boolean hasRelation(SortedSet<T> a, int allow, SortedSet<T> b) {
71 if (allow < NONE || allow > ANY) {
72 throw new IllegalArgumentException("Relation " + allow + " out of range");
75 // extract filter conditions
76 // these are the ALLOWED conditions Set
78 boolean anb = (allow & A_NOT_B) != 0;
79 boolean ab = (allow & A_AND_B) != 0;
80 boolean bna = (allow & B_NOT_A) != 0;
82 // quick check on sizes
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;
89 // check for null sets
91 if (b.size() == 0) return true;
93 } else if (b.size() == 0) {
97 // pick up first strings, and start comparing
98 Iterator<? extends T> ait = a.iterator();
99 Iterator<? extends T> bit = b.iterator();
105 int comp = aa.compareTo(bb);
107 if (!ab) return false;
108 if (!ait.hasNext()) {
109 if (!bit.hasNext()) return true;
111 } else if (!bit.hasNext()) {
116 } else if (comp < 0) {
117 if (!anb) return false;
118 if (!ait.hasNext()) {
123 if (!bna) return false;
124 if (!bit.hasNext()) {
133 * Utility that could be on SortedSet. Allows faster implementation than
134 * what is in Java for doing addAll, removeAll, retainAll, (complementAll).
136 * @param relation the relation filter, using ANY, CONTAINS, etc.
137 * @param b second set
138 * @return the new set
140 public static <T extends Object & Comparable<? super T>> SortedSet<? extends T> doOperation(SortedSet<T> a, int relation, SortedSet<T> b) {
141 // TODO: optimize this as above
142 TreeSet<? extends T> temp;
148 return a; // no action
159 // the following is the only case not really supported by Java
160 // although all could be optimized
162 temp = new TreeSet<T>(b);
168 temp = new TreeSet<T>(b);
177 throw new IllegalArgumentException("Relation " + relation + " out of range");