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 */ 017 package org.apache.commons.collections.map; 018 019 import java.io.IOException; 020 import java.io.ObjectInputStream; 021 import java.io.ObjectOutputStream; 022 import java.io.Serializable; 023 import java.util.Collection; 024 import java.util.Comparator; 025 import java.util.Map; 026 import java.util.Set; 027 import java.util.SortedMap; 028 029 import org.apache.commons.collections.Unmodifiable; 030 import org.apache.commons.collections.collection.UnmodifiableCollection; 031 import org.apache.commons.collections.set.UnmodifiableSet; 032 033 /** 034 * Decorates another <code>SortedMap</code> to ensure it can't be altered. 035 * <p> 036 * This class is Serializable from Commons Collections 3.1. 037 * 038 * @since Commons Collections 3.0 039 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 040 * 041 * @author Stephen Colebourne 042 */ 043 public final class UnmodifiableSortedMap 044 extends AbstractSortedMapDecorator 045 implements Unmodifiable, Serializable { 046 047 /** Serialization version */ 048 private static final long serialVersionUID = 5805344239827376360L; 049 050 /** 051 * Factory method to create an unmodifiable sorted map. 052 * 053 * @param map the map to decorate, must not be null 054 * @throws IllegalArgumentException if map is null 055 */ 056 public static SortedMap decorate(SortedMap map) { 057 if (map instanceof Unmodifiable) { 058 return map; 059 } 060 return new UnmodifiableSortedMap(map); 061 } 062 063 //----------------------------------------------------------------------- 064 /** 065 * Constructor that wraps (not copies). 066 * 067 * @param map the map to decorate, must not be null 068 * @throws IllegalArgumentException if map is null 069 */ 070 private UnmodifiableSortedMap(SortedMap map) { 071 super(map); 072 } 073 074 //----------------------------------------------------------------------- 075 /** 076 * Write the map out using a custom routine. 077 * 078 * @param out the output stream 079 * @throws IOException 080 * @since Commons Collections 3.1 081 */ 082 private void writeObject(ObjectOutputStream out) throws IOException { 083 out.defaultWriteObject(); 084 out.writeObject(map); 085 } 086 087 /** 088 * Read the map in using a custom routine. 089 * 090 * @param in the input stream 091 * @throws IOException 092 * @throws ClassNotFoundException 093 * @since Commons Collections 3.1 094 */ 095 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { 096 in.defaultReadObject(); 097 map = (Map) in.readObject(); 098 } 099 100 //----------------------------------------------------------------------- 101 public void clear() { 102 throw new UnsupportedOperationException(); 103 } 104 105 public Object put(Object key, Object value) { 106 throw new UnsupportedOperationException(); 107 } 108 109 public void putAll(Map mapToCopy) { 110 throw new UnsupportedOperationException(); 111 } 112 113 public Object remove(Object key) { 114 throw new UnsupportedOperationException(); 115 } 116 117 public Set entrySet() { 118 Set set = super.entrySet(); 119 return UnmodifiableEntrySet.decorate(set); 120 } 121 122 public Set keySet() { 123 Set set = super.keySet(); 124 return UnmodifiableSet.decorate(set); 125 } 126 127 public Collection values() { 128 Collection coll = super.values(); 129 return UnmodifiableCollection.decorate(coll); 130 } 131 132 //----------------------------------------------------------------------- 133 public Object firstKey() { 134 return getSortedMap().firstKey(); 135 } 136 137 public Object lastKey() { 138 return getSortedMap().lastKey(); 139 } 140 141 public Comparator comparator() { 142 return getSortedMap().comparator(); 143 } 144 145 public SortedMap subMap(Object fromKey, Object toKey) { 146 SortedMap map = getSortedMap().subMap(fromKey, toKey); 147 return new UnmodifiableSortedMap(map); 148 } 149 150 public SortedMap headMap(Object toKey) { 151 SortedMap map = getSortedMap().headMap(toKey); 152 return new UnmodifiableSortedMap(map); 153 } 154 155 public SortedMap tailMap(Object fromKey) { 156 SortedMap map = getSortedMap().tailMap(fromKey); 157 return new UnmodifiableSortedMap(map); 158 } 159 160 }