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 */ 017 package org.apache.commons.collections.list; 018 019 import java.util.Collection; 020 import java.util.Iterator; 021 import java.util.List; 022 import java.util.ListIterator; 023 024 import org.apache.commons.collections.Unmodifiable; 025 import org.apache.commons.collections.iterators.UnmodifiableIterator; 026 import org.apache.commons.collections.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 * 033 * @since Commons Collections 3.0 034 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 035 * 036 * @author Stephen Colebourne 037 */ 038 public final class UnmodifiableList 039 extends AbstractSerializableListDecorator 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 list the list to decorate, must not be null 049 * @throws IllegalArgumentException if list is null 050 */ 051 public static List decorate(List list) { 052 if (list instanceof Unmodifiable) { 053 return list; 054 } 055 return new UnmodifiableList(list); 056 } 057 058 //----------------------------------------------------------------------- 059 /** 060 * Constructor that wraps (not copies). 061 * 062 * @param list the list to decorate, must not be null 063 * @throws IllegalArgumentException if list is null 064 */ 065 private UnmodifiableList(List list) { 066 super(list); 067 } 068 069 //----------------------------------------------------------------------- 070 public Iterator iterator() { 071 return UnmodifiableIterator.decorate(getCollection().iterator()); 072 } 073 074 public boolean add(Object object) { 075 throw new UnsupportedOperationException(); 076 } 077 078 public boolean addAll(Collection coll) { 079 throw new UnsupportedOperationException(); 080 } 081 082 public void clear() { 083 throw new UnsupportedOperationException(); 084 } 085 086 public boolean remove(Object object) { 087 throw new UnsupportedOperationException(); 088 } 089 090 public boolean removeAll(Collection coll) { 091 throw new UnsupportedOperationException(); 092 } 093 094 public boolean retainAll(Collection coll) { 095 throw new UnsupportedOperationException(); 096 } 097 098 //----------------------------------------------------------------------- 099 public ListIterator listIterator() { 100 return UnmodifiableListIterator.decorate(getList().listIterator()); 101 } 102 103 public ListIterator listIterator(int index) { 104 return UnmodifiableListIterator.decorate(getList().listIterator(index)); 105 } 106 107 public void add(int index, Object object) { 108 throw new UnsupportedOperationException(); 109 } 110 111 public boolean addAll(int index, Collection coll) { 112 throw new UnsupportedOperationException(); 113 } 114 115 public Object remove(int index) { 116 throw new UnsupportedOperationException(); 117 } 118 119 public Object set(int index, Object object) { 120 throw new UnsupportedOperationException(); 121 } 122 123 public List subList(int fromIndex, int toIndex) { 124 List sub = getList().subList(fromIndex, toIndex); 125 return new UnmodifiableList(sub); 126 } 127 128 }