Is it possible to define something like this in java?
C# code:
public enum Character
{
A = 1,
B = 2,
C = 4,
D = 8
}
...
Character ch = /* from user */
if(ch & Character.A)
{
// some operation...
}
for example if ch
set to Character.B
then result of if
will be false
:
ch = 00000000 00000000 00000000 00000010
A = 00000000 00000000 00000000 00000001
------------------------------------------
& 00000000 00000000 00000000 00000000
I want to implement something like that! Is it ever possible in Java?
Enums don't have methods for iteration, like forEach() or iterator(). Instead, we can use the array of the Enum values returned by the values() method.
Numeric EnumNumeric enums are number-based enums i.e. they store string values as numbers. Enums are always assigned numeric values when they are stored. The first value always takes the numeric value of 0, while the other values in the enum are incremented by 1.
Use the len() class to get the number of elements in an enum, e.g. len(Color) . The len() function returns the length (the number of items) of an object and can directly be passed an enum.
Get the value of an Enum To get the value of enum we can simply typecast it to its type. In the first example, the default type is int so we have to typecast it to int. Also, we can get the string value of that enum by using the ToString() method as below.
Well, you can nearly do that:
public enum CharEnum // Let's avoid java.lang.* clashes
{
A(1), B(2), C(4), D(8);
private final int mask;
private CharEnum(int mask)
{
this.mask = mask;
}
public int getMask()
{
return mask;
}
}
Then:
CharEnum ch = /* from user */
if ((ch.getMask() & CharEnum.A.getMask()) > 0)
{
// some operation...
}
This can be useful in some situations, but as Michael said you should definitely look at EnumSet
for general "set of values" options.
If you do decide to go for an enum with values, you can put extra logic within the enum itself:
public enum CharEnum
{
// Other code as before
public boolean overlapsWith(CharEnum ch)
{
return (ch.mask & mask) > 0;
}
}
(Of course, choosing Character
as the name of your enum is very confusing as it creates a conflict with java.lang.Character
which is automatically imported into every compilation unit)
You can use the ordinal() method to get the integer associated with an enum constant. Anyway, I got a feeling that what you are trying to achieve may be realized by enum-defined methods. I mean, Java's enum are more powerful than C/C++'s because you can associate behavior with them.
public enum Chr {
A {
@Override public void doSomething(int c) {
// some operation ...
}
},
B,
C,
D;
public void doSomething(int c) {
// some default behavior
}
}
Following your comment:
You can define a field which will hold this value, and have the constructor initialize it, as follows:
public enum Chr {
A(1),
B(2),
C(4),
D(8)
;
public Chr(int n) { value = n; }
public final int value;
}
Character ch = ... // from user
if(ch & Chr.A.value) {
...
}
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