KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > collection > LazyList


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.util.collection;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.ListIterator JavaDoc;
30
31 /**
32  * LazyList.
33  *
34  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
35  * @version $Revision: 1958 $
36  */

37 public class LazyList implements List JavaDoc
38 {
39    /** The delegate list */
40    private List JavaDoc delegate = Collections.EMPTY_LIST;
41
42    public void add(int index, Object JavaDoc element)
43    {
44       if (delegate instanceof ArrayList JavaDoc == false)
45          delegate = new ArrayList JavaDoc(delegate);
46       delegate.add(index, element);
47    }
48
49    public boolean add(Object JavaDoc o)
50    {
51       if (delegate == Collections.EMPTY_LIST)
52       {
53          delegate = Collections.singletonList(o);
54          return true;
55       }
56       else
57       {
58          if (delegate instanceof ArrayList JavaDoc == false)
59             delegate = new ArrayList JavaDoc(delegate);
60          return delegate.add(o);
61       }
62    }
63
64    public boolean addAll(Collection JavaDoc c)
65    {
66       if (delegate instanceof ArrayList JavaDoc == false)
67          delegate = new ArrayList JavaDoc(delegate);
68       return delegate.addAll(c);
69    }
70
71    public boolean addAll(int index, Collection JavaDoc c)
72    {
73       if (delegate instanceof ArrayList JavaDoc == false)
74          delegate = new ArrayList JavaDoc(delegate);
75       return delegate.addAll(index, c);
76    }
77
78    public void clear()
79    {
80       delegate = Collections.EMPTY_LIST;
81    }
82
83    public boolean contains(Object JavaDoc o)
84    {
85       return delegate.contains(o);
86    }
87
88    public boolean containsAll(Collection JavaDoc c)
89    {
90       return delegate.containsAll(c);
91    }
92
93    public Object JavaDoc get(int index)
94    {
95       return delegate.get(index);
96    }
97
98    public int indexOf(Object JavaDoc o)
99    {
100       return delegate.indexOf(o);
101    }
102
103    public boolean isEmpty()
104    {
105       return delegate.isEmpty();
106    }
107
108    public Iterator JavaDoc iterator()
109    {
110       return delegate.iterator();
111    }
112
113    public int lastIndexOf(Object JavaDoc o)
114    {
115       return delegate.lastIndexOf(o);
116    }
117
118    public ListIterator JavaDoc listIterator()
119    {
120       return delegate.listIterator();
121    }
122
123    public ListIterator JavaDoc listIterator(int index)
124    {
125       return delegate.listIterator(index);
126    }
127
128    public Object JavaDoc remove(int index)
129    {
130       if (delegate instanceof ArrayList JavaDoc == false)
131          delegate = new ArrayList JavaDoc(delegate);
132       return delegate.remove(index);
133    }
134
135    public boolean remove(Object JavaDoc o)
136    {
137       if (delegate instanceof ArrayList JavaDoc == false)
138          delegate = new ArrayList JavaDoc(delegate);
139       return delegate.remove(o);
140    }
141
142    public boolean removeAll(Collection JavaDoc c)
143    {
144       if (delegate instanceof ArrayList JavaDoc == false)
145          delegate = new ArrayList JavaDoc(delegate);
146       return delegate.remove(c);
147    }
148
149    public boolean retainAll(Collection JavaDoc c)
150    {
151       if (delegate instanceof ArrayList JavaDoc == false)
152          delegate = new ArrayList JavaDoc(delegate);
153       return delegate.retainAll(c);
154    }
155
156    public Object JavaDoc set(int index, Object JavaDoc element)
157    {
158       if (delegate instanceof ArrayList JavaDoc == false)
159          delegate = new ArrayList JavaDoc(delegate);
160       return delegate.set(index, element);
161    }
162
163    public int size()
164    {
165       return delegate.size();
166    }
167
168    public List JavaDoc subList(int fromIndex, int toIndex)
169    {
170       return delegate.subList(fromIndex, toIndex);
171    }
172
173    public Object JavaDoc[] toArray()
174    {
175       return delegate.toArray();
176    }
177
178    public Object JavaDoc[] toArray(Object JavaDoc[] a)
179    {
180       return delegate.toArray(a);
181    }
182 }
183
Popular Tags