View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.slc.core.execution;
17  
18  /** Converts to and from primitive types. */
19  public class PrimitiveUtils {
20  	/**
21  	 * @deprecated Use {@link PrimitiveAccessor#TYPE_STRING} instead
22  	 */
23  	public final static String TYPE_STRING = PrimitiveAccessor.TYPE_STRING;
24  	/**
25  	 * @deprecated Use {@link PrimitiveAccessor#TYPE_INTEGER} instead
26  	 */
27  	public final static String TYPE_INTEGER = PrimitiveAccessor.TYPE_INTEGER;
28  	/**
29  	 * @deprecated Use {@link PrimitiveAccessor#TYPE_LONG} instead
30  	 */
31  	public final static String TYPE_LONG = PrimitiveAccessor.TYPE_LONG;
32  	/**
33  	 * @deprecated Use {@link PrimitiveAccessor#TYPE_FLOAT} instead
34  	 */
35  	public final static String TYPE_FLOAT = PrimitiveAccessor.TYPE_FLOAT;
36  	/**
37  	 * @deprecated Use {@link PrimitiveAccessor#TYPE_DOUBLE} instead
38  	 */
39  	public final static String TYPE_DOUBLE = PrimitiveAccessor.TYPE_DOUBLE;
40  	/**
41  	 * @deprecated Use {@link PrimitiveAccessor#TYPE_BOOLEAN} instead
42  	 */
43  	public final static String TYPE_BOOLEAN = PrimitiveAccessor.TYPE_BOOLEAN;
44  
45  	private PrimitiveUtils() {
46  
47  	}
48  
49  	/** @return the class or null if the provided type is not a primitive */
50  	public static Class<?> typeAsClass(String type) {
51  		if (PrimitiveAccessor.TYPE_STRING.equals(type))
52  			return String.class;
53  		else if (PrimitiveAccessor.TYPE_PASSWORD.equals(type))
54  			return char[].class;
55  		else if (PrimitiveAccessor.TYPE_INTEGER.equals(type))
56  			return Integer.class;
57  		else if (PrimitiveAccessor.TYPE_LONG.equals(type))
58  			return Long.class;
59  		else if (PrimitiveAccessor.TYPE_FLOAT.equals(type))
60  			return Float.class;
61  		else if (PrimitiveAccessor.TYPE_DOUBLE.equals(type))
62  			return Double.class;
63  		else if (PrimitiveAccessor.TYPE_BOOLEAN.equals(type))
64  			return Boolean.class;
65  		else
66  			return null;
67  	}
68  
69  	/** @return the type or null if the provided class is not a primitive */
70  	public static String classAsType(Class<?> clss) {
71  		if (String.class.isAssignableFrom(clss))
72  			return PrimitiveAccessor.TYPE_STRING;
73  		else if (char[].class.isAssignableFrom(clss))
74  			return PrimitiveAccessor.TYPE_PASSWORD;
75  		else if (Integer.class.isAssignableFrom(clss))
76  			return PrimitiveAccessor.TYPE_INTEGER;
77  		else if (Long.class.isAssignableFrom(clss))
78  			return PrimitiveAccessor.TYPE_LONG;
79  		else if (Float.class.isAssignableFrom(clss))
80  			return PrimitiveAccessor.TYPE_FLOAT;
81  		else if (Double.class.isAssignableFrom(clss))
82  			return PrimitiveAccessor.TYPE_DOUBLE;
83  		else if (Boolean.class.isAssignableFrom(clss))
84  			return PrimitiveAccessor.TYPE_BOOLEAN;
85  		else
86  			return null;
87  	}
88  
89  	/** Parse string as an object. Passwords are returned as String.*/
90  	public static Object convert(String type, String str) {
91  		if (PrimitiveAccessor.TYPE_STRING.equals(type)) {
92  			return str;
93  		} else if (PrimitiveAccessor.TYPE_PASSWORD.equals(type)) {
94  			return str;
95  		} else if (PrimitiveAccessor.TYPE_INTEGER.equals(type)) {
96  			return (Integer.parseInt(str));
97  		} else if (PrimitiveAccessor.TYPE_LONG.equals(type)) {
98  			return (Long.parseLong(str));
99  		} else if (PrimitiveAccessor.TYPE_FLOAT.equals(type)) {
100 			return (Float.parseFloat(str));
101 		} else if (PrimitiveAccessor.TYPE_DOUBLE.equals(type)) {
102 			return (Double.parseDouble(str));
103 		} else if (PrimitiveAccessor.TYPE_BOOLEAN.equals(type)) {
104 			return (Boolean.parseBoolean(str));
105 		} else {
106 			return str;
107 		}
108 	}
109 
110 }