enums

Comparison of enums values() method and class.getEnumConstants()

懵懂的女人 提交于 2021-02-18 19:33:38
问题 I'm trying to compare this two ways to achieving the enums values (with and without reflection). So this is my test class: public class ReflectionOnEnumsTests2 { enum TestEnum { ONE, TWO, THREE; } public static void main(String[] args) { long n = 600_000_000; int stub; //test without Reflection long timeStartWithoutReflection = System.currentTimeMillis(); for (int i = 0; i < n; i++){ TestEnum[] values = TestEnum.values(); stub = values.length; } System.out.println("Time consuming with

How to re-use duplicate code with Enums in Java 8?

心已入冬 提交于 2021-02-18 19:26:07
问题 I have 3 enumerator classes in my application. All 3 classes have 2 duplicate methods that we want available in every enum that we implement. public static List<String> supported(){ return Arrays.asList([[EnumClass]].values()) .stream().map(Enum::name).collect(Collectors.toList()); } public static boolean contains(String value){ boolean response = false; try { response = value != null ? [[EnumClass]].valueOf(value.trim().toUppercase()) != null : false; } catch (Exception e){ LOGGER.error(

Highest ordinal enum value

懵懂的女人 提交于 2021-02-18 18:47:25
问题 I'm looking to compute the highest ordinal enum value from a list of enum properties in a list of beans. For example, I have: @Data public class MyBean { private Priority priority; } and public enum Priority { URGENT("Urgent"), HIGH("High"), MEDIUM("Medium"), LOW("Low"); @Getter private final String value; Priority(String value) { this.value = value; } @Override public String toString() { return getValue(); } } If I have a List of MyBeans , how can I find the max ordinal value of the bean's

Highest ordinal enum value

≡放荡痞女 提交于 2021-02-18 18:46:39
问题 I'm looking to compute the highest ordinal enum value from a list of enum properties in a list of beans. For example, I have: @Data public class MyBean { private Priority priority; } and public enum Priority { URGENT("Urgent"), HIGH("High"), MEDIUM("Medium"), LOW("Low"); @Getter private final String value; Priority(String value) { this.value = value; } @Override public String toString() { return getValue(); } } If I have a List of MyBeans , how can I find the max ordinal value of the bean's

Convert parametized Enum to Enumerated Annotation in android

 ̄綄美尐妖づ 提交于 2021-02-18 12:27:20
问题 I have a question regarding to the andriod @IntDef Annotation. I know that in its basic usage, it should replace the enum . But what if I have a parameterized enum with multiple hardwired values for example public enum MyEnum { YES(true, 1), NO(false, 0); private boolean boolState; private boolean intState; MyEnum(boolean boolState, int intState) { this.boolState = boolState; this.intState = intState; } public boolean getBoolState() { return boolState; } public int getIntState() { return

Syntax error: insert “enum Identifier”, insert “EnumBody”, inset “}”

▼魔方 西西 提交于 2021-02-18 11:57:05
问题 I coded an enum type which brings up the following Syntax errors when I run my created JUnit test for it: java.lang.Error: Unresolved compilation problems: Syntax error, insert "enum Identifier" to complete EnumHeaderName Syntax error, insert "EnumBody" to complete EnumDeclaration Syntax error, insert "}" to complete ClassBody My enum type has static functions which for a particular String, returns an enum constant. Here is some of my code of the enum type: public enum MusicType { ACCIDENTAL,

Generic enum JPA AttributeConverter implementation

╄→гoц情女王★ 提交于 2021-02-18 07:39:28
问题 Problem I am trying to solve I am trying to implement enum mapping for Hibernate. So far I have researched available options, and both the @Enumerated(EnumType.ORDINAL) and @Enumerated(EnumType.STRING) seemed inadequate for my needs. The @Enumerated(EnumType.ORDINAL) seems to be very error-prone, as a mere reordering of enum constants can mess the mapping up, and the @Enumerated(EnumType.STRING) does not suffice too, as the database I work with is already full of values to be mapped, and

Hierarchical enum in Java

核能气质少年 提交于 2021-02-18 05:12:04
问题 Let's say I have a structure like this: Is it possible to create an enum that will return the string value of selected cell? For example: enum.GROUP_MAIN1.SUBGROUP1.COL1 will return value "COL1" . I was looking for nested enums but didn't find the solution to this situation. 回答1: You can do this with such trick: public interface GROUPMAIN1 { enum SUBGROUP1 implements GROUPMAIN1 { COL1, COL2, COL3 } enum SUBGROUP2 implements GROUPMAIN1 { COL3, COL4 } } So to get enum you will need to use

Hierarchical enum in Java

断了今生、忘了曾经 提交于 2021-02-18 05:06:08
问题 Let's say I have a structure like this: Is it possible to create an enum that will return the string value of selected cell? For example: enum.GROUP_MAIN1.SUBGROUP1.COL1 will return value "COL1" . I was looking for nested enums but didn't find the solution to this situation. 回答1: You can do this with such trick: public interface GROUPMAIN1 { enum SUBGROUP1 implements GROUPMAIN1 { COL1, COL2, COL3 } enum SUBGROUP2 implements GROUPMAIN1 { COL3, COL4 } } So to get enum you will need to use

Hierarchical enum in Java

喜欢而已 提交于 2021-02-18 05:06:00
问题 Let's say I have a structure like this: Is it possible to create an enum that will return the string value of selected cell? For example: enum.GROUP_MAIN1.SUBGROUP1.COL1 will return value "COL1" . I was looking for nested enums but didn't find the solution to this situation. 回答1: You can do this with such trick: public interface GROUPMAIN1 { enum SUBGROUP1 implements GROUPMAIN1 { COL1, COL2, COL3 } enum SUBGROUP2 implements GROUPMAIN1 { COL3, COL4 } } So to get enum you will need to use