001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.collections.list;
018
019 import java.util.Collection;
020 import java.util.Iterator;
021 import java.util.List;
022 import java.util.ListIterator;
023
024 import org.apache.commons.collections.BoundedCollection;
025 import org.apache.commons.collections.iterators.AbstractListIteratorDecorator;
026 import org.apache.commons.collections.iterators.UnmodifiableIterator;
027
028 /**
029 * Decorates another <code>List</code> to fix the size preventing add/remove.
030 * <p>
031 * The add, remove, clear and retain operations are unsupported.
032 * The set method is allowed (as it doesn't change the list size).
033 * <p>
034 * This class is Serializable from Commons Collections 3.1.
035 *
036 * @since Commons Collections 3.0
037 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
038 *
039 * @author Stephen Colebourne
040 * @author Paul Jack
041 */
042 public class FixedSizeList
043 extends AbstractSerializableListDecorator
044 implements BoundedCollection {
045
046 /** Serialization version */
047 private static final long serialVersionUID = -2218010673611160319L;
048
049 /**
050 * Factory method to create a fixed size list.
051 *
052 * @param list the list to decorate, must not be null
053 * @throws IllegalArgumentException if list is null
054 */
055 public static List decorate(List list) {
056 return new FixedSizeList(list);
057 }
058
059 //-----------------------------------------------------------------------
060 /**
061 * Constructor that wraps (not copies).
062 *
063 * @param list the list to decorate, must not be null
064 * @throws IllegalArgumentException if list is null
065 */
066 protected FixedSizeList(List list) {
067 super(list);
068 }
069
070 //-----------------------------------------------------------------------
071 public boolean add(Object object) {
072 throw new UnsupportedOperationException("List is fixed size");
073 }
074
075 public void add(int index, Object object) {
076 throw new UnsupportedOperationException("List is fixed size");
077 }
078
079 public boolean addAll(Collection coll) {
080 throw new UnsupportedOperationException("List is fixed size");
081 }
082
083 public boolean addAll(int index, Collection coll) {
084 throw new UnsupportedOperationException("List is fixed size");
085 }
086
087 public void clear() {
088 throw new UnsupportedOperationException("List is fixed size");
089 }
090
091 public Object get(int index) {
092 return getList().get(index);
093 }
094
095 public int indexOf(Object object) {
096 return getList().indexOf(object);
097 }
098
099 public Iterator iterator() {
100 return UnmodifiableIterator.decorate(getCollection().iterator());
101 }
102
103 public int lastIndexOf(Object object) {
104 return getList().lastIndexOf(object);
105 }
106
107 public ListIterator listIterator() {
108 return new FixedSizeListIterator(getList().listIterator(0));
109 }
110
111 public ListIterator listIterator(int index) {
112 return new FixedSizeListIterator(getList().listIterator(index));
113 }
114
115 public Object 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 Object set(int index, Object object) {
132 return getList().set(index, object);
133 }
134
135 public List subList(int fromIndex, int toIndex) {
136 List sub = getList().subList(fromIndex, toIndex);
137 return new FixedSizeList(sub);
138 }
139
140 /**
141 * List iterator that only permits changes via set()
142 */
143 static class FixedSizeListIterator extends AbstractListIteratorDecorator {
144 protected FixedSizeListIterator(ListIterator iterator) {
145 super(iterator);
146 }
147 public void remove() {
148 throw new UnsupportedOperationException("List is fixed size");
149 }
150 public void add(Object 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 }