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.ArrayList;
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.ListIterator;
26  
27  import net.jcip.annotations.NotThreadSafe;
28  
29  /**
30   * A list that is lazy initialized. This list takes very little memory when storing zero or one item.
31   * 
32   * @param <ElementType> type of elements within the list
33   */
34  @NotThreadSafe
35  public class LazyList<ElementType> implements List<ElementType>, Serializable {
36  
37      /** Serial version UID. */
38      private static final long serialVersionUID = -7741904523916701817L;
39  
40      /** Delegate list. */
41      private List<ElementType> delegate = Collections.emptyList();
42  
43      /** {@inheritDoc} */
44      public boolean add(ElementType item) {
45          if (delegate.isEmpty()) {
46              delegate = Collections.singletonList(item);
47              return true;
48          } else {
49              delegate = buildList();
50              return delegate.add(item);
51          }
52      }
53  
54      /** {@inheritDoc} */
55      public void add(int index, ElementType element) {
56          delegate = buildList();
57          delegate.add(index, element);
58      }
59  
60      /** {@inheritDoc} */
61      public boolean addAll(Collection<? extends ElementType> collection) {
62          delegate = buildList();
63          return delegate.addAll(collection);
64      }
65  
66      /** {@inheritDoc} */
67      public boolean addAll(int index, Collection<? extends ElementType> collection) {
68          delegate = buildList();
69          return delegate.addAll(index, collection);
70      }
71  
72      /** {@inheritDoc} */
73      public void clear() {
74          delegate = Collections.emptyList();
75      }
76  
77      /** {@inheritDoc} */
78      public boolean contains(Object element) {
79          return delegate.contains(element);
80      }
81  
82      /** {@inheritDoc} */
83      public boolean containsAll(Collection<?> collections) {
84          return delegate.containsAll(collections);
85      }
86  
87      /** {@inheritDoc} */
88      public ElementType get(int index) {
89          return delegate.get(index);
90      }
91  
92      /** {@inheritDoc} */
93      public int indexOf(Object element) {
94          return delegate.indexOf(element);
95      }
96  
97      /** {@inheritDoc} */
98      public boolean isEmpty() {
99          return delegate.isEmpty();
100     }
101 
102     /** {@inheritDoc} */
103     public Iterator<ElementType> iterator() {
104         return delegate.iterator();
105     }
106 
107     /** {@inheritDoc} */
108     public int lastIndexOf(Object element) {
109         return delegate.lastIndexOf(element);
110     }
111 
112     /** {@inheritDoc} */
113     public ListIterator<ElementType> listIterator() {
114         return delegate.listIterator();
115     }
116 
117     /** {@inheritDoc} */
118     public ListIterator<ElementType> listIterator(int index) {
119         return delegate.listIterator(index);
120     }
121 
122     /** {@inheritDoc} */
123     public boolean remove(Object element) {
124         delegate = buildList();
125         return delegate.remove(element);
126     }
127 
128     /** {@inheritDoc} */
129     public ElementType remove(int index) {
130         delegate = buildList();
131         return delegate.remove(index);
132     }
133 
134     /** {@inheritDoc} */
135     public boolean removeAll(Collection<?> collection) {
136         delegate = buildList();
137         return delegate.removeAll(collection);
138     }
139 
140     /** {@inheritDoc} */
141     public boolean retainAll(Collection<?> collection) {
142         delegate = buildList();
143         return delegate.retainAll(collection);
144     }
145 
146     /** {@inheritDoc} */
147     public ElementType set(int index, ElementType element) {
148         delegate = buildList();
149         return delegate.set(index, element);
150     }
151 
152     /** {@inheritDoc} */
153     public int size() {
154         return delegate.size();
155     }
156 
157     /** {@inheritDoc} */
158     public List<ElementType> subList(int fromIndex, int toIndex) {
159         return delegate.subList(fromIndex, toIndex);
160     }
161 
162     /** {@inheritDoc} */
163     public Object[] toArray() {
164         return delegate.toArray();
165     }
166 
167     /** {@inheritDoc} */
168     public <T> T[] toArray(T[] type) {
169         return delegate.toArray(type);
170     }
171 
172     /**
173      * Builds an appropriate delegate for this list.
174      * 
175      * @return delegate for this list
176      */
177     protected List<ElementType> buildList() {
178         if (delegate instanceof ArrayList) {
179             return delegate;
180         }
181 
182         return new ArrayList<ElementType>(delegate);
183     }
184 
185     /** {@inheritDoc} */
186     public String toString() {
187         return delegate.toString();
188     }
189 
190     /** {@inheritDoc} */
191     public int hashCode() {
192         return delegate.hashCode();
193     }
194 
195     /** {@inheritDoc} */
196     public boolean equals(Object obj) {
197         if (this == obj) {
198             return true;
199         }
200 
201         if (obj == null || this.getClass() != obj.getClass()) {
202             return false;
203         }
204 
205         return delegate.equals(((LazyList<?>) obj).delegate);
206     }
207 }