001/*-------------------------------------------------------------------------+ 002| | 003| Copyright 2005-2011 The ConQAT Project | 004| | 005| Licensed under the Apache License, Version 2.0 (the "License"); | 006| you may not use this file except in compliance with the License. | 007| You may obtain a copy of the License at | 008| | 009| http://www.apache.org/licenses/LICENSE-2.0 | 010| | 011| Unless required by applicable law or agreed to in writing, software | 012| distributed under the License is distributed on an "AS IS" BASIS, | 013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 014| See the License for the specific language governing permissions and | 015| limitations under the License. | 016+-------------------------------------------------------------------------*/ 017package org.conqat.lib.commons.collections; 018 019import java.util.Collection; 020import java.util.Collections; 021import java.util.List; 022 023/** 024 * This is a wrapper for a {@link List} prohibiting all calls which would modify 025 * its contents. As the construction of this class is performed in constant time 026 * it is preferred over copying the list (which takes linear time). Using this 027 * class is also preferred to using the <code>unmodifiableX()</code> in class 028 * {@link Collections} as they return the collection base type that does not 029 * signal that the object is unmodifiable. Using the classes in this package 030 * makes unmodifiability more explicit. 031 * <p> 032 * All prohibited methods throw an {@link UnsupportedOperationException}. The 033 * class is nearly the same as the one returned by 034 * {@link Collections#unmodifiableList(List)}, but by making it a public class 035 * we can make the return value of some methods more explicit. 036 * <p> 037 * This list is serializable if the wrapped list is serializable. 038 * 039 * @author Benjamin Hummel 040 */ 041public class UnmodifiableList<E> extends UnmodifiableCollection<E> implements List<E> { 042 043 /** Version used for serialization. */ 044 private static final long serialVersionUID = 1; 045 046 /** The underlying list. */ 047 protected final List<E> l; 048 049 /** 050 * Creates a new unmodifiable list from another list. All modifications to the 051 * underlying list will directly be visible in this wrapper. 052 */ 053 public UnmodifiableList(List<E> l) { 054 super(l); 055 this.l = l; 056 } 057 058 /** {@inheritDoc} */ 059 @Override 060 public E get(int index) { 061 return l.get(index); 062 } 063 064 /** {@inheritDoc} */ 065 @Override 066 public int indexOf(Object o) { 067 return l.indexOf(o); 068 } 069 070 /** {@inheritDoc} */ 071 @Override 072 public int lastIndexOf(Object o) { 073 return l.lastIndexOf(o); 074 } 075 076 /** {@inheritDoc} */ 077 @Override 078 public UnmodifiableListIterator<E> listIterator() { 079 return new UnmodifiableListIterator<>(l.listIterator()); 080 } 081 082 /** {@inheritDoc} */ 083 @Override 084 public UnmodifiableListIterator<E> listIterator(int index) { 085 return new UnmodifiableListIterator<>(l.listIterator(index)); 086 } 087 088 /** {@inheritDoc} */ 089 @Override 090 public List<E> subList(int fromIndex, int toIndex) { 091 return new UnmodifiableList<>(l.subList(fromIndex, toIndex)); 092 } 093 094 /** 095 * Operation is not supported. 096 */ 097 @Override 098 public void add(int arg0, E arg1) { 099 throw new UnsupportedOperationException(); 100 } 101 102 /** 103 * Operation is not supported. 104 */ 105 @Override 106 public boolean addAll(int arg0, Collection<? extends E> arg1) { 107 throw new UnsupportedOperationException(); 108 } 109 110 /** 111 * Operation is not supported. 112 */ 113 @Override 114 public E remove(int arg0) { 115 throw new UnsupportedOperationException(); 116 } 117 118 /** 119 * Operation is not supported. 120 */ 121 @Override 122 public E set(int arg0, E arg1) { 123 throw new UnsupportedOperationException(); 124 } 125}