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