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 import java.util.SortedMap ; 27 28 import org.apache.commons.collections.BoundedMap; 29 import org.apache.commons.collections.collection.UnmodifiableCollection; 30 import org.apache.commons.collections.set.UnmodifiableSet; 31 32 53 public class FixedSizeSortedMap 54 extends AbstractSortedMapDecorator 55 implements SortedMap , BoundedMap, Serializable { 56 57 58 private static final long serialVersionUID = 3126019624511683653L; 59 60 66 public static SortedMap decorate(SortedMap map) { 67 return new FixedSizeSortedMap(map); 68 } 69 70 77 protected FixedSizeSortedMap(SortedMap map) { 78 super(map); 79 } 80 81 86 protected SortedMap getSortedMap() { 87 return (SortedMap ) map; 88 } 89 90 94 private void writeObject(ObjectOutputStream out) throws IOException { 95 out.defaultWriteObject(); 96 out.writeObject(map); 97 } 98 99 102 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException { 103 in.defaultReadObject(); 104 map = (Map ) in.readObject(); 105 } 106 107 public Object put(Object key, Object value) { 109 if (map.containsKey(key) == false) { 110 throw new IllegalArgumentException ("Cannot put new key/value pair - Map is fixed size"); 111 } 112 return map.put(key, value); 113 } 114 115 public void putAll(Map mapToCopy) { 116 for (Iterator it = mapToCopy.keySet().iterator(); it.hasNext(); ) { 117 if (mapToCopy.containsKey(it.next()) == false) { 118 throw new IllegalArgumentException ("Cannot put new key/value pair - Map is fixed size"); 119 } 120 } 121 map.putAll(mapToCopy); 122 } 123 124 public void clear() { 125 throw new UnsupportedOperationException ("Map is fixed size"); 126 } 127 128 public Object remove(Object key) { 129 throw new UnsupportedOperationException ("Map is fixed size"); 130 } 131 132 public Set entrySet() { 133 Set set = map.entrySet(); 134 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 SortedMap subMap(Object fromKey, Object toKey) { 149 SortedMap map = getSortedMap().subMap(fromKey, toKey); 150 return new FixedSizeSortedMap(map); 151 } 152 153 public SortedMap headMap(Object toKey) { 154 SortedMap map = getSortedMap().headMap(toKey); 155 return new FixedSizeSortedMap(map); 156 } 157 158 public SortedMap tailMap(Object fromKey) { 159 SortedMap map = getSortedMap().tailMap(fromKey); 160 return new FixedSizeSortedMap(map); 161 } 162 163 public boolean isFull() { 164 return true; 165 } 166 167 public int maxSize() { 168 return size(); 169 } 170 171 } 172 | Popular Tags |