KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > collection > LazySet


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
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 GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.util.collection;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Set JavaDoc;
29
30 /**
31  * LazySet.
32  *
33  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
34  * @version $Revision: 1958 $
35  */

36 public class LazySet implements Set JavaDoc
37 {
38    /** The delegate set */
39    private Set JavaDoc delegate = Collections.EMPTY_SET;
40    
41    public boolean add(Object JavaDoc o)
42    {
43       if (delegate == Collections.EMPTY_SET)
44       {
45          delegate = Collections.singleton(o);
46          return true;
47       }
48       else
49       {
50          if (delegate instanceof HashSet JavaDoc == false)
51             delegate = new HashSet JavaDoc(delegate);
52          return delegate.add(o);
53       }
54    }
55
56    public boolean addAll(Collection JavaDoc c)
57    {
58       if (delegate instanceof HashSet JavaDoc == false)
59          delegate = new HashSet JavaDoc(delegate);
60       return delegate.addAll(c);
61    }
62
63    public void clear()
64    {
65       delegate = Collections.EMPTY_SET;
66    }
67
68    public boolean contains(Object JavaDoc o)
69    {
70       return delegate.contains(o);
71    }
72
73    public boolean containsAll(Collection JavaDoc c)
74    {
75       return delegate.containsAll(c);
76    }
77
78    public boolean isEmpty()
79    {
80       return delegate.isEmpty();
81    }
82
83    public Iterator JavaDoc iterator()
84    {
85       return delegate.iterator();
86    }
87
88    public boolean remove(Object JavaDoc o)
89    {
90       if (delegate instanceof HashSet JavaDoc == false)
91          delegate = new HashSet JavaDoc(delegate);
92       return delegate.remove(o);
93    }
94
95    public boolean removeAll(Collection JavaDoc c)
96    {
97       if (delegate instanceof HashSet JavaDoc == false)
98          delegate = new HashSet JavaDoc(delegate);
99       return delegate.removeAll(c);
100    }
101
102    public boolean retainAll(Collection JavaDoc c)
103    {
104       if (delegate instanceof HashSet JavaDoc == false)
105          delegate = new HashSet JavaDoc(delegate);
106       return delegate.retainAll(c);
107    }
108
109    public int size()
110    {
111       return delegate.size();
112    }
113
114    public Object JavaDoc[] toArray()
115    {
116       return delegate.toArray();
117    }
118
119    public Object JavaDoc[] toArray(Object JavaDoc[] a)
120    {
121       return delegate.toArray(a);
122    }
123 }
124
Popular Tags