enums

Enum-aware ServiceLoader implementation?

孤街醉人 提交于 2020-05-12 06:57:49
问题 I would like to be able to indicate an enum type as an interface implementation and then load all enums as separate instances/implementations of the interface via the ServiceLoader API. An example of this use case would be to allow downstream users of my API to specify custom values, but provide an enum with standard/common implementations. My interface only requires a String name() , so any enum implements it already. For example, the CopyOption interface in the Java NIO APIs, with the

Enum-aware ServiceLoader implementation?

泪湿孤枕 提交于 2020-05-12 06:57:21
问题 I would like to be able to indicate an enum type as an interface implementation and then load all enums as separate instances/implementations of the interface via the ServiceLoader API. An example of this use case would be to allow downstream users of my API to specify custom values, but provide an enum with standard/common implementations. My interface only requires a String name() , so any enum implements it already. For example, the CopyOption interface in the Java NIO APIs, with the

PostgreSQL INSERT into an array of enums

谁说胖子不能爱 提交于 2020-05-10 07:35:25
问题 How can I insert an array of enums ? Here is my enum : CREATE TYPE equipment AS ENUM ('projector','PAsystem','safe','PC','phone'); Then my table has an array of equipment: CREATE TABLE lecture_room ( id INTEGER DEFAULT NEXTVAL('lecture_id_seq') , seatCount int , equipment equipment[] ) INHERITS(venue); Here is my ATTEMPT to INSERT: INSERT INTO lecture_room (building_code, floorNo, roomNo, length, width , seatCount, equipment) VALUES ('IT', 4, 2, 10, 15 ,120, ARRAY['projector','PAsystem','safe

Removing enum flags

萝らか妹 提交于 2020-05-09 11:53:01
问题 I'm a little bit puzzled by the removal of enum flags to be perfectly honest. Let me make an example, let's say we have an enum that looks like this [Flags] enum Letter { A = 1, // 1 B = 2, // 10 C = 4 // 100 } Now if I want to make a variable hold the flags Letter.AB I could do foo = Letter.A | Letter.B . Now this makes sense to me, I can calculate this and it will make sense: 01 OR 10 = 11 = 3 = 1 + 2 = A + B = AB When it comes to removing flags, I am puzzled however, intuitively what I

Removing enum flags

泪湿孤枕 提交于 2020-05-09 11:52:05
问题 I'm a little bit puzzled by the removal of enum flags to be perfectly honest. Let me make an example, let's say we have an enum that looks like this [Flags] enum Letter { A = 1, // 1 B = 2, // 10 C = 4 // 100 } Now if I want to make a variable hold the flags Letter.AB I could do foo = Letter.A | Letter.B . Now this makes sense to me, I can calculate this and it will make sense: 01 OR 10 = 11 = 3 = 1 + 2 = A + B = AB When it comes to removing flags, I am puzzled however, intuitively what I

Update / change the rawValue of a enum in Swift

﹥>﹥吖頭↗ 提交于 2020-05-08 09:29:08
问题 Taking the below enum for instance enum Name : String { case Me = "Prakash" case You = "Raman" } Can I do the following Change the raw value of one "case" to something else. Name.Me = "Prak" Add a new case to the ENUM Name.Last = "Benjamin" Thanks! 回答1: Short answer: No, you can't. Enumeration types are evaluated at compile time. It's not possible to change raw values nor to add cases at runtime. The only dynamic behavior is using associated values. Reference: Swift Language Guide:

How to get valueOf & values of an Enum and call methods on an interface it implements when defined as a generic class param

房东的猫 提交于 2020-04-30 07:14:27
问题 I want to convert String s to Enum s for a set of enum types. I use an interface to mark what set of enums to convert and define a couple of methods used during conversion: public interface SymbolEnum { String getCode(); String getDescription(); } Here's an example of one of the Enum types that I want to derive from a String : @RequiredArgsConstructor // using lombok public enum Element implements SymbolEnum { IRON("FE", "iron"), HYDROGEN("H", "hydrogen"), @Getter private final String code;

How to get valueOf & values of an Enum and call methods on an interface it implements when defined as a generic class param

三世轮回 提交于 2020-04-30 07:14:13
问题 I want to convert String s to Enum s for a set of enum types. I use an interface to mark what set of enums to convert and define a couple of methods used during conversion: public interface SymbolEnum { String getCode(); String getDescription(); } Here's an example of one of the Enum types that I want to derive from a String : @RequiredArgsConstructor // using lombok public enum Element implements SymbolEnum { IRON("FE", "iron"), HYDROGEN("H", "hydrogen"), @Getter private final String code;

sealed class vs enum when using associated type

*爱你&永不变心* 提交于 2020-04-29 08:58:22
问题 I'd like to create a color object based on an Int . I can achieve the same result using sealed class and enum and was wondering if one is better than the other. Using sealed class : sealed class SealedColor(val value: Int) { class Red : SealedColor(0) class Green : SealedColor(1) class Blue : SealedColor(2) companion object { val map = hashMapOf( 0 to Red(), 1 to Green(), 2 to Blue() ) } } val sealedColor: SealedColor = SealedColor.map[0]!! when (sealedColor) { is SealedColor.Red -> print(

How to sort enum using a custom order attribute?

孤街浪徒 提交于 2020-04-29 07:34:26
问题 I have an enum like this: enum MyEnum{ [Order(1)] ElementA = 1, [Order(0)] ElementB = 2, [Order(2)] ElementC = 3 } And I want to list its elements sorted by a custom order attribute I wrote, so that I get a list of items sorted. I am getting the Description Attribute but just for one element like this: FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); It may be