KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jbet > InternSet


1 /*
2  * JBET - Java Binary Enhancement Tool
3  * Copyright (c) 2003 Networks Associates Technology, Inc.
4  *
5  * This software was developed under DARPA/SPAWAR contract
6  * N66001-00-C-8602 "SPMA" as part of the
7  * DARPA OASIS research program.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */

30
31 /* An extended set where add2() returns the object actually in the set. */
32
33 package jbet;
34 import java.util.*;
35
36 public class InternSet implements Collection
37 {
38     Hashtable h;
39
40     public InternSet() { h = new Hashtable(); }
41     public InternSet (InternSet in) { h = new Hashtable (in.h); }
42     public InternSet (Object JavaDoc o) { this(); add (o); }
43
44     public Iterator iterator () { return h.values().iterator(); }
45     public int size() { return h.size(); }
46     public boolean isEmpty() { return h.isEmpty(); }
47     public boolean contains (Object JavaDoc o) { return h.get (o) != null; }
48     public boolean add (Object JavaDoc o) { if (o == null) throw new IllegalStateException JavaDoc ("add(null)"); return null == h.put (o, o); }
49     public boolean remove (Object JavaDoc o) { return null != h.remove (o); }
50     public void clear() { h.clear (); }
51     public Object JavaDoc clone () throws CloneNotSupportedException JavaDoc {
52     throw new CloneNotSupportedException JavaDoc ("no InternSet.clone");
53     }
54
55     public boolean addAll (Collection c) {
56     boolean change = false;
57     for (Iterator i = c.iterator(); i.hasNext(); )
58         if (add (i.next()))
59         change = true;
60     return change;
61     }
62
63     public boolean removeAll (Collection c) {
64     boolean change = false;
65     for (Iterator i = c.iterator(); i.hasNext(); )
66         if (remove (i.next()))
67         change = true;
68     return change;
69     }
70
71     public boolean retainAll (Collection c) {
72     boolean change = false;
73     for (Iterator i = h.values().iterator(); i.hasNext(); ) {
74         Object JavaDoc o = i.next();
75         if (!c.contains (o)) {
76         remove (o);
77         change = true;
78         }
79     }
80     return change;
81     }
82
83     public boolean containsAll (Collection c) {
84     boolean all = true;
85     for (Iterator i = c.iterator(); i.hasNext(); )
86         all &= contains (i.next());
87     return all;
88     }
89
90     public Object JavaDoc[] toArray () {
91     Object JavaDoc[] a = new Object JavaDoc[size()];
92     int n = 0;
93     for (Iterator i = h.values().iterator(); i.hasNext(); )
94         a[n++] = i.next();
95     if (n != a.length)
96         throw new IllegalStateException JavaDoc ("InternSet.toArray failed");
97     return a;
98     }
99
100     public Object JavaDoc[] toArray (Object JavaDoc[] a) {
101     if (a.length < size()) {
102         a = (Object JavaDoc[])
103         java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size());
104     }
105     int n = 0;
106     for (Iterator i = h.values().iterator(); i.hasNext(); )
107         a[n++] = i.next();
108     if (n != a.length)
109         throw new IllegalStateException JavaDoc ("InternSet.toArray failed");
110     return a;
111     }
112
113     public Object JavaDoc add2 (Object JavaDoc in) {
114     Object JavaDoc o = h.get (in);
115     if (o != null)
116         return o;
117
118     h.put (in, in);
119     return in;
120     }
121
122     public java.io.ObjectOutputStream JavaDoc writeObject () { throw new IllegalStateException JavaDoc ("no InternSet.writeObject"); }
123     public void readObject (java.io.ObjectInputStream JavaDoc o) { throw new IllegalStateException JavaDoc ("no InternSet.readObject"); }
124
125     public static InternSet EMPTY = new InternSet();
126 }
127
Popular Tags