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