1:
44:
45: package ;
46:
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56:
57: import ;
58: import ;
59:
60:
63: public class KeyToGroupMap implements Cloneable, PublicCloneable, Serializable {
64:
65:
66: private static final long serialVersionUID = -2228169345475318082L;
67:
68:
69: private Comparable defaultGroup;
70:
71:
72: private List groups;
73:
74:
75: private Map keyToGroupMap;
76:
77:
80: public KeyToGroupMap() {
81: this("Default Group");
82: }
83:
84:
89: public KeyToGroupMap(Comparable defaultGroup) {
90: if (defaultGroup == null) {
91: throw new IllegalArgumentException("Null 'defaultGroup' argument.");
92: }
93: this.defaultGroup = defaultGroup;
94: this.groups = new ArrayList();
95: this.keyToGroupMap = new HashMap();
96: }
97:
98:
103: public int getGroupCount() {
104: return this.groups.size() + 1;
105: }
106:
107:
114: public List getGroups() {
115: List result = new ArrayList();
116: result.add(this.defaultGroup);
117: Iterator iterator = this.groups.iterator();
118: while (iterator.hasNext()) {
119: Comparable group = (Comparable) iterator.next();
120: if (!result.contains(group)) {
121: result.add(group);
122: }
123: }
124: return result;
125: }
126:
127:
135: public int getGroupIndex(Comparable group) {
136: int result = this.groups.indexOf(group);
137: if (result < 0) {
138: if (this.defaultGroup.equals(group)) {
139: result = 0;
140: }
141: }
142: else {
143: result = result + 1;
144: }
145: return result;
146: }
147:
148:
156: public Comparable getGroup(Comparable key) {
157: if (key == null) {
158: throw new IllegalArgumentException("Null 'key' argument.");
159: }
160: Comparable result = this.defaultGroup;
161: Comparable group = (Comparable) this.keyToGroupMap.get(key);
162: if (group != null) {
163: result = group;
164: }
165: return result;
166: }
167:
168:
175: public void mapKeyToGroup(Comparable key, Comparable group) {
176: if (key == null) {
177: throw new IllegalArgumentException("Null 'key' argument.");
178: }
179: Comparable currentGroup = getGroup(key);
180: if (!currentGroup.equals(this.defaultGroup)) {
181: if (!currentGroup.equals(group)) {
182: int count = getKeyCount(currentGroup);
183: if (count == 1) {
184: this.groups.remove(currentGroup);
185: }
186: }
187: }
188: if (group == null) {
189: this.keyToGroupMap.remove(key);
190: }
191: else {
192: if (!this.groups.contains(group)) {
193: if (!this.defaultGroup.equals(group)) {
194: this.groups.add(group);
195: }
196: }
197: this.keyToGroupMap.put(key, group);
198: }
199: }
200:
201:
210: public int getKeyCount(Comparable group) {
211: if (group == null) {
212: throw new IllegalArgumentException("Null 'group' argument.");
213: }
214: int result = 0;
215: Iterator iterator = this.keyToGroupMap.values().iterator();
216: while (iterator.hasNext()) {
217: Comparable g = (Comparable) iterator.next();
218: if (group.equals(g)) {
219: result++;
220: }
221: }
222: return result;
223: }
224:
225:
232: public boolean equals(Object obj) {
233: if (obj == this) {
234: return true;
235: }
236: if (!(obj instanceof KeyToGroupMap)) {
237: return false;
238: }
239: KeyToGroupMap that = (KeyToGroupMap) obj;
240: if (!ObjectUtilities.equal(this.defaultGroup, that.defaultGroup)) {
241: return false;
242: }
243: if (!this.keyToGroupMap.equals(that.keyToGroupMap)) {
244: return false;
245: }
246: return true;
247: }
248:
249:
257: public Object clone() throws CloneNotSupportedException {
258: KeyToGroupMap result = (KeyToGroupMap) super.clone();
259: result.defaultGroup
260: = (Comparable) KeyToGroupMap.clone(this.defaultGroup);
261: result.groups = (List) KeyToGroupMap.clone(this.groups);
262: result.keyToGroupMap = (Map) KeyToGroupMap.clone(this.keyToGroupMap);
263: return result;
264: }
265:
266:
273: private static Object clone(Object object) {
274: if (object == null) {
275: return null;
276: }
277: Class c = object.getClass();
278: Object result = null;
279: try {
280: Method m = c.getMethod("clone", (Class[]) null);
281: if (Modifier.isPublic(m.getModifiers())) {
282: try {
283: result = m.invoke(object, (Object[]) null);
284: }
285: catch (Exception e) {
286: e.printStackTrace();
287: }
288: }
289: }
290: catch (NoSuchMethodException e) {
291: result = object;
292: }
293: return result;
294: }
295:
296:
305: private static Collection clone(Collection list)
306: throws CloneNotSupportedException {
307: Collection result = null;
308: if (list != null) {
309: try {
310: List clone = (List) list.getClass().newInstance();
311: Iterator iterator = list.iterator();
312: while (iterator.hasNext()) {
313: clone.add(KeyToGroupMap.clone(iterator.next()));
314: }
315: result = clone;
316: }
317: catch (Exception e) {
318: throw new CloneNotSupportedException("Exception.");
319: }
320: }
321: return result;
322: }
323:
324: }