KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > codegen > ecore > genmodel > impl > Literals


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available 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  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: Literals.java,v 1.4 2005/06/08 06:18:44 nickb Exp $
16  */

17 package org.eclipse.emf.codegen.ecore.genmodel.impl;
18
19
20 import java.math.BigDecimal JavaDoc;
21 import java.math.BigInteger JavaDoc;
22 import java.util.Date JavaDoc;
23
24 import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
25
26
27 /**
28  * Utility class for converting primitive values, strings, and classes to
29  * literals that could appear in code.
30  */

31 public class Literals
32 {
33   // Supress default constructor for non-instantiability.
34
private Literals() {}
35
36   /**
37    * Convenience dispatch method. If the argument is an instance of
38    * <code>Boolean</code>, <code>Byte</code>, <code>Short</code>,
39    * <code>Integer</code>, <code>Long</code>, <code>Float</code>,
40    * <code>Double</code>, <code>Character</code>, <code>String</code>,
41    * <code>BigDecimal</code>, <code>BigInteger</code>, <code>Date</code>,
42    * or <code>Class</code>, the appropriate conversion method is called,
43    * with the unwrapped primitive, or the typed object as an argument.
44    * Class names are never imported; the qualified name is used.
45    */

46   public static String JavaDoc toLiteral(Object JavaDoc o)
47   {
48     return toLiteral(o, null);
49   }
50
51   /**
52    * Convenience dispatch method. If the argument is an instance of
53    * <code>Boolean</code>, <code>Byte</code>, <code>Short</code>,
54    * <code>Integer</code>, <code>Long</code>, <code>Float</code>,
55    * <code>Double</code>, <code>Character</code>, <code>String</code>,
56    * <code>BigDecimal</code>, <code>BigInteger</code>, <code>Date</code>,
57    * or <code>Class</code>, the appropriate conversion method is called,
58    * with the unwrapped primitive, or the typed object as an argument.
59    * The specified {@link org.eclipse.emf.codegen.ecore.genmodel.GenModel},
60    * if non-null, is used when necessary to import class names.
61    */

62   public static String JavaDoc toLiteral(Object JavaDoc o, GenModel genModel)
63   {
64     if (o instanceof Boolean JavaDoc)
65     {
66       return toBooleanLiteral(((Boolean JavaDoc)o).booleanValue(), genModel);
67     }
68     if (o instanceof Byte JavaDoc)
69     {
70       return toByteLiteral(((Byte JavaDoc)o).byteValue(), genModel);
71     }
72     if (o instanceof Short JavaDoc)
73     {
74       return toShortLiteral(((Short JavaDoc)o).shortValue(), genModel);
75     }
76     if (o instanceof Integer JavaDoc)
77     {
78       return toIntLiteral(((Integer JavaDoc)o).intValue(), genModel);
79     }
80     if (o instanceof Long JavaDoc)
81     {
82       return toLongLiteral(((Long JavaDoc)o).longValue(), genModel);
83     }
84     if (o instanceof Float JavaDoc)
85     {
86       return toFloatLiteral(((Float JavaDoc)o).floatValue(), genModel);
87     }
88     if (o instanceof Double JavaDoc)
89     {
90       return toDoubleLiteral(((Double JavaDoc)o).doubleValue(), genModel);
91     }
92     if (o instanceof Character JavaDoc)
93     {
94       return toCharLiteral(((Character JavaDoc)o).charValue(), genModel);
95     }
96     if (o instanceof String JavaDoc)
97     {
98       return toStringLiteral((String JavaDoc)o, genModel);
99     }
100     if (o instanceof BigDecimal JavaDoc)
101     {
102       return toBigDecimalLiteral((BigDecimal JavaDoc)o, genModel);
103     }
104     if (o instanceof BigInteger JavaDoc)
105     {
106       return toBigIntegerLiteral((BigInteger JavaDoc)o, genModel);
107     }
108     if (o instanceof Date JavaDoc)
109     {
110       return toDateLiteral((Date JavaDoc)o, genModel);
111     }
112     if (o instanceof Class JavaDoc)
113     {
114       return toClassLiteral((Class JavaDoc)o, genModel);
115     }
116     return null;
117   }
118
119   /**
120    * Returns the literal expression for the given <code>boolean</code> value.
121    */

122   public static String JavaDoc toBooleanLiteral(boolean b, GenModel genModel)
123   {
124     return b ? "true" : "false";
125   }
126
127   /**
128    * Returns the decimal literal expression for the given <code>byte</code>
129    * value.
130    */

131   public static String JavaDoc toByteLiteral(byte b, GenModel genModel)
132   {
133     return Byte.toString(b);
134   }
135
136   /**
137    * Returns the decimal literal expression for the given <code>short</code>
138    * value.
139    */

140   public static String JavaDoc toShortLiteral(short s, GenModel genModel)
141   {
142     return Short.toString(s);
143   }
144
145   /**
146    * Returns the decimal literal expression for the given <code>int</code>
147    * value.
148    */

149   public static String JavaDoc toIntLiteral(int i, GenModel genModel)
150   {
151     return Integer.toString(i);
152   }
153
154   /**
155    * Returns the decimal literal expression for the given <code>long</code>
156    * value.
157    */

158   public static String JavaDoc toLongLiteral(long l, GenModel genModel)
159   {
160     return Long.toString(l) + "L";
161   }
162
163   /**
164    * Returns a literal expression for the given <code>float</code> value.
165    * This literal may be in simple form or exponential notation, or it may
166    * be one of the special values <code>java.lang.Float.NaN</code>,
167    * <code>java.lang.Float.POSITIVE_INFINITY</code>, or
168    * <code>java.lang.Float.NEGATIVE_INFINITY</code>.
169    */

170   public static String JavaDoc toFloatLiteral(float f, GenModel genModel)
171   {
172     if (Float.isNaN(f)) return importName("java.lang.Float", genModel) + ".NaN";
173     if (Float.isInfinite(f)) return f > 0 ?
174       importName("java.lang.Float", genModel) + ".POSITIVE_INFINITY" :
175       importName("java.lang.Float", genModel) + ".NEGATIVE_INFINITY";
176     return Float.toString(f) + "F";
177   }
178
179   /**
180    * Returns a literal expression for the given <code>double</code> value.
181    * This literal may be in simple form or exponential notation, or it may
182    * be one of the special values <code>java.lang.Double.NaN</code>,
183    * <code>java.lang.Double.POSITIVE_INFINITY</code>, or
184    * <code>java.lang.Double.NEGATIVE_INFINITY</code>.
185    */

186   public static String JavaDoc toDoubleLiteral(double d, GenModel genModel)
187   {
188     if (Double.isNaN(d)) return importName("java.lang.Double", genModel) + ".NaN";
189     if (Double.isInfinite(d)) return d > 0 ?
190       importName("java.lang.Double", genModel) + ".POSITIVE_INFINITY" :
191       importName("java.lang.Double", genModel) + ".NEGATIVE_INFINITY";
192     return Double.toString(d);
193   }
194
195   private static String JavaDoc importName(String JavaDoc name, GenModel genModel)
196   {
197     return genModel != null ? genModel.getImportedName(name) : name;
198   }
199
200   /**
201    * Returns a literal expression for the given <code>char</code> value.
202    * This literal will be in its escaped form if it is backspace,
203    * horizontal tab, newline, form feed, carriage return, double quote,
204    * single quote, or backslash. If it is within the common printable
205    * range of space (32) to <code>~</code> (126), it will simply be the
206    * character literal. Otherwise, it will be in the escaped Unicode
207    * encoding form.
208    */

209   public static String JavaDoc toCharLiteral(char c, GenModel genModel)
210   {
211     StringBuffer JavaDoc result = new StringBuffer JavaDoc(8);
212     result.append('\'');
213     result.append(escapeChar(c));
214     result.append('\'');
215     return result.toString();
216   }
217
218   /**
219    * Returns a literal expression for the given <code>String</code>. Each
220    * of its characters will appear in the same form as if it was the
221    * argument to {@link #toCharLiteral}.
222    */

223   public static String JavaDoc toStringLiteral(String JavaDoc s, GenModel genModel)
224   {
225     if (s == null) return "null";
226     int len = s.length();
227     StringBuffer JavaDoc result = new StringBuffer JavaDoc(len + 16);
228     result.append('\"');
229     for (int i = 0; i < len; i++)
230     {
231       result.append(escapeChar(s.charAt(i)));
232     }
233     result.append('\"');
234     return result.toString();
235   }
236
237   private static String JavaDoc escapeChar(char c)
238   {
239     if (c == '\b') return "\\b";
240     if (c == '\t') return "\\t";
241     if (c == '\n') return "\\n";
242     if (c == '\f') return "\\f";
243     if (c == '\r') return "\\r";
244     if (c == '\"') return "\\\"";
245     if (c == '\'') return "\\\'";
246     if (c == '\\') return "\\\\";
247     if (c >= 32 && c < 127) return String.valueOf(c);
248
249     // escaped unicode form
250
String JavaDoc num = Integer.toHexString(c);
251     switch(num.length()) {
252       case 1: return "\\u000" + num;
253       case 2: return "\\u00" + num;
254       case 3: return "\\u0" + num;
255     }
256     return "\\u" + num;
257   }
258
259   /**
260    * Returns a literal expression for the given <code>BigDecimal</code> value.
261    */

262   public static String JavaDoc toBigDecimalLiteral(BigDecimal JavaDoc bigDecimal, GenModel genModel)
263   {
264     if (bigDecimal == null) return "null";
265     return "new " + importName("java.math.BigDecimal", genModel) + "(\"" + bigDecimal.toString() + "\")";
266   }
267
268   /**
269    * Returns a literal expression for the given <code>BigInteger</code> value.
270    */

271   public static String JavaDoc toBigIntegerLiteral(BigInteger JavaDoc bigInteger, GenModel genModel)
272   {
273     if (bigInteger == null) return "null";
274     return "new " + importName("java.math.BigInteger", genModel) + "(\"" + bigInteger.toString() + "\")";
275   }
276
277   /**
278    * Returns a literal expression for the given <code>Date</code> value.
279    */

280   public static String JavaDoc toDateLiteral(Date JavaDoc date, GenModel genModel)
281   {
282     String JavaDoc timeLiteral = toLongLiteral(date.getTime(), genModel);
283     return "new " + importName("java.util.Date", genModel) + "(" + timeLiteral + ")";
284   }
285
286   /**
287    * Returns a literal expression for the given <code>Class</code> value.
288    */

289   public static String JavaDoc toClassLiteral(Class JavaDoc c, GenModel genModel)
290   {
291     if (c == null) return "null";
292     String JavaDoc name = c.getName();
293
294     // See java.lang.Class.getName() javadoc for explanation of array encoding.
295
int arrayDepth = 0;
296     for (; name.charAt(arrayDepth) == '['; arrayDepth++);
297
298     if (arrayDepth > 0)
299     {
300       if (name.charAt(arrayDepth) == 'B') name = "byte";
301       else if (name.charAt(arrayDepth) == 'C') name = "char";
302       else if (name.charAt(arrayDepth) == 'D') name = "double";
303       else if (name.charAt(arrayDepth) == 'F') name = "float";
304       else if (name.charAt(arrayDepth) == 'I') name = "int";
305       else if (name.charAt(arrayDepth) == 'J') name = "long";
306       else if (name.charAt(arrayDepth) == 'S') name = "short";
307       else if (name.charAt(arrayDepth) == 'Z') name = "boolean";
308       else if (name.charAt(arrayDepth) == 'L') name = importName(name.substring(arrayDepth + 1, name.length() - 1), genModel);
309       else throw new IllegalArgumentException JavaDoc("Invalid class name: " + name);
310     }
311     else if (!c.isPrimitive())
312     {
313       name = importName(name, genModel);
314     }
315     
316     StringBuffer JavaDoc result = new StringBuffer JavaDoc(name.length() + 2 * arrayDepth + 8);
317     result.append(name);
318     for (int i = 0; i < arrayDepth; i++)
319     {
320       result.append('[');
321       result.append(']');
322     }
323     result.append(".class");
324     return result.toString();
325   }
326 }
327
Popular Tags