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.bidimap;
  17. import java.util.Collection;
  18. import java.util.Map;
  19. import java.util.Set;
  20. import java.util.SortedMap;
  21. import org.apache.commons.collections.BidiMap;
  22. import org.apache.commons.collections.MapIterator;
  23. import org.apache.commons.collections.OrderedBidiMap;
  24. import org.apache.commons.collections.OrderedMapIterator;
  25. import org.apache.commons.collections.SortedBidiMap;
  26. import org.apache.commons.collections.Unmodifiable;
  27. import org.apache.commons.collections.collection.UnmodifiableCollection;
  28. import org.apache.commons.collections.iterators.UnmodifiableOrderedMapIterator;
  29. import org.apache.commons.collections.map.UnmodifiableEntrySet;
  30. import org.apache.commons.collections.map.UnmodifiableSortedMap;
  31. import org.apache.commons.collections.set.UnmodifiableSet;
  32. /**
  33. * Decorates another <code>SortedBidiMap</code> to ensure it can't be altered.
  34. *
  35. * @since Commons Collections 3.0
  36. * @version $Revision: 1.5 $ $Date: 2004/05/15 12:13:03 $
  37. *
  38. * @author Stephen Colebourne
  39. */
  40. public final class UnmodifiableSortedBidiMap
  41. extends AbstractSortedBidiMapDecorator implements Unmodifiable {
  42. /** The inverse unmodifiable map */
  43. private UnmodifiableSortedBidiMap inverse;
  44. /**
  45. * Factory method to create an unmodifiable map.
  46. * <p>
  47. * If the map passed in is already unmodifiable, it is returned.
  48. *
  49. * @param map the map to decorate, must not be null
  50. * @return an unmodifiable SortedBidiMap
  51. * @throws IllegalArgumentException if map is null
  52. */
  53. public static SortedBidiMap decorate(SortedBidiMap map) {
  54. if (map instanceof Unmodifiable) {
  55. return map;
  56. }
  57. return new UnmodifiableSortedBidiMap(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 UnmodifiableSortedBidiMap(SortedBidiMap map) {
  67. super(map);
  68. }
  69. //-----------------------------------------------------------------------
  70. public void clear() {
  71. throw new UnsupportedOperationException();
  72. }
  73. public Object put(Object key, Object value) {
  74. throw new UnsupportedOperationException();
  75. }
  76. public void putAll(Map mapToCopy) {
  77. throw new UnsupportedOperationException();
  78. }
  79. public Object remove(Object key) {
  80. throw new UnsupportedOperationException();
  81. }
  82. public Set entrySet() {
  83. Set set = super.entrySet();
  84. return UnmodifiableEntrySet.decorate(set);
  85. }
  86. public Set keySet() {
  87. Set set = super.keySet();
  88. return UnmodifiableSet.decorate(set);
  89. }
  90. public Collection values() {
  91. Collection coll = super.values();
  92. return UnmodifiableCollection.decorate(coll);
  93. }
  94. //-----------------------------------------------------------------------
  95. public Object removeValue(Object value) {
  96. throw new UnsupportedOperationException();
  97. }
  98. public MapIterator mapIterator() {
  99. return orderedMapIterator();
  100. }
  101. public BidiMap inverseBidiMap() {
  102. return inverseSortedBidiMap();
  103. }
  104. //-----------------------------------------------------------------------
  105. public OrderedMapIterator orderedMapIterator() {
  106. OrderedMapIterator it = getSortedBidiMap().orderedMapIterator();
  107. return UnmodifiableOrderedMapIterator.decorate(it);
  108. }
  109. public OrderedBidiMap inverseOrderedBidiMap() {
  110. return inverseSortedBidiMap();
  111. }
  112. //-----------------------------------------------------------------------
  113. public SortedBidiMap inverseSortedBidiMap() {
  114. if (inverse == null) {
  115. inverse = new UnmodifiableSortedBidiMap(getSortedBidiMap().inverseSortedBidiMap());
  116. inverse.inverse = this;
  117. }
  118. return inverse;
  119. }
  120. public SortedMap subMap(Object fromKey, Object toKey) {
  121. SortedMap sm = getSortedBidiMap().subMap(fromKey, toKey);
  122. return UnmodifiableSortedMap.decorate(sm);
  123. }
  124. public SortedMap headMap(Object toKey) {
  125. SortedMap sm = getSortedBidiMap().headMap(toKey);
  126. return UnmodifiableSortedMap.decorate(sm);
  127. }
  128. public SortedMap tailMap(Object fromKey) {
  129. SortedMap sm = getSortedBidiMap().tailMap(fromKey);
  130. return UnmodifiableSortedMap.decorate(sm);
  131. }
  132. }