1. /*
  2. * Copyright 2001-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.functors;
  17. import java.io.Serializable;
  18. import org.apache.commons.collections.Predicate;
  19. import org.apache.commons.collections.Transformer;
  20. /**
  21. * Predicate implementation that transforms the given object before invoking
  22. * another <code>Predicate</code>.
  23. *
  24. * @since Commons Collections 3.1
  25. * @version $Revision: 1.4 $ $Date: 2004/05/31 16:43:17 $
  26. * @author Alban Peignier
  27. * @author Stephen Colebourne
  28. */
  29. public final class TransformedPredicate implements Predicate, PredicateDecorator, Serializable {
  30. /** Serial version UID */
  31. static final long serialVersionUID = -5596090919668315834L;
  32. /** The transformer to call */
  33. private final Transformer iTransformer;
  34. /** The predicate to call */
  35. private final Predicate iPredicate;
  36. /**
  37. * Factory to create the predicate.
  38. *
  39. * @param transformer the transformer to call
  40. * @param predicate the predicate to call with the result of the transform
  41. * @return the predicate
  42. * @throws IllegalArgumentException if the transformer or the predicate is null
  43. */
  44. public static Predicate getInstance(Transformer transformer, Predicate predicate) {
  45. if (transformer == null) {
  46. throw new IllegalArgumentException("The transformer to call must not be null");
  47. }
  48. if (predicate == null) {
  49. throw new IllegalArgumentException("The predicate to call must not be null");
  50. }
  51. return new TransformedPredicate(transformer, predicate);
  52. }
  53. /**
  54. * Constructor that performs no validation.
  55. * Use <code>getInstance</code> if you want that.
  56. *
  57. * @param transformer the transformer to use
  58. * @param predicate the predicate to decorate
  59. */
  60. public TransformedPredicate(Transformer transformer, Predicate predicate) {
  61. iTransformer = transformer;
  62. iPredicate = predicate;
  63. }
  64. /**
  65. * Evaluates the predicate returning the result of the decorated predicate
  66. * once the input has been transformed
  67. *
  68. * @param object the input object which will be transformed
  69. * @return true if decorated predicate returns true
  70. */
  71. public boolean evaluate(Object object) {
  72. Object result = iTransformer.transform(object);
  73. return iPredicate.evaluate(result);
  74. }
  75. /**
  76. * Gets the predicate being decorated.
  77. *
  78. * @return the predicate as the only element in an array
  79. * @since Commons Collections 3.1
  80. */
  81. public Predicate[] getPredicates() {
  82. return new Predicate[] {iPredicate};
  83. }
  84. /**
  85. * Gets the transformer in use.
  86. *
  87. * @return the transformer
  88. */
  89. public Transformer getTransformer() {
  90. return iTransformer;
  91. }
  92. }