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