001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.list;
017:
018: import java.util.Collection;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.ListIterator;
022:
023: import org.apache.commons.collections.BoundedCollection;
024: import org.apache.commons.collections.iterators.AbstractListIteratorDecorator;
025: import org.apache.commons.collections.iterators.UnmodifiableIterator;
026:
027: /**
028: * Decorates another <code>List</code> to fix the size preventing add/remove.
029: * <p>
030: * The add, remove, clear and retain operations are unsupported.
031: * The set method is allowed (as it doesn't change the list size).
032: * <p>
033: * This class is Serializable from Commons Collections 3.1.
034: *
035: * @since Commons Collections 3.0
036: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
037: *
038: * @author Stephen Colebourne
039: * @author Paul Jack
040: */
041: public class FixedSizeList extends AbstractSerializableListDecorator
042: implements BoundedCollection {
043:
044: /** Serialization version */
045: private static final long serialVersionUID = -2218010673611160319L;
046:
047: /**
048: * Factory method to create a fixed size list.
049: *
050: * @param list the list to decorate, must not be null
051: * @throws IllegalArgumentException if list is null
052: */
053: public static List decorate(List list) {
054: return new FixedSizeList(list);
055: }
056:
057: //-----------------------------------------------------------------------
058: /**
059: * Constructor that wraps (not copies).
060: *
061: * @param list the list to decorate, must not be null
062: * @throws IllegalArgumentException if list is null
063: */
064: protected FixedSizeList(List list) {
065: super (list);
066: }
067:
068: //-----------------------------------------------------------------------
069: public boolean add(Object object) {
070: throw new UnsupportedOperationException("List is fixed size");
071: }
072:
073: public void add(int index, Object object) {
074: throw new UnsupportedOperationException("List is fixed size");
075: }
076:
077: public boolean addAll(Collection coll) {
078: throw new UnsupportedOperationException("List is fixed size");
079: }
080:
081: public boolean addAll(int index, Collection coll) {
082: throw new UnsupportedOperationException("List is fixed size");
083: }
084:
085: public void clear() {
086: throw new UnsupportedOperationException("List is fixed size");
087: }
088:
089: public Object get(int index) {
090: return getList().get(index);
091: }
092:
093: public int indexOf(Object object) {
094: return getList().indexOf(object);
095: }
096:
097: public Iterator iterator() {
098: return UnmodifiableIterator
099: .decorate(getCollection().iterator());
100: }
101:
102: public int lastIndexOf(Object object) {
103: return getList().lastIndexOf(object);
104: }
105:
106: public ListIterator listIterator() {
107: return new FixedSizeListIterator(getList().listIterator(0));
108: }
109:
110: public ListIterator listIterator(int index) {
111: return new FixedSizeListIterator(getList().listIterator(index));
112: }
113:
114: public Object remove(int index) {
115: throw new UnsupportedOperationException("List is fixed size");
116: }
117:
118: public boolean remove(Object object) {
119: throw new UnsupportedOperationException("List is fixed size");
120: }
121:
122: public boolean removeAll(Collection coll) {
123: throw new UnsupportedOperationException("List is fixed size");
124: }
125:
126: public boolean retainAll(Collection coll) {
127: throw new UnsupportedOperationException("List is fixed size");
128: }
129:
130: public Object set(int index, Object object) {
131: return getList().set(index, object);
132: }
133:
134: public List subList(int fromIndex, int toIndex) {
135: List 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
143: AbstractListIteratorDecorator {
144: protected FixedSizeListIterator(ListIterator iterator) {
145: super (iterator);
146: }
147:
148: public void remove() {
149: throw new UnsupportedOperationException(
150: "List is fixed size");
151: }
152:
153: public void add(Object object) {
154: throw new UnsupportedOperationException(
155: "List is fixed size");
156: }
157: }
158:
159: public boolean isFull() {
160: return true;
161: }
162:
163: public int maxSize() {
164: return size();
165: }
166:
167: }
|