View Javadoc

1   /*
2    * Copyright 2008 University Corporation for Advanced Internet Development, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.opensaml.xml.util;
18  
19  import java.io.Serializable;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.Set;
25  
26  import net.jcip.annotations.NotThreadSafe;
27  
28  /**
29   * A set that is lazy initialized. This set takes very little memory when storing zero or one item.
30   * 
31   * @param <ElementType> type of the elements within the set
32   */
33  @NotThreadSafe
34  public class LazySet<ElementType> implements Set<ElementType>, Serializable {
35  
36      /** Serial version UID. */
37      private static final long serialVersionUID = -1596445680460115174L;
38  
39      /** The delegate set. */
40      private Set<ElementType> delegate = Collections.emptySet();
41  
42      /** {@inheritDoc} */
43      public boolean add(ElementType element) {
44          if (delegate.isEmpty()) {
45              delegate = Collections.singleton(element);
46              return true;
47          } else {
48              delegate = createImplementation();
49              return delegate.add(element);
50          }
51      }
52  
53      /** {@inheritDoc} */
54      public boolean addAll(Collection<? extends ElementType> collection) {
55          delegate = createImplementation();
56          return delegate.addAll(collection);
57      }
58  
59      /** {@inheritDoc} */
60      public void clear() {
61          delegate = Collections.emptySet();
62      }
63  
64      /** {@inheritDoc} */
65      public boolean contains(Object element) {
66          return delegate.contains(element);
67      }
68  
69      /** {@inheritDoc} */
70      public boolean containsAll(Collection<?> collection) {
71          return delegate.containsAll(collection);
72      }
73  
74      /** {@inheritDoc} */
75      public boolean isEmpty() {
76          return delegate.isEmpty();
77      }
78  
79      /** {@inheritDoc} */
80      public Iterator<ElementType> iterator() {
81          delegate = createImplementation();
82          return delegate.iterator();
83      }
84  
85      /** {@inheritDoc} */
86      public boolean remove(Object element) {
87          delegate = createImplementation();
88          return delegate.remove(element);
89      }
90  
91      /** {@inheritDoc} */
92      public boolean removeAll(Collection<?> collection) {
93          delegate = createImplementation();
94          return delegate.removeAll(collection);
95      }
96  
97      /** {@inheritDoc} */
98      public boolean retainAll(Collection<?> collection) {
99          delegate = createImplementation();
100         return delegate.retainAll(collection);
101     }
102 
103     /** {@inheritDoc} */
104     public int size() {
105         return delegate.size();
106     }
107 
108     /** {@inheritDoc} */
109     public Object[] toArray() {
110         return delegate.toArray();
111     }
112 
113     /** {@inheritDoc} */
114     public <T> T[] toArray(T[] type) {
115         return delegate.toArray(type);
116     }
117 
118     /**
119      * Builds an appropriate delegate set.
120      * 
121      * @return the delegate set
122      */
123     private Set<ElementType> createImplementation() {
124         if (delegate instanceof HashSet) {
125             return delegate;
126         }
127 
128         return new HashSet<ElementType>(delegate);
129     }
130 
131     /** {@inheritDoc} */
132     public String toString() {
133         return delegate.toString();
134     }
135 
136     /** {@inheritDoc} */
137     public int hashCode() {
138         return delegate.hashCode();
139     }
140 
141     /** {@inheritDoc} */
142     public boolean equals(Object obj) {
143         if (this == obj) {
144             return true;
145         }
146 
147         if (obj == null || this.getClass() != obj.getClass()) {
148             return false;
149         }
150 
151         return delegate.equals(((LazySet<?>) obj).delegate);
152     }
153 }