I have the following enum.
Now, I also have objects, which have a property that is a constant of this Enum. (isolation or compound). Now I am searching for a way of saving these objects (to textfiles), and loading them.
Which means that the property of the object which is the enum should be saved in a way (i guess using the associated int) that is simple.
Also I should be able to load this enum again. However I do not know how to do this, since a constructor of an enum is private, and the method that loads does not know wether the 0 or 1 stands for isolation or compound.
How can I do this, without using switch-like things? I have much larger enums then the one I showed here.
Thanks
Code:
**
* An enum to represent types of mechanics.
*
* @author
* @version 1.0
*/
public enum Mechanics {
ISOLATION(0),
COMPOUND(1);
/**
* Variable to hold the value of the mechanics.
*/
private final int mechanics;
/**
* Constructor to create a mechanics
*
* @variable newMechanics
* The value of the new mechanics
*/
Mechanics (int newMechanics) {
this.mechanics = newMechanics;
}
/**
* An inspector method that returns the value of the calling operation.
*
* @return the value of the calling mechanics.
* | result >= 0 && result < 2
*/
public int getValue(){
return mechanics;
}
/**
* A method to check whether the value of a given mechanics
* matches that of this mechanics
*
* @variable other
* The mechanics to be compared with this mechanics
*/
public boolean equals(Mechanics other){
return this.getValue() == other.getValue();
}
}
Which means that the property of the object which is the enum should be saved in a way (i guess using the associated int) that is simple.
Also I should be able to load this enum again. However I do not know how to do this, since a constructor of an enum is private, and the method that loads does not know wether the 0 or 1 stands for isolation or compound.
How can I do this, without using switch-like things? I have much larger enums then the one I showed here.
Thanks
Comment