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