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