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. /**
  20. * Predicate implementation that returns false if the input is null.
  21. *
  22. * @since Commons Collections 3.0
  23. * @version $Revision: 1.6 $ $Date: 2004/05/31 16:43:17 $
  24. *
  25. * @author Stephen Colebourne
  26. */
  27. public final class NullIsFalsePredicate implements Predicate, PredicateDecorator, Serializable {
  28. /** Serial version UID */
  29. static final long serialVersionUID = -2997501534564735525L;
  30. /** The predicate to decorate */
  31. private final Predicate iPredicate;
  32. /**
  33. * Factory to create the null false predicate.
  34. *
  35. * @param predicate the predicate to decorate, not null
  36. * @return the predicate
  37. * @throws IllegalArgumentException if the predicate is null
  38. */
  39. public static Predicate getInstance(Predicate predicate) {
  40. if (predicate == null) {
  41. throw new IllegalArgumentException("Predicate must not be null");
  42. }
  43. return new NullIsFalsePredicate(predicate);
  44. }
  45. /**
  46. * Constructor that performs no validation.
  47. * Use <code>getInstance</code> if you want that.
  48. *
  49. * @param predicate the predicate to call after the null check
  50. */
  51. public NullIsFalsePredicate(Predicate predicate) {
  52. super();
  53. iPredicate = predicate;
  54. }
  55. /**
  56. * Evaluates the predicate returning the result of the decorated predicate
  57. * once a null check is performed.
  58. *
  59. * @param object the input object
  60. * @return true if decorated predicate returns true, false if input is null
  61. */
  62. public boolean evaluate(Object object) {
  63. if (object == null) {
  64. return false;
  65. }
  66. return iPredicate.evaluate(object);
  67. }
  68. /**
  69. * Gets the predicate being decorated.
  70. *
  71. * @return the predicate as the only element in an array
  72. * @since Commons Collections 3.1
  73. */
  74. public Predicate[] getPredicates() {
  75. return new Predicate[] {iPredicate};
  76. }
  77. }