View Javadoc

1   /*	
2   	Copyright 2007-2014 Fraunhofer IGD, http://www.igd.fraunhofer.de
3   	Fraunhofer-Gesellschaft - Institute for Computer Graphics Research
4   	
5   	See the NOTICE file distributed with this work for additional 
6   	information regarding copyright ownership
7   	
8   	Licensed under the Apache License, Version 2.0 (the "License");
9   	you may not use this file except in compliance with the License.
10  	You may obtain a copy of the License at
11  	
12  	  http://www.apache.org/licenses/LICENSE-2.0
13  	
14  	Unless required by applicable law or agreed to in writing, software
15  	distributed under the License is distributed on an "AS IS" BASIS,
16  	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  	See the License for the specific language governing permissions and
18  	limitations under the License.
19   */
20  package org.universAAL.middleware.rdf;
21  
22  import java.util.Collection;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.ListIterator;
26  
27  public final class UnmodifiableResourceList implements List {
28  
29      /** A safe iterator that does not allow modifications. */
30      private static class SafeIterator implements Iterator {
31  	private Iterator it;
32  
33  	SafeIterator(Iterator it) {
34  	    this.it = it;
35  	}
36  
37  	public boolean hasNext() {
38  	    return it.hasNext();
39  	}
40  
41  	public Object next() {
42  	    return UnmodifiableResource.getUnmodifiable(it.next());
43  	}
44  
45  	public void remove() {
46  	}
47      }
48  
49      /** A safe iterator that does not allow modifications. */
50      private static class SafeListIterator extends SafeIterator implements
51  	    ListIterator {
52  	private ListIterator it;
53  
54  	SafeListIterator(ListIterator it) {
55  	    super(it);
56  	    this.it = it;
57  	}
58  
59  	public void add(Object arg0) {
60  	}
61  
62  	public boolean hasPrevious() {
63  	    return it.hasPrevious();
64  	}
65  
66  	public int nextIndex() {
67  	    return it.nextIndex();
68  	}
69  
70  	public Object previous() {
71  	    return UnmodifiableResource.getUnmodifiable(it.previous());
72  	}
73  
74  	public int previousIndex() {
75  	    return it.nextIndex();
76  	}
77  
78  	public void set(Object arg0) {
79  	}
80      }
81  
82      private List l;
83  
84      public UnmodifiableResourceList(List l) {
85  	this.l = l;
86      }
87  
88      public static final SafeIterator getIterator(Iterator it) {
89  	return new SafeIterator(it);
90      }
91  
92      public boolean add(Object arg0) {
93  	return false;
94      }
95  
96      public void add(int arg0, Object arg1) {
97      }
98  
99      public boolean addAll(Collection arg0) {
100 	return false;
101     }
102 
103     public boolean addAll(int arg0, Collection arg1) {
104 	return false;
105     }
106 
107     public void clear() {
108     }
109 
110     public boolean contains(Object o) {
111 	return l.contains(o);
112     }
113 
114     public boolean containsAll(Collection arg0) {
115 	return l.containsAll(arg0);
116     }
117 
118     public Object get(int index) {
119 	Object o = l.get(index);
120 	if (o instanceof Resource)
121 	    return new UnmodifiableResource((Resource) o);
122 	return o;
123     }
124 
125     public int indexOf(Object o) {
126 	return l.indexOf(o);
127     }
128 
129     public boolean isEmpty() {
130 	return l.isEmpty();
131     }
132 
133     public Iterator iterator() {
134 	return new SafeIterator(l.iterator());
135     }
136 
137     public int lastIndexOf(Object o) {
138 	return l.lastIndexOf(o);
139     }
140 
141     public ListIterator listIterator() {
142 	return new SafeListIterator(l.listIterator());
143     }
144 
145     public ListIterator listIterator(int index) {
146 	return new SafeListIterator(l.listIterator(index));
147     }
148 
149     public boolean remove(Object o) {
150 	return false;
151     }
152 
153     public Object remove(int index) {
154 	return null;
155     }
156 
157     public boolean removeAll(Collection arg0) {
158 	return false;
159     }
160 
161     public boolean retainAll(Collection arg0) {
162 	return false;
163     }
164 
165     public Object set(int arg0, Object arg1) {
166 	return null;
167     }
168 
169     public int size() {
170 	return l.size();
171     }
172 
173     public List subList(int fromIndex, int toIndex) {
174 	return new UnmodifiableResourceList(l.subList(fromIndex, toIndex));
175     }
176 
177     public Object[] toArray() {
178 	Object[] o = l.toArray();
179 	for (int i = 0; i < o.length; i++) {
180 	    if (o[i] instanceof Resource)
181 		o[i] = new UnmodifiableResource((Resource) o[i]);
182 	}
183 	return o;
184     }
185 
186     public Object[] toArray(Object[] arg0) {
187 	Object[] o = l.toArray(arg0);
188 	for (int i = 0; i < o.length; i++) {
189 	    if (o[i] instanceof Resource)
190 		o[i] = new UnmodifiableResource((Resource) o[i]);
191 	}
192 	return o;
193     }
194 }