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 true if the input is the same object
  21. * as the one stored in this predicate by equals.
  22. *
  23. * @since Commons Collections 3.0
  24. * @version $Revision: 1.5 $ $Date: 2004/05/16 11:16:01 $
  25. *
  26. * @author Stephen Colebourne
  27. */
  28. public final class EqualPredicate implements Predicate, Serializable {
  29. /** Serial version UID */
  30. static final long serialVersionUID = 5633766978029907089L;
  31. /** The value to compare to */
  32. private final Object iValue;
  33. /**
  34. * Factory to create the identity predicate.
  35. *
  36. * @param object the object to compare to
  37. * @return the predicate
  38. * @throws IllegalArgumentException if the predicate is null
  39. */
  40. public static Predicate getInstance(Object object) {
  41. if (object == null) {
  42. return NullPredicate.INSTANCE;
  43. }
  44. return new EqualPredicate(object);
  45. }
  46. /**
  47. * Constructor that performs no validation.
  48. * Use <code>getInstance</code> if you want that.
  49. *
  50. * @param object the object to compare to
  51. */
  52. public EqualPredicate(Object object) {
  53. super();
  54. iValue = object;
  55. }
  56. /**
  57. * Evaluates the predicate returning true if the input equals the stored value.
  58. *
  59. * @param object the input object
  60. * @return true if input object equals stored value
  61. */
  62. public boolean evaluate(Object object) {
  63. return (iValue.equals(object));
  64. }
  65. /**
  66. * Gets the value.
  67. *
  68. * @return the value
  69. * @since Commons Collections 3.1
  70. */
  71. public Object getValue() {
  72. return iValue;
  73. }
  74. }