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.collection;
018
019import java.util.Collection;
020import java.util.Iterator;
021
022import org.apache.commons.collections4.BoundedCollection;
023import org.apache.commons.collections4.Unmodifiable;
024import org.apache.commons.collections4.iterators.UnmodifiableIterator;
025
026/**
027 * {@link UnmodifiableBoundedCollection} decorates another
028 * {@link BoundedCollection} to ensure it can't be altered.
029 * <p>
030 * If a BoundedCollection is first wrapped in some other collection decorator,
031 * such as synchronized or predicated, the BoundedCollection methods are no
032 * longer accessible.
033 * The factory on this class will attempt to retrieve the bounded nature by
034 * examining the package scope variables.
035 * <p>
036 * This class is Serializable from Commons Collections 3.1.
037 * <p>
038 * Attempts to modify it will result in an UnsupportedOperationException.
039 *
040 * @since 3.0
041 * @version $Id: UnmodifiableBoundedCollection.html 972421 2015-11-14 20:00:04Z tn $
042 */
043public final class UnmodifiableBoundedCollection<E> extends AbstractCollectionDecorator<E>
044        implements BoundedCollection<E>, Unmodifiable {
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 <E> the type of the elements in the collection
053     * @param coll  the <code>BoundedCollection</code> to decorate, must not be null
054     * @return a new unmodifiable bounded collection
055     * @throws IllegalArgumentException if {@code coll} is {@code null}
056     * @since 4.0
057     */
058    public static <E> BoundedCollection<E> unmodifiableBoundedCollection(final BoundedCollection<? extends E> coll) {
059        if (coll instanceof Unmodifiable) {
060            @SuppressWarnings("unchecked") // safe to upcast
061            final BoundedCollection<E> tmpColl = (BoundedCollection<E>) coll;
062            return tmpColl;
063        }
064        return new UnmodifiableBoundedCollection<E>(coll);
065    }
066
067    /**
068     * Factory method to create an unmodifiable bounded collection.
069     * <p>
070     * This method is capable of drilling down through up to 1000 other decorators
071     * to find a suitable BoundedCollection.
072     *
073     * @param <E> the type of the elements in the collection
074     * @param coll  the <code>BoundedCollection</code> to decorate, must not be null
075     * @return a new unmodifiable bounded collection
076     * @throws IllegalArgumentException if {@code coll} is {@code null}
077     * @since 4.0
078     */
079    @SuppressWarnings("unchecked")
080    public static <E> BoundedCollection<E> unmodifiableBoundedCollection(Collection<? extends E> coll) {
081        if (coll == null) {
082            throw new IllegalArgumentException("The collection must not be null");
083        }
084
085        // handle decorators
086        for (int i = 0; i < 1000; i++) {  // counter to prevent infinite looping
087            if (coll instanceof BoundedCollection) {
088                break;  // normal loop exit
089            }
090            if (coll instanceof AbstractCollectionDecorator) {
091                coll = ((AbstractCollectionDecorator<E>) coll).decorated();
092            } else if (coll instanceof SynchronizedCollection) {
093                coll = ((SynchronizedCollection<E>) coll).decorated();
094            }
095        }
096
097        if (coll instanceof BoundedCollection == false) {
098            throw new IllegalArgumentException("The collection is not a bounded collection");
099        }
100        return new UnmodifiableBoundedCollection<E>((BoundedCollection<E>) coll);
101    }
102
103    /**
104     * Constructor that wraps (not copies).
105     *
106     * @param coll  the collection to decorate, must not be null
107     * @throws IllegalArgumentException if coll is null
108     */
109    @SuppressWarnings("unchecked") // safe to upcast
110    private UnmodifiableBoundedCollection(final BoundedCollection<? extends E> coll) {
111        super((BoundedCollection<E>) coll);
112    }
113
114    //-----------------------------------------------------------------------
115    @Override
116    public Iterator<E> iterator() {
117        return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
118    }
119
120    @Override
121    public boolean add(final E object) {
122        throw new UnsupportedOperationException();
123    }
124
125    @Override
126    public boolean addAll(final Collection<? extends E> coll) {
127        throw new UnsupportedOperationException();
128    }
129
130    @Override
131    public void clear() {
132        throw new UnsupportedOperationException();
133    }
134
135    @Override
136    public boolean remove(final Object object) {
137        throw new UnsupportedOperationException();
138    }
139
140    @Override
141    public boolean removeAll(final Collection<?> coll) {
142        throw new UnsupportedOperationException();
143    }
144
145    @Override
146    public boolean retainAll(final Collection<?> coll) {
147        throw new UnsupportedOperationException();
148    }
149
150    //-----------------------------------------------------------------------
151    public boolean isFull() {
152        return decorated().isFull();
153    }
154
155    public int maxSize() {
156        return decorated().maxSize();
157    }
158
159    @Override
160    protected BoundedCollection<E> decorated() {
161        return (BoundedCollection<E>) super.decorated();
162    }
163}