Example usage for java.lang Class equals

List of usage examples for java.lang Class equals

Introduction

In this page you can find the example usage for java.lang Class equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:com.siemens.sw360.commonIO.TypeMappings.java

@SuppressWarnings("unchecked")
public static <T> List<T> getAllFromDB(LicenseService.Iface licenseClient, Class<T> clazz) throws TException {
    if (clazz.equals(LicenseType.class)) {
        return (List<T>) licenseClient.getLicenseTypes();
    } else if (clazz.equals(Obligation.class)) {
        return (List<T>) licenseClient.getAllObligations();
    } else if (clazz.equals(RiskCategory.class)) {
        return (List<T>) licenseClient.getRiskCategories();
    }//  ww w.java2s  .c  o m
    throw new SW360Exception("Unknown Type requested");
}

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 w  w  w  .  j  ava 2  s  . c  om*/
 *
 * @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:TypeUtils.java

/**
 * Check if the right-hand side type may be assigned to the left-hand side
 * type, assuming setting by reflection. Considers primitive wrapper
 * classes as assignable to the corresponding primitive types.
 * @param lhsType the target type//ww w . j  a va2s . c  o m
 * @param rhsType the value type that should be assigned to the target type
 * @return if the target type is assignable from the value type
 * @see TypeUtils#isAssignable
 */
public static boolean isAssignable(Class lhsType, Class rhsType) {

    return (lhsType.isAssignableFrom(rhsType) || lhsType.equals(primitiveWrapperTypeMap.get(rhsType)));
}

From source file:com.siemens.sw360.commonIO.TypeMappings.java

@SuppressWarnings("unchecked")
public static <T> List<T> simpleConvert(List<CSVRecord> records, Class<T> clazz) throws SW360Exception {
    if (clazz.equals(LicenseType.class)) {
        return (List<T>) ConvertRecord.convertLicenseTypes(records);
    } else if (clazz.equals(Obligation.class)) {
        return (List<T>) ConvertRecord.convertObligation(records);
    } else if (clazz.equals(RiskCategory.class)) {
        return (List<T>) ConvertRecord.convertRiskCategories(records);
    }/*from w  w  w.  j a  va  2  s  .  c  om*/
    throw new SW360Exception("Unknown Type requested");
}

From source file:NumberUtils.java

/**
 * Parse the given text into a number instance of the given target class,
 * using the corresponding default <code>decode</code> methods. Trims the
 * input <code>String</code> before attempting to parse the number. Supports
 * numbers in hex format (with leading 0x) and in octal format (with leading 0).
 * @param text the text to convert/*from w  w  w  . ja v a  2 s. c  o m*/
 * @param targetClass the target class to parse into
 * @return the parsed number
 * @throws IllegalArgumentException if the target class is not supported
 * (i.e. not a standard Number subclass as included in the JDK)
 * @see java.lang.Byte#decode
 * @see java.lang.Short#decode
 * @see java.lang.Integer#decode
 * @see java.lang.Long#decode
 * @see #decodeBigInteger(String)
 * @see java.lang.Float#valueOf
 * @see java.lang.Double#valueOf
 * @see java.math.BigDecimal#BigDecimal(String)
 */
public static Number parseNumber(String text, Class targetClass) {

    String trimmed = text.trim();

    if (targetClass.equals(Byte.class)) {
        return Byte.decode(trimmed);
    } else if (targetClass.equals(Short.class)) {
        return Short.decode(trimmed);
    } else if (targetClass.equals(Integer.class)) {
        return Integer.decode(trimmed);
    } else if (targetClass.equals(Long.class)) {
        return Long.decode(trimmed);
    } else if (targetClass.equals(BigInteger.class)) {
        return decodeBigInteger(trimmed);
    } else if (targetClass.equals(Float.class)) {
        return Float.valueOf(trimmed);
    } else if (targetClass.equals(Double.class)) {
        return Double.valueOf(trimmed);
    } else if (targetClass.equals(BigDecimal.class) || targetClass.equals(Number.class)) {
        return new BigDecimal(trimmed);
    } else {
        throw new IllegalArgumentException(
                "Cannot convert String [" + text + "] to target class [" + targetClass.getName() + "]");
    }
}

From source file:com.arpnetworking.jackson.BuilderDeserializer.java

static boolean isSetterMethod(final Class<?> builderClass, final Method method) {
    return method.getName().startsWith(SETTER_PREFIX) && builderClass.equals(method.getReturnType())
            && !method.isVarArgs() && method.getParameterTypes().length == 1;
}

From source file:io.konik.utils.RandomInvoiceGenerator.java

private static boolean isLeafType(Class<?> type) {
    if (type.equals(Class.class))
        return true;
    if (isAssignable(ZfDate.class, type))
        return true;
    if (isAssignable(Date.class, type))
        return true;
    if (isAssignable(String.class, type))
        return true;
    if (BigDecimal.class.isAssignableFrom(type))
        return true;
    if (type.isEnum())
        return true;
    if (type.equals(Profile.class))
        return true;
    return isPrimitiveOrWrapper(type);
}

From source file:edu.umn.msi.tropix.proteomics.parameters.ParameterUtils.java

/**
 * Uses reflection to dynamically set a parameter bean's parameters from a given map of Strings.
 * /*from   ww  w  .  ja va  2 s . c o  m*/
 * @param parameterMap
 *          Mapping of parameter names (attributes) to values.
 * @param parameterObject
 *          Java bean to set parameter values of of. Setter methods must conform to the Java bean naming scheme and must take in one argument of
 *          type String, Integer, Long, Float, or Double.
 * @exception IllegalArgumentException
 *              Thrown if a given setter method cannot be found or if the setter method does not conform to the rules described.
 */
@SuppressWarnings("unchecked")
public static void setParametersFromMap(final Map parameterMap, final Object parameterObject) {
    final Method[] methods = parameterObject.getClass().getMethods();
    final Map<String, Method> objectSetMethodMap = new HashMap<String, Method>(methods.length);

    for (final Method method : parameterObject.getClass().getMethods()) {
        final String methodName = method.getName();
        if (methodName.contains("set")) {
            objectSetMethodMap.put(method.getName(), method);
        }
    }

    for (final Object keyObject : parameterMap.keySet()) {
        final String key = (String) keyObject;
        final String value = (String) parameterMap.get(key);
        final String setterMethodName = "set" + key.substring(0, 1).toUpperCase() + key.substring(1);
        final Method setterMethod = objectSetMethodMap.get(setterMethodName);
        if (setterMethod == null) {
            continue;
        }
        final Class<?>[] parameterTypes = setterMethod.getParameterTypes();
        if (parameterTypes.length != 1) {
            throw new IllegalArgumentException(
                    "Illegal setter method found, must take in exactly one argument.");
        }
        final Class<?> argumentType = parameterTypes[0];
        try {
            if (argumentType.equals(String.class)) {
                setterMethod.invoke(parameterObject, value);
                continue;
            }
            if (value == null || value.trim().equals("")) {
                setterMethod.invoke(parameterObject, (Object) null);
                continue;
            }
            if (argumentType.equals(Double.class) || argumentType.equals(double.class)) {
                setterMethod.invoke(parameterObject, Double.parseDouble(value));
            } else if (argumentType.equals(Integer.class) || argumentType.equals(int.class)) {
                setterMethod.invoke(parameterObject, Integer.parseInt(value));
            } else if (argumentType.equals(Long.class) || argumentType.equals(long.class)) {
                setterMethod.invoke(parameterObject, Long.parseLong(value));
            } else if (argumentType.equals(Float.class) || argumentType.equals(float.class)) {
                setterMethod.invoke(parameterObject, Float.parseFloat(value));
            } else if (argumentType.equals(Boolean.class) || argumentType.equals(boolean.class)) {
                setterMethod.invoke(parameterObject, Boolean.parseBoolean(value));
            } else if (argumentType.equals(Short.class) || argumentType.equals(short.class)) {
                setterMethod.invoke(parameterObject, Short.parseShort(value));
            } else {
                throw new IllegalArgumentException("Illegal type found for argument to setter bean - type is "
                        + argumentType + " - key is " + key);
            }
        } catch (final Exception e) {
            throw new IllegalArgumentException(e);
        }
    }
}

From source file:org.jfree.data.time.RegularTimePeriod.java

/**
 * Returns a subclass of {@link RegularTimePeriod} that is smaller than
 * the specified class.//  w w  w  . ja v  a  2  s  .  c  o  m
 *
 * @param c  a subclass of {@link RegularTimePeriod}.
 *
 * @return A class.
 */
public static Class downsize(Class c) {
    if (c.equals(Year.class)) {
        return Quarter.class;
    } else if (c.equals(Quarter.class)) {
        return Month.class;
    } else if (c.equals(Month.class)) {
        return Day.class;
    } else if (c.equals(Day.class)) {
        return Hour.class;
    } else if (c.equals(Hour.class)) {
        return Minute.class;
    } else if (c.equals(Minute.class)) {
        return Second.class;
    } else if (c.equals(Second.class)) {
        return Millisecond.class;
    } else {
        return Millisecond.class;
    }
}

From source file:Main.java

/**
 * search the method and return the defined method.
 * it will {@link Class#getMethod(String, Class[])}, if exception occurs,
 * it will search for all methods, and find the most fit method.
 *///from   w w  w. j  ava 2  s.  c o m
public static Method searchMethod(Class<?> currentClass, String name, Class<?>[] parameterTypes, boolean boxed)
        throws NoSuchMethodException {
    if (currentClass == null) {
        throw new NoSuchMethodException("class == null");
    }
    try {
        return currentClass.getMethod(name, parameterTypes);
    } catch (NoSuchMethodException e) {
        Method likeMethod = null;
        for (Method method : currentClass.getMethods()) {
            if (method.getName().equals(name) && parameterTypes.length == method.getParameterTypes().length
                    && Modifier.isPublic(method.getModifiers())) {
                if (parameterTypes.length > 0) {
                    Class<?>[] types = method.getParameterTypes();
                    boolean eq = true;
                    boolean like = true;
                    for (int i = 0; i < parameterTypes.length; i++) {
                        Class<?> type = types[i];
                        Class<?> parameterType = parameterTypes[i];
                        if (type != null && parameterType != null && !type.equals(parameterType)) {
                            eq = false;
                            if (boxed) {
                                type = getBoxedClass(type);
                                parameterType = getBoxedClass(parameterType);
                            }
                            if (!type.isAssignableFrom(parameterType)) {
                                eq = false;
                                like = false;
                                break;
                            }
                        }
                    }
                    if (!eq) {
                        if (like && (likeMethod == null || likeMethod.getParameterTypes()[0]
                                .isAssignableFrom(method.getParameterTypes()[0]))) {
                            likeMethod = method;
                        }
                        continue;
                    }
                }
                return method;
            }
        }
        if (likeMethod != null) {
            return likeMethod;
        }
        throw e;
    }
}