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.bag;
  17. import java.util.Set;
  18. import org.apache.commons.collections.Bag;
  19. import org.apache.commons.collections.Transformer;
  20. import org.apache.commons.collections.collection.TransformedCollection;
  21. import org.apache.commons.collections.set.TransformedSet;
  22. /**
  23. * Decorates another <code>Bag</code> to transform objects that are added.
  24. * <p>
  25. * The add methods are affected by this class.
  26. * Thus objects must be removed or searched for using their transformed form.
  27. * For example, if the transformation converts Strings to Integers, you must
  28. * use the Integer form to remove objects.
  29. * <p>
  30. * This class is Serializable from Commons Collections 3.1.
  31. *
  32. * @since Commons Collections 3.0
  33. * @version $Revision: 1.7 $ $Date: 2004/06/03 22:02:12 $
  34. *
  35. * @author Stephen Colebourne
  36. */
  37. public class TransformedBag
  38. extends TransformedCollection implements Bag {
  39. /** Serialization version */
  40. private static final long serialVersionUID = 5421170911299074185L;
  41. /**
  42. * Factory method to create a transforming bag.
  43. * <p>
  44. * If there are any elements already in the bag being decorated, they
  45. * are NOT transformed.
  46. *
  47. * @param bag the bag to decorate, must not be null
  48. * @param transformer the transformer to use for conversion, must not be null
  49. * @return a new transformed Bag
  50. * @throws IllegalArgumentException if bag or transformer is null
  51. */
  52. public static Bag decorate(Bag bag, Transformer transformer) {
  53. return new TransformedBag(bag, transformer);
  54. }
  55. //-----------------------------------------------------------------------
  56. /**
  57. * Constructor that wraps (not copies).
  58. * <p>
  59. * If there are any elements already in the bag being decorated, they
  60. * are NOT transformed.
  61. *
  62. * @param bag the bag to decorate, must not be null
  63. * @param transformer the transformer to use for conversion, must not be null
  64. * @throws IllegalArgumentException if bag or transformer is null
  65. */
  66. protected TransformedBag(Bag bag, Transformer transformer) {
  67. super(bag, transformer);
  68. }
  69. /**
  70. * Gets the decorated bag.
  71. *
  72. * @return the decorated bag
  73. */
  74. protected Bag getBag() {
  75. return (Bag) collection;
  76. }
  77. //-----------------------------------------------------------------------
  78. public int getCount(Object object) {
  79. return getBag().getCount(object);
  80. }
  81. public boolean remove(Object object, int nCopies) {
  82. return getBag().remove(object, nCopies);
  83. }
  84. //-----------------------------------------------------------------------
  85. public boolean add(Object object, int nCopies) {
  86. object = transform(object);
  87. return getBag().add(object, nCopies);
  88. }
  89. public Set uniqueSet() {
  90. Set set = getBag().uniqueSet();
  91. return TransformedSet.decorate(set, transformer);
  92. }
  93. }