KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > list > UnmodifiableList


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.list;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.ListIterator JavaDoc;
22
23 import org.apache.commons.collections.Unmodifiable;
24 import org.apache.commons.collections.iterators.UnmodifiableIterator;
25 import org.apache.commons.collections.iterators.UnmodifiableListIterator;
26
27 /**
28  * Decorates another <code>List</code> to ensure it can't be altered.
29  * <p>
30  * This class is Serializable from Commons Collections 3.1.
31  *
32  * @since Commons Collections 3.0
33  * @version $Revision: 1.7 $ $Date: 2004/06/03 22:02:13 $
34  *
35  * @author Stephen Colebourne
36  */

37 public final class UnmodifiableList
38         extends AbstractSerializableListDecorator
39         implements Unmodifiable {
40
41     /** Serialization version */
42     private static final long serialVersionUID = 6595182819922443652L;
43
44     /**
45      * Factory method to create an unmodifiable list.
46      *
47      * @param list the list to decorate, must not be null
48      * @throws IllegalArgumentException if list is null
49      */

50     public static List JavaDoc decorate(List JavaDoc list) {
51         if (list instanceof Unmodifiable) {
52             return list;
53         }
54         return new UnmodifiableList(list);
55     }
56
57     //-----------------------------------------------------------------------
58
/**
59      * Constructor that wraps (not copies).
60      *
61      * @param list the list to decorate, must not be null
62      * @throws IllegalArgumentException if list is null
63      */

64     private UnmodifiableList(List JavaDoc list) {
65         super(list);
66     }
67
68     //-----------------------------------------------------------------------
69
public Iterator JavaDoc iterator() {
70         return UnmodifiableIterator.decorate(getCollection().iterator());
71     }
72
73     public boolean add(Object JavaDoc object) {
74         throw new UnsupportedOperationException JavaDoc();
75     }
76
77     public boolean addAll(Collection JavaDoc coll) {
78         throw new UnsupportedOperationException JavaDoc();
79     }
80
81     public void clear() {
82         throw new UnsupportedOperationException JavaDoc();
83     }
84
85     public boolean remove(Object JavaDoc object) {
86         throw new UnsupportedOperationException JavaDoc();
87     }
88
89     public boolean removeAll(Collection JavaDoc coll) {
90         throw new UnsupportedOperationException JavaDoc();
91     }
92
93     public boolean retainAll(Collection JavaDoc coll) {
94         throw new UnsupportedOperationException JavaDoc();
95     }
96
97     //-----------------------------------------------------------------------
98
public ListIterator JavaDoc listIterator() {
99         return UnmodifiableListIterator.decorate(getList().listIterator());
100     }
101
102     public ListIterator JavaDoc listIterator(int index) {
103         return UnmodifiableListIterator.decorate(getList().listIterator(index));
104     }
105
106     public void add(int index, Object JavaDoc object) {
107         throw new UnsupportedOperationException JavaDoc();
108     }
109
110     public boolean addAll(int index, Collection JavaDoc coll) {
111         throw new UnsupportedOperationException JavaDoc();
112     }
113
114     public Object JavaDoc remove(int index) {
115         throw new UnsupportedOperationException JavaDoc();
116     }
117
118     public Object JavaDoc set(int index, Object JavaDoc object) {
119         throw new UnsupportedOperationException JavaDoc();
120     }
121
122     public List JavaDoc subList(int fromIndex, int toIndex) {
123         List JavaDoc sub = getList().subList(fromIndex, toIndex);
124         return new UnmodifiableList(sub);
125     }
126
127 }
128
Popular Tags