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.Unmodifiable;
26 import org.apache.commons.collections4.iterators.UnmodifiableIterator;
27 import org.apache.commons.collections4.iterators.UnmodifiableListIterator;
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public final class UnmodifiableList<E>
42 extends AbstractSerializableListDecorator<E>
43 implements Unmodifiable {
44
45
46 private static final long serialVersionUID = 6595182819922443652L;
47
48
49
50
51
52
53
54
55
56
57 public static <E> List<E> unmodifiableList(final List<? extends E> list) {
58 if (list instanceof Unmodifiable) {
59 @SuppressWarnings("unchecked")
60 final List<E> tmpList = (List<E>) list;
61 return tmpList;
62 }
63 return new UnmodifiableList<>(list);
64 }
65
66
67
68
69
70
71
72 @SuppressWarnings("unchecked")
73 public UnmodifiableList(final List<? extends E> list) {
74 super((List<E>) list);
75 }
76
77 @Override
78 public void add(final int index, final E object) {
79 throw new UnsupportedOperationException();
80 }
81
82 @Override
83 public boolean add(final Object object) {
84 throw new UnsupportedOperationException();
85 }
86
87 @Override
88 public boolean addAll(final Collection<? extends E> coll) {
89 throw new UnsupportedOperationException();
90 }
91
92 @Override
93 public boolean addAll(final int index, final Collection<? extends E> coll) {
94 throw new UnsupportedOperationException();
95 }
96
97 @Override
98 public void clear() {
99 throw new UnsupportedOperationException();
100 }
101
102 @Override
103 public Iterator<E> iterator() {
104 return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
105 }
106
107 @Override
108 public ListIterator<E> listIterator() {
109 return UnmodifiableListIterator.unmodifiableListIterator(decorated().listIterator());
110 }
111
112 @Override
113 public ListIterator<E> listIterator(final int index) {
114 return UnmodifiableListIterator.unmodifiableListIterator(decorated().listIterator(index));
115 }
116
117 @Override
118 public E remove(final int index) {
119 throw new UnsupportedOperationException();
120 }
121
122 @Override
123 public boolean remove(final Object object) {
124 throw new UnsupportedOperationException();
125 }
126
127 @Override
128 public boolean removeAll(final Collection<?> coll) {
129 throw new UnsupportedOperationException();
130 }
131
132
133
134
135 @Override
136 public boolean removeIf(final Predicate<? super E> filter) {
137 throw new UnsupportedOperationException();
138 }
139
140 @Override
141 public boolean retainAll(final Collection<?> coll) {
142 throw new UnsupportedOperationException();
143 }
144
145 @Override
146 public E set(final int index, final E object) {
147 throw new UnsupportedOperationException();
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 UnmodifiableList<>(sub);
154 }
155
156 }