1
2
3
4 package jwutil.collections;
5
6 import java.util.Collection;
7 import java.util.Collections;
8 import java.util.HashSet;
9 import java.util.Set;
10 import java.util.TreeSet;
11
12 /***
13 * <code>SetFactory</code> is a <code>Set</code> generator.
14 * Subclasses should implement constructions of specific types of
15 * <code>Set</code>s.
16 *
17 * @author Felix S. Klock II <pnkfelix@mit.edu>
18 * @version $Id: SetFactory.java 2249 2005-04-29 02:32:27Z joewhaley $
19 */
20 public abstract class SetFactory extends CollectionFactory {
21
22 /*** A <code>SetFactory</code> that generates <code>HashSet</code>s. */
23 public static final SetFactory hashSetFactory = new SerialSetFactory() {
24 /***
25 * Version ID for serialization.
26 */
27 private static final long serialVersionUID = 3689347719475246897L;
28
29 public Set makeSet(Collection c) {
30 return new HashSet(c);
31 }
32 public Set makeSet(int i) {
33 return new HashSet(i);
34 }
35 };
36
37 /*** A <code>SetFactory</code> that generates
38 <code>LinearSet</code>s backed by <code>ArrayList</code>s. */
39 public static final SetFactory linearSetFactory = new SerialSetFactory() {
40 /***
41 * Version ID for serialization.
42 */
43 private static final long serialVersionUID = 3258688814758245937L;
44
45 public Set makeSet(Collection c) {
46 Set ls;
47 if (c instanceof Set) {
48 ls = new LinearSet((Set)c);
49 } else {
50 ls = new LinearSet(c.size());
51 ls.addAll(c);
52 }
53 return ls;
54 }
55 public Set makeSet(int i) {
56 return new LinearSet(i);
57 }
58 };
59
60 /*** A <code>SetFactory</code> that generates <code>TreeSet</code>s. */
61 public static final SetFactory treeSetFactory = new SerialSetFactory() {
62 /***
63 * Version ID for serialization.
64 */
65 private static final long serialVersionUID = 3257008752384423731L;
66
67 public Set makeSet(Collection c) {
68 return new TreeSet(c);
69 }
70 };
71
72 /*** Creates a <code>SetFactory</code>. */
73 public SetFactory() {
74 super();
75 }
76
77
78
79
80 public final Collection makeCollection(Collection c) {
81 return makeSet(c);
82 }
83
84
85
86
87 public final Collection makeCollection(int initCapacity) {
88 return makeSet(initCapacity);
89 }
90
91 /*** Returns a <code>SetFactory</code> that generates synchronized
92 (thread-safe) <code>Set</code>s. The <code>Set</code>s
93 generated are backed by the <code>Set</code>s generated by
94 <code>sf</code>.
95 @see java.util.Collections#synchronizedSet(java.util.Set)
96 */
97 public static SetFactory synchronizedSetFactory(final SetFactory sf) {
98 return new SerialSetFactory() {
99 /***
100 * Version ID for serialization.
101 */
102 private static final long serialVersionUID = 3257286950267402040L;
103
104 public java.util.Set makeSet(Collection c) {
105 return Collections.synchronizedSet(sf.makeSet(c));
106 }
107 };
108 }
109
110 /*** Generates a new, mutable, empty <code>Set</code>. */
111 public final java.util.Set makeSet() {
112 return makeSet(Collections.EMPTY_SET);
113 }
114
115 /*** Generates a new, mutable, empty <code>Set</code>, using
116 <code>initialCapacity</code> as a hint to use for the capacity
117 for the produced <code>Set</code>. */
118 public java.util.Set makeSet(int initialCapacity) {
119 return makeSet();
120 }
121
122 /*** Generates a new mutable <code>Set</code>, using the elements
123 of <code>c</code> as a template for its initial contents.
124 */
125 public abstract Set makeSet(Collection c);
126
127
128
129
130 private abstract static class SerialSetFactory
131 extends SetFactory implements java.io.Serializable { }
132 }