1 /* 2 * Copyright 2004-2009 the original author or authors. 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 17 package org.springmodules.validation.util.condition; 18 19 /** 20 * An abstract implementation of the {@link Condition} interface. This class takes care of the 21 * logical operation of the condition. It is highly recommended for condition implementation to 22 * sub-class this class when possible. 23 * 24 * @author Uri Boness 25 */ 26 public abstract class AbstractCondition implements Condition { 27 28 /** 29 * See {@link Condition#check(Object)}. 30 * <p/> 31 * Delegates to the {@link #doCheck(Object)}. Provides callback methods for sub-classes to intercept 32 * the checking. 33 */ 34 public final boolean check(Object object) { 35 beforeObjectChecked(object); 36 boolean result = doCheck(object); 37 return afterObjectChecked(object, result); 38 } 39 40 /** 41 * Performs the actual checking of this condition on the checked object. 42 * 43 * @param object The object to be checked. 44 * @return <code>true</code> if the given object adheres to this condition, <code>false</code> otherwise. 45 */ 46 public abstract boolean doCheck(Object object); 47 48 /** 49 * See {@link Condition#and(Condition)} 50 */ 51 public Condition and(Condition condition) { 52 return Conditions.and(this, condition); 53 } 54 55 /** 56 * See {@link Condition#or(Condition)} 57 */ 58 public Condition or(Condition condition) { 59 return Conditions.or(this, condition); 60 } 61 62 /** 63 * A callback method that enables sub-classes intercept the object checking before it is 64 * being checked. Sub-classes may override this method and perform custom assertions and prevent the 65 * checking by throwing an {@link IllegalArgumentException}; 66 * 67 * @param object 68 */ 69 protected void beforeObjectChecked(Object object) throws IllegalArgumentException { 70 } 71 72 /** 73 * A callback method that enables sub-classes to intercept the object checking after it was checked. Sub-classes 74 * can override this method and change the check result. By default, this method returns the original check result. 75 * 76 * @param object The checked object. 77 * @param originalResult The original check result as returned by the specific condition implementation. 78 * @return The final check result. 79 */ 80 protected boolean afterObjectChecked(Object object, boolean originalResult) { 81 return originalResult; 82 } 83 }