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 006 * of 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 014 * under the License. 015 */ 016 package org.springframework.rules.constraint; 017 018 import org.springframework.rules.closure.Closure; 019 import org.springframework.rules.constraint.Constraint; 020 import org.springframework.util.Assert; 021 022 /** 023 * Tests the result returned from evaluating a closure closure. 024 * 025 * @author Keith Donald 026 */ 027 public class ClosureResultConstraint implements Constraint { 028 private Constraint constraint; 029 030 private Closure closure; 031 032 /** 033 * Creates a ClosureResultConstraint that tests the result returned from 034 * evaulating the specified unary closure. 035 * 036 * @param closure 037 * The closure to test. 038 * @param constraint 039 * The predicate that will test the closure return value. 040 */ 041 public ClosureResultConstraint(Closure closure, Constraint constraint) { 042 Assert.notNull(closure, "closure is required"); 043 Assert.notNull(constraint, "constraint is required"); 044 this.constraint = constraint; 045 this.closure = closure; 046 } 047 048 /** 049 * Tests the result returned by evaluating the specified argument against 050 * the configured closure. 051 * 052 * @see Constraint#test(java.lang.Object) 053 */ 054 public boolean test(Object argument) { 055 Object returnValue = closure.call(argument); 056 return this.constraint.test(returnValue); 057 } 058 059 public Closure getFunction() { 060 return closure; 061 } 062 063 public Constraint getPredicate() { 064 return constraint; 065 } 066 067 public String toString() { 068 return "[" + closure.toString() + " " + constraint.toString() + "]"; 069 } 070 }