001    /*--------------------------------------------------------------------------+
002    $Id: UnmodifiableSortedSet.java 26283 2010-02-18 11:18:57Z juergens $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | You may obtain a copy of the License at                                  |
009    |                                                                          |
010    |    http://www.apache.org/licenses/LICENSE-2.0                            |
011    |                                                                          |
012    | Unless required by applicable law or agreed to in writing, software      |
013    | distributed under the License is distributed on an "AS IS" BASIS,        |
014    | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015    | See the License for the specific language governing permissions and      |
016    | limitations under the License.                                           |
017    +--------------------------------------------------------------------------*/
018    package edu.tum.cs.commons.collections;
019    
020    import java.util.Collections;
021    import java.util.Comparator;
022    import java.util.SortedSet;
023    
024    /**
025     * This is a wrapper for a {@link SortedSet} prohibiting all calls which would
026     * modify its contents. As the construction of this class is performed in
027     * constant time it is prefered over copying the set (which takes linear time).
028     * Using this class is also preferred to using the <code>unmodifiableX()</code>
029     * in class {@link Collections} as they return the collection base type that
030     * does not signal, that the object ist unmodifiable. Using the classes in this
031     * package makes unmodifiability more explicit.
032     * <p>
033     * All prohibited methods throw an {@link UnsupportedOperationException}. The
034     * class is nearly the same as the one returned by
035     * {@link Collections#unmodifiableSortedSet(java.util.SortedSet)}, but by
036     * making it a public class we can make the return value of some methods more
037     * explicit.
038     * 
039     * @author Benjamin Hummel
040     * @author $Author: juergens $
041     * 
042     * @version $Revision: 26283 $
043     * @levd.rating GREEN Hash: 4A8EA373582003F0CC4E466C009BEA02
044     */
045    public class UnmodifiableSortedSet<E> extends UnmodifiableSet<E> implements
046                    SortedSet<E> {
047    
048            /** The underlying sorted set. */
049            private final SortedSet<E> s;
050    
051            /**
052             * Creates a new unmodifiable sorted set from another sorted set. All
053             * modifications to the underlying set will directly be visible in this
054             * wrapper.
055             */
056            public UnmodifiableSortedSet(SortedSet<E> s) {
057                    super(s);
058                    this.s = s;
059            }
060    
061            /** {@inheritDoc} */
062            public Comparator<? super E> comparator() {
063                    return s.comparator();
064            }
065    
066            /** {@inheritDoc} */
067            public E first() {
068                    return s.first();
069            }
070    
071            /** {@inheritDoc} */
072            public UnmodifiableSortedSet<E> headSet(E toElement) {
073                    return new UnmodifiableSortedSet<E>(s.headSet(toElement));
074            }
075    
076            /** {@inheritDoc} */
077            public E last() {
078                    return s.last();
079            }
080    
081            /** {@inheritDoc} */
082            public UnmodifiableSortedSet<E> subSet(E fromElement, E toElement) {
083                    return new UnmodifiableSortedSet<E>(s.subSet(fromElement, toElement));
084            }
085    
086            /** {@inheritDoc} */
087            public UnmodifiableSortedSet<E> tailSet(E fromElement) {
088                    return new UnmodifiableSortedSet<E>(s.tailSet(fromElement));
089            }
090    }