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.util.Collection;
  18. import java.util.Map;
  19. import java.util.Set;
  20. /**
  21. * Provides a base decorator that enables additional functionality to be added
  22. * to a Map via decoration.
  23. * <p>
  24. * Methods are forwarded directly to the decorated map.
  25. * <p>
  26. * This implementation does not perform any special processing with
  27. * {@link #entrySet()}, {@link #keySet()} or {@link #values()}. Instead
  28. * it simply returns the set/collection from the wrapped map. This may be
  29. * undesirable, for example if you are trying to write a validating
  30. * implementation it would provide a loophole around the validation.
  31. * But, you might want that loophole, so this class is kept simple.
  32. *
  33. * @since Commons Collections 3.0
  34. * @version $Revision: 1.5 $ $Date: 2004/04/02 21:02:54 $
  35. *
  36. * @author Daniel Rall
  37. * @author Stephen Colebourne
  38. */
  39. public abstract class AbstractMapDecorator implements Map {
  40. /** The map to decorate */
  41. protected transient Map map;
  42. /**
  43. * Constructor only used in deserialization, do not use otherwise.
  44. * @since Commons Collections 3.1
  45. */
  46. protected AbstractMapDecorator() {
  47. super();
  48. }
  49. /**
  50. * Constructor that wraps (not copies).
  51. *
  52. * @param map the map to decorate, must not be null
  53. * @throws IllegalArgumentException if the collection is null
  54. */
  55. public AbstractMapDecorator(Map map) {
  56. if (map == null) {
  57. throw new IllegalArgumentException("Map must not be null");
  58. }
  59. this.map = map;
  60. }
  61. /**
  62. * Gets the map being decorated.
  63. *
  64. * @return the decorated map
  65. */
  66. protected Map getMap() {
  67. return map;
  68. }
  69. //-----------------------------------------------------------------------
  70. public void clear() {
  71. map.clear();
  72. }
  73. public boolean containsKey(Object key) {
  74. return map.containsKey(key);
  75. }
  76. public boolean containsValue(Object value) {
  77. return map.containsValue(value);
  78. }
  79. public Set entrySet() {
  80. return map.entrySet();
  81. }
  82. public Object get(Object key) {
  83. return map.get(key);
  84. }
  85. public boolean isEmpty() {
  86. return map.isEmpty();
  87. }
  88. public Set keySet() {
  89. return map.keySet();
  90. }
  91. public Object put(Object key, Object value) {
  92. return map.put(key, value);
  93. }
  94. public void putAll(Map mapToCopy) {
  95. map.putAll(mapToCopy);
  96. }
  97. public Object remove(Object key) {
  98. return map.remove(key);
  99. }
  100. public int size() {
  101. return map.size();
  102. }
  103. public Collection values() {
  104. return map.values();
  105. }
  106. public boolean equals(Object object) {
  107. if (object == this) {
  108. return true;
  109. }
  110. return map.equals(object);
  111. }
  112. public int hashCode() {
  113. return map.hashCode();
  114. }
  115. public String toString() {
  116. return map.toString();
  117. }
  118. }