1 16 package org.apache.commons.collections.map; 17 18 import java.io.IOException ; 19 import java.io.ObjectInputStream ; 20 import java.io.ObjectOutputStream ; 21 import java.io.Serializable ; 22 import java.util.Collection ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 import org.apache.commons.collections.IterableMap; 27 import org.apache.commons.collections.MapIterator; 28 import org.apache.commons.collections.Unmodifiable; 29 import org.apache.commons.collections.collection.UnmodifiableCollection; 30 import org.apache.commons.collections.iterators.EntrySetMapIterator; 31 import org.apache.commons.collections.iterators.UnmodifiableMapIterator; 32 import org.apache.commons.collections.set.UnmodifiableSet; 33 34 44 public final class UnmodifiableMap 45 extends AbstractMapDecorator 46 implements IterableMap, Unmodifiable, Serializable { 47 48 49 private static final long serialVersionUID = 2737023427269031941L; 50 51 57 public static Map decorate(Map map) { 58 if (map instanceof Unmodifiable) { 59 return map; 60 } 61 return new UnmodifiableMap(map); 62 } 63 64 71 private UnmodifiableMap(Map map) { 72 super(map); 73 } 74 75 83 private void writeObject(ObjectOutputStream out) throws IOException { 84 out.defaultWriteObject(); 85 out.writeObject(map); 86 } 87 88 96 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException { 97 in.defaultReadObject(); 98 map = (Map ) in.readObject(); 99 } 100 101 public void clear() { 103 throw new UnsupportedOperationException (); 104 } 105 106 public Object put(Object key, Object value) { 107 throw new UnsupportedOperationException (); 108 } 109 110 public void putAll(Map mapToCopy) { 111 throw new UnsupportedOperationException (); 112 } 113 114 public Object remove(Object key) { 115 throw new UnsupportedOperationException (); 116 } 117 118 public MapIterator mapIterator() { 119 if (map instanceof IterableMap) { 120 MapIterator it = ((IterableMap) map).mapIterator(); 121 return UnmodifiableMapIterator.decorate(it); 122 } else { 123 MapIterator it = new EntrySetMapIterator(map); 124 return UnmodifiableMapIterator.decorate(it); 125 } 126 } 127 128 public Set entrySet() { 129 Set set = super.entrySet(); 130 return UnmodifiableEntrySet.decorate(set); 131 } 132 133 public Set keySet() { 134 Set set = super.keySet(); 135 return UnmodifiableSet.decorate(set); 136 } 137 138 public Collection values() { 139 Collection coll = super.values(); 140 return UnmodifiableCollection.decorate(coll); 141 } 142 143 } 144 | Popular Tags |