You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by mc...@apache.org on 2003/12/02 08:36:30 UTC

cvs commit: avalon-sandbox/repository/spi/src/java/org/apache/avalon/repository/criteria Parameter.java

mcconnell    2003/12/01 23:36:30

  Modified:    repository/spi/src/java/org/apache/avalon/repository/criteria
                        Parameter.java
  Log:
  Add value resolution locally so we can extend parameters and override the value resolution operation.
  
  Revision  Changes    Path
  1.6       +94 -5     avalon-sandbox/repository/spi/src/java/org/apache/avalon/repository/criteria/Parameter.java
  
  Index: Parameter.java
  ===================================================================
  RCS file: /home/cvs/avalon-sandbox/repository/spi/src/java/org/apache/avalon/repository/criteria/Parameter.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Parameter.java	30 Nov 2003 08:11:37 -0000	1.5
  +++ Parameter.java	2 Dec 2003 07:36:30 -0000	1.6
  @@ -50,6 +50,9 @@
   
   package org.apache.avalon.repository.criteria ;
   
  +import java.lang.reflect.Constructor;
  +
  +
   /**
    * 
    * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
  @@ -57,18 +60,46 @@
    */
   public class Parameter
   {
  +    //--------------------------------------------------------------
  +    // static
  +    //--------------------------------------------------------------
  +
  +   /**
  +    * Return the set of keys corresponding to the supplied set of 
  +    * parameters.
  +    * @param params the parameter sequence
  +    * @return the corresponding keys
  +    */
  +    public static String[] getKeys( Parameter[] params )
  +    {
  +        String[] keys = new String[ params.length ];
  +        for( int i=0; i<params.length; i++ )
  +        {
  +            keys[i] = params[i].getKey();
  +        }
  +        return keys;
  +    }
  +
  +    //--------------------------------------------------------------
  +    // immutable state
  +    //--------------------------------------------------------------
  +
       private final String m_key;
  -    private final String m_type;
  +    private final Class m_type;
       private final boolean m_required;
       private final Object m_default;
   
  +    //--------------------------------------------------------------
  +    // constructors
  +    //--------------------------------------------------------------
  +
       /**
        * Creation of a new required parameter constraint.
        * @param key the parameter key
        * @param type the name of a class constraining assigned values
        */
       public Parameter( 
  -      final String key, final String type ) 
  +      final String key, final Class type ) 
       {
           m_key = key;
           m_type = type;
  @@ -83,7 +114,7 @@
        * @param value the default value
        */
       public Parameter( 
  -      final String key, final String type, Object value ) 
  +      final String key, final Class type, Object value ) 
       {
           m_key = key;
           m_type = type;
  @@ -91,6 +122,10 @@
           m_default = value;
       }
   
  +    //--------------------------------------------------------------
  +    // implementation
  +    //--------------------------------------------------------------
  +
      /**
       * Return the key for the parameter.
       * @return the key
  @@ -104,7 +139,7 @@
       * Return the classname for the parameter.
       * @return the classname
       */
  -    public String getClassname()
  +    public Class getParameterClass()
       {
           return m_type;
       }
  @@ -136,4 +171,58 @@
           return m_default;
       }
   
  +   /**
  +    * Resolve a supplied argument to a value.
  +    * @param arg the supplied argument
  +    * @return the resolved object
  +    * @exception Exception if an error occurs
  +    */
  +    public Object resolve( Object value ) throws ValidationException
  +    {
  +        if( value == null ) return null;
  +        if( m_type.isInstance( value ) )
  +        {
  +            return value;
  +        }
  +        else
  +        { 
  +            Constructor constructor = null;
  +            try
  +            {
  +                constructor = 
  +                  m_type.getConstructor( 
  +                    new Class[]{ value.getClass() } );
  +            }
  +            catch( NoSuchMethodException nsme )
  +            {
  +                final String error =
  +                  "Value of class: [" 
  +                  + value.getClass().getName() 
  +                  + "] supplied for key [" 
  +                  + getKey() 
  +                  + "] is not an instance of type: [" 
  +                  + m_type.getName()
  +                  + "].";
  +                throw new IllegalArgumentException( error );
  +            }
  +
  +            try
  +            {
  +                return constructor.newInstance( 
  +                  new Object[]{ value } );
  +            }
  +            catch( Throwable e )
  +            {
  +                final String error =
  +                  "Value of class: [" 
  +                  + value.getClass().getName() 
  +                  + "] supplied for key [" 
  +                  + getKey() 
  +                  + "] is not an instance of or was not resolvable to the type: [" 
  +                  + m_type.getName()
  +                  + "].";
  +                throw new ValidationException( error, e );
  +            }
  +        }
  +    }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org