Because there is only one instance of each enum constant, it is permissible to use the == operator in place of the equals method when comparing two object references if it is known that at least one of them refers to an enum constant.
The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared.
Creation of Enum instance is thread-safeBy default, the Enum instance is thread-safe, and you don't need to worry about double-checked locking. In summary, the Singleton pattern is the best way to create Singleton in Java 5 world, given the Serialization and thread-safety guaranteed and with some line of code enum.
In Java (from 1.5), enums are represented using enum data type. Java enums are more powerful than C/C++ enums. In Java, we can also add variables, methods, and constructors to it. The main objective of enum is to define our own data types(Enumerated Data Types).
Maybe use something like EnumSet?
if (EnumSet<Mood>.of(HAPPY, OPTIMISTIC, ENERGETIC).contains(currentMood))
{
//Do stuff...
}
Give your enums an int attribute.
enum Mood {
private int id;
HAPPY(1), SAD(2), CALM(4), SLEEPY(8), OPTIMISTIC(16), PENSIVE(32), ENERGETIC(64);
Mood( int id ) {
this.id = id;
}
}
On the if, test against this atrribute.
if ( currentMood & ( HAPPY.id | OPTIMISTIC.id | ENERGETIC.id ) != 0 ) { ... }
Apart from other answers: if that particular set of states has a semantically defined meaning in your application (rather independent of your particular method, specific to the Enum) , I'd recommend to included that test as a method in the same Enum class.
enum Mood {
HAPPY, SAD...
public boolean isRatherPositive() {
....
}
}
(or a static method) to be implemented by any of the logic suggested by the other answers (I'd favour the EnumSet, which could be statically o lazily instantiated).
To have the method in the Enum is useful to avoid code duplication, but above all, to prevent bugs in case you add members to the Enum or modify it.
Yes
switch(currentmood){
case HAPPY:
case OPTIMISTIC:...
Then do this
break;
default://else
}
You could give your enum a boolean flag and test for that.
enum Mood {
HAPPY(true), SAD, CALM, SLEEPY, OPTIMISTIC(true), PENSIVE, ENERGETIC(true);
final boolean good;
Mood(){this(false);}
Mood(boolean isgood){this.good = isgood;}
public isGood(){return good;}
}
This way the check is short
if (currentMood.isGood()) {}
One approach is to have EnumSet/switch/if etc. This makes sense if you are classifying the enumeration in some way that is not intrinsically part of the enumeration itself. For example, whether a color is part of the national flag's color. The 'dependency model' is that national flag depends on Color, but Color doesn't care about the existence of national flags.
On the other hand, if you are trying to determine whether a color is a primary color or not (intrinsic to human model of color), then it belongs on the enum class itself. So,
enum Color {
private final boolean _crtPrimary;
Red(true), Green(true), Blue(true), Black(false)
Color(boolean crtPrimary) {
_crtPrimary = crtPrimary;
}
public boolean isCRTPrimary() {
return _crtPrimary;
}
}
You can use YourEnum.values()
For example, I have Class called Enumerations. In it, I have multiple Enum Classes.
For example:
public class Enumerations {
public enum smtg
{
Fast(1),
Slow(2);
private int number;
private smtg(final int num) { number = num; }
public int getsmtg()
{
return number;
}
}
......
}
When I want to use bitwise, I just use it like that:
Enumerations.smtg.values()[Enumerations.smtg.Slow.ordinal() | Enumerations.smtg.Fast.ordinal()]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With