KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > util > criteria > Parameter


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.util.criteria;
19
20 import java.lang.reflect.Constructor JavaDoc;
21 import java.io.Serializable JavaDoc;
22
23 /**
24  * A parameter is an immutable class that contains a description
25  * of an allowable parameter within a crieria instance.
26  *
27  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
28  * @version $Revision: 1.5 $
29  */

30 public class Parameter implements Serializable JavaDoc
31 {
32     //--------------------------------------------------------------
33
// static
34
//--------------------------------------------------------------
35

36    /**
37     * Return the set of keys corresponding to the supplied set of
38     * parameters.
39     * @param params the parameter sequence
40     * @return the corresponding keys
41     */

42     public static String JavaDoc[] getKeys( Parameter[] params )
43     {
44         String JavaDoc[] keys = new String JavaDoc[ params.length ];
45         for( int i=0; i<params.length; i++ )
46         {
47             keys[i] = params[i].getKey();
48         }
49         return keys;
50     }
51
52     //--------------------------------------------------------------
53
// immutable state
54
//--------------------------------------------------------------
55

56     private final String JavaDoc m_key;
57     private final Class JavaDoc m_type;
58     private final boolean m_required;
59     private final Object JavaDoc m_default;
60
61     //--------------------------------------------------------------
62
// constructors
63
//--------------------------------------------------------------
64

65     /**
66      * Creation of a new required parameter constraint.
67      * @param key the parameter key
68      * @param type the name of a class constraining assigned values
69      */

70     public Parameter(
71       final String JavaDoc key, final Class JavaDoc type )
72     {
73         m_key = key;
74         m_type = type;
75         m_required = true;
76         m_default = null;
77     }
78
79     /**
80      * Creation of a new optional parameter constraint.
81      * @param key the parameter key
82      * @param type the name of a class constraining assigned values
83      * @param value the default value
84      */

85     public Parameter(
86       final String JavaDoc key, final Class JavaDoc type, Object JavaDoc value )
87     {
88         m_key = key;
89         m_type = type;
90         m_required = false;
91         m_default = value;
92     }
93
94     //--------------------------------------------------------------
95
// implementation
96
//--------------------------------------------------------------
97

98    /**
99     * Return the key for the parameter.
100     * @return the key
101     */

102     public String JavaDoc getKey()
103     {
104         return m_key;
105     }
106
107    /**
108     * Return the classname for the parameter.
109     * @return the classname
110     */

111     public Class JavaDoc getParameterClass()
112     {
113         return m_type;
114     }
115
116    /**
117     * Return TRUE is the parameter is required.
118     * @return the required status of the parameter
119     */

120     public boolean isRequired()
121     {
122         return m_required;
123     }
124
125    /**
126     * Return TRUE is the parameter is optional.
127     * @return the optional status of the parameter
128     */

129     public boolean isOptional()
130     {
131         return !isRequired();
132     }
133
134    /**
135     * Return the default value for this parameter.
136     * @return the default value
137     */

138     public Object JavaDoc getDefault()
139     {
140         return m_default;
141     }
142
143    /**
144     * Resolve a supplied argument to a value.
145     * @param value the supplied argument
146     * @return the resolved object
147     * @exception Exception if an error occurs
148     */

149     public Object JavaDoc resolve( Object JavaDoc value ) throws CriteriaException
150     {
151         return resolve( m_type, value );
152     }
153
154    /**
155     * Resolve a supplied argument to a value.
156     * @param type the base class
157     * @param value the supplied argument
158     * @return the resolved object
159     * @exception Exception if an error occurs
160     */

161     protected Object JavaDoc resolve( Class JavaDoc type, Object JavaDoc value ) throws CriteriaException
162     {
163         if( value == null )
164             return null;
165         if( type == null )
166             throw new NullPointerException JavaDoc( "type" );
167         if( type.isInstance( value ) )
168         {
169             return value;
170         }
171         else
172         {
173             Constructor JavaDoc constructor = null;
174             try
175             {
176                 constructor =
177                   type.getConstructor(
178                     new Class JavaDoc[]{ value.getClass() } );
179             }
180             catch( NoSuchMethodException JavaDoc nsme )
181             {
182                 final String JavaDoc error =
183                   "Value of class: ["
184                   + value.getClass().getName()
185                   + "] supplied for key ["
186                   + getKey()
187                   + "] is not an instance of type: ["
188                   + type.getName()
189                   + "].";
190                 throw new CriteriaException( error );
191             }
192
193             try
194             {
195                 return constructor.newInstance(
196                   new Object JavaDoc[]{ value } );
197             }
198             catch( Throwable JavaDoc e )
199             {
200                 final String JavaDoc error =
201                   "Value of class: ["
202                   + value.getClass().getName()
203                   + "] supplied for key ["
204                   + getKey()
205                   + "] is not an instance of or was not resolvable to the type: ["
206                   + type.getName()
207                   + "].";
208                 throw new CriteriaException( error, e );
209             }
210         }
211     }
212 }
213
Popular Tags