KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > util > ConversionUtil


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

19
20
21 package org.apache.cayenne.util;
22
23 import java.math.BigDecimal JavaDoc;
24 import java.math.BigInteger JavaDoc;
25
26 import org.apache.cayenne.exp.ExpressionException;
27
28 /**
29  * A collection of static conversion utility methods.
30  *
31  * @since 1.1
32  * @author Andrus Adamchik
33  */

34 public final class ConversionUtil {
35
36     public static int toInt(Object JavaDoc object, int defaultValue) {
37         if (object == null) {
38             return defaultValue;
39         }
40         else if (object instanceof Number JavaDoc) {
41             return ((Number JavaDoc) object).intValue();
42         }
43         else if (object instanceof String JavaDoc) {
44             try {
45                 return Integer.parseInt((String JavaDoc) object);
46             }
47             catch (NumberFormatException JavaDoc ex) {
48                 return defaultValue;
49             }
50         }
51
52         return defaultValue;
53     }
54
55     public static boolean toBoolean(Object JavaDoc object) {
56         if (object instanceof Boolean JavaDoc) {
57             return ((Boolean JavaDoc) object).booleanValue();
58         }
59
60         if (object instanceof Number JavaDoc) {
61             return ((Number JavaDoc) object).intValue() != 0;
62         }
63
64         return object != null;
65     }
66
67     public static BigDecimal JavaDoc toBigDecimal(Object JavaDoc object) {
68
69         if (object == null) {
70             return null;
71         }
72         else if (object instanceof BigDecimal JavaDoc) {
73             return (BigDecimal JavaDoc) object;
74         }
75         else if (object instanceof BigInteger JavaDoc) {
76             return new BigDecimal JavaDoc((BigInteger JavaDoc) object);
77         }
78         else if (object instanceof Number JavaDoc) {
79             return new BigDecimal JavaDoc(((Number JavaDoc) object).doubleValue());
80         }
81
82         throw new ExpressionException("Can't convert to BigDecimal: " + object);
83     }
84
85     /**
86      * Attempts to convert an object to Comparable instance.
87      */

88     public static Comparable JavaDoc toComparable(Object JavaDoc object) {
89         if (object == null) {
90             return null;
91         }
92         else if (object instanceof Comparable JavaDoc) {
93             return (Comparable JavaDoc) object;
94         }
95         else if (object instanceof StringBuffer JavaDoc) {
96             return object.toString();
97         }
98         else if (object instanceof char[]) {
99             return new String JavaDoc((char[]) object);
100         }
101         else {
102             throw new ClassCastException JavaDoc(
103                 "Invalid Comparable class:" + object.getClass().getName());
104         }
105     }
106
107     /**
108      * Attempts to convert an object to Comparable instance.
109      */

110     public static String JavaDoc toString(Object JavaDoc object) {
111         if (object == null) {
112             return null;
113         }
114         else if (object instanceof String JavaDoc) {
115             return (String JavaDoc) object;
116         }
117         else if (object instanceof StringBuffer JavaDoc) {
118             return object.toString();
119         }
120         else if (object instanceof char[]) {
121             return new String JavaDoc((char[]) object);
122         }
123         else {
124             throw new ClassCastException JavaDoc(
125                 "Invalid class for String conversion:" + object.getClass().getName());
126         }
127     }
128
129     /**
130      * Attempts to convert an object to an uppercase string.
131      */

132     public static Object JavaDoc toUpperCase(Object JavaDoc object) {
133         if ((object instanceof String JavaDoc) || (object instanceof StringBuffer JavaDoc)) {
134             return object.toString().toUpperCase();
135         }
136         else if (object instanceof char[]) {
137             return new String JavaDoc((char[]) object).toUpperCase();
138         }
139         else {
140             return object;
141         }
142     }
143
144     private ConversionUtil() {
145     }
146 }
147
Popular Tags