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