01: /*
02: * Distributed as part of debuggen v.0.1.0
03: *
04: * Copyright (C) 2005 Machinery For Change, Inc.
05: *
06: * Author: Steve Waldman <swaldman@mchange.com>
07: *
08: * This library is free software; you can redistribute it and/or modify
09: * it under the terms of the GNU Lesser General Public License version 2.1, as
10: * published by the Free Software Foundation.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public License
18: * along with this software; see the file LICENSE. If not, write to the
19: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20: * Boston, MA 02111-1307, USA.
21: */
22:
23: package com.mchange.v1.util;
24:
25: import java.util.Iterator;
26: import java.util.Set;
27: import java.util.AbstractSet;
28: import java.util.HashSet;
29:
30: public final class SetUtils {
31: public static Set oneElementUnmodifiableSet(final Object elem) {
32: return new AbstractSet() {
33: public Iterator iterator() {
34: return IteratorUtils
35: .oneElementUnmodifiableIterator(elem);
36: }
37:
38: public int size() {
39: return 1;
40: }
41:
42: public boolean isEmpty() {
43: return false;
44: }
45:
46: public boolean contains(Object o) {
47: return o == elem;
48: }
49:
50: };
51: }
52:
53: public static Set setFromArray(Object[] array) {
54: HashSet out = new HashSet();
55: for (int i = 0, len = array.length; i < len; ++i)
56: out.add(array[i]);
57: return out;
58: }
59:
60: public static boolean equivalentDisregardingSort(Set a, Set b) {
61: return a.containsAll(b) && b.containsAll(a);
62: }
63:
64: /**
65: * finds a hash value which takes into account
66: * the value of all elements, such that two sets
67: * for which equivalentDisregardingSort(a, b) returns
68: * true will hashContentsDisregardingSort() to the same value
69: */
70: public static int hashContentsDisregardingSort(Set s) {
71: int out = 0;
72: for (Iterator ii = s.iterator(); ii.hasNext();) {
73: Object o = ii.next();
74: if (o != null)
75: out ^= o.hashCode();
76: }
77: return out;
78: }
79: }
|