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.Iterator ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import org.apache.commons.collections.BoundedMap; 28 import org.apache.commons.collections.collection.UnmodifiableCollection; 29 import org.apache.commons.collections.set.UnmodifiableSet; 30 31 52 public class FixedSizeMap 53 extends AbstractMapDecorator 54 implements Map , BoundedMap, Serializable { 55 56 57 private static final long serialVersionUID = 7450927208116179316L; 58 59 65 public static Map decorate(Map map) { 66 return new FixedSizeMap(map); 67 } 68 69 76 protected FixedSizeMap(Map map) { 77 super(map); 78 } 79 80 88 private void writeObject(ObjectOutputStream out) throws IOException { 89 out.defaultWriteObject(); 90 out.writeObject(map); 91 } 92 93 101 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException { 102 in.defaultReadObject(); 103 map = (Map ) in.readObject(); 104 } 105 106 public Object put(Object key, Object value) { 108 if (map.containsKey(key) == false) { 109 throw new IllegalArgumentException ("Cannot put new key/value pair - Map is fixed size"); 110 } 111 return map.put(key, value); 112 } 113 114 public void putAll(Map mapToCopy) { 115 for (Iterator it = mapToCopy.keySet().iterator(); it.hasNext(); ) { 116 if (mapToCopy.containsKey(it.next()) == false) { 117 throw new IllegalArgumentException ("Cannot put new key/value pair - Map is fixed size"); 118 } 119 } 120 map.putAll(mapToCopy); 121 } 122 123 public void clear() { 124 throw new UnsupportedOperationException ("Map is fixed size"); 125 } 126 127 public Object remove(Object key) { 128 throw new UnsupportedOperationException ("Map is fixed size"); 129 } 130 131 public Set entrySet() { 132 Set set = map.entrySet(); 133 return UnmodifiableSet.decorate(set); 135 } 136 137 public Set keySet() { 138 Set set = map.keySet(); 139 return UnmodifiableSet.decorate(set); 140 } 141 142 public Collection values() { 143 Collection coll = map.values(); 144 return UnmodifiableCollection.decorate(coll); 145 } 146 147 public boolean isFull() { 148 return true; 149 } 150 151 public int maxSize() { 152 return size(); 153 } 154 155 } 156 | Popular Tags |