Java Enum getDeclaringClass vs getClass

Java enum values are permitted to have value-specific class bodies, e.g. (and I hope this syntax is correct…) public enum MyEnum { A { void doSomething() { … } }, B { void doSomethingElse() { … } }; } This will generate inner classes representing the class bodies for A and B. These inner classes … Read more

MVC 5.1 Razor DisplayFor not working with Enum DisplayName

Create new folder Views/Shared/DisplayTemplates Add empty Partial View named Enum, to the folder Replace Enum View code with: @model Enum @if (EnumHelper.IsValidForEnumHelper(ViewData.ModelMetadata)) { // Display Enum using same names (from [Display] attributes) as in editors string displayName = null; foreach (SelectListItem item in EnumHelper.GetSelectList(ViewData.ModelMetadata, (Enum)Model)) { if (item.Selected) { displayName = item.Text ?? item.Value; } … Read more

Why Java does not allow overriding equals(Object) in an Enum?

Anything but return this == other would be counter intuitive and violate the principle of least astonishment. Two enum constants are expected to be equal if and only if they are the same object and the ability to override this behavior would be error prone. Same reasoning applies to hashCode(), clone(), compareTo(Object), name(), ordinal(), and … Read more

Is it possible to write a generic enum converter for JPA?

Based on @scottb solution I made this, tested against hibernate 4.3: (no hibernate classes, should run on JPA just fine) Interface enum must implement: public interface PersistableEnum<T> { public T getValue(); } Base abstract converter: @Converter public abstract class AbstractEnumConverter<T extends Enum<T> & PersistableEnum<E>, E> implements AttributeConverter<T, E> { private final Class<T> clazz; public AbstractEnumConverter(Class<T> … Read more

looping through enum values

Given enum Foo {Bar=0,Baz,…,Last}; you can iterate the elements like: for(int i=Bar; i<=Last; i++) { … } Note that this exposes the really-just-an-int nature of a C enum. In particular, you can see that a C enum doesn’t really provide type safety, as you can use an int in place of an enum value and … Read more

Why use the Bitwise-Shift operator for values in a C enum definition?

Maybe writing the values in hexadecimal (or binary) helps 🙂 enum { kCGDisplayBeginConfigurationFlag = (1 << 0), /* 0b0000000000000001 */ kCGDisplayMovedFlag = (1 << 1), /* 0b0000000000000010 */ kCGDisplaySetMainFlag = (1 << 2), /* 0b0000000000000100 */ kCGDisplaySetModeFlag = (1 << 3), /* 0b0000000000001000 */ kCGDisplayAddFlag = (1 << 4), /* 0b0000000000010000 */ kCGDisplayRemoveFlag = (1 … Read more

How to save enum field in the database room?

You can make a convert to each enum, like this: class Converters { @TypeConverter fun toHealth(value: String) = enumValueOf<Health>(value) @TypeConverter fun fromHealth(value: Health) = value.name } Or if you prefer store it as SQL integer, you can use ordinal too: class Converters { @TypeConverter fun toHealth(value: Int) = enumValues<Health>()[value] @TypeConverter fun fromHealth(value: Health) = value.ordinal … Read more