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          if(collection == null || collection.isEmpty()){
56              return false;
57          }
58          
59          delegate = createImplementation();
60          return delegate.addAll(collection);
61      }
62  
63      /** {@inheritDoc} */
64      public void clear() {
65          delegate = Collections.emptySet();
66      }
67  
68      /** {@inheritDoc} */
69      public boolean contains(Object element) {
70          return delegate.contains(element);
71      }
72  
73      /** {@inheritDoc} */
74      public boolean containsAll(Collection<?> collection) {
75          return delegate.containsAll(collection);
76      }
77  
78      /** {@inheritDoc} */
79      public boolean isEmpty() {
80          return delegate.isEmpty();
81      }
82  
83      /** {@inheritDoc} */
84      public Iterator<ElementType> iterator() {
85          return delegate.iterator();
86      }
87  
88      /** {@inheritDoc} */
89      public boolean remove(Object element) {
90          delegate = createImplementation();
91          return delegate.remove(element);
92      }
93  
94      /** {@inheritDoc} */
95      public boolean removeAll(Collection<?> collection) {
96          if(collection == null || collection.isEmpty()){
97              return false;
98          }
99          
100         delegate = createImplementation();
101         return delegate.removeAll(collection);
102     }
103 
104     /** {@inheritDoc} */
105     public boolean retainAll(Collection<?> collection) {
106         delegate = createImplementation();
107         return delegate.retainAll(collection);
108     }
109 
110     /** {@inheritDoc} */
111     public int size() {
112         return delegate.size();
113     }
114 
115     /** {@inheritDoc} */
116     public Object[] toArray() {
117         return delegate.toArray();
118     }
119 
120     /** {@inheritDoc} */
121     public <T> T[] toArray(T[] type) {
122         return delegate.toArray(type);
123     }
124 
125     /**
126      * Builds an appropriate delegate set.
127      * 
128      * @return the delegate set
129      */
130     private Set<ElementType> createImplementation() {
131         if (delegate instanceof HashSet) {
132             return delegate;
133         }
134 
135         return new HashSet<ElementType>(delegate);
136     }
137 }