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