Connecting Tech Pros Worldwide Help | Site Map

getting Enum constant

Member
 
Join Date: Feb 2008
Posts: 89
#1: Aug 11 '08
I have the following enum.

Expand|Select|Wrap|Line Numbers
  1. **
  2.  * An enum to represent types of mechanics.
  3.  * 
  4.  * @author 
  5.  * @version 1.0
  6.  */
  7. public enum Mechanics {
  8.     ISOLATION(0),
  9.     COMPOUND(1);
  10.  
  11.     /**
  12.      * Variable to hold the value of the mechanics.
  13.      */
  14.     private final int mechanics;
  15.  
  16.     /**
  17.      * Constructor to create a mechanics
  18.      * 
  19.      * @variable    newMechanics
  20.      *                 The value of the new mechanics
  21.      */
  22.     Mechanics (int newMechanics) {
  23.         this.mechanics = newMechanics;
  24.     }
  25.  
  26.     /**
  27.      * An inspector method that returns the value of the calling operation.
  28.      * 
  29.      * @return     the value of the calling mechanics.
  30.      *             | result >= 0 && result < 2
  31.      */
  32.      public int getValue(){
  33.          return mechanics;
  34.      }
  35.  
  36.      /**
  37.       * A method to check whether the value of a given mechanics
  38.       * matches that of this mechanics
  39.       * 
  40.       * @variable    other
  41.       *             The mechanics to be compared with this mechanics
  42.       */
  43.      public boolean equals(Mechanics other){
  44.          return this.getValue() == other.getValue();
  45.      }
  46. }
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
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#2: Aug 12 '08

re: getting Enum constant


Your enum is over-engineered ;-)

The following in sufficient:

Expand|Select|Wrap|Line Numbers
  1. public enum Mechanics {ISOLATION, COMPOUND};
There is no need to define a "value" property as you did. Enum defines a property ordinal that does the same thing. Enum also overrides toString, equals, hashCode and clone correctly. Enums implements Serializable and Comparable as well.

To save and restore an enum, you can convert it to a String and back:

Expand|Select|Wrap|Line Numbers
  1. String s = m1.name(); //or m1.toString();
  2. Mechanics m2 = Mechanics.valueOf(s);
or convert it to its ordinal and back:

Expand|Select|Wrap|Line Numbers
  1. int ord = m1.ordinal();
  2. Mechanics m2 = Mechanics.values()[ord];
As mentioned, enums are serializable so you could save and restore objects with enum fields that way, but java beans can be persisted with XMLEncoder/Decoder as well:

Expand|Select|Wrap|Line Numbers
  1. public class SaveMe {
  2.     private Mechanics mech;
  3.     private int another;
  4.  
  5.     public SaveMe() {
  6.     }
  7.  
  8.     public SaveMe(Mechanics mech, int another) {
  9.         setMechanics(mech);
  10.         setAnother(another);
  11.     }
  12.  
  13.     public void setMechanics(Mechanics mech) {
  14.         this.mech = mech;
  15.     }
  16.  
  17.     public Mechanics getMechanics() {
  18.         return mech;
  19.     }
  20.  
  21.     public void setAnother(int another) {
  22.         this.another = another;
  23.     }
  24.  
  25.     public int getAnother() {
  26.         return another;
  27.     }
  28.  
  29.     public String toString() {
  30.         return String.format("[mech=%s, another=%d]",
  31.             getMechanics(), getAnother());
  32.     }
  33. }
Expand|Select|Wrap|Line Numbers
  1. import java.beans.*;
  2. import java.io.*;
  3.  
  4. public class EnumTest {
  5.     public static void main(String[] args)  throws IOException {
  6.         testParse();
  7.         testIO();
  8.     }
  9.  
  10.     static void testParse() {
  11.         String[] input = {"ISOLATION","COMPOUND"};
  12.         for(String s : input) {
  13.             Mechanics m = Mechanics.valueOf(s);
  14.             System.out.format("%s --> %s, with ordinal %d%n", s, m, m.ordinal());
  15.         }
  16.     }
  17.  
  18.     static void testIO() throws IOException {
  19.         File file = new File("test.xml");
  20.  
  21.         XMLEncoder e = new XMLEncoder(
  22.             new BufferedOutputStream(
  23.                 new FileOutputStream(file)));
  24.         e.writeObject(new SaveMe(Mechanics.COMPOUND, 17));
  25.         e.close();
  26.  
  27.         XMLDecoder d = new XMLDecoder(
  28.             new BufferedInputStream(
  29.             new FileInputStream(file)));
  30.         Object result = d.readObject();
  31.         d.close();
  32.         System.out.println(result);
  33.     }
  34. }
The method testIO create the file test.xml:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <java version="1.6.0" class="java.beans.XMLDecoder"> 
  3.  <object class="SaveMe"> 
  4.   <void property="another"> 
  5.    <int>17</int> 
  6.   </void> 
  7.   <void property="mechanics"> 
  8.    <object class="Mechanics" method="valueOf"> 
  9.     <string>COMPOUND</string> 
  10.    </object> 
  11.   </void> 
  12.  </object> 
  13. </java> 
  14.  
Conclusion: enums are simpler and easier to use than you first thought!
Member
 
Join Date: Feb 2008
Posts: 89
#3: Aug 13 '08

re: getting Enum constant


I see. Damn I thought they weren't that easy.

Thanx for everything!
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#4: Aug 14 '08

re: getting Enum constant


ps.

There are times when it makes sense to add methods (even polymorphic methods) to an enumerated type, Check out the Planet and Operation examples:

[http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html]
Reply