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