1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.list;
18
19 import java.util.Collection;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.ListIterator;
23 import java.util.function.Predicate;
24
25 import org.apache.commons.collections4.BoundedCollection;
26 import org.apache.commons.collections4.iterators.AbstractListIteratorDecorator;
27 import org.apache.commons.collections4.iterators.UnmodifiableIterator;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class FixedSizeList<E>
49 extends AbstractSerializableListDecorator<E>
50 implements BoundedCollection<E> {
51
52
53 private static final long serialVersionUID = -2218010673611160319L;
54
55
56
57
58
59
60
61
62
63
64 public static <E> FixedSizeList<E> fixedSizeList(final List<E> list) {
65 return new FixedSizeList<>(list);
66 }
67
68
69
70
71
72
73
74
75 protected FixedSizeList(final List<E> list) {
76 super(list);
77 }
78
79
80 @Override
81 public boolean add(final E object) {
82 throw unsupportedOperationException();
83 }
84
85 @Override
86 public void add(final int index, final E object) {
87 throw unsupportedOperationException();
88 }
89
90 @Override
91 public boolean addAll(final Collection<? extends E> coll) {
92 throw unsupportedOperationException();
93 }
94
95 @Override
96 public boolean addAll(final int index, final Collection<? extends E> coll) {
97 throw unsupportedOperationException();
98 }
99
100 @Override
101 public void clear() {
102 throw unsupportedOperationException();
103 }
104
105 @Override
106 public E get(final int index) {
107 return decorated().get(index);
108 }
109
110 @Override
111 public int indexOf(final Object object) {
112 return decorated().indexOf(object);
113 }
114
115 @Override
116 public Iterator<E> iterator() {
117 return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
118 }
119
120 @Override
121 public int lastIndexOf(final Object object) {
122 return decorated().lastIndexOf(object);
123 }
124
125 @Override
126 public ListIterator<E> listIterator() {
127 return new FixedSizeListIterator(decorated().listIterator(0));
128 }
129
130 @Override
131 public ListIterator<E> listIterator(final int index) {
132 return new FixedSizeListIterator(decorated().listIterator(index));
133 }
134
135 @Override
136 public E remove(final int index) {
137 throw unsupportedOperationException();
138 }
139
140 @Override
141 public boolean remove(final Object object) {
142 throw unsupportedOperationException();
143 }
144
145
146
147
148 @Override
149 public boolean removeIf(Predicate<? super E> filter) {
150 throw unsupportedOperationException();
151 }
152
153 @Override
154 public boolean removeAll(final Collection<?> coll) {
155 throw unsupportedOperationException();
156 }
157
158 @Override
159 public boolean retainAll(final Collection<?> coll) {
160 throw unsupportedOperationException();
161 }
162
163 @Override
164 public E set(final int index, final E object) {
165 return decorated().set(index, object);
166 }
167
168 @Override
169 public List<E> subList(final int fromIndex, final int toIndex) {
170 final List<E> sub = decorated().subList(fromIndex, toIndex);
171 return new FixedSizeList<>(sub);
172 }
173
174
175
176
177 private class FixedSizeListIterator extends AbstractListIteratorDecorator<E> {
178 protected FixedSizeListIterator(final ListIterator<E> iterator) {
179 super(iterator);
180 }
181 @Override
182 public void remove() {
183 throw unsupportedOperationException();
184 }
185 @Override
186 public void add(final Object object) {
187 throw unsupportedOperationException();
188 }
189 }
190
191 @Override
192 public boolean isFull() {
193 return true;
194 }
195
196 @Override
197 public int maxSize() {
198 return size();
199 }
200
201 private static UnsupportedOperationException unsupportedOperationException() {
202 return new UnsupportedOperationException("List is fixed size");
203 }
204
205 }