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 * NOTE: 035 * Modifying the decorated list directly would results in influencing the outcome 036 * of method calls on this object. For example, the bounds of this list would reflect 037 * a newly added object to the underlying list. 038 * <p> 039 * This class is Serializable from Commons Collections 3.1. 040 * 041 * @param <E> the type of elements in this collection 042 * @since 3.0 043 */ 044public class FixedSizeList<E> 045 extends AbstractSerializableListDecorator<E> 046 implements BoundedCollection<E> { 047 048 /** Serialization version */ 049 private static final long serialVersionUID = -2218010673611160319L; 050 051 /** 052 * Factory method to create a fixed size list. 053 * 054 * @param <E> the type of the elements in the list 055 * @param list the list to decorate, must not be null 056 * @return a new fixed size list 057 * @throws NullPointerException if list is null 058 * @since 4.0 059 */ 060 public static <E> FixedSizeList<E> fixedSizeList(final List<E> list) { 061 return new FixedSizeList<>(list); 062 } 063 064 //----------------------------------------------------------------------- 065 /** 066 * Constructor that wraps (not copies). 067 * 068 * @param list the list to decorate, must not be null 069 * @throws NullPointerException if list is null 070 */ 071 protected FixedSizeList(final List<E> list) { 072 super(list); 073 } 074 075 //----------------------------------------------------------------------- 076 @Override 077 public boolean add(final E object) { 078 throw unsupportedOperationException(); 079 } 080 081 @Override 082 public void add(final int index, final E object) { 083 throw unsupportedOperationException(); 084 } 085 086 @Override 087 public boolean addAll(final Collection<? extends E> coll) { 088 throw unsupportedOperationException(); 089 } 090 091 @Override 092 public boolean addAll(final int index, final Collection<? extends E> coll) { 093 throw unsupportedOperationException(); 094 } 095 096 @Override 097 public void clear() { 098 throw unsupportedOperationException(); 099 } 100 101 @Override 102 public E get(final int index) { 103 return decorated().get(index); 104 } 105 106 @Override 107 public int indexOf(final Object object) { 108 return decorated().indexOf(object); 109 } 110 111 @Override 112 public Iterator<E> iterator() { 113 return UnmodifiableIterator.unmodifiableIterator(decorated().iterator()); 114 } 115 116 @Override 117 public int lastIndexOf(final Object object) { 118 return decorated().lastIndexOf(object); 119 } 120 121 @Override 122 public ListIterator<E> listIterator() { 123 return new FixedSizeListIterator(decorated().listIterator(0)); 124 } 125 126 @Override 127 public ListIterator<E> listIterator(final int index) { 128 return new FixedSizeListIterator(decorated().listIterator(index)); 129 } 130 131 @Override 132 public E remove(final int index) { 133 throw unsupportedOperationException(); 134 } 135 136 @Override 137 public boolean remove(final Object object) { 138 throw unsupportedOperationException(); 139 } 140 141 @Override 142 public boolean removeAll(final Collection<?> coll) { 143 throw unsupportedOperationException(); 144 } 145 146 @Override 147 public boolean retainAll(final Collection<?> coll) { 148 throw unsupportedOperationException(); 149 } 150 151 @Override 152 public E set(final int index, final E object) { 153 return decorated().set(index, object); 154 } 155 156 @Override 157 public List<E> subList(final int fromIndex, final int toIndex) { 158 final List<E> sub = decorated().subList(fromIndex, toIndex); 159 return new FixedSizeList<>(sub); 160 } 161 162 /** 163 * List iterator that only permits changes via set() 164 */ 165 private class FixedSizeListIterator extends AbstractListIteratorDecorator<E> { 166 protected FixedSizeListIterator(final ListIterator<E> iterator) { 167 super(iterator); 168 } 169 @Override 170 public void remove() { 171 throw unsupportedOperationException(); 172 } 173 @Override 174 public void add(final Object object) { 175 throw unsupportedOperationException(); 176 } 177 } 178 179 @Override 180 public boolean isFull() { 181 return true; 182 } 183 184 @Override 185 public int maxSize() { 186 return size(); 187 } 188 189 private static UnsupportedOperationException unsupportedOperationException() { 190 return new UnsupportedOperationException("List is fixed size"); 191 } 192 193}