Example usage for java.lang Enum name

List of usage examples for java.lang Enum name

Introduction

In this page you can find the example usage for java.lang Enum name.

Prototype

String name

To view the source code for java.lang Enum name.

Click Source Link

Document

The name of this enum constant, as declared in the enum declaration.

Usage

From source file:com.flexive.shared.FxSharedUtils.java

/**
 * Returns the localized label for the given enum value. The enum translations are
 * stored in FxSharedMessages.properties and are standardized as
 * {@code FQCN.value},/*ww w .  jav  a  2  s  .c om*/
 * e.g. {@code com.flexive.shared.search.query.ValueComparator.LIKE}.
 *
 * @param value the enum value to be translated
 * @param args  optional arguments to be replaced in the localized messages
 * @return the localized label for the given enum value
 */
public static FxString getEnumLabel(Enum<?> value, Object... args) {
    final Class<? extends Enum> valueClass = value.getClass();
    final String clsName;
    if (valueClass.getEnclosingClass() != null && Enum.class.isAssignableFrom(valueClass.getEnclosingClass())) {
        // don't include anonymous inner class definitions often used by enums in class name
        clsName = valueClass.getEnclosingClass().getName();
    } else {
        clsName = valueClass.getName();
    }
    return getMessage(SHARED_BUNDLE, clsName + "." + value.name(), args);
}

From source file:org.efaps.esjp.common.uiform.Field_Base.java

/**
 * @param _parameter    Parameter as passed from the eFaps API
 * @return Return containing Html Snipplet
 * @throws EFapsException on error//from   w  w  w . ja v a 2s.c o m
 */
public Return getOptionList4Enum(final Parameter _parameter) throws EFapsException {
    final List<DropDownPosition> values = new ArrayList<>();
    final String enumName = getProperty(_parameter, "Enum");
    if (enumName != null) {
        final boolean orderByOrdinal = "true".equalsIgnoreCase(getProperty(_parameter, "OrderByOrdinal"));
        try {
            final Class<?> enumClazz = Class.forName(enumName);

            if (enumClazz.isEnum()) {
                final Object[] consts = enumClazz.getEnumConstants();
                Integer selected = -1;

                final Object uiObject = _parameter.get(ParameterValues.UIOBJECT);
                if (uiObject instanceof IUIValue && ((IUIValue) uiObject).getObject() != null) {
                    selected = (Integer) ((IUIValue) uiObject).getObject();
                } else if (containsProperty(_parameter, "DefaultValue")) {
                    final String defaultValue = getProperty(_parameter, "DefaultValue");
                    for (final Object con : consts) {
                        if (((Enum<?>) con).name().equals(defaultValue)) {
                            selected = ((Enum<?>) con).ordinal();
                            break;
                        }
                    }
                }
                if (org.efaps.admin.ui.field.Field.Display.EDITABLE
                        .equals(((IUIValue) uiObject).getDisplay())) {
                    for (final Object con : consts) {
                        final Enum<?> enumVal = (Enum<?>) con;
                        final Integer ordinal = enumVal.ordinal();
                        final String label = DBProperties.getProperty(enumName + "." + enumVal.name());
                        final DropDownPosition pos = new DropDownPosition(ordinal, label,
                                orderByOrdinal ? ordinal : label);
                        values.add(pos);
                        pos.setSelected(ordinal == selected);
                    }
                    Collections.sort(values, new Comparator<DropDownPosition>() {

                        @SuppressWarnings("unchecked")
                        @Override
                        public int compare(final DropDownPosition _o1, final DropDownPosition _o2) {
                            return _o1.getOrderValue().compareTo(_o2.getOrderValue());
                        }
                    });
                }
            }
        } catch (final ClassNotFoundException e) {
            LOG.error("ClassNotFoundException", e);
        }
    }
    final Return ret = new Return();
    ret.put(ReturnValues.VALUES, values);
    return ret;
}

From source file:at.treedb.db.Base.java

/**
 * Invokes a callback before updating the entity.
 * /*from w  w w  .ja v  a 2s.c o  m*/
 * @param dao
 *            {@code DAOiface} data access object
 * @param user
 *            user {@code User} who updates the entity
 * @param map
 *            map containing the data
 * @param info
 *            optional context object
 * @return {@code true} if a update takes place, {@code false} if not
 * @throws Exception
 */
private boolean invokeCallbackUpdate(DAOiface dao, User user, UpdateMap map, Object context) throws Exception {
    HashSet<String> set = callbackUpdateFields.get(this.getClass());
    if (set == null) {
        return false;
    }
    for (Enum<?> e : map.getMap().keySet()) {
        if (set.contains(e.name())) {
            this.callbackUpdate(dao, user, map, context);
            return true;
        }
    }
    return false;
}

From source file:at.treedb.db.Base.java

/**
 * Updates only embedded data types of an entity.
 * //from  w  w w . j  a v  a 2s.  com
 * @param map
 *            map containing the changes
 * @param
 * @throws Exception
 */
public void simpleUpdate(UpdateMap map, boolean strict) throws Exception {
    Class<?> c = this.getClass();

    for (Enum<?> field : map.getMap().keySet()) {
        Update u = map.get(field);
        Field f = c.getDeclaredField(field.name());
        f.setAccessible(true);
        switch (u.getType()) {
        case STRING:
            f.set(this, u.getString());
            break;
        case DOUBLE:
            f.set(this, u.getDouble());
            break;
        case FLOAT:
            f.set(this, u.getFloat());
            break;
        case LONG:
            f.set(this, u.getLong());
            break;
        case INT:
            f.set(this, u.getInt());
            break;
        case LAZY_BINARY:
        case BINARY:
            f.set(this, u.getBinary());
            break;
        case BOOLEAN:
            f.set(this, u.getBoolean());
            break;
        case DATE:
            f.set(this, u.getDate());
            break;
        case ENUM:
            f.set(this, u.getEnum());
            break;
        case BIGDECIMAL:
            f.set(this, u.getBigDecimal());
            break;
        default:
            if (strict) {
                throw new Exception("Base.update(): Type not implemented!");
            }
        }
    }
}

From source file:at.treedb.db.Base.java

/**
 * Checks the map containing the changes. Redundant update entries will be
 * removed./*ww  w.j  ava2  s . c om*/
 * 
 * @param map
 *            map containing the changes
 * @throws Exception
 */
protected void check(UpdateMap map) throws Exception {
    Class<?> c = this.getClass();
    Enum<?>[] list = map.getMap().keySet().toArray(new Enum[map.getMap().keySet().size()]);
    for (Enum<?> field : list) {
        Update u = map.get(field);
        Field f;
        try {
            f = c.getDeclaredField(field.name());
        } catch (java.lang.NoSuchFieldException e) {
            f = c.getSuperclass().getDeclaredField(field.name());
        }
        f.setAccessible(true);
        switch (u.getType()) {
        case STRING:
            String value = u.getString();
            String s = (String) f.get(this);
            if ((value == null && s == null) || (s != null && value != null && s.equals(value))) {
                map.remove(field);
            }
            break;
        case DOUBLE:
            if (u.getDouble() == (Double) f.get(this)) {
                map.remove(field);
            }
            break;
        case LONG:
            if (u.getLong() == (Long) f.get(this)) {
                map.remove(field);
            }
            break;
        case INT:
            if (u.getInt() == (Integer) f.get(this)) {
                map.remove(field);
            }
            break;
        case BINARY:
            byte[] a = (byte[]) f.get(this);
            byte[] b = u.getBinary();
            if ((a == null && b == null) || (a != null && b != null && Arrays.equals(a, b))) {
                map.remove(field);
            }
            break;
        case BOOLEAN:
            if (u.getBoolean() == (Boolean) f.get(this)) {
                map.remove(field);
            }
            break;
        case DATE:
            Date dvalue = u.getDate();
            Date d = (Date) f.get(this);
            if ((dvalue == null && d == null) || (d != null && dvalue != null && d.equals(dvalue))) {
                map.remove(field);
            }
            break;
        case ENUM:
            if (u.getEnum() == (Enum<?>) f.get(this)) {
                map.remove(field);
            }
            break;
        case BIGDECIMAL:
            if (u.getBigDecimal().equals((BigDecimal) f.get(this))) {
                map.remove(field);
            }
            // no check for these types
        case IMAGE:
        case ISTRING_DELETE:
        case ISTRING:
        case IMAGE_DELETE:
        case IMAGE_DUMMY:
            break;
        default:
            throw new Exception("Base.check(): Type not implemented: " + u.getType().toString());
        }
    }
}

From source file:at.treedb.db.Base.java

/**
 * Updates an entity.//from  w ww  .  j  a  v a2 s  . c  o m
 * 
 * @param dao
 *            {@code DAOiface} (data access object)
 * @param user
 *            user who updates the entity
 * @param map
 *            map containing the changes
 * @throws Exception
 */
protected void update(DAOiface dao, User user, UpdateMap map) throws Exception {
    Class<?> c = this.getClass();

    for (Enum<?> field : map.getMap().keySet()) {
        Update u = map.get(field);
        Field f;
        try {
            f = c.getDeclaredField(field.name());
        } catch (java.lang.NoSuchFieldException e) {
            f = c.getSuperclass().getDeclaredField(field.name());
        }
        f.setAccessible(true);
        switch (u.getType()) {
        case STRING:
            f.set(this, u.getString());
            break;
        case DOUBLE:
            f.set(this, u.getDouble());
            break;
        case FLOAT:
            f.set(this, u.getFloat());
            break;
        case LONG:
            f.set(this, u.getLong());
            break;
        case INT:
            f.set(this, u.getInt());
            break;
        case LAZY_BINARY:
        case BINARY:
            f.set(this, u.getBinary());
            break;
        case BOOLEAN:
            f.set(this, u.getBoolean());
            break;
        case DATE:
            f.set(this, u.getDate());
            break;
        case ENUM:
            f.set(this, u.getEnum());
            break;
        case BIGDECIMAL:
            f.set(this, u.getBigDecimal());
            break;
        case ISTRING: {
            DBkey a = f.getAnnotation(DBkey.class);
            if (a == null || !a.value().equals(Istring.class)) {
                throw new Exception("Base.update(): Field type mismatch for an IString");
            }
            ArrayList<IstringDummy> list = u.getIstringDummy();
            // dao.flush();
            for (IstringDummy i : list) {
                Istring istr = null;
                int userId = user != null ? user.getHistId() : 0;
                if (f.getInt(this) == 0) {
                    istr = Istring.create(dao, domain, user, this.getCID(), i.getText(), i.getLanguage());
                } else {
                    istr = Istring.saveOrUpdate(dao, domain, userId, f.getInt(this), i.getText(),
                            i.getLanguage(), i.getCountry(), this.getCID());
                }
                // only for CI make a reference form IString to the owner
                if (this instanceof CI) {
                    istr.setCI(this.getHistId());
                }
                if (f.getInt(this) == 0) {
                    f.setInt(this, istr.getHistId());
                    dao.update(this);
                }
            }
            break;
        }
        case ISTRING_DELETE: {
            DBkey a = f.getAnnotation(DBkey.class);
            if (a == null || !a.value().equals(Istring.class)) {
                throw new Exception("Base.update(): Field type mismatch for an IString");
            }
            ArrayList<IstringDummy> list = u.getIstringDummy();
            for (IstringDummy i : list) {
                if (i.getCountry() == null && i.getLanguage() == null) {
                    Istring.delete(dao, user, f.getInt(this));
                } else if (i.getCountry() == null && i.getLanguage() != null) {
                    Istring.delete(dao, user, f.getInt(this), i.getLanguage());
                } else if (i.getCountry() != null && i.getLanguage() != null) {
                    Istring.delete(dao, user, f.getInt(this), i.getLanguage(), i.getCountry());
                }
            }
            break;
        }
        case IMAGE: {
            DBkey a = f.getAnnotation(DBkey.class);
            if (a == null || !a.value().equals(Image.class)) {
                throw new Exception("Base.update(): Field type mismatch for an Image");
            }
            Image.update(dao, user, f.getInt(this), u.getUpdateMap());
            break;
        }
        case IMAGE_DUMMY: {
            DBkey a = f.getAnnotation(DBkey.class);
            if (a == null || !a.value().equals(Image.class)) {
                throw new Exception("Base.update(): Field type mismatch for an Image");
            }
            int id = f.getInt(this);
            if (id == 0) {
                ImageDummy idummy = u.getImageDummy();
                if (this instanceof User) {
                    User uuser = (User) this;
                    Image i = Image.create(dao, null, user, "userImage_" + Base.getRandomLong(),
                            idummy.getData(), idummy.getMimeType(), idummy.getLicense());
                    uuser.setImage(i.getHistId());
                } else if (this instanceof DBcategory) {
                    DBcategory cat = (DBcategory) this;
                    Image i = Image.create(dao, null, user, "catImage_" + Base.getRandomLong(),
                            idummy.getData(), idummy.getMimeType(), idummy.getLicense());
                    cat.setIcon(i.getHistId());
                } else {
                    throw new Exception("Base.update(): ImageDummy for this relationship is't defined");
                }
            } else {
                Image.update(dao, user, id, u.getImageDummy().getImageUpdateMap());
            }
            break;
        }
        case IMAGE_DELETE: {
            DBkey a = f.getAnnotation(DBkey.class);
            if (a == null || !a.value().equals(Image.class)) {
                throw new Exception("Base.update(): Field type mismatch for an Image");
            }
            Image.delete(dao, user, f.getInt(this));
            f.setInt(this, 0);
            break;
        }
        default:
            throw new Exception("Base.update(): Type not implemented!");
        }
    }
}

From source file:org.apache.geode.internal.InternalDataSerializer.java

private static void writeGemFireEnum(Enum<?> e, DataOutput out) throws IOException {
    boolean isGemFireObject = isGemfireObject(e);
    DataSerializer.writePrimitiveByte(isGemFireObject ? GEMFIRE_ENUM : PDX_INLINE_ENUM, out);
    DataSerializer.writeString(e.getDeclaringClass().getName(), out);
    DataSerializer.writeString(e.name(), out);
    if (!isGemFireObject) {
        InternalDataSerializer.writeArrayLength(e.ordinal(), out);
    }//from  ww w. j av  a  2 s  .co m
}

From source file:net.sf.jasperreports.engine.util.JRApiWriter.java

/**
 *
 *///  www .java 2s .  co  m
protected void write(String pattern, Enum<?> value, Enum<?> defaultValue) {
    if (value != null && value != defaultValue) {
        write(MessageFormat.format(pattern, new Object[] { value.getClass().getName() + "." + value.name() }));
    }
}

From source file:ca.oson.json.Oson.java

private <E> Enum<?> json2Enum(FieldData objectDTO) {
    objectDTO.valueToProcess = StringUtil.unquote(objectDTO.valueToProcess, isEscapeHtml());
    Object valueToProcess = objectDTO.valueToProcess;
    Class<E> returnType = objectDTO.returnType;
    boolean required = objectDTO.required();
    Enum defaultValue = (Enum) objectDTO.defaultValue;
    boolean json2Java = objectDTO.json2Java;

    if (returnType == null || valueToProcess == null) {
        if (required) {
            return defaultValue;
        }/*from  w  ww  . j  a v  a2s.c  o  m*/

        return null;
    }

    String value = (String) valueToProcess;

    Class<Enum> enumType = (Class<Enum>) returnType;

    try {
        Function function = objectDTO.getDeserializer();

        if (function != null) {

            Object returnedValue = null;

            if (function instanceof Json2DataMapperFunction) {
                DataMapper classData = new DataMapper(returnType, value, objectDTO.classMapper, objectDTO.level,
                        getPrettyIndentation());
                returnedValue = ((Json2DataMapperFunction) function).apply(classData);

            } else if (function instanceof Json2FieldDataFunction) {
                Json2FieldDataFunction f = (Json2FieldDataFunction) function;
                FieldData fieldData = objectDTO.clone();

                returnedValue = f.apply(fieldData);

            } else if (function instanceof Json2EnumFunction) {
                return (Enum<?>) ((Json2EnumFunction) function).apply(value);
            } else {
                returnedValue = function.apply(value);
            }

            if (returnedValue instanceof Optional) {
                returnedValue = ObjectUtil.unwrap(returnedValue);
            }

            if (returnedValue == null) {
                return null;
            }

            Class type = returnedValue.getClass();

            if (Enum.class.isAssignableFrom(type)) {
                return (Enum<?>) returnedValue;

            } else if (Number.class.isAssignableFrom(type)) {
                int ordinal = ((Number) returnedValue).intValue();

                for (Enum enumValue : enumType.getEnumConstants()) {
                    if (enumValue.ordinal() == ordinal) {
                        return enumValue;
                    }
                }

            } else {
                String name = returnedValue.toString();

                for (Enum enumValue : enumType.getEnumConstants()) {
                    if (enumValue.toString().equalsIgnoreCase(name)
                            || enumValue.name().equalsIgnoreCase(name)) {
                        return enumValue;
                    }
                }
            }

        }

    } catch (Exception ex) {
    }

    for (Method method : enumType.getDeclaredMethods()) {
        for (Annotation annotation : method.getDeclaredAnnotations()) {
            String aname = annotation.annotationType().getName();

            switch (aname) {
            case "com.fasterxml.jackson.annotation.JsonCreator":
            case "org.codehaus.jackson.annotate.JsonCreator":
                return ObjectUtil.getMethodValue(null, method, value);

            case "ca.oson.json.annotation.FieldMapper":
                ca.oson.json.annotation.FieldMapper fieldMapper = (ca.oson.json.annotation.FieldMapper) annotation;
                if (fieldMapper.jsonCreator() != null && fieldMapper.jsonCreator() == BOOLEAN.TRUE) {
                    return ObjectUtil.getMethodValue(null, method, value);
                }
            }
        }

    }

    String fieldName = null;
    for (Field field : enumType.getDeclaredFields()) {
        String name = null;
        ca.oson.json.annotation.FieldMapper fieldMapper = field
                .getAnnotation(ca.oson.json.annotation.FieldMapper.class);
        if (fieldMapper != null) {
            name = fieldMapper.name();

            if (value.equalsIgnoreCase(name)) {
                fieldName = field.getName();
                break;
            }

        } else {
            for (Annotation annotation : field.getAnnotations()) {
                name = ObjectUtil.getName(annotation);
                if (value.equalsIgnoreCase(name)) {
                    fieldName = field.getName();
                    break;
                }
            }
        }
    }
    if (fieldName != null) {
        try {
            return Enum.valueOf(enumType, fieldName.toUpperCase());
        } catch (IllegalArgumentException ex) {
        }
    }

    try {
        return Enum.valueOf(enumType, value.toUpperCase());
    } catch (IllegalArgumentException ex) {
    }

    for (Enum enumValue : enumType.getEnumConstants()) {
        if (enumValue.toString().equalsIgnoreCase(value) || enumValue.name().equalsIgnoreCase(value)) {
            return enumValue;
        }
    }

    FieldData fieldData = new FieldData(value, Integer.class, true);
    Integer ordinal = json2Integer(fieldData);

    if (ordinal != null) {
        for (Enum enumValue : enumType.getEnumConstants()) {
            if (enumValue.ordinal() == ordinal) {
                return enumValue;
            }
        }
    }

    return null;
}

From source file:ca.oson.json.Oson.java

private <E> String enum2Json(FieldData objectDTO) {
    Object value = objectDTO.valueToProcess;
    Class<E> valueType = objectDTO.returnType;
    EnumType enumType = objectDTO.getEnumType();

    if (value == null) {
        return null;
    }/*from w ww.j ava 2  s . c  o  m*/

    Enum en = (Enum) value;

    try {
        Function function = objectDTO.getSerializer();
        if (function != null) {
            try {
                if (function instanceof DataMapper2JsonFunction) {
                    DataMapper classData = new DataMapper(objectDTO.returnType, value, objectDTO.classMapper,
                            objectDTO.level, getPrettyIndentation());
                    return ((DataMapper2JsonFunction) function).apply(classData);

                } else if (function instanceof Enum2JsonFunction) {
                    return ((Enum2JsonFunction) function).apply(en);

                } else {

                    Object returnedValue = null;
                    if (function instanceof FieldData2JsonFunction) {
                        FieldData2JsonFunction f = (FieldData2JsonFunction) function;
                        FieldData fieldData = objectDTO.clone();
                        returnedValue = f.apply(fieldData);
                    } else {
                        returnedValue = function.apply(value);
                    }

                    if (returnedValue instanceof Optional) {
                        returnedValue = ObjectUtil.unwrap(returnedValue);
                    }

                    if (returnedValue == null) {
                        return null; // just ignore it, right?
                    } else if (Enum.class.isAssignableFrom(returnedValue.getClass())) {
                        en = (Enum) returnedValue;

                    } else {
                        objectDTO.valueToProcess = returnedValue;
                        return object2String(objectDTO);
                    }

                }

            } catch (Exception e) {
            }
        }
    } catch (Exception ex) {
    }

    String name = en.name();

    if (enumType == null || enumType == EnumType.STRING) {

        for (Method method : valueType.getDeclaredMethods()) {
            for (Annotation annotation : method.getDeclaredAnnotations()) {
                String aname = annotation.annotationType().getName();

                switch (aname) {
                case "com.fasterxml.jackson.annotation.JsonValue":
                case "org.codehaus.jackson.annotate.JsonValue":
                    return ObjectUtil.getMethodValue(en, method);

                case "ca.oson.json.annotation.FieldMapper":
                    ca.oson.json.annotation.FieldMapper fieldMapper = (ca.oson.json.annotation.FieldMapper) annotation;
                    if (fieldMapper.jsonValue() != null && fieldMapper.jsonValue() == BOOLEAN.TRUE) {
                        return ObjectUtil.getMethodValue(en, method);
                    }
                }
            }

        }

        for (Field field : valueType.getDeclaredFields()) {
            if (name.equalsIgnoreCase(field.getName())) {
                ca.oson.json.annotation.FieldMapper fieldMapper = field
                        .getAnnotation(ca.oson.json.annotation.FieldMapper.class);
                if (fieldMapper != null) {
                    String aname = fieldMapper.name();
                    if (!StringUtil.isEmpty(aname)) {
                        return aname;
                    }

                } else {
                    for (Annotation annotation : field.getAnnotations()) {
                        String aname = ObjectUtil.getName(annotation);
                        if (!StringUtil.isEmpty(aname)) {
                            return aname;
                        }
                    }
                }
            }
        }

        return name;
    }

    switch (enumType) {
    case STRING:
        return en.name();
    case ORDINAL:
    default:
        return "" + en.ordinal();
    }
}