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.Map;
  23. import java.util.Set;
  24. import org.apache.commons.collections.IterableMap;
  25. import org.apache.commons.collections.MapIterator;
  26. import org.apache.commons.collections.Unmodifiable;
  27. import org.apache.commons.collections.collection.UnmodifiableCollection;
  28. import org.apache.commons.collections.iterators.EntrySetMapIterator;
  29. import org.apache.commons.collections.iterators.UnmodifiableMapIterator;
  30. import org.apache.commons.collections.set.UnmodifiableSet;
  31. /**
  32. * Decorates another <code>Map</code> to ensure it can't be altered.
  33. * <p>
  34. * This class is Serializable from Commons Collections 3.1.
  35. *
  36. * @since Commons Collections 3.0
  37. * @version $Revision: 1.10 $ $Date: 2004/04/09 10:32:25 $
  38. *
  39. * @author Stephen Colebourne
  40. */
  41. public final class UnmodifiableMap
  42. extends AbstractMapDecorator
  43. implements IterableMap, Unmodifiable, Serializable {
  44. /** Serialization version */
  45. private static final long serialVersionUID = 2737023427269031941L;
  46. /**
  47. * Factory method to create an unmodifiable map.
  48. *
  49. * @param map the map to decorate, must not be null
  50. * @throws IllegalArgumentException if map is null
  51. */
  52. public static Map decorate(Map map) {
  53. if (map instanceof Unmodifiable) {
  54. return map;
  55. }
  56. return new UnmodifiableMap(map);
  57. }
  58. //-----------------------------------------------------------------------
  59. /**
  60. * Constructor that wraps (not copies).
  61. *
  62. * @param map the map to decorate, must not be null
  63. * @throws IllegalArgumentException if map is null
  64. */
  65. private UnmodifiableMap(Map map) {
  66. super(map);
  67. }
  68. //-----------------------------------------------------------------------
  69. /**
  70. * Write the map out using a custom routine.
  71. *
  72. * @param out the output stream
  73. * @throws IOException
  74. * @since Commons Collections 3.1
  75. */
  76. private void writeObject(ObjectOutputStream out) throws IOException {
  77. out.defaultWriteObject();
  78. out.writeObject(map);
  79. }
  80. /**
  81. * Read the map in using a custom routine.
  82. *
  83. * @param in the input stream
  84. * @throws IOException
  85. * @throws ClassNotFoundException
  86. * @since Commons Collections 3.1
  87. */
  88. private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
  89. in.defaultReadObject();
  90. map = (Map) in.readObject();
  91. }
  92. //-----------------------------------------------------------------------
  93. public void clear() {
  94. throw new UnsupportedOperationException();
  95. }
  96. public Object put(Object key, Object value) {
  97. throw new UnsupportedOperationException();
  98. }
  99. public void putAll(Map mapToCopy) {
  100. throw new UnsupportedOperationException();
  101. }
  102. public Object remove(Object key) {
  103. throw new UnsupportedOperationException();
  104. }
  105. public MapIterator mapIterator() {
  106. if (map instanceof IterableMap) {
  107. MapIterator it = ((IterableMap) map).mapIterator();
  108. return UnmodifiableMapIterator.decorate(it);
  109. } else {
  110. MapIterator it = new EntrySetMapIterator(map);
  111. return UnmodifiableMapIterator.decorate(it);
  112. }
  113. }
  114. public Set entrySet() {
  115. Set set = super.entrySet();
  116. return UnmodifiableEntrySet.decorate(set);
  117. }
  118. public Set keySet() {
  119. Set set = super.keySet();
  120. return UnmodifiableSet.decorate(set);
  121. }
  122. public Collection values() {
  123. Collection coll = super.values();
  124. return UnmodifiableCollection.decorate(coll);
  125. }
  126. }