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:Main.java

/**
 * This method converts a given number into a target class. This method does not change the value (except when
 * explicitly casting to a more general type, e.g. from double to int), just the internal type representation. While
 * this is unnecessary while using normal java code, reflection based access to method parameters is a bit more
 * difficult. As far as possible, this method will prevent the ArgumentMismatch error when passing numbers as
 * parameters.// w w w .jav  a2  s. c om
 * <p/>
 * If the value can not be converted to the given target class, it will be returned unchanged.
 *
 * @param targetClass Class to which the number should be converted, if possible.
 * @param value       Number value to convert.
 * @return 'value' converted to an instance of 'targetClass'.
 */
public static Object convertNumber(final Class targetClass, final Number value) {
    if (targetClass.equals(Double.class) || targetClass.equals(Double.TYPE))
        return value.doubleValue();
    if (targetClass.equals(Integer.class) || targetClass.equals(Integer.TYPE))
        return value.intValue();
    if (targetClass.equals(Long.class) || targetClass.equals(Long.TYPE))
        return value.longValue();
    if (targetClass.equals(Short.class) || targetClass.equals(Short.TYPE))
        return value.shortValue();
    if (targetClass.equals(Byte.class) || targetClass.equals(Byte.TYPE))
        return value.byteValue();
    if (targetClass.equals(Character.class) || targetClass.equals(Character.TYPE))
        return value.intValue();
    if (targetClass.equals(Float.class) || targetClass.equals(Float.TYPE))
        return value.floatValue();
    return value;
}

From source file:net.sf.ij_plugins.util.DialogUtil.java

/**
 * Utility to automatically create ImageJ's GenericDialog for editing bean properties. It uses BeanInfo to extract
 * display names for each field. If a fields type is not supported irs name will be displayed with a tag
 * "[Unsupported type: class_name]".//from  ww  w.  java 2 s.  com
 *
 * @param bean
 * @return <code>true</code> if user closed bean dialog using OK button, <code>false</code> otherwise.
 */
static public boolean showGenericDialog(final Object bean, final String title) {
    final BeanInfo beanInfo;
    try {
        beanInfo = Introspector.getBeanInfo(bean.getClass());
    } catch (IntrospectionException e) {
        throw new IJPluginsRuntimeException("Error extracting bean info.", e);
    }

    final GenericDialog genericDialog = new GenericDialog(title);

    // Create generic dialog fields for each bean's property
    final PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
    try {
        for (int i = 0; i < propertyDescriptors.length; i++) {
            final PropertyDescriptor pd = propertyDescriptors[i];
            final Class type = pd.getPropertyType();
            if (type.equals(Class.class) && "class".equals(pd.getName())) {
                continue;
            }

            final Object o = PropertyUtils.getSimpleProperty(bean, pd.getName());
            if (type.equals(Boolean.TYPE)) {
                boolean value = ((Boolean) o).booleanValue();
                genericDialog.addCheckbox(pd.getDisplayName(), value);
            } else if (type.equals(Integer.TYPE)) {
                int value = ((Integer) o).intValue();
                genericDialog.addNumericField(pd.getDisplayName(), value, 0);
            } else if (type.equals(Float.TYPE)) {
                double value = ((Float) o).doubleValue();
                genericDialog.addNumericField(pd.getDisplayName(), value, 6, 10, "");
            } else if (type.equals(Double.TYPE)) {
                double value = ((Double) o).doubleValue();
                genericDialog.addNumericField(pd.getDisplayName(), value, 6, 10, "");
            } else {
                genericDialog.addMessage(pd.getDisplayName() + "[Unsupported type: " + type + "]");
            }

        }
    } catch (IllegalAccessException e) {
        throw new IJPluginsRuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new IJPluginsRuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new IJPluginsRuntimeException(e);
    }

    //        final Panel helpPanel = new Panel();
    //        helpPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0));
    //        final Button helpButton = new Button("Help");
    ////                helpButton.addActionListener(this);
    //        helpPanel.add(helpButton);
    //        genericDialog.addPanel(helpPanel, GridBagConstraints.EAST, new Insets(15,0,0,0));

    // Show modal dialog
    genericDialog.showDialog();
    if (genericDialog.wasCanceled()) {
        return false;
    }

    // Read fields from generic dialog into bean's properties.
    try {
        for (int i = 0; i < propertyDescriptors.length; i++) {
            final PropertyDescriptor pd = propertyDescriptors[i];
            final Class type = pd.getPropertyType();
            final Object propertyValue;
            if (type.equals(Boolean.TYPE)) {
                boolean value = genericDialog.getNextBoolean();
                propertyValue = Boolean.valueOf(value);
            } else if (type.equals(Integer.TYPE)) {
                int value = (int) Math.round(genericDialog.getNextNumber());
                propertyValue = new Integer(value);
            } else if (type.equals(Float.TYPE)) {
                double value = genericDialog.getNextNumber();
                propertyValue = new Float(value);
            } else if (type.equals(Double.TYPE)) {
                double value = genericDialog.getNextNumber();
                propertyValue = new Double(value);
            } else {
                continue;
            }
            PropertyUtils.setProperty(bean, pd.getName(), propertyValue);
        }
    } catch (IllegalAccessException e) {
        throw new IJPluginsRuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new IJPluginsRuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new IJPluginsRuntimeException(e);
    }

    return true;
}

From source file:org.lunarray.model.descriptor.builder.annotation.util.RenderDefaultsUtil.java

/**
 * Fills the defaults./* w  w  w.ja  v  a2  s  .  c  om*/
 */
private RenderDefaultsUtil() {
    this.renderDefaults = new HashMap<Class<?>, RenderType>();
    this.renderDefaults.put(Calendar.class, RenderType.DATE_PICKER);
    this.renderDefaults.put(Date.class, RenderType.DATE_PICKER);
    this.renderDefaults.put(java.sql.Date.class, RenderType.DATE_PICKER);
    this.renderDefaults.put(String.class, RenderType.TEXT);
    this.renderDefaults.put(Integer.class, RenderType.TEXT);
    this.renderDefaults.put(Double.class, RenderType.TEXT);
    this.renderDefaults.put(Float.class, RenderType.TEXT);
    this.renderDefaults.put(Long.class, RenderType.TEXT);
    this.renderDefaults.put(Byte.class, RenderType.TEXT);
    this.renderDefaults.put(Short.class, RenderType.TEXT);
    this.renderDefaults.put(Character.class, RenderType.TEXT);
    this.renderDefaults.put(Integer.TYPE, RenderType.TEXT);
    this.renderDefaults.put(Double.TYPE, RenderType.TEXT);
    this.renderDefaults.put(Float.TYPE, RenderType.TEXT);
    this.renderDefaults.put(Long.TYPE, RenderType.TEXT);
    this.renderDefaults.put(Byte.TYPE, RenderType.TEXT);
    this.renderDefaults.put(Short.TYPE, RenderType.TEXT);
    this.renderDefaults.put(Character.TYPE, RenderType.TEXT);
    this.renderDefaults.put(BigDecimal.class, RenderType.TEXT);
    this.renderDefaults.put(BigInteger.class, RenderType.TEXT);
    this.renderDefaults.put(Boolean.class, RenderType.CHECKBOX);
    this.renderDefaults.put(Boolean.TYPE, RenderType.CHECKBOX);
}

From source file:com.textuality.lifesaver2.Columns.java

public Columns(Context context, String[] names, Class<?>[] classes, String key1, String key2) {
    this.mNames = names;
    mTypes = new Type[names.length];
    mKey1 = key1;/*from  w  ww  .j a  v  a 2  s .c  o  m*/
    mKey2 = key2;
    mNameFinder = new NameFinder(context);
    for (int i = 0; i < names.length; i++) {

        if (classes[i] == String.class)
            mTypes[i] = Type.STRING;
        else if (classes[i] == Integer.TYPE || classes[i] == Integer.class)
            mTypes[i] = Type.INT;
        else if (classes[i] == Long.TYPE || classes[i] == Long.class)
            mTypes[i] = Type.LONG;
        else if (classes[i] == Float.TYPE || classes[i] == Float.class)
            mTypes[i] = Type.FLOAT;
        else if (classes[i] == Double.TYPE || classes[i] == Double.class)
            mTypes[i] = Type.DOUBLE;
    }
}

From source file:org.openTwoFactor.clientExt.net.sf.ezmorph.object.NumberMorpher.java

/**
 * Creates a new morpher for the target type.
 *
 * @param type must be a primitive or wrapper type. BigDecimal and BigInteger
 *        are also supported./* ww w .j a  va2 s.com*/
 */
public NumberMorpher(Class type) {
    super(false);

    if (type == null) {
        throw new MorphException("Must specify a type");
    }

    if (type != Byte.TYPE && type != Short.TYPE && type != Integer.TYPE && type != Long.TYPE
            && type != Float.TYPE && type != Double.TYPE && !Byte.class.isAssignableFrom(type)
            && !Short.class.isAssignableFrom(type) && !Integer.class.isAssignableFrom(type)
            && !Long.class.isAssignableFrom(type) && !Float.class.isAssignableFrom(type)
            && !Double.class.isAssignableFrom(type) && !BigInteger.class.isAssignableFrom(type)
            && !BigDecimal.class.isAssignableFrom(type)) {
        throw new MorphException("Must specify a Number subclass");
    }

    this.type = type;
}

From source file:Main.java

private static boolean isAssignableFrom(Class<?> parameterType, Object value) {
    if (parameterType.isPrimitive()) {
        if (value == null) {
            return false;
        }/*from  w  w  w . j  a v  a 2s .c  o  m*/
        Class<?> valueClass = value.getClass();

        if (parameterType == Boolean.TYPE) {
            return valueClass == Boolean.class;
        } else if (parameterType == Byte.TYPE) {
            return valueClass == Byte.class;
        } else if (parameterType == Character.TYPE) {
            return valueClass == Character.class;
        } else if (parameterType == Short.TYPE) {
            return valueClass == Short.class || valueClass == Byte.class;
        } else if (parameterType == Integer.TYPE) {
            return valueClass == Integer.class || valueClass == Character.class || valueClass == Short.class
                    || valueClass == Byte.class;
        } else if (parameterType == Long.TYPE) {
            return valueClass == Long.class || valueClass == Integer.class || valueClass == Character.class
                    || valueClass == Short.class || valueClass == Byte.class;
        } else if (parameterType == Float.TYPE) {
            return valueClass == Float.class || valueClass == Long.class || valueClass == Integer.class
                    || valueClass == Character.class || valueClass == Short.class || valueClass == Byte.class;
        } else if (parameterType == Double.TYPE) {
            return valueClass == Double.class || valueClass == Float.class || valueClass == Long.class
                    || valueClass == Integer.class || valueClass == Character.class || valueClass == Short.class
                    || valueClass == Byte.class;
        } else {
            return false;
        }
    } else {
        return value == null || parameterType.isAssignableFrom(value.getClass());
    }
}

From source file:org.exolab.castor.xml.parsing.primitive.objects.PrimitiveObjectFactory.java

private PrimitiveObjectFactory() {
    typeHandlers.put(String.class, PrimitiveString.class);

    typeHandlers.put(Enum.class, PrimitiveEnum.class);

    typeHandlers.put(Integer.TYPE, PrimitiveInteger.class);
    typeHandlers.put(Integer.class, PrimitiveInteger.class);

    typeHandlers.put(Boolean.TYPE, PrimitiveBoolean.class);
    typeHandlers.put(Boolean.class, PrimitiveBoolean.class);

    typeHandlers.put(Double.TYPE, PrimitiveDouble.class);
    typeHandlers.put(Double.class, PrimitiveDouble.class);

    typeHandlers.put(Long.TYPE, PrimitiveLong.class);
    typeHandlers.put(Long.class, PrimitiveLong.class);

    typeHandlers.put(Character.TYPE, PrimitiveChar.class);
    typeHandlers.put(Character.class, PrimitiveChar.class);

    typeHandlers.put(Short.TYPE, PrimitiveShort.class);
    typeHandlers.put(Short.class, PrimitiveShort.class);

    typeHandlers.put(Float.TYPE, PrimitiveFloat.class);
    typeHandlers.put(Float.class, PrimitiveFloat.class);

    typeHandlers.put(Byte.TYPE, PrimitiveByte.class);
    typeHandlers.put(Byte.class, PrimitiveByte.class);

    typeHandlers.put(BigInteger.class, PrimitiveBigInteger.class);

    typeHandlers.put(BigDecimal.class, PrimitiveBigDecimal.class);
}

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

/**
 * //from ww w .j  a v a  2s . co  m
 * @param conn
 * @param config
 * @param table{key,value,map,list}
 */
public static void loadConf4db(Connection conn, PreparedStatement ps, BaseConfigMBean config, String[] table) {
    ResultSet rs = null;
    String key_ = table[0];
    String value_ = table[1];
    try {
        rs = ps.executeQuery();

        Class<?> type = null;
        String name;
        String value;
        while (rs.next()) {
            name = rs.getString(key_);
            value = rs.getString(value_);
            java.lang.reflect.Field f = null;
            try {
                f = config.getClass().getDeclaredField(name);
            } catch (java.lang.NoSuchFieldException e) {
                continue;
            }

            if (f != null)
                type = f.getType();
            Object args = null;
            java.lang.reflect.Method m = getMethod(config.getClass(), "set" + name);
            if (m != null && notNull(value) && type != null) {
                if (String.class == type) {
                    args = value;
                } else if (java.lang.Integer.class == type || Integer.TYPE == type) {
                    args = Integer.valueOf(value);
                } else if (java.lang.Boolean.class == type || Boolean.TYPE == type) {
                    args = Boolean.parseBoolean(value);
                } else if (java.lang.Long.class == type || Long.TYPE == type) {
                    args = Long.valueOf(value);
                } else if (java.lang.Float.class == type || Float.TYPE == type) {
                    args = Float.valueOf(value);
                } else {
                    logger.info("not map Class:" + type);
                }
                m.invoke(config, args);
                logger.info(name + ":" + value);
            } else if (notNull(value))
                logger.warn("can't find element:" + value);
        }
    } catch (Exception e) {
        logger.error("load config error:", e);
    } finally {
        try {
            conn.close();
            ps.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            logger.error("load config error:", e);
        }
        conn = null;
    }
}

From source file:io.github.benas.jpopulator.impl.DefaultRandomizer.java

/**
 * Generate a random value for the given type.
 *
 * @param type the type for which a random value will be generated
 * @return a random value for the given type or null if the type is not supported
 *///from w  w  w  .j  a v  a2 s.  c o m
public static Object getRandomValue(final Class type) {

    /*
     * String and Character types
     */
    if (type.equals(String.class)) {
        return RandomStringUtils.randomAlphabetic(ConstantsUtil.DEFAULT_STRING_LENGTH);
    }
    if (type.equals(Character.TYPE) || type.equals(Character.class)) {
        return RandomStringUtils.randomAlphabetic(1).charAt(0);
    }

    /*
     * Boolean type
     */
    if (type.equals(Boolean.TYPE) || type.equals(Boolean.class)) {
        return ConstantsUtil.RANDOM.nextBoolean();
    }

    /*
     * Numeric types
     */
    if (type.equals(Byte.TYPE) || type.equals(Byte.class)) {
        return (byte) (ConstantsUtil.RANDOM.nextInt());
    }
    if (type.equals(Short.TYPE) || type.equals(Short.class)) {
        return (short) (ConstantsUtil.RANDOM.nextInt());
    }
    if (type.equals(Integer.TYPE) || type.equals(Integer.class)) {
        return ConstantsUtil.RANDOM.nextInt();
    }
    if (type.equals(Long.TYPE) || type.equals(Long.class)) {
        return ConstantsUtil.RANDOM.nextLong();
    }
    if (type.equals(Double.TYPE) || type.equals(Double.class)) {
        return ConstantsUtil.RANDOM.nextDouble();
    }
    if (type.equals(Float.TYPE) || type.equals(Float.class)) {
        return ConstantsUtil.RANDOM.nextFloat();
    }
    if (type.equals(BigInteger.class)) {
        return new BigInteger(
                Math.abs(ConstantsUtil.RANDOM.nextInt(ConstantsUtil.DEFAULT_BIG_INTEGER_NUM_BITS_LENGTH)),
                ConstantsUtil.RANDOM);
    }
    if (type.equals(BigDecimal.class)) {
        return new BigDecimal(ConstantsUtil.RANDOM.nextDouble());
    }
    if (type.equals(AtomicLong.class)) {
        return new AtomicLong(ConstantsUtil.RANDOM.nextLong());
    }
    if (type.equals(AtomicInteger.class)) {
        return new AtomicInteger(ConstantsUtil.RANDOM.nextInt());
    }

    /*
     * Date and time types
     */
    if (type.equals(java.util.Date.class)) {
        return ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue();
    }
    if (type.equals(java.sql.Date.class)) {
        return new java.sql.Date(ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue().getTime());
    }
    if (type.equals(java.sql.Time.class)) {
        return new java.sql.Time(ConstantsUtil.RANDOM.nextLong());
    }
    if (type.equals(java.sql.Timestamp.class)) {
        return new java.sql.Timestamp(ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue().getTime());
    }
    if (type.equals(Calendar.class)) {
        return Calendar.getInstance();
    }
    if (type.equals(org.joda.time.DateTime.class)) {
        return new org.joda.time.DateTime(ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue().getTime());
    }
    if (type.equals(org.joda.time.LocalDate.class)) {
        return new org.joda.time.LocalDate(ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue().getTime());
    }
    if (type.equals(org.joda.time.LocalTime.class)) {
        return new org.joda.time.LocalTime(ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue().getTime());
    }
    if (type.equals(org.joda.time.LocalDateTime.class)) {
        return new org.joda.time.LocalDateTime(ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue().getTime());
    }
    if (type.equals(org.joda.time.Duration.class)) {
        return new org.joda.time.Duration(Math.abs(ConstantsUtil.RANDOM.nextLong()));
    }
    if (type.equals(org.joda.time.Period.class)) {
        return new org.joda.time.Period(Math.abs(ConstantsUtil.RANDOM.nextInt()));
    }
    if (type.equals(org.joda.time.Interval.class)) {
        long startDate = Math.abs(ConstantsUtil.RANDOM.nextInt());
        long endDate = startDate + Math.abs(ConstantsUtil.RANDOM.nextInt());
        return new org.joda.time.Interval(startDate, endDate);
    }

    /*
     * Enum type
     */
    if (type.isEnum() && type.getEnumConstants().length > 0) {
        Object[] enumConstants = type.getEnumConstants();
        return enumConstants[ConstantsUtil.RANDOM.nextInt(enumConstants.length)];
    }

    /*
     * Return null for any unsupported type
     */
    return null;

}

From source file:it.greenvulcano.gvesb.http.ProtocolFactory.java

/**
 *
 * @param objectClass//from   w  w w . j av a2s .  co  m
 *        Object class to instantiate
 * @param node
 *        The configuration of constructor parameters.
 * @return
 */
private static Object createObjectUsingConstructor(Class<?> objectClass, Node node) throws Exception {
    NodeList paramList = XMLConfig.getNodeList(node, "constructor-param");
    Class<?>[] types = new Class[paramList.getLength()];
    Object[] params = new Object[types.length];

    for (int i = 0; i < types.length; ++i) {
        Node paramNode = paramList.item(i);
        String type = XMLConfig.get(paramNode, "@type");
        String value = XMLConfig.getDecrypted(paramNode, "@value");
        Class<?> cls = null;
        if (type.equals("byte")) {
            cls = Byte.TYPE;
        } else if (type.equals("boolean")) {
            cls = Boolean.TYPE;
        } else if (type.equals("char")) {
            cls = Character.TYPE;
        } else if (type.equals("double")) {
            cls = Double.TYPE;
        } else if (type.equals("float")) {
            cls = Float.TYPE;
        } else if (type.equals("int")) {
            cls = Integer.TYPE;
        } else if (type.equals("long")) {
            cls = Long.TYPE;
        } else if (type.equals("short")) {
            cls = Short.TYPE;
        } else if (type.equals("String")) {
            cls = String.class;
        }
        types[i] = cls;
        params[i] = cast(value, cls);
    }

    Constructor<?> constr = objectClass.getConstructor(types);
    return constr.newInstance(params);
}