KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > list > FixedSizeList


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
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 package org.apache.commons.collections.list;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.ListIterator JavaDoc;
22
23 import org.apache.commons.collections.BoundedCollection;
24 import org.apache.commons.collections.iterators.AbstractListIteratorDecorator;
25 import org.apache.commons.collections.iterators.UnmodifiableIterator;
26
27 /**
28  * Decorates another <code>List</code> to fix the size preventing add/remove.
29  * <p>
30  * The add, remove, clear and retain operations are unsupported.
31  * The set method is allowed (as it doesn't change the list size).
32  * <p>
33  * This class is Serializable from Commons Collections 3.1.
34  *
35  * @since Commons Collections 3.0
36  * @version $Revision: 1.8 $ $Date: 2004/06/03 22:02:13 $
37  *
38  * @author Stephen Colebourne
39  * @author Paul Jack
40  */

41 public class FixedSizeList
42         extends AbstractSerializableListDecorator
43         implements BoundedCollection {
44
45     /** Serialization version */
46     private static final long serialVersionUID = -2218010673611160319L;
47
48     /**
49      * Factory method to create a fixed size list.
50      *
51      * @param list the list to decorate, must not be null
52      * @throws IllegalArgumentException if list is null
53      */

54     public static List JavaDoc decorate(List JavaDoc list) {
55         return new FixedSizeList(list);
56     }
57
58     //-----------------------------------------------------------------------
59
/**
60      * Constructor that wraps (not copies).
61      *
62      * @param list the list to decorate, must not be null
63      * @throws IllegalArgumentException if list is null
64      */

65     protected FixedSizeList(List JavaDoc list) {
66         super(list);
67     }
68
69     //-----------------------------------------------------------------------
70
public boolean add(Object JavaDoc object) {
71         throw new UnsupportedOperationException JavaDoc("List is fixed size");
72     }
73
74     public void add(int index, Object JavaDoc object) {
75         throw new UnsupportedOperationException JavaDoc("List is fixed size");
76     }
77
78     public boolean addAll(Collection JavaDoc coll) {
79         throw new UnsupportedOperationException JavaDoc("List is fixed size");
80     }
81
82     public boolean addAll(int index, Collection JavaDoc coll) {
83         throw new UnsupportedOperationException JavaDoc("List is fixed size");
84     }
85
86     public void clear() {
87         throw new UnsupportedOperationException JavaDoc("List is fixed size");
88     }
89
90     public Object JavaDoc get(int index) {
91         return getList().get(index);
92     }
93
94     public int indexOf(Object JavaDoc object) {
95         return getList().indexOf(object);
96     }
97
98     public Iterator JavaDoc iterator() {
99         return UnmodifiableIterator.decorate(getCollection().iterator());
100     }
101
102     public int lastIndexOf(Object JavaDoc object) {
103         return getList().lastIndexOf(object);
104     }
105
106     public ListIterator JavaDoc listIterator() {
107         return new FixedSizeListIterator(getList().listIterator(0));
108     }
109
110     public ListIterator JavaDoc listIterator(int index) {
111         return new FixedSizeListIterator(getList().listIterator(index));
112     }
113
114     public Object JavaDoc remove(int index) {
115         throw new UnsupportedOperationException JavaDoc("List is fixed size");
116     }
117
118     public boolean remove(Object JavaDoc object) {
119         throw new UnsupportedOperationException JavaDoc("List is fixed size");
120     }
121
122     public boolean removeAll(Collection JavaDoc coll) {
123         throw new UnsupportedOperationException JavaDoc("List is fixed size");
124     }
125
126     public boolean retainAll(Collection JavaDoc coll) {
127         throw new UnsupportedOperationException JavaDoc("List is fixed size");
128     }
129
130     public Object JavaDoc set(int index, Object JavaDoc object) {
131         return getList().set(index, object);
132     }
133
134     public List JavaDoc subList(int fromIndex, int toIndex) {
135         List JavaDoc sub = getList().subList(fromIndex, toIndex);
136         return new FixedSizeList(sub);
137     }
138
139     /**
140      * List iterator that only permits changes via set()
141      */

142     static class FixedSizeListIterator extends AbstractListIteratorDecorator {
143         protected FixedSizeListIterator(ListIterator JavaDoc iterator) {
144             super(iterator);
145         }
146         public void remove() {
147             throw new UnsupportedOperationException JavaDoc("List is fixed size");
148         }
149         public void add(Object JavaDoc object) {
150             throw new UnsupportedOperationException JavaDoc("List is fixed size");
151         }
152     }
153
154     public boolean isFull() {
155         return true;
156     }
157
158     public int maxSize() {
159         return size();
160     }
161
162 }
163
Popular Tags