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/08/12 23:25:38 UTC

cvs commit: jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/emptyqueryable EmptyQueryable.java EmptyQueryableUtils.java

scolebourne    2002/08/12 14:25:38

  Added:       pattern/src/java/org/apache/commons/pattern/validqueryable
                        ValidQueryable.java ValidQueryableUtils.java
               pattern/src/java/org/apache/commons/pattern/nameable
                        Nameable.java NameableUtils.java
               pattern/src/java/org/apache/commons/pattern/emptyqueryable
                        EmptyQueryable.java EmptyQueryableUtils.java
  Log:
  Add new pattern interfaces
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/validqueryable/ValidQueryable.java
  
  Index: ValidQueryable.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.validqueryable;
  
  /**
   * <code>ValidQueryable</code> defines a method signature used to get 
   * 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: ValidQueryable.java,v 1.1 2002/08/12 21:25:38 scolebourne Exp $
   */
  public interface ValidQueryable {
      
      /**
       * Is the object 'valid'.
       *
       * @return true if valid
       */
      public boolean isValid();
      
  }
  
  
  
  1.1                  jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/validqueryable/ValidQueryableUtils.java
  
  Index: ValidQueryableUtils.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.validqueryable;
  
  import java.io.Serializable;
  import java.lang.reflect.Method;
  
  import org.apache.commons.pattern.predicate.Predicate;
  import org.apache.commons.pattern.transformer.TransformerException;
  /**
   * <code>ValidQueryableUtils</code> provides reference implementations and utilities
   * for the ValidQueryable pattern interface. The supplied implementations are:
   * <ul>
   * <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: ValidQueryableUtils.java,v 1.1 2002/08/12 21:25:38 scolebourne Exp $
   */
  public class ValidQueryableUtils {
  
      private static final Predicate IS_VALID_PREDICATE = new IsValidPredicate();
  
      /**
       * Restructive constructor
       */
      private ValidQueryableUtils() {
          super();
      }
  
      /**
       * Gets a Predicate that calls the isValid method of the input object.
       * The Predicate tries to cast the object to ValidQueryable. If that fails
       * reflection is used.
       *
       * @return the predicate
       */
      public static Predicate isValidPredicate() {
          return IS_VALID_PREDICATE;
      }
  
      // IsValidPredicate
      //----------------------------------------------------------------------------------
  
      /**
       * IsValidPredicate calls the isValid method on the object
       */
      private static class IsValidPredicate implements Predicate, Serializable {
  
          /**
           * Constructor
           */
          private IsValidPredicate() {
              super();
          }
  
          /**
           * Call the isValid method on the object
           */
          public boolean evaluate(Object input) {
              if (input instanceof ValidQueryable) {
                  return ((ValidQueryable) input).isValid();
              } else if (input == null) {
                  return true;
              } else {
                  try {
                      Method method = input.getClass().getMethod("isValid", null);
                      if (method.getReturnType() != Boolean.TYPE) {
                          throw new TransformerException("IsValidPredicate: The isValid method did not return a boolean");
                      }
                      Boolean b = (Boolean) method.invoke(input, null);
                      return b.booleanValue();
  
                  } catch (Exception ex) {
                      throw new TransformerException("IsValidPredicate: The isValid method could not be accessed", ex);
                  }
              }
          }
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/nameable/Nameable.java
  
  Index: Nameable.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.nameable;
  
  /**
   * <code>Nameable</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: Nameable.java,v 1.1 2002/08/12 21:25:38 scolebourne Exp $
   */
  public interface Nameable {
      
      /**
       * Get the name of the object.
       *
       * @return the name
       */
      public String getName();
      
  }
  
  
  
  1.1                  jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/nameable/NameableUtils.java
  
  Index: NameableUtils.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.nameable;
  
  import java.io.Serializable;
  import java.lang.reflect.Method;
  
  import org.apache.commons.pattern.transformer.Transformer;
  import org.apache.commons.pattern.transformer.TransformerException;
  /**
   * <code>ValidQueryableUtils</code> provides reference implementations and utilities
   * for the Nameable pattern interface. The supplied implementations are:
   * <ul>
   * <li>GetNameTransformer - returns the name of the Nameable input object
   * </ul>
   * All the supplied implementations are Serializable.
   *
   * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
   * @version $Id: NameableUtils.java,v 1.1 2002/08/12 21:25:38 scolebourne Exp $
   */
  public class NameableUtils {
  
      private static final Transformer NAME_TRANSFORMER = new NameTransformer();
  
      /**
       * Restructive constructor
       */
      private NameableUtils() {
          super();
      }
  
      /**
       * Gets a Transformer that returns the name of the input object.
       * The Transformer tries to cast the object to Nameable. If that fails
       * reflection is used.
       *
       * @return the transformer
       */
      public static Transformer getNameTransformer() {
          return NAME_TRANSFORMER;
      }
  
      // NameTransformer
      //----------------------------------------------------------------------------------
  
      /**
       * NameTransformer gets the name from the input object
       */
      private static class NameTransformer implements Transformer, Serializable {
  
          /**
           * Constructor
           */
          private NameTransformer() {
              super();
          }
  
          /**
           * Always throw an exception
           */
          public Object transform(Object input) {
              if (input instanceof Nameable) {
                  return ((Nameable) input).getName();
              } else if (input == null) {
                  return null;
              } else {
                  try {
                      Method method = input.getClass().getMethod("getName", null);
                      if (method.getReturnType() != String.class) {
                          throw new TransformerException("NameTransformer: The getName method did not return a String");
                      }
                      return method.invoke(input, null);
  
                  } catch (Exception ex) {
                      throw new TransformerException("NameTransformer: The getName method could not be accessed", ex);
                  }
              }
          }
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/emptyqueryable/EmptyQueryable.java
  
  Index: EmptyQueryable.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.emptyqueryable;
  
  /**
   * <code>ValidQueryable</code> defines a method signature used to get 
   * 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: EmptyQueryable.java,v 1.1 2002/08/12 21:25:38 scolebourne Exp $
   */
  public interface EmptyQueryable {
      
      /**
       * Is the object 'empty'.
       *
       * @return true if empty
       */
      public boolean isEmpty();
      
  }
  
  
  
  1.1                  jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/emptyqueryable/EmptyQueryableUtils.java
  
  Index: EmptyQueryableUtils.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.emptyqueryable;
  
  import java.io.Serializable;
  import java.lang.reflect.Method;
  
  import org.apache.commons.pattern.predicate.Predicate;
  import org.apache.commons.pattern.transformer.TransformerException;
  /**
   * <code>EmptyQueryableUtils</code> provides reference implementations and utilities
   * for the ValidQueryable pattern interface. The supplied implementations are:
   * <ul>
   * <li>IsEmptyPredicate - returns whether the object is empty or not
   * </ul>
   * All the supplied implementations are Serializable.
   *
   * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
   * @version $Id: EmptyQueryableUtils.java,v 1.1 2002/08/12 21:25:38 scolebourne Exp $
   */
  public class EmptyQueryableUtils {
  
      private static final Predicate IS_EMPTY_PREDICATE = new IsEmptyPredicate();
  
      /**
       * Restructive constructor
       */
      private EmptyQueryableUtils() {
          super();
      }
  
      /**
       * Gets a Predicate that calls the isEmpty method of the input object.
       * The Predicate tries to cast the object to EmptyQueryable. If that fails
       * reflection is used.
       *
       * @return the predicate
       */
      public static Predicate isEmptyPredicate() {
          return IS_EMPTY_PREDICATE;
      }
  
      // IsEmptyPredicate
      //----------------------------------------------------------------------------------
  
      /**
       * IsEmptyPredicate calls the isValid method on the object
       */
      private static class IsEmptyPredicate implements Predicate, Serializable {
  
          /**
           * Constructor
           */
          private IsEmptyPredicate() {
              super();
          }
  
          /**
           * Call the isEmpty method on the object
           */
          public boolean evaluate(Object input) {
              if (input instanceof EmptyQueryable) {
                  return ((EmptyQueryable) input).isEmpty();
              } else if (input == null) {
                  return true;
              } else {
                  try {
                      Method method = input.getClass().getMethod("isEmpty", null);
                      if (method.getReturnType() != Boolean.TYPE) {
                          throw new TransformerException("IsEmptyPredicate: The isEmpty method did not return a boolean");
                      }
                      Boolean b = (Boolean) method.invoke(input, null);
                      return b.booleanValue();
  
                  } catch (Exception ex) {
                      throw new TransformerException("IsEmptyPredicate: The isEmpty method could not be accessed", ex);
                  }
              }
          }
      }
  
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>