Pick a random value from an enum?

The only thing I would suggest is caching the result of values() because each call copies an array. Also, don’t create a Random every time. Keep one. Other than that what you’re doing is fine. So: public enum Letter { A, B, C, //… private static final List<Letter> VALUES = Collections.unmodifiableList(Arrays.asList(values())); private static final int … Read more

Implementing Singleton with an Enum (in Java)

This, public enum MySingleton { INSTANCE; } has an implicit empty constructor. Make it explicit instead, public enum MySingleton { INSTANCE; private MySingleton() { System.out.println(“Here”); } } If you then added another class with a main() method like public static void main(String[] args) { System.out.println(MySingleton.INSTANCE); } You would see Here INSTANCE enum fields are compile … Read more

Most common C# bitwise operations on enums

I did some more work on these extensions – You can find the code here I wrote some extension methods that extend System.Enum that I use often… I’m not claiming that they are bulletproof, but they have helped… Comments removed… namespace Enum.Extensions { public static class EnumerationExtensions { public static bool Has<T>(this System.Enum type, T … Read more

Can a C++ enum class have methods?

No, they can’t. I can understand that the enum class part for strongly typed enums in C++11 might seem to imply that your enum has class traits too, but it’s not the case. My educated guess is that the choice of the keywords was inspired by the pattern we used before C++11 to get scoped … Read more

Map enum in JPA with fixed values?

For versions earlier than JPA 2.1, JPA provides only two ways to deal with enums, by their name or by their ordinal. And the standard JPA doesn’t support custom types. So: If you want to do custom type conversions, you’ll have to use a provider extension (with Hibernate UserType, EclipseLink Converter, etc). (the second solution). … Read more

How to remove an item for a OR’d enum?

You need to & it with the ~ (complement) of ‘BLUE’. The complement operator essentially reverses or ‘flips’ all bits for the given data type. As such, if you use the AND operator (&) with some value (let’s call that value ‘X’) and the complement of one or more set bits (let’s call those bits … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)