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