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.Collections;
020import java.util.Comparator;
021import java.util.SortedSet;
022
023/**
024 * This is a wrapper for a {@link SortedSet} prohibiting all calls which would
025 * modify its contents. As the construction of this class is performed in
026 * constant time it is prefered over copying the set (which takes linear time).
027 * Using this class is also preferred to using the <code>unmodifiableX()</code>
028 * in class {@link Collections} as they return the collection base type that
029 * does not signal, that the object ist unmodifiable. Using the classes in this
030 * package 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#unmodifiableSortedSet(java.util.SortedSet)}, but by making
035 * it a public class we can make the return value of some methods more explicit.
036 * 
037 * @author Benjamin Hummel
038 */
039public class UnmodifiableSortedSet<E> extends UnmodifiableSet<E> implements SortedSet<E> {
040
041        /** Version used for serialization. */
042        private static final long serialVersionUID = 1;
043
044        /** The underlying sorted set. */
045        private final SortedSet<E> s;
046
047        /**
048         * Creates a new unmodifiable sorted set from another sorted set. All
049         * modifications to the underlying set will directly be visible in this
050         * wrapper.
051         */
052        public UnmodifiableSortedSet(SortedSet<E> s) {
053                super(s);
054                this.s = s;
055        }
056
057        /** {@inheritDoc} */
058        @Override
059        public Comparator<? super E> comparator() {
060                return s.comparator();
061        }
062
063        /** {@inheritDoc} */
064        @Override
065        public E first() {
066                return s.first();
067        }
068
069        /** {@inheritDoc} */
070        @Override
071        public UnmodifiableSortedSet<E> headSet(E toElement) {
072                return new UnmodifiableSortedSet<E>(s.headSet(toElement));
073        }
074
075        /** {@inheritDoc} */
076        @Override
077        public E last() {
078                return s.last();
079        }
080
081        /** {@inheritDoc} */
082        @Override
083        public UnmodifiableSortedSet<E> subSet(E fromElement, E toElement) {
084                return new UnmodifiableSortedSet<E>(s.subSet(fromElement, toElement));
085        }
086
087        /** {@inheritDoc} */
088        @Override
089        public UnmodifiableSortedSet<E> tailSet(E fromElement) {
090                return new UnmodifiableSortedSet<E>(s.tailSet(fromElement));
091        }
092}