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.SortedMap;
022
023/**
024 * This is a wrapper for a {@link SortedMap} 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 map (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#unmodifiableSortedMap(SortedMap)}, but by making it a
035 * public class we can make the return value of some methods more explicit.
036 * 
037 * @author Benjamin Hummel
038 */
039public class UnmodifiableSortedMap<K, V> extends UnmodifiableMap<K, V> implements SortedMap<K, V> {
040
041        /** Version used for serialization. */
042        private static final long serialVersionUID = 1;
043
044        /** The underlying sorted map. */
045        private final SortedMap<K, V> m;
046
047        /**
048         * Creates a new unmodifiable sorted map from another sorted map. All
049         * modifications to the underlying map will directly be visible in this
050         * wrapper.
051         */
052        public UnmodifiableSortedMap(SortedMap<K, V> m) {
053                super(m);
054                this.m = m;
055        }
056
057        /** {@inheritDoc} */
058        @Override
059        public Comparator<? super K> comparator() {
060                return m.comparator();
061        }
062
063        /** {@inheritDoc} */
064        @Override
065        public K firstKey() {
066                return m.firstKey();
067        }
068
069        /** {@inheritDoc} */
070        @Override
071        public UnmodifiableSortedMap<K, V> headMap(K toKey) {
072                return new UnmodifiableSortedMap<K, V>(m.headMap(toKey));
073        }
074
075        /** {@inheritDoc} */
076        @Override
077        public K lastKey() {
078                return m.lastKey();
079        }
080
081        /** {@inheritDoc} */
082        @Override
083        public UnmodifiableSortedMap<K, V> subMap(K fromKey, K toKey) {
084                return new UnmodifiableSortedMap<K, V>(m.subMap(fromKey, toKey));
085        }
086
087        /** {@inheritDoc} */
088        @Override
089        public UnmodifiableSortedMap<K, V> tailMap(K fromKey) {
090                return new UnmodifiableSortedMap<K, V>(m.tailMap(fromKey));
091        }
092}