You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2002/09/08 20:01:00 UTC
cvs commit: jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/bean/action Validate.java ActionUtils.java Clear.java Reset.java
scolebourne 2002/09/08 11:01:00
Added: pattern/src/java/org/apache/commons/pattern/bean/get
GetName.java GetUtils.java GetIdentifier.java
pattern/src/java/org/apache/commons/pattern/bean/query
IsValid.java IsEmpty.java QueryUtils.java
pattern/src/java/org/apache/commons/pattern/bean/action
Validate.java ActionUtils.java Clear.java
Reset.java
Log:
Patterns used when working with beans
Revision Changes Path
1.1 jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/bean/get/GetName.java
Index: GetName.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.pattern.bean.get;
/**
* <code>GetName</code> defines a method signature used to get a name from
* an object.
* <p>
* The name typically represents a human readable name for the object. Examples
* in Java (that don't implement the interface) are Method and Field in the
* reflection API.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: GetName.java,v 1.1 2002/09/08 18:00:59 scolebourne Exp $
*/
public interface GetName {
/**
* Get the name of the object.
*
* @return the name
*/
public String getName();
}
1.1 jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/bean/get/GetUtils.java
Index: GetUtils.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.pattern.bean.get;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.pattern.transformer.Transformer;
import org.apache.commons.pattern.transformer.TransformerException;
/**
* <code>GetUtils</code> provides reference implementations and utilities
* for the Get pattern interfaces. The supplied implementations are:
* <ul>
* <li>GetNameTransformer - returns the name of the GetName input object
* </ul>
* All the supplied implementations are Serializable.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: GetUtils.java,v 1.1 2002/09/08 18:00:59 scolebourne Exp $
*/
public class GetUtils {
private static final Transformer GET_NAME_TRANSFORMER = new GetNameTransformer();
private static final Transformer GET_IDENTIFIER_TRANSFORMER = new GetIdentifierTransformer();
/**
* Restrictive constructor
*/
protected GetUtils() {
super();
}
/**
* Gets a Transformer that returns the name of the input object.
* The Transformer tries to cast the object to GetName. If that fails
* reflection is used.
*
* @return the transformer
*/
public static Transformer getNameTransformer() {
return GET_NAME_TRANSFORMER;
}
/**
* Gets a Transformer that returns the identifier of the input object.
* The Transformer tries to cast the object to GetIdentifier. If that fails
* reflection is used.
*
* @return the transformer
*/
public static Transformer getIdentifierTransformer() {
return GET_IDENTIFIER_TRANSFORMER;
}
// GetTransformer
//----------------------------------------------------------------------------------
/**
* GetTransformer is the superclass for the Transformers.
*/
private static abstract class GetTransformer implements Transformer, Serializable {
/** Cache of Class to Method */
private static final Map cCache = new HashMap();
/** Null placeholder */
private static final Object NULL = new Object();
/**
* Constructor
*/
protected GetTransformer() {
super();
}
/**
* Call a method on the object by reflection
*/
protected Object transform(Object input, String methodName, Class returnType) {
if (input == null) {
throw new TransformerException("GetTransformer: The input object cannot be null");
} else {
Object methodObj = cCache.get(input.getClass());
if (methodObj == NULL) {
throw new TransformerException("GetTransformer: The " + methodName + " method could not be accessed on " + input.getClass());
}
Method method = (Method) methodObj;
if (method == null) {
try {
method = input.getClass().getMethod(methodName, null);
if (method.getReturnType() != returnType) {
throw new TransformerException("GetTransformer: The "+ methodName + " method does not return a " + returnType.getName() + " on " + input.getClass());
}
} catch (NoSuchMethodException ex) {
cCache.put(input.getClass(), NULL);
throw new TransformerException("GetTransformer: The " + methodName + " method does not exist on " + input.getClass(), ex);
}
}
try {
return method.invoke(input, null);
} catch (IllegalAccessException ex) {
cCache.put(input.getClass(), NULL);
throw new TransformerException("GetTransformer: The " + methodName + " method could not be accessed on " + input.getClass(), ex);
} catch (InvocationTargetException ex) {
throw new TransformerException("GetTransformer: The " + methodName + " method threw an exception on " + input.getClass(), ex);
}
}
}
}
// GetNameTransformer
//----------------------------------------------------------------------------------
/**
* GetNameTransformer calls the getName method on the object
*/
private static class GetNameTransformer extends GetTransformer {
/**
* Constructor
*/
private GetNameTransformer() {
super();
}
public Object transform(Object input) {
if (input instanceof GetName) {
return ((GetName) input).getName();
} else {
return transform(input, "getName", String.class);
}
}
}
// GetIdentifierTransformer
//----------------------------------------------------------------------------------
/**
* GetIdentifierTransformer calls the getIdentifier method on the object
*/
private static class GetIdentifierTransformer extends GetTransformer {
/**
* Constructor
*/
private GetIdentifierTransformer() {
super();
}
public Object transform(Object input) {
if (input instanceof GetIdentifier) {
return ((GetIdentifier) input).getIdentifier();
} else {
return transform(input, "getIdentifier", String.class);
}
}
}
}
1.1 jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/bean/get/GetIdentifier.java
Index: GetIdentifier.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.pattern.bean.get;
/**
* <code>GetIdentifier</code> defines an interface implemented by classes that
* have a unique String identifier.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: GetIdentifier.java,v 1.1 2002/09/08 18:00:59 scolebourne Exp $
*/
public interface GetIdentifier {
/**
* Get the String identifier for this object.
*
* @return the identifier
*/
public String getIdentifier();
}
1.1 jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/bean/query/IsValid.java
Index: IsValid.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.pattern.bean.query;
/**
* <code>IsValid</code> defines a method signature used to query if
* an object is valid or not.
* <p>
* This interface signature can be used when the validation of an object
* is encapsulated within the object.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: IsValid.java,v 1.1 2002/09/08 18:00:59 scolebourne Exp $
*/
public interface IsValid {
/**
* Is the object 'valid'.
*
* @return true if valid
*/
public boolean isValid();
}
1.1 jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/bean/query/IsEmpty.java
Index: IsEmpty.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.pattern.bean.query;
/**
* <code>IsEmpty</code> defines a method signature used to query if
* an object is empty or not.
* <p>
* For example, consider an Address bean with street, town city and postal
* code fields. A typical validation is to say that if any one field is
* entered, then they all must be. A call to isEmpty() allows the validation
* code to determine the state of the four fields in one call.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: IsEmpty.java,v 1.1 2002/09/08 18:00:59 scolebourne Exp $
*/
public interface IsEmpty {
/**
* Is the object 'empty'.
*
* @return true if empty
*/
public boolean isEmpty();
}
1.1 jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/bean/query/QueryUtils.java
Index: QueryUtils.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.pattern.bean.query;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.pattern.predicate.Predicate;
import org.apache.commons.pattern.predicate.PredicateException;
/**
* <code>QueryUtils</code> provides reference implementations and utilities
* for the Query pattern interfaces. The supplied implementations are:
* <ul>
* <li>IsEmptyPredicate - returns whether the object is empty or not
* <li>IsValidPredicate - returns whether the object is valid or not
* </ul>
* All the supplied implementations are Serializable.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: QueryUtils.java,v 1.1 2002/09/08 18:00:59 scolebourne Exp $
*/
public class QueryUtils {
private static final Predicate IS_EMPTY_PREDICATE = new IsEmptyPredicate();
private static final Predicate IS_VALID_PREDICATE = new IsValidPredicate();
/**
* Restrictive constructor
*/
protected QueryUtils() {
super();
}
/**
* Gets a Predicate that calls the isEmpty method of the input object.
* The Predicate tries to cast the object to IsEmpty. If that fails
* reflection is used.
*
* @return the predicate
*/
public static Predicate isEmptyPredicate() {
return IS_EMPTY_PREDICATE;
}
/**
* Gets a Predicate that calls the isValid method of the input object.
* The Predicate tries to cast the object to IsValid. If that fails
* reflection is used.
*
* @return the predicate
*/
public static Predicate isValidPredicate() {
return IS_VALID_PREDICATE;
}
// QueryPredicate
//----------------------------------------------------------------------------------
/**
* QueryPredicate is the superclass for the Predicates.
*/
private static abstract class QueryPredicate implements Predicate, Serializable {
/** Cache of Class to Method */
private static final Map cCache = new HashMap();
/** Null placeholder */
private static final Object NULL = new Object();
/**
* Constructor
*/
protected QueryPredicate() {
super();
}
/**
* Call a method on the object by reflection
*/
protected boolean evaluate(Object input, String methodName) {
if (input == null) {
throw new PredicateException("QueryPredicate: The input object cannot be null");
} else {
Object methodObj = cCache.get(input.getClass());
if (methodObj == NULL) {
throw new PredicateException("QueryPredicate: The " + methodName + " method could not be accessed on " + input.getClass());
}
Method method = (Method) methodObj;
if (method == null) {
try {
method = input.getClass().getMethod(methodName, null);
if (method.getReturnType() != Boolean.TYPE) {
throw new PredicateException("QueryPredicate: The "+ methodName + " method does not return a boolean on " + input.getClass());
}
} catch (NoSuchMethodException ex) {
cCache.put(input.getClass(), NULL);
throw new PredicateException("QueryPredicate: The " + methodName + " method does not exist on " + input.getClass(), ex);
}
}
try {
Boolean b = (Boolean) method.invoke(input, null);
return b.booleanValue();
} catch (IllegalAccessException ex) {
cCache.put(input.getClass(), NULL);
throw new PredicateException("QueryPredicate: The " + methodName + " method could not be accessed on " + input.getClass(), ex);
} catch (InvocationTargetException ex) {
throw new PredicateException("QueryPredicate: The " + methodName + " method threw an exception on " + input.getClass(), ex);
}
}
}
}
// IsEmptyPredicate
//----------------------------------------------------------------------------------
/**
* IsEmptyPredicate calls the isEmpty method on the object
*/
private static class IsEmptyPredicate extends QueryPredicate {
/**
* Constructor
*/
private IsEmptyPredicate() {
super();
}
public boolean evaluate(Object input) {
if (input instanceof IsEmpty) {
return ((IsEmpty) input).isEmpty();
} else {
return evaluate(input, "isEmpty");
}
}
}
// IsValidPredicate
//----------------------------------------------------------------------------------
/**
* IsValidPredicate calls the isValid method on the object
*/
private static class IsValidPredicate extends QueryPredicate {
/**
* Constructor
*/
private IsValidPredicate() {
super();
}
public boolean evaluate(Object input) {
if (input instanceof IsEmpty) {
return ((IsEmpty) input).isEmpty();
} else {
return evaluate(input, "isValid");
}
}
}
}
1.1 jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/bean/action/Validate.java
Index: Validate.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.pattern.bean.action;
/**
* <code>Validate</code> defines an interface implemented by classes that
* can validate themselves.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: Validate.java,v 1.1 2002/09/08 18:00:59 scolebourne Exp $
*/
public interface Validate {
/**
* Validates the object.
*/
public void validate();
}
1.1 jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/bean/action/ActionUtils.java
Index: ActionUtils.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.pattern.bean.action;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.pattern.command.Command;
import org.apache.commons.pattern.command.CommandException;
import org.apache.commons.pattern.bean.get.GetName;
import org.apache.commons.pattern.transformer.Transformer;
/**
* <code>ActionUtils</code> provides reference implementations and utilities
* for the Action pattern interfaces. The supplied implementations are:
* <ul>
* <li>ClearCommand - calls clear on the input object
* <li>ResetCommand - calls clear on the input object
* <li>ValidateCommand - calls validate on the input object
* </ul>
* All the supplied implementations are Serializable.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: ActionUtils.java,v 1.1 2002/09/08 18:00:59 scolebourne Exp $
*/
public class ActionUtils {
private static final Command CLEAR_COMMAND = new ClearCommand();
private static final Command RESET_COMMAND = new ResetCommand();
private static final Command VALIDATE_COMMAND = new ValidateCommand();
/**
* Restrictive constructor
*/
protected ActionUtils() {
super();
}
/**
* Gets a Command that clears the input object.
* The Command tries to cast the object to Clear. If that fails
* reflection is used.
*
* @return the command
*/
public static Command getClearCommand() {
return CLEAR_COMMAND;
}
/**
* Gets a Command that resets the input object.
* The Command tries to cast the object to Reset. If that fails
* reflection is used.
*
* @return the command
*/
public static Command getResetCommand() {
return RESET_COMMAND;
}
/**
* Gets a Command that validates the input object.
* The Command tries to cast the object to Validate. If that fails
* reflection is used.
*
* @return the command
*/
public static Command getValidateCommand() {
return VALIDATE_COMMAND;
}
// SimpleCommand
//----------------------------------------------------------------------------------
/**
* SimpleCommand is the superclass for the Command.
*/
private static abstract class SimpleCommand implements Command, Serializable {
/** Cache of Class to Method */
private static final Map cCache = new HashMap();
/** Null placeholder */
private static final Object NULL = new Object();
/**
* Constructor
*/
protected SimpleCommand() {
super();
}
/**
* Call a method on the object by reflection
*/
protected void execute(Object input, String methodName) {
if (input == null) {
throw new CommandException("SimpleCommand: The input object cannot be null");
} else {
Object methodObj = cCache.get(input.getClass());
if (methodObj == NULL) {
throw new CommandException("SimpleCommand: The " + methodName + " method could not be accessed on " + input.getClass());
}
Method method = (Method) methodObj;
if (method == null) {
try {
method = input.getClass().getMethod(methodName, null);
if (method.getReturnType() != Void.TYPE) {
throw new CommandException("SimpleCommand: The "+ methodName + " method does not return void on " + input.getClass());
}
} catch (NoSuchMethodException ex) {
cCache.put(input.getClass(), NULL);
throw new CommandException("SimpleCommand: The " + methodName + " method does not exist on " + input.getClass(), ex);
}
}
try {
method.invoke(input, null);
return;
} catch (IllegalAccessException ex) {
cCache.put(input.getClass(), NULL);
throw new CommandException("SimpleCommand: The " + methodName + " method could not be accessed on " + input.getClass(), ex);
} catch (InvocationTargetException ex) {
throw new CommandException("SimpleCommand: The " + methodName + " method threw an exception on " + input.getClass(), ex);
}
}
}
}
// ClearCommand
//----------------------------------------------------------------------------------
/**
* ClearCommand calls the clear method on the object
*/
private static class ClearCommand extends SimpleCommand {
/**
* Constructor
*/
private ClearCommand() {
super();
}
public void execute(Object input) {
if (input instanceof Clear) {
((Clear) input).clear();
} else {
execute(input, "clear");
}
}
}
// ResetCommand
//----------------------------------------------------------------------------------
/**
* ResetCommand calls the clear method on the object
*/
private static class ResetCommand extends SimpleCommand {
/**
* Constructor
*/
private ResetCommand() {
super();
}
public void execute(Object input) {
if (input instanceof Reset) {
((Reset) input).reset();
} else {
execute(input, "reset");
}
}
}
// ValidateCommand
//----------------------------------------------------------------------------------
/**
* ValidateCommand calls the validate method on the object
*/
private static class ValidateCommand extends SimpleCommand {
/**
* Constructor
*/
private ValidateCommand() {
super();
}
public void execute(Object input) {
if (input instanceof Validate) {
((Validate) input).validate();
} else {
execute(input, "validate");
}
}
}
}
1.1 jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/bean/action/Clear.java
Index: Clear.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.pattern.bean.action;
/**
* <code>Clear</code> defines an interface implemented by classes that
* can clear themselves.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: Clear.java,v 1.1 2002/09/08 18:00:59 scolebourne Exp $
*/
public interface Clear {
/**
* Clear the object.
*/
public void clear();
}
1.1 jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/bean/action/Reset.java
Index: Reset.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.pattern.bean.action;
/**
* <code>Reset</code> defines an interface implemented by classes that
* can reset themselves.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: Reset.java,v 1.1 2002/09/08 18:00:59 scolebourne Exp $
*/
public interface Reset {
/**
* Reset the object to an initial state.
*/
public void reset();
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>