Reputation: 819
I've got enums
UNDEFINED(-1),
FIS(0),
MANUELL(1)
defined as
public enum Ausloesungsart { UNDEFINED( -1), FIS( 0), MANUELL( 1); }
however at runtime i'm adding another enum if it's not contained in the list as UNDEFINED
with the parsed code, as in 123
.
Here is how I take the Enum:
public static Ausloesungsart fromIdentifier(Integer code) {
if (enumsByIdentifier.containsKey(code)) {
return enumsByIdentifier.get(code);
} else {
enumsByIdentifier.put(code, Ausloesungsart.UNDEFINED);
return enumsByIdentifier.get(code);
}
}
by now the list should contain
UNDEFINED(-1),
FIS(0),
MANUELL(1),
UNDEFINED(123)
when parsing the file it obviously sets the value 88 and searches for it. However it returns -1 in the end on the frontend.
It never even jumps into the else clausel, somehow it's already in?
Any idea what I'm missing?
Where the parsing happens:
Ausloesung ausloesung = new Ausloesung(Ausloesungsart.fromIdentifier(header.getReleaseType()));
at this point, releaseType
is 123
but in the end there is -1 = Undefined
instead of
123 = Undefined
in the database and frontend.
Any Status not in the enumeration should be displayed in the frontend as in
<CODE> = Undefined
Currently the message is built with a property attribute:
my.properties:
Ausloesungsart.UNDEFINED= {0} \= Unbekannt
Ausloesungsart.FIS=0 \= FIS
Ausloesungsart.MANUELL=1 \= Manuell
Upvotes: 2
Views: 868
Reputation: 131456
however at runtime i'm adding another enum if it's not contained in the list as UNDEFINED with the parsed code, as in 123.
Enums are designed to represent immutable things.
You cannot add a new enum value at runtime and you should not try to modify the actual either.
However it returns -1 in the end on the frontend.
Associating an integer value to an enum value in a map will not modify the state of the enum itself.
If multiple integer values may be associated to UNDEFINED
enum value and that these integer values are not specified at compile time, you should probably not use the enum field to set this information.
It doesn't mean that the UNDEFINED
enum could not be associated to multiple numeric values. But this should not done directly in the state itself of the enum values.
You could use a custom class that provides the two information : the String
placeholder and the numeric code.
You could introduce a static method in Ausloesungsart
that returns it.
public static getAusloesungsartWithValue(Integer code){
for (Ausloesungsart current : values()){
if (current.numericValue.equals(code)){
return new AusloesungsartWithValue(current, code);
}
}
return new AusloesungsartWithValue(UNDEFINED, code);
}
Then call it :
AusloesungsartWithValue value = Ausloesungsart.getAusloesungsartWithValue(123); // UNDEFINED
AusloesungsartWithValue otherValue = Ausloesungsart.getAusloesungsartWithValue(0); // FIS
Upvotes: 1
Reputation: 15008
What do you mean "it returns -1 in the end on the frontend". You probably implemented it is like this:
enum Ausloesungsart {
UNDEFINED(-1),
FIS(0),
MANUELL(1);
private int value;
public void getValue() { return value; }
}
Now you can put these enum items into maps all you want, its value will never change.
To display the list you mention in your comment to this answer, do
for (int value : yourList) {
Ausloesungsart art = Arrays.stream(Ausloesungsart.values())
.filter(a -> a.getValue() == value)
.findFirst().orElse(Ausloesungsart.UNDEFINED);
System.out.println(String.format("<%d>: %s", value, art.toString());
}
Upvotes: 0
Reputation: 48287
your are putting values in the map and that is working... what you will never get to work is that you return a enum with that integer attribute...
your method is doing insertiong in a Map
so you can at the end have something like
UNDEFINED(-1),
FIS(0),
MANUELL(1),
UNDEFINED(123),
UNDEFINED(1231),
UNDEFINED(12311),
UNDEFINED(1233123),
but all those calues are still the same enum: Ausloesungsart.UNDEFINED
no matter how much the map changes, you have at the end the same enum
public enum Ausloesungsart { UNDEFINED( -1), FIS( 0), MANUELL( 1); }
Upvotes: 0