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.set;
  17. import java.util.Comparator;
  18. import java.util.SortedSet;
  19. import org.apache.commons.collections.Transformer;
  20. /**
  21. * Decorates another <code>SortedSet</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.5 $ $Date: 2004/06/03 22:02:13 $
  32. *
  33. * @author Stephen Colebourne
  34. */
  35. public class TransformedSortedSet extends TransformedSet implements SortedSet {
  36. /** Serialization version */
  37. private static final long serialVersionUID = -1675486811351124386L;
  38. /**
  39. * Factory method to create a transforming sorted set.
  40. * <p>
  41. * If there are any elements already in the set being decorated, they
  42. * are NOT transformed.
  43. *
  44. * @param set the set to decorate, must not be null
  45. * @param transformer the transformer to use for conversion, must not be null
  46. * @throws IllegalArgumentException if set or transformer is null
  47. */
  48. public static SortedSet decorate(SortedSet set, Transformer transformer) {
  49. return new TransformedSortedSet(set, transformer);
  50. }
  51. //-----------------------------------------------------------------------
  52. /**
  53. * Constructor that wraps (not copies).
  54. * <p>
  55. * If there are any elements already in the set being decorated, they
  56. * are NOT transformed.
  57. *
  58. * @param set the set to decorate, must not be null
  59. * @param transformer the transformer to use for conversion, must not be null
  60. * @throws IllegalArgumentException if set or transformer is null
  61. */
  62. protected TransformedSortedSet(SortedSet set, Transformer transformer) {
  63. super(set, transformer);
  64. }
  65. /**
  66. * Gets the decorated set.
  67. *
  68. * @return the decorated set
  69. */
  70. protected SortedSet getSortedSet() {
  71. return (SortedSet) collection;
  72. }
  73. //-----------------------------------------------------------------------
  74. public Object first() {
  75. return getSortedSet().first();
  76. }
  77. public Object last() {
  78. return getSortedSet().last();
  79. }
  80. public Comparator comparator() {
  81. return getSortedSet().comparator();
  82. }
  83. //-----------------------------------------------------------------------
  84. public SortedSet subSet(Object fromElement, Object toElement) {
  85. SortedSet set = getSortedSet().subSet(fromElement, toElement);
  86. return new TransformedSortedSet(set, transformer);
  87. }
  88. public SortedSet headSet(Object toElement) {
  89. SortedSet set = getSortedSet().headSet(toElement);
  90. return new TransformedSortedSet(set, transformer);
  91. }
  92. public SortedSet tailSet(Object fromElement) {
  93. SortedSet set = getSortedSet().tailSet(fromElement);
  94. return new TransformedSortedSet(set, transformer);
  95. }
  96. }