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 */
017package org.apache.commons.collections4.list;
018
019import java.util.Collection;
020import java.util.Iterator;
021import java.util.List;
022import java.util.ListIterator;
023
024import org.apache.commons.collections4.BoundedCollection;
025import org.apache.commons.collections4.iterators.AbstractListIteratorDecorator;
026import org.apache.commons.collections4.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 * @param <E> the type of elements in this collection
037 * @since 3.0
038 */
039public class FixedSizeList<E>
040        extends AbstractSerializableListDecorator<E>
041        implements BoundedCollection<E> {
042
043    /** Serialization version */
044    private static final long serialVersionUID = -2218010673611160319L;
045
046    /**
047     * Factory method to create a fixed size list.
048     *
049     * @param <E> the type of the elements in the list
050     * @param list  the list to decorate, must not be null
051     * @return a new fixed size list
052     * @throws NullPointerException if list is null
053     * @since 4.0
054     */
055    public static <E> FixedSizeList<E> fixedSizeList(final List<E> 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 NullPointerException if list is null
065     */
066    protected FixedSizeList(final List<E> list) {
067        super(list);
068    }
069
070    //-----------------------------------------------------------------------
071    @Override
072    public boolean add(final E object) {
073        throw new UnsupportedOperationException("List is fixed size");
074    }
075
076    @Override
077    public void add(final int index, final E object) {
078        throw new UnsupportedOperationException("List is fixed size");
079    }
080
081    @Override
082    public boolean addAll(final Collection<? extends E> coll) {
083        throw new UnsupportedOperationException("List is fixed size");
084    }
085
086    @Override
087    public boolean addAll(final int index, final Collection<? extends E> coll) {
088        throw new UnsupportedOperationException("List is fixed size");
089    }
090
091    @Override
092    public void clear() {
093        throw new UnsupportedOperationException("List is fixed size");
094    }
095
096    @Override
097    public E get(final int index) {
098        return decorated().get(index);
099    }
100
101    @Override
102    public int indexOf(final Object object) {
103        return decorated().indexOf(object);
104    }
105
106    @Override
107    public Iterator<E> iterator() {
108        return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
109    }
110
111    @Override
112    public int lastIndexOf(final Object object) {
113        return decorated().lastIndexOf(object);
114    }
115
116    @Override
117    public ListIterator<E> listIterator() {
118        return new FixedSizeListIterator(decorated().listIterator(0));
119    }
120
121    @Override
122    public ListIterator<E> listIterator(final int index) {
123        return new FixedSizeListIterator(decorated().listIterator(index));
124    }
125
126    @Override
127    public E remove(final int index) {
128        throw new UnsupportedOperationException("List is fixed size");
129    }
130
131    @Override
132    public boolean remove(final Object object) {
133        throw new UnsupportedOperationException("List is fixed size");
134    }
135
136    @Override
137    public boolean removeAll(final Collection<?> coll) {
138        throw new UnsupportedOperationException("List is fixed size");
139    }
140
141    @Override
142    public boolean retainAll(final Collection<?> coll) {
143        throw new UnsupportedOperationException("List is fixed size");
144    }
145
146    @Override
147    public E set(final int index, final E object) {
148        return decorated().set(index, object);
149    }
150
151    @Override
152    public List<E> subList(final int fromIndex, final int toIndex) {
153        final List<E> sub = decorated().subList(fromIndex, toIndex);
154        return new FixedSizeList<>(sub);
155    }
156
157    /**
158     * List iterator that only permits changes via set()
159     */
160    private class FixedSizeListIterator extends AbstractListIteratorDecorator<E> {
161        protected FixedSizeListIterator(final ListIterator<E> iterator) {
162            super(iterator);
163        }
164        @Override
165        public void remove() {
166            throw new UnsupportedOperationException("List is fixed size");
167        }
168        @Override
169        public void add(final Object object) {
170            throw new UnsupportedOperationException("List is fixed size");
171        }
172    }
173
174    @Override
175    public boolean isFull() {
176        return true;
177    }
178
179    @Override
180    public int maxSize() {
181        return size();
182    }
183
184}