001 /*
002 * Copyright 2002-2004 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package org.springframework.rules.constraint;
017
018 import java.util.Comparator;
019
020 import org.springframework.rules.constraint.Constraint;
021 import org.springframework.rules.closure.BinaryConstraint;
022 import org.springframework.util.ObjectUtils;
023
024 /**
025 * Constraint that tests object equality (not identity.)
026 *
027 * @author Keith Donald
028 */
029 public class EqualTo extends ComparisonBinaryPredicate {
030
031 public static EqualTo INSTANCE = new EqualTo();
032
033 public static BinaryConstraint instance() {
034 return INSTANCE;
035 }
036
037 public static void load(EqualTo instance) {
038 INSTANCE = instance;
039 }
040
041 public static BinaryConstraint instance(Comparator c) {
042 return new EqualTo(c);
043 }
044
045 public static Constraint value(Object value) {
046 return INSTANCE.bind(instance(), value);
047 }
048
049 public static Constraint value(Object value, Comparator comparator) {
050 return INSTANCE.bind(instance(comparator), value);
051 }
052
053 public EqualTo() {
054 super();
055 }
056
057 public EqualTo(Comparator comparator) {
058 super(comparator);
059 }
060
061 /**
062 * Test if the two arguments are equal.
063 *
064 * @param argument1
065 * the first argument
066 * @param argument2
067 * the second argument
068 * @return true if they are equal, false otherwise
069 */
070 public boolean test(Object argument1, Object argument2) {
071 if (getComparator() == null)
072 return ObjectUtils.nullSafeEquals(argument1, argument2);
073
074 return super.test(argument1, argument2);
075 }
076
077 protected boolean testCompareResult(int result) {
078 return result == 0;
079 }
080
081 public String toString() {
082 return RelationalOperator.EQUAL_TO.toString();
083 }
084 }