Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.aries.jmx.util;
 
 import static org.osgi.jmx.JmxConstants.BIGDECIMAL;
 import static org.osgi.jmx.JmxConstants.BIGINTEGER;
 import static org.osgi.jmx.JmxConstants.BOOLEAN;
 import static org.osgi.jmx.JmxConstants.BYTE;
 import static org.osgi.jmx.JmxConstants.CHARACTER;
 import static org.osgi.jmx.JmxConstants.DOUBLE;
 import static org.osgi.jmx.JmxConstants.FLOAT;
 import static org.osgi.jmx.JmxConstants.INTEGER;
 import static org.osgi.jmx.JmxConstants.LONG;
 import static org.osgi.jmx.JmxConstants.P_BOOLEAN;
 import static org.osgi.jmx.JmxConstants.P_BYTE;
 import static org.osgi.jmx.JmxConstants.P_CHAR;
 import static org.osgi.jmx.JmxConstants.P_DOUBLE;
 import static org.osgi.jmx.JmxConstants.P_FLOAT;
 import static org.osgi.jmx.JmxConstants.P_INT;
 import static org.osgi.jmx.JmxConstants.P_LONG;
 import static org.osgi.jmx.JmxConstants.P_SHORT;
 import static org.osgi.jmx.JmxConstants.SHORT;
 import static org.osgi.jmx.JmxConstants.STRING;
 import static org.osgi.jmx.JmxConstants.VERSION;
 
 import java.util.Map;
 
This class provides common utilities related to type conversions for the MBean implementations

Version:
$Rev: 1195503 $ $Date: 2011-10-31 11:02:18 -0400 (Mon, 31 Oct 2011) $
 
 public class TypeUtils {
 
     private TypeUtils() {
         super();
     }
 
     public static Map<StringClass<? extends Object>> primitiveTypes = new HashMap<StringClass<? extends Object>>();
     public static Map<StringClass<? extends Object>> wrapperTypes = new HashMap<StringClass<? extends Object>>();
     public static Map<StringClass<? extends Object>> mathTypes = new HashMap<StringClass<? extends Object>>();
     public static Map<Class<? extends Object>, Class<? extends Object>> primitiveToWrapper = new HashMap<Class<? extends Object>, Class<? extends Object>>();
     public static Map<StringClass<? extends Object>> types = new HashMap<StringClass<? extends Object>>();
 
     static {
         .put(.);
         .put(.);
         .put(.);
         .put(.);
         .put(.);
         .put(.);
         .put(.);
         .put(.);
         .put(.Float.class);
         .put(.Integer.class);
         .put(.Long.class);
         .put(.Double.class);
         .put(.Byte.class);
         .put(.Short.class);
         .put(.Boolean.class);
         .put(Integer.class);
         .put(Float.class);
         .put(Long.class);
         .put(Double.class);
         .put(Byte.class);
         .put(Short.class);
         .put(Boolean.class);
         .put(Character.class);
         .put(Version.class);
         .put(BigDecimal.class);
         .put(BigInteger.class);
         .put(String.class);
         .putAll();
         .putAll();
         .putAll();
    }

    
Converts a Dictionary object to a Map

Parameters:
dictionary
Returns:
    public static Map<StringStringfromDictionary(Dictionary<StringStringdictionary) {
        Map<StringStringresult = new HashMap<StringString>();
        Enumeration<Stringkeys = dictionary.keys();
        while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            result.put(keydictionary.get(key));
        }
        return result;
    }

    
Converts primitive long[] array to Long[]

Parameters:
array
Returns:
    public static Long[] toLong(long[] array) {
        Long[] toArray = (array == null) ? new Long[0] : new Long[array.length];
        for (int i = 0; i < toArray.lengthi++) {
            toArray[i] = array[i];
        }
        return toArray;
    }

    
Converts Long[] array to primitive

Parameters:
array
Returns:
    public static long[] toPrimitive(Long[] array) {
        long[] toArray = (array == null) ? new long[0] : new long[array.length];
        for (int i = 0; i < toArray.lengthi++) {
            toArray[i] = array[i];
        }
        return toArray;
    }

    
Converts a String value to an Object of the specified type

Parameters:
type one of types listed in types
value
Returns:
instance of class type
Throws:
java.lang.IllegalArgumentException if type or value are null or if the Class type does not support a valueOf() or cannot be converted to a wrapper type
    @SuppressWarnings("unchecked")
    public static <T> T fromString(Class<T> typeString value) {
        if (type == null || !.containsValue(type)) {
            throw new IllegalArgumentException("Cannot convert to type argument : " + type);
        }
        if (value == null || value.length() < 1) {
            throw new IllegalArgumentException("Argument value cannot be null or empty");
        }
        T result = null;
        try {
            if (type.equals(String.class)) {
                result = (T) value;
            } else if (type.equals(Character.class) || type.equals(.)) {
                result = (T) Character.valueOf(value.charAt(0));
            } else if (.containsValue(type) || .containsValue(type)) {
                Constructor<? extends Objectconstructor = type.getConstructor(String.class);
                result = (T) constructor.newInstance(value);
            } else if (.containsKey(type)) { // attempt to promote to wrapper and resolve to the base
                                                               // type
                Class<? extends ObjectpromotedType = .get(type);
                char[] simpleTypeName = type.getName().toCharArray();
                simpleTypeName[0] = Character.toUpperCase(simpleTypeName[0]);
                String parseMethodName = "parse" + new String(simpleTypeName);
                Method parseMethod = promotedType.getDeclaredMethod(parseMethodNameString.class);
                result = (T) parseMethod.invoke(nullvalue);
            }
        } catch (SecurityException e) {
            throw new IllegalArgumentException("Cannot convert value [" + value + "] to type [" + type + "]"e);
        } catch (NoSuchMethodException e) {
            throw new IllegalArgumentException("Cannot convert value [" + value + "] to type [" + type + "]"e);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Cannot convert value [" + value + "] to type [" + type + "]"e);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException("Cannot convert value [" + value + "] to type [" + type + "]"e);
        } catch (InvocationTargetException e) {
            throw new IllegalArgumentException("Cannot convert value [" + value + "] to type [" + type + "]"e);
        } catch (InstantiationException e) {
            throw new IllegalArgumentException("Cannot convert value [" + value + "] to type [" + type + "]"e);
        }
        return result;
    }
New to GrepCode? Check out our FAQ X