001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. 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.apache.commons.collections4.list; 018 019import java.util.Collection; 020import java.util.Iterator; 021import java.util.List; 022import java.util.ListIterator; 023 024import org.apache.commons.collections4.Unmodifiable; 025import org.apache.commons.collections4.iterators.UnmodifiableIterator; 026import org.apache.commons.collections4.iterators.UnmodifiableListIterator; 027 028/** 029 * Decorates another <code>List</code> to ensure it can't be altered. 030 * <p> 031 * This class is Serializable from Commons Collections 3.1. 032 * <p> 033 * Attempts to modify it will result in an UnsupportedOperationException. 034 * 035 * @since 3.0 036 * @version $Id: UnmodifiableList.html 972421 2015-11-14 20:00:04Z tn $ 037 */ 038public final class UnmodifiableList<E> 039 extends AbstractSerializableListDecorator<E> 040 implements Unmodifiable { 041 042 /** Serialization version */ 043 private static final long serialVersionUID = 6595182819922443652L; 044 045 /** 046 * Factory method to create an unmodifiable list. 047 * 048 * @param <E> the type of the elements in the list 049 * @param list the list to decorate, must not be null 050 * @return a new unmodifiable list 051 * @throws IllegalArgumentException if list is null 052 * @since 4.0 053 */ 054 public static <E> List<E> unmodifiableList(final List<? extends E> list) { 055 if (list instanceof Unmodifiable) { 056 @SuppressWarnings("unchecked") // safe to upcast 057 final List<E> tmpList = (List<E>) list; 058 return tmpList; 059 } 060 return new UnmodifiableList<E>(list); 061 } 062 063 //----------------------------------------------------------------------- 064 /** 065 * Constructor that wraps (not copies). 066 * 067 * @param list the list to decorate, must not be null 068 * @throws IllegalArgumentException if list is null 069 */ 070 @SuppressWarnings("unchecked") // safe to upcast 071 public UnmodifiableList(final List<? extends E> list) { 072 super((List<E>) list); 073 } 074 075 //----------------------------------------------------------------------- 076 @Override 077 public Iterator<E> iterator() { 078 return UnmodifiableIterator.unmodifiableIterator(decorated().iterator()); 079 } 080 081 @Override 082 public boolean add(final Object object) { 083 throw new UnsupportedOperationException(); 084 } 085 086 @Override 087 public boolean addAll(final Collection<? extends E> coll) { 088 throw new UnsupportedOperationException(); 089 } 090 091 @Override 092 public void clear() { 093 throw new UnsupportedOperationException(); 094 } 095 096 @Override 097 public boolean remove(final Object object) { 098 throw new UnsupportedOperationException(); 099 } 100 101 @Override 102 public boolean removeAll(final Collection<?> coll) { 103 throw new UnsupportedOperationException(); 104 } 105 106 @Override 107 public boolean retainAll(final Collection<?> coll) { 108 throw new UnsupportedOperationException(); 109 } 110 111 //----------------------------------------------------------------------- 112 @Override 113 public ListIterator<E> listIterator() { 114 return UnmodifiableListIterator.umodifiableListIterator(decorated().listIterator()); 115 } 116 117 @Override 118 public ListIterator<E> listIterator(final int index) { 119 return UnmodifiableListIterator.umodifiableListIterator(decorated().listIterator(index)); 120 } 121 122 @Override 123 public void add(final int index, final E object) { 124 throw new UnsupportedOperationException(); 125 } 126 127 @Override 128 public boolean addAll(final int index, final Collection<? extends E> coll) { 129 throw new UnsupportedOperationException(); 130 } 131 132 @Override 133 public E remove(final int index) { 134 throw new UnsupportedOperationException(); 135 } 136 137 @Override 138 public E set(final int index, final E object) { 139 throw new UnsupportedOperationException(); 140 } 141 142 @Override 143 public List<E> subList(final int fromIndex, final int toIndex) { 144 final List<E> sub = decorated().subList(fromIndex, toIndex); 145 return new UnmodifiableList<E>(sub); 146 } 147 148}