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 org.apache.commons.collections.BoundedMap;
  26. import org.apache.commons.collections.collection.UnmodifiableCollection;
  27. import org.apache.commons.collections.set.UnmodifiableSet;
  28. /**
  29. * Decorates another <code>Map</code> to fix the size, preventing add/remove.
  30. * <p>
  31. * Any action that would change the size of the map is disallowed.
  32. * The put method is allowed to change the value associated with an existing
  33. * key however.
  34. * <p>
  35. * If trying to remove or clear the map, an UnsupportedOperationException is
  36. * thrown. If trying to put a new mapping into the map, an
  37. * IllegalArgumentException is thrown. This is because the put method can
  38. * succeed if the mapping's key already exists in the map, so the put method
  39. * is not always unsupported.
  40. * <p>
  41. * This class is Serializable from Commons Collections 3.1.
  42. *
  43. * @since Commons Collections 3.0
  44. * @version $Revision: 1.8 $ $Date: 2004/05/07 23:58:33 $
  45. *
  46. * @author Stephen Colebourne
  47. * @author Paul Jack
  48. */
  49. public class FixedSizeMap
  50. extends AbstractMapDecorator
  51. implements Map, BoundedMap, Serializable {
  52. /** Serialization version */
  53. private static final long serialVersionUID = 7450927208116179316L;
  54. /**
  55. * Factory method to create a fixed size map.
  56. *
  57. * @param map the map to decorate, must not be null
  58. * @throws IllegalArgumentException if map is null
  59. */
  60. public static Map decorate(Map map) {
  61. return new FixedSizeMap(map);
  62. }
  63. //-----------------------------------------------------------------------
  64. /**
  65. * Constructor that wraps (not copies).
  66. *
  67. * @param map the map to decorate, must not be null
  68. * @throws IllegalArgumentException if map is null
  69. */
  70. protected FixedSizeMap(Map map) {
  71. super(map);
  72. }
  73. //-----------------------------------------------------------------------
  74. /**
  75. * Write the map out using a custom routine.
  76. *
  77. * @param out the output stream
  78. * @throws IOException
  79. * @since Commons Collections 3.1
  80. */
  81. private void writeObject(ObjectOutputStream out) throws IOException {
  82. out.defaultWriteObject();
  83. out.writeObject(map);
  84. }
  85. /**
  86. * Read the map in using a custom routine.
  87. *
  88. * @param in the input stream
  89. * @throws IOException
  90. * @throws ClassNotFoundException
  91. * @since Commons Collections 3.1
  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. // unmodifiable set will still allow modification via Map.Entry objects
  121. return UnmodifiableSet.decorate(set);
  122. }
  123. public Set keySet() {
  124. Set set = map.keySet();
  125. return UnmodifiableSet.decorate(set);
  126. }
  127. public Collection values() {
  128. Collection coll = map.values();
  129. return UnmodifiableCollection.decorate(coll);
  130. }
  131. public boolean isFull() {
  132. return true;
  133. }
  134. public int maxSize() {
  135. return size();
  136. }
  137. }