enums

Using attributes to cut down on enum to enum mapping and enum/const to action switch statments

徘徊边缘 提交于 2020-02-03 04:55:12
问题 I imagine everyone has seen code like: public void Server2ClientEnumConvert( ServerEnum server) { switch(server) { case ServerEnum.One: return ClientEnum.ABC //And so on. Instead of this badness we could do somthing like: public enum ServerEnum { [Enum2Enum(ClientEnum.ABC)] One, } Now we can use reflection to rip through ServerEnum and get the conversion mappings from the enum declaration itself. The problem I am having here is in the declaration of the Enum2Enum attribute. This works but

Retrieve enum value using unique property

霸气de小男生 提交于 2020-02-02 14:45:31
问题 Is it possible to create a generic method or class to retrieve an enum value from a given unique property (field with getter method)? So you would have: public enum EnumToMap { E1("key1"), E2("key2"), ; private final String key; private EnumToMap(String key) { this.key = key; } public String getKey() { return key; } } so required is the same functionality as public static EnumToMap getByKey(String key) ... would provide. Preferably without reflection and as generic as possible (but in this

Retrieve enum value using unique property

不羁岁月 提交于 2020-02-02 14:45:10
问题 Is it possible to create a generic method or class to retrieve an enum value from a given unique property (field with getter method)? So you would have: public enum EnumToMap { E1("key1"), E2("key2"), ; private final String key; private EnumToMap(String key) { this.key = key; } public String getKey() { return key; } } so required is the same functionality as public static EnumToMap getByKey(String key) ... would provide. Preferably without reflection and as generic as possible (but in this

enum and singletons - top level vs nested enum

£可爱£侵袭症+ 提交于 2020-02-02 08:40:52
问题 As is now well known the recommended way for creating a singleton in java is via an enum (see for instance here) But (for example in this answer) it seems to be considered (by @MikeAdler who replied to me in the comments) the right thing to have the enum in the singleton class (see for instance here for a full example, or the code given below). I do not seem to really understand the need/use of this - can someone please elaborate (and preferably give the correct dialect for this idiom) ?

enum and singletons - top level vs nested enum

邮差的信 提交于 2020-02-02 08:39:07
问题 As is now well known the recommended way for creating a singleton in java is via an enum (see for instance here) But (for example in this answer) it seems to be considered (by @MikeAdler who replied to me in the comments) the right thing to have the enum in the singleton class (see for instance here for a full example, or the code given below). I do not seem to really understand the need/use of this - can someone please elaborate (and preferably give the correct dialect for this idiom) ?

Storing Enum Values with JPA

不羁岁月 提交于 2020-02-02 04:23:08
问题 Say I have an enum: public enum NotificationType { Store("S"), Employee("E"), Department("D"), All("A"); public String value; NotificationType(String value) { this.value = value; } } I want to store S or E rather than Store or Employee in the database. Currently, I've mapped it in the entity as follows: @Enumerated(EnumType.STRING) private NotificationType notificationType; but unsure how to get what I want, if possible. 回答1: You can declare own user type to do the conversion between the

How to implement Swift-like enums with associated values in JavaScript?

爱⌒轻易说出口 提交于 2020-02-01 12:22:09
问题 The Swift language has a fantastic enum support. Not only can one define a standard enum with cases, but cases can have optional values "associated to them." For example, taken from the Swift docs: enum Barcode { case UPCA(Int, Int, Int, Int) case QRCode(String) case Other } Such that one could create a Barcode enum by passing in a value, like so: var productBarcode = Barcode.UPCA(8, 85909, 51226, 3) and also switch on productBarcode at a later date to retrieve the associated value (a tuple

How to implement Swift-like enums with associated values in JavaScript?

与世无争的帅哥 提交于 2020-02-01 12:17:06
问题 The Swift language has a fantastic enum support. Not only can one define a standard enum with cases, but cases can have optional values "associated to them." For example, taken from the Swift docs: enum Barcode { case UPCA(Int, Int, Int, Int) case QRCode(String) case Other } Such that one could create a Barcode enum by passing in a value, like so: var productBarcode = Barcode.UPCA(8, 85909, 51226, 3) and also switch on productBarcode at a later date to retrieve the associated value (a tuple

Boxed nullable underlying type can be cast to enum but boxed enum type can't be cast to nullable type

二次信任 提交于 2020-02-01 04:40:06
问题 Boxed nullable underlying type can be cast to enum but boxed enum type can't be cast to nullable type. And similarly, Boxed nullable enum can be cast to underlying type but boxed underlying type can't be cast to nullable enum. Ok, I know "boxed nullable type" is not the best way to describe it, but it's for the sake of the question. I'm aware it's the underlying value type that's getting boxed. I will show it with examples. Assume I have an enum with int as the underlying type. enum Sex {

Assigning an @Annotation enum a value

▼魔方 西西 提交于 2020-01-31 07:07:10
问题 I created enum Restrictions{ none, enumeration, fractionDigits, length, maxExclusive, maxInclusive, maxLength, minExclusive, minInclusive, minLength, pattern, totalDigits, whiteSpace; public Restrictions setValue(int value){ this.value = value; return this; } public int value; } So that I could happily do something like this, which is perfectly legal syntax. Restrictions r1 = Restrictions.maxLength.setValue(64); The reason being is, I am using enum to restrict the type of restriction that