KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > selector > TypeCaster


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2000-2001,2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42  */

43
44 package org.exolab.jms.selector;
45
46
47 /**
48  * Utility class for casting from an SObject to a derived class,
49  * raising a TypeMismatchException if the operation is invalid.
50  *
51  * @version $Revision: 1.1 $ $Date: 2004/11/26 01:50:45 $
52  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
53  * @see Expression
54  * @see SBool
55  * @see SNumber
56  * @see SString
57  * @see TypeMismatchException
58  */

59 final class TypeCaster {
60
61     /**
62      * Private constructor
63      */

64     private TypeCaster() {
65     }
66
67     /**
68      * Cast an object to boolean
69      *
70      * @param obj the object to cast
71      * @param context the context where the object was created
72      * @return the cast object
73      * @throws TypeMismatchException if <code>obj</code> can't be cast
74      */

75     public static SBool castToBool(final SObject obj, final String JavaDoc context)
76         throws TypeMismatchException {
77
78         SBool result = null;
79         if (obj instanceof SBool) {
80             result = (SBool) obj;
81         } else if (obj != null) {
82             typeMismatch(Type.BOOLEAN, obj, context);
83         }
84         return result;
85     }
86
87     /**
88      * Cast an object to numeric
89      *
90      * @param obj the object to cast
91      * @param context the context where the object was created
92      * @return the cast object
93      * @throws TypeMismatchException if <code>obj</code> can't be cast
94      */

95     public static SNumber castToNumber(final SObject obj, final String JavaDoc context)
96         throws TypeMismatchException {
97
98         SNumber result = null;
99         if (obj instanceof SNumber) {
100             result = (SNumber) obj;
101         } else if (obj != null) {
102             typeMismatch(Type.NUMERIC, obj, context);
103         }
104         return result;
105     }
106
107     /**
108      * Cast an object to string
109      *
110      * @param obj the object to cast
111      * @param context the context where the object was created
112      * @return the cast object
113      * @throws TypeMismatchException if <code>obj</code> can't be cast
114      */

115     public static SString castToString(final SObject obj, final String JavaDoc context)
116         throws TypeMismatchException {
117
118         SString result = null;
119         if (obj instanceof SString) {
120             result = (SString) obj;
121         } else if (obj != null) {
122             typeMismatch(Type.STRING, obj, context);
123         }
124         return result;
125     }
126
127     /**
128      * Raises a type mismatch exception
129      *
130      * @param expected the expected type
131      * @param value the actual value
132      * @param context the context where <code>value</code> was created
133      * @throws TypeMismatchException corresponding to the expected and actual
134      * values
135      */

136     private static void typeMismatch(final Type expected, final SObject value,
137                                      final String JavaDoc context)
138         throws TypeMismatchException {
139
140         StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
141         msg.append("expecting a ");
142         msg.append(expected);
143         msg.append(" expression");
144         if (context != null) {
145             msg.append(" for ");
146             msg.append(context);
147         }
148         msg.append(", found a ");
149         msg.append(value.type());
150         throw new TypeMismatchException(msg.toString());
151     }
152
153 } //-- TypeCaster
154
Popular Tags