enums

Are enums allowed to have setters in Java?

喜你入骨 提交于 2020-08-26 21:29:53
问题 I have an enum and it has a parameter (field) that is a String . Am I allowed to have a setter for this field? public enum Blah { Monday("a"), Tuesday("b"); } private final String letter; Blah(String letter){ this.letter = letter; } Am I allowed to do the following? public String setLetter(String letter){ this.letter = letter; } 回答1: You need to remove the final modifier of the field in order for it to be settable: public enum Blah { Monday("a"), Tuesday("b"); private String letter; private

The static field should be accessed in a static way

廉价感情. 提交于 2020-08-26 08:04:22
问题 I have different Exception category Enum as below public enum GSBBCacheCategory { SEARCH(9001), UPDATE_PERSECURITY(9002), CROSS_REFERENCING_PERSECURITY(9003), METADATA_SEARCH(9004), REMOVEALL(9005), UPDATE_BACKOFFICE(9002); private int exceptionCode; GSBBCacheCategory(int exceptionCode) { this.exceptionCode = exceptionCode; } public int getExceptionCode() { return exceptionCode; } } public enum GSBBEncryptionCategory { . . . } I want to provide one place to access these Enum in client code.

The static field should be accessed in a static way

北城余情 提交于 2020-08-26 08:04:08
问题 I have different Exception category Enum as below public enum GSBBCacheCategory { SEARCH(9001), UPDATE_PERSECURITY(9002), CROSS_REFERENCING_PERSECURITY(9003), METADATA_SEARCH(9004), REMOVEALL(9005), UPDATE_BACKOFFICE(9002); private int exceptionCode; GSBBCacheCategory(int exceptionCode) { this.exceptionCode = exceptionCode; } public int getExceptionCode() { return exceptionCode; } } public enum GSBBEncryptionCategory { . . . } I want to provide one place to access these Enum in client code.

Multiple enum implementing protocols questions

十年热恋 提交于 2020-08-25 09:20:33
问题 I defined enums as confirming to a protocol Eventable: protocol Eventable { var name: String { get } static var all: [Eventable] { get } } enum MyEnum: String, Eventable { case bla = "bla" case blu = "blu" var name: String { return self.rawValue } static var all: [Eventable] { return [ MyEnum.bla, MyEnum.blu ] } } I have other enums like MyEnum also under the form: enum Bla: String, Eventable { } I have two questions: for the enums having a String data type, I would like to avoid duplicating

Generic BiFunction that accepts generic wildcard

徘徊边缘 提交于 2020-08-24 04:22:06
问题 I have Enum and want to have a generic function that could accept any class that implements a maker interface. public interface IComponentDataExporter { // Marker Interface } public class ComponentsDataExporter implements IComponentDataExporter { public ComponentTeaser populateFeatured(ComponentTeaserConfiguration componentConfig) { return null; } } public class BussinessComponentsDataExporter implements IComponentDataExporter { // methods } public enum ComponentTypeEnum { FEATURED

JPA AttributeConverter not honored in a Spring-data/hibernate query when used in a composite key

这一生的挚爱 提交于 2020-08-22 05:30:52
问题 This is my enum: public enum FooBarType { Foo, Bar; @javax.persistence.Converter public static class Converter implements AttributeConverter<FooBarType, String> { @Override public String convertToDatabaseColumn(FooBarType t) { return t.toString(); } @Override public FooBarType convertToEntityAttribute(String s) { for (FooBarType value : FooBarType.values()) { if (value.name().equalsIgnoreCase(s)) { return value; } } throw new IllegalArgumentException("Invalid value for enum: " + s); } } }

Should 3.4 enums use UPPER_CASE_WITH_UNDERSCORES?

筅森魡賤 提交于 2020-08-21 07:03:10
问题 As the documentation says, an enumeration is a set of symbolic names (members) bound to unique, constant values. The PEP8 says that constants are usually named as UPPER_CASE , should I use this notation in Python 3.4 enums? If yes, why the examples in the docs are using lower_case ? 回答1: Update The BDFL (Benevolent Dictator For Life) has spoken, and the Enum documentation has changed to reflect all upper-case member names. The examples in the [previous] docs are lower-case primarily because

Is it possible to add custom properties to c# enum object?

陌路散爱 提交于 2020-08-20 05:42:46
问题 Using c# Is it possible using to associate properties for each enum items? I have used the Description Attribute to add English description to an enum item. To add English description to each item I have done the following public enum MyEnum { [Description("My First Item")] First, [Description("My Second Item")] Second, [Description("My Third Item")] Third } Then I added an extension method to my enum called GetDescription() which allows me to get the description like so public static string

Is it possible to add custom properties to c# enum object?

馋奶兔 提交于 2020-08-20 05:41:43
问题 Using c# Is it possible using to associate properties for each enum items? I have used the Description Attribute to add English description to an enum item. To add English description to each item I have done the following public enum MyEnum { [Description("My First Item")] First, [Description("My Second Item")] Second, [Description("My Third Item")] Third } Then I added an extension method to my enum called GetDescription() which allows me to get the description like so public static string

Java Enum linking to another Enum

♀尐吖头ヾ 提交于 2020-08-19 12:17:11
问题 I would like to have enumerator in Java having other enum as attribute. public enum Direction { Up(Down), Down(Up), Left(Right), Right(Left); private Direction opposite; Direction(Direction opposite){ this.opposite = opposite; } } So I have different Direction, and for each I want to know the opposite. It is working fine for Down and Right, but I can't initialize Up because Down is not known yet (same fort Left). How can I edit enum variables after initialisation ? 回答1: One of the possible