KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > runtime > internal > Conversions


1 /* *******************************************************************
2  * Copyright (c) 1999-2001 Xerox Corporation,
3  * 2002 Palo Alto Research Center, Incorporated (PARC).
4  * All rights reserved.
5  * This program and the accompanying materials are made available
6  * under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * Xerox/PARC initial implementation
12  * ******************************************************************/

13
14
15 package org.aspectj.runtime.internal;
16
17 public final class Conversions {
18     // Can't make instances of me
19
private Conversions() {}
20
21     // we might want to keep a cache of small integers around
22
public static Object JavaDoc intObject(int i) {
23         return new Integer JavaDoc(i);
24     }
25     public static Object JavaDoc shortObject(short i) {
26         return new Short JavaDoc(i);
27     }
28     public static Object JavaDoc byteObject(byte i) {
29         return new Byte JavaDoc(i);
30     }
31     public static Object JavaDoc charObject(char i) {
32         return new Character JavaDoc(i);
33     }
34     public static Object JavaDoc longObject(long i) {
35         return new Long JavaDoc(i);
36     }
37     public static Object JavaDoc floatObject(float i) {
38         return new Float JavaDoc(i);
39     }
40     public static Object JavaDoc doubleObject(double i) {
41         return new Double JavaDoc(i);
42     }
43     public static Object JavaDoc booleanObject(boolean i) {
44         return new Boolean JavaDoc(i);
45     }
46     public static Object JavaDoc voidObject() {
47         return null;
48     }
49
50
51     public static int intValue(Object JavaDoc o) {
52         if (o == null) {
53             return 0;
54         } else if (o instanceof Number JavaDoc) {
55             return ((Number JavaDoc)o).intValue();
56         } else {
57             throw new ClassCastException JavaDoc(o.getClass().getName() +
58                                          " can not be converted to int");
59         }
60     }
61     public static long longValue(Object JavaDoc o) {
62         if (o == null) {
63             return 0;
64         } else if (o instanceof Number JavaDoc) {
65             return ((Number JavaDoc)o).longValue();
66         } else {
67             throw new ClassCastException JavaDoc(o.getClass().getName() +
68                                          " can not be converted to long");
69         }
70     }
71     public static float floatValue(Object JavaDoc o) {
72         if (o == null) {
73             return 0;
74         } else if (o instanceof Number JavaDoc) {
75             return ((Number JavaDoc)o).floatValue();
76         } else {
77             throw new ClassCastException JavaDoc(o.getClass().getName() +
78                                          " can not be converted to float");
79         }
80     }
81     public static double doubleValue(Object JavaDoc o) {
82         if (o == null) {
83             return 0;
84         } else if (o instanceof Number JavaDoc) {
85             return ((Number JavaDoc)o).doubleValue();
86         } else {
87             throw new ClassCastException JavaDoc(o.getClass().getName() +
88                                          " can not be converted to double");
89         }
90     }
91     public static byte byteValue(Object JavaDoc o) {
92         if (o == null) {
93             return 0;
94         } else if (o instanceof Number JavaDoc) {
95             return ((Number JavaDoc)o).byteValue();
96         } else {
97             throw new ClassCastException JavaDoc(o.getClass().getName() +
98                                          " can not be converted to byte");
99         }
100     }
101     public static short shortValue(Object JavaDoc o) {
102         if (o == null) {
103             return 0;
104         } else if (o instanceof Number JavaDoc) {
105             return ((Number JavaDoc)o).shortValue();
106         } else {
107             throw new ClassCastException JavaDoc(o.getClass().getName() +
108                                          " can not be converted to short");
109         }
110     }
111     public static char charValue(Object JavaDoc o) {
112         if (o == null) {
113             return 0;
114         } else if (o instanceof Character JavaDoc) {
115             return ((Character JavaDoc)o).charValue();
116         } else {
117             throw new ClassCastException JavaDoc(o.getClass().getName() +
118                                          " can not be converted to char");
119         }
120     }
121     public static boolean booleanValue(Object JavaDoc o) {
122         if (o == null) {
123             return false;
124         } else if (o instanceof Boolean JavaDoc) {
125             return ((Boolean JavaDoc)o).booleanValue();
126         } else {
127             throw new ClassCastException JavaDoc(o.getClass().getName() +
128                                          " can not be converted to boolean");
129         }
130     }
131     
132     /**
133      * identity function for now. This is not typed to "void" because we happen
134      * to know that in Java, any void context (i.e., {@link ExprStmt})
135      * can also handle a return value.
136      */

137     public static Object JavaDoc voidValue(Object JavaDoc o) {
138         if (o == null) {
139             return o;
140         } else {
141             // !!! this may be an error in the future
142
return o;
143         }
144     }
145 }
146
Popular Tags