1. /*
  2. * Copyright 2003-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.collections.map;
  17. import java.io.IOException;
  18. import java.io.ObjectInputStream;
  19. import java.io.ObjectOutputStream;
  20. import java.io.Serializable;
  21. import java.util.Collection;
  22. import java.util.Iterator;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import java.util.SortedMap;
  26. import org.apache.commons.collections.BoundedMap;
  27. import org.apache.commons.collections.collection.UnmodifiableCollection;
  28. import org.apache.commons.collections.set.UnmodifiableSet;
  29. /**
  30. * Decorates another <code>SortedMap</code> to fix the size blocking add/remove.
  31. * <p>
  32. * Any action that would change the size of the map is disallowed.
  33. * The put method is allowed to change the value associated with an existing
  34. * key however.
  35. * <p>
  36. * If trying to remove or clear the map, an UnsupportedOperationException is
  37. * thrown. If trying to put a new mapping into the map, an
  38. * IllegalArgumentException is thrown. This is because the put method can
  39. * succeed if the mapping's key already exists in the map, so the put method
  40. * is not always unsupported.
  41. * <p>
  42. * This class is Serializable from Commons Collections 3.1.
  43. *
  44. * @since Commons Collections 3.0
  45. * @version $Revision: 1.8 $ $Date: 2004/06/03 22:26:52 $
  46. *
  47. * @author Stephen Colebourne
  48. * @author Paul Jack
  49. */
  50. public class FixedSizeSortedMap
  51. extends AbstractSortedMapDecorator
  52. implements SortedMap, BoundedMap, Serializable {
  53. /** Serialization version */
  54. private static final long serialVersionUID = 3126019624511683653L;
  55. /**
  56. * Factory method to create a fixed size sorted map.
  57. *
  58. * @param map the map to decorate, must not be null
  59. * @throws IllegalArgumentException if map is null
  60. */
  61. public static SortedMap decorate(SortedMap map) {
  62. return new FixedSizeSortedMap(map);
  63. }
  64. //-----------------------------------------------------------------------
  65. /**
  66. * Constructor that wraps (not copies).
  67. *
  68. * @param map the map to decorate, must not be null
  69. * @throws IllegalArgumentException if map is null
  70. */
  71. protected FixedSizeSortedMap(SortedMap map) {
  72. super(map);
  73. }
  74. /**
  75. * Gets the map being decorated.
  76. *
  77. * @return the decorated map
  78. */
  79. protected SortedMap getSortedMap() {
  80. return (SortedMap) map;
  81. }
  82. //-----------------------------------------------------------------------
  83. /**
  84. * Write the map out using a custom routine.
  85. */
  86. private void writeObject(ObjectOutputStream out) throws IOException {
  87. out.defaultWriteObject();
  88. out.writeObject(map);
  89. }
  90. /**
  91. * Read the map in using a custom routine.
  92. */
  93. private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
  94. in.defaultReadObject();
  95. map = (Map) in.readObject();
  96. }
  97. //-----------------------------------------------------------------------
  98. public Object put(Object key, Object value) {
  99. if (map.containsKey(key) == false) {
  100. throw new IllegalArgumentException("Cannot put new key/value pair - Map is fixed size");
  101. }
  102. return map.put(key, value);
  103. }
  104. public void putAll(Map mapToCopy) {
  105. for (Iterator it = mapToCopy.keySet().iterator(); it.hasNext(); ) {
  106. if (mapToCopy.containsKey(it.next()) == false) {
  107. throw new IllegalArgumentException("Cannot put new key/value pair - Map is fixed size");
  108. }
  109. }
  110. map.putAll(mapToCopy);
  111. }
  112. public void clear() {
  113. throw new UnsupportedOperationException("Map is fixed size");
  114. }
  115. public Object remove(Object key) {
  116. throw new UnsupportedOperationException("Map is fixed size");
  117. }
  118. public Set entrySet() {
  119. Set set = map.entrySet();
  120. return UnmodifiableSet.decorate(set);
  121. }
  122. public Set keySet() {
  123. Set set = map.keySet();
  124. return UnmodifiableSet.decorate(set);
  125. }
  126. public Collection values() {
  127. Collection coll = map.values();
  128. return UnmodifiableCollection.decorate(coll);
  129. }
  130. //-----------------------------------------------------------------------
  131. public SortedMap subMap(Object fromKey, Object toKey) {
  132. SortedMap map = getSortedMap().subMap(fromKey, toKey);
  133. return new FixedSizeSortedMap(map);
  134. }
  135. public SortedMap headMap(Object toKey) {
  136. SortedMap map = getSortedMap().headMap(toKey);
  137. return new FixedSizeSortedMap(map);
  138. }
  139. public SortedMap tailMap(Object fromKey) {
  140. SortedMap map = getSortedMap().tailMap(fromKey);
  141. return new FixedSizeSortedMap(map);
  142. }
  143. public boolean isFull() {
  144. return true;
  145. }
  146. public int maxSize() {
  147. return size();
  148. }
  149. }