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