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.collection; 018 019 import java.util.Collection; 020 import java.util.Iterator; 021 022 import org.apache.commons.collections.BoundedCollection; 023 import org.apache.commons.collections.iterators.UnmodifiableIterator; 024 025 /** 026 * <code>UnmodifiableBoundedCollection</code> decorates another 027 * <code>BoundedCollection</code> to ensure it can't be altered. 028 * <p> 029 * If a BoundedCollection is first wrapped in some other collection decorator, 030 * such as synchronized or predicated, the BoundedCollection methods are no 031 * longer accessible. 032 * The factory on this class will attempt to retrieve the bounded nature by 033 * examining the package scope variables. 034 * <p> 035 * This class is Serializable from Commons Collections 3.1. 036 * 037 * @since Commons Collections 3.0 038 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 039 * 040 * @author Stephen Colebourne 041 */ 042 public final class UnmodifiableBoundedCollection 043 extends AbstractSerializableCollectionDecorator 044 implements BoundedCollection { 045 046 /** Serialization version */ 047 private static final long serialVersionUID = -7112672385450340330L; 048 049 /** 050 * Factory method to create an unmodifiable bounded collection. 051 * 052 * @param coll the <code>BoundedCollection</code> to decorate, must not be null 053 * @return a new unmodifiable bounded collection 054 * @throws IllegalArgumentException if bag is null 055 */ 056 public static BoundedCollection decorate(BoundedCollection coll) { 057 return new UnmodifiableBoundedCollection(coll); 058 } 059 060 /** 061 * Factory method to create an unmodifiable bounded collection. 062 * <p> 063 * This method is capable of drilling down through up to 1000 other decorators 064 * to find a suitable BoundedCollection. 065 * 066 * @param coll the <code>BoundedCollection</code> to decorate, must not be null 067 * @return a new unmodifiable bounded collection 068 * @throws IllegalArgumentException if bag is null 069 */ 070 public static BoundedCollection decorateUsing(Collection coll) { 071 if (coll == null) { 072 throw new IllegalArgumentException("The collection must not be null"); 073 } 074 075 // handle decorators 076 for (int i = 0; i < 1000; i++) { // counter to prevent infinite looping 077 if (coll instanceof BoundedCollection) { 078 break; // normal loop exit 079 } else if (coll instanceof AbstractCollectionDecorator) { 080 coll = ((AbstractCollectionDecorator) coll).collection; 081 } else if (coll instanceof SynchronizedCollection) { 082 coll = ((SynchronizedCollection) coll).collection; 083 } else { 084 break; // normal loop exit 085 } 086 } 087 088 if (coll instanceof BoundedCollection == false) { 089 throw new IllegalArgumentException("The collection is not a bounded collection"); 090 } 091 return new UnmodifiableBoundedCollection((BoundedCollection) coll); 092 } 093 094 /** 095 * Constructor that wraps (not copies). 096 * 097 * @param coll the collection to decorate, must not be null 098 * @throws IllegalArgumentException if coll is null 099 */ 100 private UnmodifiableBoundedCollection(BoundedCollection coll) { 101 super(coll); 102 } 103 104 //----------------------------------------------------------------------- 105 public Iterator iterator() { 106 return UnmodifiableIterator.decorate(getCollection().iterator()); 107 } 108 109 public boolean add(Object object) { 110 throw new UnsupportedOperationException(); 111 } 112 113 public boolean addAll(Collection coll) { 114 throw new UnsupportedOperationException(); 115 } 116 117 public void clear() { 118 throw new UnsupportedOperationException(); 119 } 120 121 public boolean remove(Object object) { 122 throw new UnsupportedOperationException(); 123 } 124 125 public boolean removeAll(Collection coll) { 126 throw new UnsupportedOperationException(); 127 } 128 129 public boolean retainAll(Collection coll) { 130 throw new UnsupportedOperationException(); 131 } 132 133 //----------------------------------------------------------------------- 134 public boolean isFull() { 135 return ((BoundedCollection) collection).isFull(); 136 } 137 138 public int maxSize() { 139 return ((BoundedCollection) collection).maxSize(); 140 } 141 142 }