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.FunctorException;
  19. import org.apache.commons.collections.Predicate;
  20. import org.apache.commons.collections.Transformer;
  21. /**
  22. * Predicate implementation that returns the result of a transformer.
  23. *
  24. * @since Commons Collections 3.0
  25. * @version $Revision: 1.6 $ $Date: 2004/05/16 11:16:01 $
  26. *
  27. * @author Stephen Colebourne
  28. */
  29. public final class TransformerPredicate implements Predicate, Serializable {
  30. /** Serial version UID */
  31. static final long serialVersionUID = -2407966402920578741L;
  32. /** The transformer to call */
  33. private final Transformer iTransformer;
  34. /**
  35. * Factory to create the predicate.
  36. *
  37. * @param transformer the transformer to decorate
  38. * @return the predicate
  39. * @throws IllegalArgumentException if the transformer is null
  40. */
  41. public static Predicate getInstance(Transformer transformer) {
  42. if (transformer == null) {
  43. throw new IllegalArgumentException("The transformer to call must not be null");
  44. }
  45. return new TransformerPredicate(transformer);
  46. }
  47. /**
  48. * Constructor that performs no validation.
  49. * Use <code>getInstance</code> if you want that.
  50. *
  51. * @param transformer the transformer to decorate
  52. */
  53. public TransformerPredicate(Transformer transformer) {
  54. super();
  55. iTransformer = transformer;
  56. }
  57. /**
  58. * Evaluates the predicate returning the result of the decorated transformer.
  59. *
  60. * @param object the input object
  61. * @return true if decorated transformer returns Boolean.TRUE
  62. * @throws FunctorException if the transformer returns an invalid type
  63. */
  64. public boolean evaluate(Object object) {
  65. Object result = iTransformer.transform(object);
  66. if (result instanceof Boolean == false) {
  67. throw new FunctorException(
  68. "Transformer must return an instanceof Boolean, it was a "
  69. + (result == null ? "null object" : result.getClass().getName()));
  70. }
  71. return ((Boolean) result).booleanValue();
  72. }
  73. /**
  74. * Gets the transformer.
  75. *
  76. * @return the transformer
  77. * @since Commons Collections 3.1
  78. */
  79. public Transformer getTransformer() {
  80. return iTransformer;
  81. }
  82. }