KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
21
22 /**
23  * A abstract utility class that can be used to simplify the
24  * creation of domain specific criteria.
25  *
26  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
27  * @version $Revision: 1.5 $
28  */

29 public class Criteria extends HashMap JavaDoc
30 {
31     //--------------------------------------------------------------
32
// state
33
//--------------------------------------------------------------
34

35      private final Parameter[] m_params;
36
37     //--------------------------------------------------------------
38
// constructor
39
//--------------------------------------------------------------
40

41
42    /**
43     * Creation of a new criteria instance.
44     * @param params the set of parameters managed by the criteria
45     */

46     public Criteria( final Parameter[] params )
47     {
48         if( null == params )
49           throw new NullPointerException JavaDoc( "params" );
50         m_params = params;
51     }
52
53     //--------------------------------------------------------------
54
// Criteria
55
//--------------------------------------------------------------
56

57    /**
58     * Set a named parameter of the criteria to a value.
59     * @param key the parameter key
60     * @param value the value to assign to the key
61     * @return the original value
62     * @exception CriteriaRuntimeException if the supplied value fails
63     * the validation test for its associated parameter
64     */

65     public Object JavaDoc put( final Object JavaDoc key, final Object JavaDoc value )
66     {
67         if( !(key instanceof String JavaDoc ))
68         {
69             final String JavaDoc error =
70               "Invalid key: " + key;
71             throw new IllegalArgumentException JavaDoc( error );
72         }
73
74         Object JavaDoc current = super.get( key );
75
76         if( null == value )
77         {
78             super.put( key, null );
79             return current;
80         }
81
82         final Parameter p = getParameter( (String JavaDoc) key );
83
84         try
85         {
86             final Object JavaDoc v = p.resolve( value );
87             if( p.getParameterClass().isInstance( v ) )
88             {
89                 super.put( key, v );
90                 return current;
91             }
92             else
93             {
94                 final String JavaDoc error =
95                   "Resolved value: " + v
96                   + " does not implement the parameter type: "
97                   + p.getParameterClass();
98                 throw new IllegalArgumentException JavaDoc( error );
99             }
100         }
101         catch( Throwable JavaDoc e )
102         {
103             final String JavaDoc error =
104               "Unable to assign a value to the key: " + key;
105             throw new CriteriaRuntimeException( error, e );
106         }
107     }
108
109    /**
110     * Return the currently assigned value for a key.
111     * @return the assigned value
112     */

113     public Object JavaDoc get( final Object JavaDoc key )
114     {
115         Parameter param = getParameterFromObject( key );
116         Object JavaDoc value = super.get( param.getKey() );
117         if( null != value )
118         {
119             return value;
120         }
121         else
122         {
123             return param.getDefault();
124         }
125     }
126
127     //--------------------------------------------------------------
128
// private and protected
129
//--------------------------------------------------------------
130

131    /**
132     * Return the currently assigned value for a key.
133     * @return the assigned value
134     */

135     protected Object JavaDoc getValue( final Parameter param )
136     {
137         return get( param.getKey() );
138     }
139
140    /**
141     * Return the parameter keys associated with the criteria.
142     * @return the keys
143     */

144     protected String JavaDoc[] getKeys()
145     {
146         return Parameter.getKeys( getParameters() );
147     }
148
149    /**
150     * Return the parameter associated to the criteria.
151     * @return the parameters
152     */

153     protected Parameter[] getParameters()
154     {
155         return m_params;
156     }
157
158     protected Parameter getParameter( final String JavaDoc key )
159     {
160         Parameter[] params = getParameters();
161         for( int i=0; i<params.length; i++ )
162         {
163             Parameter parameter = params[i];
164             if( parameter.getKey().equals( key ) ) return parameter;
165         }
166
167         final String JavaDoc error =
168           "Unknown key: [" + key + "].";
169         throw new IllegalArgumentException JavaDoc( error );
170     }
171
172     private Parameter getParameterFromObject( Object JavaDoc key )
173     {
174         if( key instanceof Parameter )
175         {
176             return (Parameter) key;
177         }
178         else
179         {
180             return getParameter( key.toString() );
181         }
182     }
183
184
185 }
186
Popular Tags