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          return delegate.iterator();
82      }
83  
84      /** {@inheritDoc} */
85      public boolean remove(Object element) {
86          delegate = createImplementation();
87          return delegate.remove(element);
88      }
89  
90      /** {@inheritDoc} */
91      public boolean removeAll(Collection<?> collection) {
92          delegate = createImplementation();
93          return delegate.removeAll(collection);
94      }
95  
96      /** {@inheritDoc} */
97      public boolean retainAll(Collection<?> collection) {
98          delegate = createImplementation();
99          return delegate.retainAll(collection);
100     }
101 
102     /** {@inheritDoc} */
103     public int size() {
104         return delegate.size();
105     }
106 
107     /** {@inheritDoc} */
108     public Object[] toArray() {
109         return delegate.toArray();
110     }
111 
112     /** {@inheritDoc} */
113     public <T> T[] toArray(T[] type) {
114         return delegate.toArray(type);
115     }
116 
117     /**
118      * Builds an appropriate delegate set.
119      * 
120      * @return the delegate set
121      */
122     private Set<ElementType> createImplementation() {
123         if (delegate instanceof HashSet) {
124             return delegate;
125         }
126 
127         return new HashSet<ElementType>(delegate);
128     }
129 
130     /** {@inheritDoc} */
131     public String toString() {
132         return delegate.toString();
133     }
134 
135     /** {@inheritDoc} */
136     public int hashCode() {
137         return delegate.hashCode();
138     }
139 
140     /** {@inheritDoc} */
141     public boolean equals(Object obj) {
142         if (this == obj) {
143             return true;
144         }
145 
146         if (obj == null || this.getClass() != obj.getClass()) {
147             return false;
148         }
149 
150         return delegate.equals(((LazySet<?>) obj).delegate);
151     }
152 }