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