ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adammurd...@apache.org
Subject cvs commit: jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/type DefaultTypeFactory.java TypeFactory.java
Date Thu, 14 Feb 2002 02:00:07 GMT
adammurdoch    02/02/13 18:00:07

  Modified:    proposal/myrmidon/src/java/org/apache/myrmidon/components/type
                        MultiSourceTypeFactory.java
               proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/type
                        DefaultTypeFactory.java TypeFactory.java
  Log:
  Added TypeFactory.canCreate() method.
  
  Revision  Changes    Path
  1.11      +31 -16    jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/type/MultiSourceTypeFactory.java
  
  Index: MultiSourceTypeFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/type/MultiSourceTypeFactory.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- MultiSourceTypeFactory.java	7 Feb 2002 13:02:20 -0000	1.10
  +++ MultiSourceTypeFactory.java	14 Feb 2002 02:00:07 -0000	1.11
  @@ -17,7 +17,7 @@
    * This factory acts as a proxy to set of object factorys.
    *
    * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  - * @version $Revision: 1.10 $ $Date: 2002/02/07 13:02:20 $
  + * @version $Revision: 1.11 $ $Date: 2002/02/14 02:00:07 $
    */
   public class MultiSourceTypeFactory
       implements TypeFactory
  @@ -55,6 +55,14 @@
       }
   
       /**
  +     * Determines if this factory can create instances of a particular type.
  +     */
  +    public boolean canCreate( final String name )
  +    {
  +        return ( findFactory( name ) != null );
  +    }
  +
  +    /**
        * Create a type instance based on name.
        *
        * @param name the name
  @@ -64,30 +72,37 @@
       public Object create( final String name )
           throws TypeException
       {
  -        TypeFactory factory = getTypeFactory( name );
  -
  -        if( null == factory && null != m_parent )
  -        {
  -            factory = m_parent.getTypeFactory( name );
  -        }
  -
  +        // Locate the factory to use
  +        TypeFactory factory = findFactory( name );
           if( null == factory )
           {
               final String message = REZ.getString( "no-factory.error", name );
               throw new TypeException( message );
           }
  -        else
  +
  +        // Create the object
  +        final Object object = factory.create( name );
  +        if( !m_type.isInstance( object ) )
           {
  -            final Object object = factory.create( name );
  +            final String message = REZ.getString( "mismatched-type.error", name, object.getClass().getName()
);
  +            throw new TypeException( message );
  +        }
   
  -            if( !m_type.isInstance( object ) )
  -            {
  -                final String message = REZ.getString( "mismatched-type.error", name, object.getClass().getName()
);
  -                throw new TypeException( message );
  -            }
  +        return object;
  +    }
   
  -            return object;
  +    /**
  +     * Locates the type factory to use for a particular type.
  +     */
  +    private TypeFactory findFactory( final String name )
  +    {
  +        TypeFactory factory = getTypeFactory( name );
  +        if( null == factory && null != m_parent )
  +        {
  +            factory = m_parent.getTypeFactory( name );
           }
  +
  +        return factory;
       }
   
       /**
  
  
  
  1.9       +18 -10    jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/type/DefaultTypeFactory.java
  
  Index: DefaultTypeFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/type/DefaultTypeFactory.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- DefaultTypeFactory.java	7 Feb 2002 10:35:16 -0000	1.8
  +++ DefaultTypeFactory.java	14 Feb 2002 02:00:07 -0000	1.9
  @@ -15,7 +15,7 @@
    * Create a type instance based on name.
    *
    * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  - * @version CVS $Revision: 1.8 $ $Date: 2002/02/07 10:35:16 $
  + * @version CVS $Revision: 1.9 $ $Date: 2002/02/14 02:00:07 $
    */
   public class DefaultTypeFactory
       implements TypeFactory
  @@ -59,6 +59,14 @@
       }
   
       /**
  +     * Determines if this factory can create instances of a particular type.
  +     */
  +    public boolean canCreate( String name )
  +    {
  +        return ( getClassName( name ) != null );
  +    }
  +
  +    /**
        * Create a type instance with appropriate name.
        *
        * @param name the name
  @@ -68,7 +76,15 @@
       public Object create( final String name )
           throws TypeException
       {
  +        // Determine the name of the class to instantiate
           final String className = getClassName( name );
  +        if( null == className )
  +        {
  +            final String message = REZ.getString( "no-mapping.error", name );
  +            throw new TypeException( message );
  +        }
  +
  +        // Instantiate the object
           try
           {
               final ClassLoader classLoader = getClassLoader();
  @@ -83,16 +99,8 @@
       }
   
       private String getClassName( final String name )
  -        throws TypeException
       {
  -        final String className = (String)m_classNames.get( name );
  -        if( null == className )
  -        {
  -            final String message = REZ.getString( "no-mapping.error", name );
  -            throw new TypeException( message );
  -        }
  -
  -        return className;
  +        return  (String)m_classNames.get( name );
       }
   
       protected ClassLoader getClassLoader()
  
  
  
  1.4       +10 -3     jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/type/TypeFactory.java
  
  Index: TypeFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/type/TypeFactory.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TypeFactory.java	23 Dec 2001 06:25:37 -0000	1.3
  +++ TypeFactory.java	14 Feb 2002 02:00:07 -0000	1.4
  @@ -11,16 +11,23 @@
    * Create an instance on name.
    *
    * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  - * @version CVS $Revision: 1.3 $ $Date: 2001/12/23 06:25:37 $
  + * @version CVS $Revision: 1.4 $ $Date: 2002/02/14 02:00:07 $
    */
   public interface TypeFactory
   {
       /**
  +     * Determines if this factory can create instances of a particular type.
  +     *
  +     * @param name the type name.
  +     */
  +    boolean canCreate( String name );
  +
  +    /**
        * Create a type instance based on name.
        *
  -     * @param name the name
  +     * @param name the type name
        * @return the type instance
  -     * @exception TypeException if an error occurs
  +     * @exception TypeException if the type is unknown, or an error occurs.
        */
       Object create( String name )
           throws TypeException;
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message