Example usage for java.lang Float TYPE

List of usage examples for java.lang Float TYPE

Introduction

In this page you can find the example usage for java.lang Float TYPE.

Prototype

Class TYPE

To view the source code for java.lang Float TYPE.

Click Source Link

Document

The Class instance representing the primitive type float .

Usage

From source file:com.sun.socialsite.util.DebugBeanJsonConverter.java

@SuppressWarnings("boxing")
private <T> void callSetterWithValue(T pojo, Method method, JSONObject jsonObject, String fieldName)
        throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, JSONException {

    log.debug("Calling setter [" + method.getName() + "]");

    Class<?> expectedType = method.getParameterTypes()[0];
    Object value = null;// w w  w  .  j a va  2 s.c  o m

    if (!jsonObject.has(fieldName)) {
        // Skip
    } else if (expectedType.equals(List.class)) {
        ParameterizedType genericListType = (ParameterizedType) method.getGenericParameterTypes()[0];
        Type type = genericListType.getActualTypeArguments()[0];
        Class<?> rawType;
        Class<?> listElementClass;
        if (type instanceof ParameterizedType) {
            listElementClass = (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0];
            rawType = (Class<?>) ((ParameterizedType) type).getRawType();
        } else {
            listElementClass = (Class<?>) type;
            rawType = listElementClass;
        }

        List<Object> list = Lists.newArrayList();
        JSONArray jsonArray = jsonObject.getJSONArray(fieldName);
        for (int i = 0; i < jsonArray.length(); i++) {
            if (org.apache.shindig.protocol.model.Enum.class.isAssignableFrom(rawType)) {
                list.add(convertEnum(listElementClass, jsonArray.getJSONObject(i)));
            } else {
                list.add(convertToObject(jsonArray.getString(i), listElementClass));
            }
        }

        value = list;

    } else if (expectedType.equals(Map.class)) {
        ParameterizedType genericListType = (ParameterizedType) method.getGenericParameterTypes()[0];
        Type[] types = genericListType.getActualTypeArguments();
        Class<?> valueClass = (Class<?>) types[1];

        // We only support keys being typed as Strings.
        // Nothing else really makes sense in json.
        Map<String, Object> map = Maps.newHashMap();
        JSONObject jsonMap = jsonObject.getJSONObject(fieldName);

        Iterator<?> keys = jsonMap.keys();
        while (keys.hasNext()) {
            String keyName = (String) keys.next();
            map.put(keyName, convertToObject(jsonMap.getString(keyName), valueClass));
        }

        value = map;

    } else if (org.apache.shindig.protocol.model.Enum.class.isAssignableFrom(expectedType)) {
        // TODO Need to stop using Enum as a class name :(
        value = convertEnum((Class<?>) ((ParameterizedType) method.getGenericParameterTypes()[0])
                .getActualTypeArguments()[0], jsonObject.getJSONObject(fieldName));
    } else if (expectedType.isEnum()) {
        if (jsonObject.has(fieldName)) {
            for (Object v : expectedType.getEnumConstants()) {
                if (v.toString().equals(jsonObject.getString(fieldName))) {
                    value = v;
                    break;
                }
            }
            if (value == null) {
                throw new IllegalArgumentException("No enum value  '" + jsonObject.getString(fieldName)
                        + "' in " + expectedType.getName());
            }
        }
    } else if (expectedType.equals(String.class)) {
        value = jsonObject.getString(fieldName);
    } else if (expectedType.equals(Date.class)) {
        // Use JODA ISO parsing for the conversion
        value = new DateTime(jsonObject.getString(fieldName)).toDate();
    } else if (expectedType.equals(Long.class) || expectedType.equals(Long.TYPE)) {
        value = jsonObject.getLong(fieldName);
    } else if (expectedType.equals(Integer.class) || expectedType.equals(Integer.TYPE)) {
        value = jsonObject.getInt(fieldName);
    } else if (expectedType.equals(Boolean.class) || expectedType.equals(Boolean.TYPE)) {
        value = jsonObject.getBoolean(fieldName);
    } else if (expectedType.equals(Float.class) || expectedType.equals(Float.TYPE)) {
        value = ((Double) jsonObject.getDouble(fieldName)).floatValue();
    } else if (expectedType.equals(Double.class) || expectedType.equals(Double.TYPE)) {
        value = jsonObject.getDouble(fieldName);
    } else {
        // Assume its an injected type
        value = convertToObject(jsonObject.getJSONObject(fieldName).toString(), expectedType);
    }

    if (value != null) {
        method.invoke(pojo, value);
    }
}

From source file:org.jcommon.com.util.config.ConfigLoader.java

@SuppressWarnings("unchecked")
private static StringBuilder updateString(InputStream is, BaseConfigMBean config) throws FileNotFoundException {
    StringBuilder sb = new StringBuilder();
    if (is == null)
        throw new FileNotFoundException();
    try {/* w w w  .ja  v a2s .c om*/
        boolean empty = false;
        Document doc = null;
        try {
            doc = new SAXReader().read(is);
        } catch (Exception e) {
            empty = true;
            doc = DocumentHelper.createDocument();
        }

        Element root = doc.getRootElement();

        String element = null;
        Class<?> type = null;
        java.lang.reflect.Field[] fs = config.getClass().getDeclaredFields();
        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n").append("<config>\n");
        for (java.lang.reflect.Field f : fs) {
            element = f.getName();
            if (getTextFromElement(root, element) == null && !empty)
                continue;
            Object value = null;
            type = f.getType();
            java.lang.reflect.Method m = getMethod(config.getClass(), "get" + element);
            if (m == null)
                m = getMethod(config.getClass(), "is" + element);
            if (m != null) {
                try {
                    value = m.invoke(config);
                    logger.info(element + ":" + value);
                } catch (Exception e) {
                    logger.warn(e);
                    continue;
                }
                if (String.class == type) {
                    String data = String.valueOf(value);
                    if (data.startsWith("{")) {
                        org.json.JSONObject jsonO = JsonUtils.getJSONObject(data);
                        if (jsonO != null) {
                            Iterator<String> it = jsonO.keys();
                            sb.append("   <" + element + " ");
                            for (; it != null && it.hasNext();) {
                                String k = it.next();
                                String v = jsonO.getString(k);
                                sb.append(k).append("=").append("\"" + v + "\" ");
                            }
                            sb.append("/>\n");
                            continue;
                        }
                    }
                    sb.append("   <" + element + ">").append(data).append("</" + element + ">\n");
                } else if ((java.lang.Integer.class == type || Integer.TYPE == type)
                        || (java.lang.Boolean.class == type || Boolean.TYPE == type)
                        || (java.lang.Long.class == type || Long.TYPE == type)
                        || (java.lang.Float.class == type || Float.TYPE == type)) {
                    sb.append("   <" + element + ">").append(String.valueOf(value))
                            .append("</" + element + ">\n");
                } else if (java.util.Collection.class.isAssignableFrom(type)) {
                    Collection<String> list = (Collection<String>) value;
                    for (String s : list) {
                        sb.append("   <" + element + ">").append(s).append("</" + element + ">\n");
                    }
                } else if (java.util.Map.class.isAssignableFrom(type)) {
                    Map<String, String> map = (Map<String, String>) value;
                    for (String s : map.keySet()) {
                        String v = map.get(s);
                        sb.append("   <" + element).append(" key=\"" + s + "\"").append(" value=\"" + v + "\"")
                                .append(" />\n");
                    }
                } else {
                    logger.info("not map Class:" + type);
                    continue;
                }
            } else
                logger.warn("can't find element:" + element);
        }
        sb.append("</config>");
        logger.info(String.format("Read config file:  %s", is));

    } catch (Throwable t) {
        //t.printStackTrace();
        logger.error("read config file error:" + is + "\n" + t.getMessage());
    }
    return sb;
}

From source file:de.ma.it.common.sm.transition.MethodTransition.java

private boolean match(Class<?> paramType, Object arg, Class<?> argType) {
    if (paramType.isPrimitive()) {
        if (paramType.equals(Boolean.TYPE)) {
            return arg instanceof Boolean;
        }/*  w ww.j a va2 s  . c  o m*/
        if (paramType.equals(Integer.TYPE)) {
            return arg instanceof Integer;
        }
        if (paramType.equals(Long.TYPE)) {
            return arg instanceof Long;
        }
        if (paramType.equals(Short.TYPE)) {
            return arg instanceof Short;
        }
        if (paramType.equals(Byte.TYPE)) {
            return arg instanceof Byte;
        }
        if (paramType.equals(Double.TYPE)) {
            return arg instanceof Double;
        }
        if (paramType.equals(Float.TYPE)) {
            return arg instanceof Float;
        }
        if (paramType.equals(Character.TYPE)) {
            return arg instanceof Character;
        }
    }
    return argType.isAssignableFrom(paramType) && paramType.isAssignableFrom(arg.getClass());
}

From source file:com.xwtec.xwserver.util.json.util.JSONUtils.java

/**
 * Tests if Class represents a primitive number or wrapper.<br>
 *///from ww  w . j  a v  a 2 s.c o  m
public static boolean isNumber(Class clazz) {
    return clazz != null && (Byte.TYPE.isAssignableFrom(clazz) || Short.TYPE.isAssignableFrom(clazz)
            || Integer.TYPE.isAssignableFrom(clazz) || Long.TYPE.isAssignableFrom(clazz)
            || Float.TYPE.isAssignableFrom(clazz) || Double.TYPE.isAssignableFrom(clazz)
            || Number.class.isAssignableFrom(clazz));
}

From source file:com.mawujun.util.AnnotationUtils.java

/**
 * Helper method for comparing two objects of an array type.
 *
 * @param componentType the component type of the array
 * @param o1 the first object/*from ww  w  .j ava 2 s.co  m*/
 * @param o2 the second object
 * @return a flag whether these objects are equal
 */
private static boolean arrayMemberEquals(Class<?> componentType, Object o1, Object o2) {
    if (componentType.isAnnotation()) {
        return annotationArrayMemberEquals((Annotation[]) o1, (Annotation[]) o2);
    }
    if (componentType.equals(Byte.TYPE)) {
        return Arrays.equals((byte[]) o1, (byte[]) o2);
    }
    if (componentType.equals(Short.TYPE)) {
        return Arrays.equals((short[]) o1, (short[]) o2);
    }
    if (componentType.equals(Integer.TYPE)) {
        return Arrays.equals((int[]) o1, (int[]) o2);
    }
    if (componentType.equals(Character.TYPE)) {
        return Arrays.equals((char[]) o1, (char[]) o2);
    }
    if (componentType.equals(Long.TYPE)) {
        return Arrays.equals((long[]) o1, (long[]) o2);
    }
    if (componentType.equals(Float.TYPE)) {
        return Arrays.equals((float[]) o1, (float[]) o2);
    }
    if (componentType.equals(Double.TYPE)) {
        return Arrays.equals((double[]) o1, (double[]) o2);
    }
    if (componentType.equals(Boolean.TYPE)) {
        return Arrays.equals((boolean[]) o1, (boolean[]) o2);
    }
    return Arrays.equals((Object[]) o1, (Object[]) o2);
}

From source file:org.lunarray.model.descriptor.converter.def.AbstractDefaultConverterTool.java

/**
 * Populates the simple converters./*from w ww .j  a v a  2s .  c  o  m*/
 */
private void populateSimpleConverters() {
    // Primitives.
    this.converters.put(Integer.TYPE, new IntegerConverter());
    this.converters.put(Double.TYPE, new DoubleConverter());
    this.converters.put(Float.TYPE, new FloatConverter());
    this.converters.put(Long.TYPE, new LongConverter());
    this.converters.put(Byte.TYPE, new ByteConverter());
    this.converters.put(Short.TYPE, new ShortConverter());
    this.converters.put(Character.TYPE, new CharacterConverter());
    this.converters.put(Boolean.TYPE, new BooleanConverter());
    // Date types.
    this.addConverter(new CalendarConverter());
    this.addConverter(new DateConverter());
    // SQL Date types.
    this.addConverter(new SqlDateConverter());
    this.addConverter(new SqlTimeConverter());
    this.addConverter(new SqlTimestampConverter());
    // String.
    this.addConverter(new IdempotentConverter());
    // Numbers.
    this.addConverter(new IntegerConverter());
    this.addConverter(new DoubleConverter());
    this.addConverter(new FloatConverter());
    this.addConverter(new LongConverter());
    this.addConverter(new ByteConverter());
    this.addConverter(new ShortConverter());
    // Character.
    this.addConverter(new CharacterConverter());
    // Math types.
    this.addConverter(new BigDecimalConverter());
    this.addConverter(new BigIntegerConverter());
    // Boolean.
    this.addConverter(new BooleanConverter());
}

From source file:org.wisdom.template.thymeleaf.OgnlOpsByReflectionTest.java

@Test
public void testConvertValue() throws Exception {
    Class[] classes = new Class[] { Object.class, Class.class };
    // Null value
    assertThat(invoke("convertValue", classes, null, Long.TYPE)).isEqualTo(0l);
    assertThat(invoke("convertValue", classes, null, String.class)).isNull();

    // Primitive//from   w  w w .j ava2s .  c o m
    assertThat(invoke("convertValue", classes, "42", Integer.class)).isEqualTo(42);
    assertThat(invoke("convertValue", classes, "42", Integer.TYPE)).isEqualTo(42);
    assertThat(invoke("convertValue", classes, "42", Byte.class)).isEqualTo(Byte.valueOf("42"));
    assertThat(invoke("convertValue", classes, "42", Byte.TYPE)).isEqualTo(Byte.valueOf("42"));
    assertThat(invoke("convertValue", classes, "42", Short.class)).isEqualTo(Short.valueOf("42"));
    assertThat(invoke("convertValue", classes, "42", Short.TYPE)).isEqualTo(Short.valueOf("42"));
    assertThat(invoke("convertValue", classes, String.valueOf((int) 'c'), Character.class)).isEqualTo('c');
    assertThat(invoke("convertValue", classes, String.valueOf((int) 'c'), Character.TYPE)).isEqualTo('c');
    assertThat(invoke("convertValue", classes, "42", Long.class)).isEqualTo(42l);
    assertThat(invoke("convertValue", classes, "42", Long.TYPE)).isEqualTo(42l);
    assertThat(invoke("convertValue", classes, "true", Boolean.class)).isEqualTo(true);
    assertThat(invoke("convertValue", classes, "true", Boolean.TYPE)).isEqualTo(true);
    assertThat(invoke("convertValue", classes, "42", Double.class)).isEqualTo(42.0);
    assertThat(invoke("convertValue", classes, "42", Double.TYPE)).isEqualTo(42.0);
    assertThat(invoke("convertValue", classes, "42", Float.class)).isEqualTo(42.0f);
    assertThat(invoke("convertValue", classes, "42", Float.TYPE)).isEqualTo(42.0f);

    // BigInteger, BigDecimal and String
    assertThat(invoke("convertValue", classes, "42", BigDecimal.class)).isEqualTo(new BigDecimal("42"));
    assertThat(invoke("convertValue", classes, "42", BigInteger.class)).isEqualTo(new BigInteger("42"));
    assertThat(invoke("convertValue", classes, "42", String.class)).isEqualTo("42");

    //Array
    assertThat(invoke("convertValue", classes, new Object[] { 1, 2, 3 }, (new int[0]).getClass())).isNotNull();
}

From source file:org.fhcrc.cpl.toolbox.filehandler.TabLoader.java

/**
 * Look at first 5 lines of the file and infer col names, data types.
 * Most useful if maps are being returned, otherwise use inferColumnInfo(reader, clazz) to
 * use properties of a bean instead.//from   w w w .ja v  a 2 s  . c om
 *
 * @param reader
 * @throws IOException
 */
private void inferColumnInfo(BufferedReader reader) throws IOException {
    reader.mark(4096 * _scanAheadLineCount);

    String[] lines = new String[_scanAheadLineCount + Math.max(_skipLines, 0)];
    int i;
    for (i = 0; i < lines.length;) {
        String line = reader.readLine();
        if (null == line)
            break;
        if (line.length() == 0 || line.charAt(0) == '#')
            continue;
        lines[i++] = line;
    }
    int nLines = i;
    reader.reset();
    if (nLines == 0) {
        _columns = new ColumnDescriptor[0];
        return;
    }

    int nCols = 0;
    String[][] lineFields = new String[nLines][];
    for (i = 0; i < nLines; i++) {
        lineFields[i] = parseLine(lines[i]);
        nCols = Math.max(nCols, lineFields[i].length);
    }

    ColumnDescriptor[] colDescs = new ColumnDescriptor[nCols];
    for (i = 0; i < nCols; i++)
        colDescs[i] = new ColumnDescriptor();

    //Try to infer types
    int inferStartLine = _skipLines == -1 ? 1 : _skipLines;
    for (int f = 0; f < nCols; f++) {
        int classIndex = -1;
        for (int line = inferStartLine; line < nLines; line++) {
            if (f >= lineFields[line].length)
                continue;
            String field = lineFields[line][f];
            if ("".equals(field))
                continue;

            for (int c = Math.max(classIndex, 0); c < convertClasses.length; c++) {
                //noinspection EmptyCatchBlock
                try {
                    Object o = ConvertUtils.convert(field, convertClasses[c]);
                    //We found a type that works. If it is more general than
                    //what we had before, we must use it.
                    if (o != null && c > classIndex)
                        classIndex = c;
                    break;
                } catch (Exception x) {
                }
            }
        }
        colDescs[f].clazz = classIndex == -1 ? String.class : convertClasses[classIndex];
    }

    //If first line is compatible type for all fields, AND all fields not Strings (dhmay adding 20100502)
    // then there is no header row
    if (_skipLines == -1) {
        boolean firstLineCompat = true;
        boolean allStrings = true;
        String[] fields = lineFields[0];
        for (int f = 0; f < nCols; f++) {
            if ("".equals(fields[f]))
                continue;
            if (colDescs[f].clazz.equals(Integer.TYPE) || colDescs[f].clazz.equals(Double.TYPE)
                    || colDescs[f].clazz.equals(Float.TYPE))
                allStrings = false;
            try {
                Object o = ConvertUtils.convert(fields[f], colDescs[f].clazz);
                if (null == o) {
                    firstLineCompat = false;
                    break;
                }
            } catch (Exception x) {
                firstLineCompat = false;
                break;
            }
        }
        if (firstLineCompat && !allStrings)
            _skipLines = 0;
        else
            _skipLines = 1;
    }

    if (_skipLines > 0) {
        String[] headers = lineFields[_skipLines - 1];
        for (int f = 0; f < nCols; f++)
            colDescs[f].name = (f >= headers.length || "".equals(headers[f])) ? "column" + f : headers[f];
    } else {
        for (int f = 0; f < colDescs.length; f++) {
            ColumnDescriptor colDesc = colDescs[f];
            colDesc.name = "column" + f;
        }
    }

    _columns = colDescs;
}

From source file:net.sf.json.JSONDynaBean.java

/**
 * DOCUMENT ME!//  w  ww. j  av  a2  s .  c  o  m
 *
 * @param dest DOCUMENT ME!
 * @param src DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 */
protected boolean isDynaAssignable(Class dest, Class src) {
    boolean assignable = dest.isAssignableFrom(src);
    assignable = ((dest == Boolean.TYPE) && (src == Boolean.class)) ? true : assignable;
    assignable = ((dest == Byte.TYPE) && (src == Byte.class)) ? true : assignable;
    assignable = ((dest == Character.TYPE) && (src == Character.class)) ? true : assignable;
    assignable = ((dest == Short.TYPE) && (src == Short.class)) ? true : assignable;
    assignable = ((dest == Integer.TYPE) && (src == Integer.class)) ? true : assignable;
    assignable = ((dest == Long.TYPE) && (src == Long.class)) ? true : assignable;
    assignable = ((dest == Float.TYPE) && (src == Float.class)) ? true : assignable;
    assignable = ((dest == Double.TYPE) && (src == Double.class)) ? true : assignable;

    if ((src == Double.TYPE) || Double.class.isAssignableFrom(src)) {
        assignable = (isByte(dest) || isShort(dest) || isInteger(dest) || isLong(dest) || isFloat(dest)) ? true
                : assignable;
    }

    if ((src == Float.TYPE) || Float.class.isAssignableFrom(src)) {
        assignable = (isByte(dest) || isShort(dest) || isInteger(dest) || isLong(dest)) ? true : assignable;
    }

    if ((src == Long.TYPE) || Long.class.isAssignableFrom(src)) {
        assignable = (isByte(dest) || isShort(dest) || isInteger(dest)) ? true : assignable;
    }

    if ((src == Integer.TYPE) || Integer.class.isAssignableFrom(src)) {
        assignable = (isByte(dest) || isShort(dest)) ? true : assignable;
    }

    if ((src == Short.TYPE) || Short.class.isAssignableFrom(src)) {
        assignable = (isByte(dest)) ? true : assignable;
    }

    return assignable;
}

From source file:net.sf.qooxdoo.rpc.RemoteCallUtils.java

/**
 * Converts "normal" java types to JSON stuff.
 *
 * @param       obj                 the object to convert.
 */// ww  w  .j a  va  2  s  .  c  om

public Object fromJava(Object obj)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    if (obj == null) {
        return JSONObject.NULL;
    }
    if (obj instanceof String) {
        return obj;
    }
    if (obj instanceof Date) {
        return obj;
    }
    if (obj instanceof Integer || obj instanceof Double || obj instanceof Boolean) {
        return obj;
    }
    if (obj instanceof Float) {
        return new Double(((Float) obj).doubleValue());
    }
    // FIXME: find a better way to handle longs
    if (obj instanceof Long) {
        return new Double(((Long) obj).doubleValue());
    }
    if (obj instanceof Object[]) {
        Object[] objectArray = (Object[]) obj;
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < objectArray.length; ++i) {
            jsonArray.put(fromJava(objectArray[i]));
        }
        return jsonArray;
    }
    Class componentType = obj.getClass().getComponentType();
    if (componentType == Integer.TYPE) {
        JSONArray jsonArray = new JSONArray();
        int[] intArray = (int[]) obj;
        for (int i = 0; i < intArray.length; ++i) {
            jsonArray.put(intArray[i]);
        }
        return jsonArray;
    }
    if (componentType == Float.TYPE) {
        JSONArray jsonArray = new JSONArray();
        float[] floatArray = (float[]) obj;
        for (int i = 0; i < floatArray.length; ++i) {
            jsonArray.put((double) (floatArray[i]));
        }
        return jsonArray;
    }
    // FIXME: find a better way to handle longs
    if (componentType == Long.TYPE) {
        JSONArray jsonArray = new JSONArray();
        long[] longArray = (long[]) obj;
        for (int i = 0; i < longArray.length; ++i) {
            jsonArray.put((double) (longArray[i]));
        }
        return jsonArray;
    }
    if (componentType == Double.TYPE) {
        JSONArray jsonArray = new JSONArray();
        double[] doubleArray = (double[]) obj;
        for (int i = 0; i < doubleArray.length; ++i) {
            jsonArray.put(doubleArray[i]);
        }
        return jsonArray;
    }
    if (componentType == Boolean.TYPE) {
        JSONArray jsonArray = new JSONArray();
        boolean[] booleanArray = (boolean[]) obj;
        for (int i = 0; i < booleanArray.length; ++i) {
            jsonArray.put(booleanArray[i]);
        }
        return jsonArray;
    }
    if (obj instanceof Map) {
        Map map = (Map) obj;
        Iterator keyIterator = map.keySet().iterator();
        JSONObject jsonObject = new JSONObject();
        Object key;
        while (keyIterator.hasNext()) {
            key = keyIterator.next();
            jsonObject.put((key == null ? null : key.toString()), fromJava(map.get(key)));
        }
        return jsonObject;
    }
    if (obj instanceof Set) {
        Set set = (Set) obj;
        Iterator iterator = set.iterator();
        JSONObject jsonObject = new JSONObject();
        Object key;
        while (iterator.hasNext()) {
            key = iterator.next();
            jsonObject.put((key == null ? null : key.toString()), true);
        }
        return jsonObject;
    }
    return fromJava(filter(obj, PropertyUtils.describe(obj)));
}