View Javadoc

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 net.sf.collections15.list;
17  
18  import java.util.Collection;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.ListIterator;
22  
23  import net.sf.collections15.BoundedCollection;
24  import net.sf.collections15.iterators.AbstractListIteratorDecorator;
25  import net.sf.collections15.iterators.UnmodifiableIterator;
26  
27  
28  /***
29   * Decorates another <code>List</code> to fix the size preventing add/remove.
30   * <p>
31   * The add, remove, clear and retain operations are unsupported.
32   * The set method is allowed (as it doesn't change the list size).
33   * <p>
34   * This class is Serializable from Commons Collections 3.1.
35   *
36   * @since Commons Collections 3.0
37   * @version $Revision: 1.1 $ $Date: 2005/05/03 22:45:38 $
38   * 
39   * @author Stephen Colebourne
40   * @author Paul Jack
41   */
42  public class FixedSizeList<E>
43          extends AbstractSerializableListDecorator<E>
44          implements BoundedCollection<E> {
45  
46  	/*** Serialization version */
47  	private static final long serialVersionUID = 3544669598625771570L;
48  
49      /***
50       * Factory method to create a fixed size list.
51       * 
52       * @param list  the list to decorate, must not be null
53       * @throws IllegalArgumentException if list is null
54       */
55      public static <T> List<T> decorate(List<T> list) {
56          return new FixedSizeList<T>(list);
57      }
58  
59      //-----------------------------------------------------------------------
60      /***
61       * Constructor that wraps (not copies).
62       * 
63       * @param list  the list to decorate, must not be null
64       * @throws IllegalArgumentException if list is null
65       */
66      protected FixedSizeList(List<E> list) {
67          super(list);
68      }
69  
70      //-----------------------------------------------------------------------
71      public boolean add(E object) {
72          throw new UnsupportedOperationException("List is fixed size");
73      }
74  
75      public void add(int index, E object) {
76          throw new UnsupportedOperationException("List is fixed size");
77      }
78  
79      public boolean addAll(Collection<? extends E> coll) {
80          throw new UnsupportedOperationException("List is fixed size");
81      }
82  
83      public boolean addAll(int index, Collection<? extends E> coll) {
84          throw new UnsupportedOperationException("List is fixed size");
85      }
86  
87      public void clear() {
88          throw new UnsupportedOperationException("List is fixed size");
89      }
90  
91      public E get(int index) {
92          return getList().get(index);
93      }
94  
95      public int indexOf(Object object) {
96          return getList().indexOf(object);
97      }
98  
99      public Iterator<E> iterator() {
100         return UnmodifiableIterator.<E>decorate(getCollection().iterator());
101     }
102 
103     public int lastIndexOf(Object object) {
104         return getList().lastIndexOf(object);
105     }
106 
107     public ListIterator<E> listIterator() {
108         return new FixedSizeListIterator<E>(getList().listIterator(0));
109     }
110 
111     public ListIterator<E> listIterator(int index) {
112         return new FixedSizeListIterator<E>(getList().listIterator(index));
113     }
114 
115     public E remove(int index) {
116         throw new UnsupportedOperationException("List is fixed size");
117     }
118 
119     public boolean remove(Object object) {
120         throw new UnsupportedOperationException("List is fixed size");
121     }
122 
123     public boolean removeAll(Collection<?> coll) {
124         throw new UnsupportedOperationException("List is fixed size");
125     }
126 
127     public boolean retainAll(Collection<?> coll) {
128         throw new UnsupportedOperationException("List is fixed size");
129     }
130 
131     public E set(int index, E object) {
132         return getList().set(index, object);
133     }
134 
135     public List<E> subList(int fromIndex, int toIndex) {
136         List<E> sub = getList().subList(fromIndex, toIndex);
137         return new FixedSizeList<E>(sub);
138     }
139 
140     /***
141      * List iterator that only permits changes via set()
142      */
143     static class FixedSizeListIterator<T> extends AbstractListIteratorDecorator<T> {
144         protected FixedSizeListIterator(ListIterator<T> iterator) {
145             super(iterator);
146         }
147         public void remove() {
148             throw new UnsupportedOperationException("List is fixed size");
149         }
150         public void add(T object) {
151             throw new UnsupportedOperationException("List is fixed size");
152         }
153     }
154 
155     public boolean isFull() {
156         return true;
157     }
158 
159     public int maxSize() {
160         return size();
161     }
162 
163 }