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 org.springframework.rules.constraint.Constraint; 019 import org.springframework.rules.closure.BinaryConstraint; 020 021 /** 022 * A unary constraint adapting a binary constraint that uses a parameterized 023 * constant value as the second argument when testing. 024 * 025 * @author Keith Donald 026 */ 027 public class ParameterizedBinaryConstraint implements Constraint { 028 private BinaryConstraint constraint; 029 030 private Object parameter; 031 032 /** 033 * Creates a ParameterizedBinaryPredicate that binds the provided parameter 034 * constant as the second argument to the constraint during tests. 035 * 036 * @param constraint 037 * The binary constraint to adapt as a unary constraint 038 * @param parameter 039 * The constant parameter value 040 */ 041 public ParameterizedBinaryConstraint(BinaryConstraint constraint, Object parameter) { 042 this.constraint = constraint; 043 this.parameter = parameter; 044 } 045 046 /** 047 * Convenience constructor for <code>short</code> parameters. 048 */ 049 public ParameterizedBinaryConstraint(BinaryConstraint constraint, short number) { 050 this(constraint, new Short(number)); 051 } 052 053 /** 054 * Convenience constructor for <code>byte</code> parameters. 055 */ 056 public ParameterizedBinaryConstraint(BinaryConstraint constraint, byte b) { 057 this(constraint, new Byte(b)); 058 } 059 060 /** 061 * Convenience constructor for <code>integer</code> parameters. 062 */ 063 public ParameterizedBinaryConstraint(BinaryConstraint constraint, int number) { 064 this(constraint, new Integer(number)); 065 } 066 067 /** 068 * Convenience constructor for <code>float</code> parameters. 069 */ 070 public ParameterizedBinaryConstraint(BinaryConstraint constraint, float number) { 071 this(constraint, new Float(number)); 072 } 073 074 /** 075 * Convenience constructor for <code>double</code> parameters. 076 */ 077 public ParameterizedBinaryConstraint(BinaryConstraint constraint, double number) { 078 this(constraint, new Double(number)); 079 } 080 081 /** 082 * Convenience constructor for <code>boolean</code> parameters. 083 */ 084 public ParameterizedBinaryConstraint(BinaryConstraint constraint, boolean bool) { 085 this(constraint, (bool ? Boolean.TRUE : Boolean.FALSE)); 086 } 087 088 public Object getParameter() { 089 return parameter; 090 } 091 092 public BinaryConstraint getConstraint() { 093 return constraint; 094 } 095 096 /** 097 * Tests the wrapped binary constraint with the variable argument value, 098 * passing in the parameter constant as the second argument. 099 * 100 * @see Constraint#test(java.lang.Object) 101 */ 102 public boolean test(Object value) { 103 return constraint.test(value, this.parameter); 104 } 105 106 public String toString() { 107 return constraint.toString() + " " + getParameter(); 108 } 109 110 }