swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 'typedef enum' in Java?


From: Stephen C. Upton
Subject: Re: 'typedef enum' in Java?
Date: Fri, 21 Jan 2000 10:44:00 -0700

Sven,

Check out pages 258-259 of Bruce Eckel's "Thinking in Java" for a description of another techniques for mimicking enum objects. He has an int version (no type safety), and another based on strings with a private constructor that provides type safety. As an aside the Enumerated stuff in collections is decprecated, i.e., Enumerated should no longer be used - instead use the Iterator methods. Enumerated is only meant to traverse a list, so isn't too related to what you want to do. For Eckel's book (2nd Edition, 3rd Revision in pdf and MS Word formats) see:
http://www.EckelObjects.com/TIJ2/index.html
IMHO, it's a most excellent book on java, and has alot of good stuff on designing also.

hope that helps
upton


At 10:47 AM 1/21/2000 -0600, you wrote:

I'm converting an app from objC to Java, and this particular app happens to use a lot of 'typedef enum' data definitions. As far as I can ascertain, Java doesn't implement enumerated data types. I have therefore tried to whip up an enumeration object to fill the void. (A million static final constants seems messy.)

I'd like comments from the Java gurus out there if this seems like a reasonable solution, or if there's something else I could be doing. I realize that if the object didn't contain a current value, I could possibly use the Class object instead of instance objects (the data variables would then just be integers). I don't see the need to iterate over the possible enumerated values, so I haven't messed with the Enumerated stuff in collections.

Thanks!
Sven

Code follows:
-------------

// ------------------------------------------------------------------------ //
// EnumeratedClasses.java
// Part of NTC project "Virtual Consumer" (c) Auburn University 1999
// ------------------------------------------------------------------------ //

// The possible values for a person's ethnicity (for census purposes)
// (Replaces an 'enumerated' data type in objC.)

// Typical usages:

// private CensusEthnicType myEthnicity;
//
// if (!myEthnicity.setValue (myEthnicity.AsianAmerican())) { .. error .. };
// alternatively:
// if (myEthnicity.setValue(newValue) != newValue) { .. error .. };
//
// if (myEthnicity.value() == myEthnicity.AsianAmerican())
// {
//     // do stuff
// }
// else if (myEthnicity.value() == myEthnicity.AfricanAmerican())
// {
//     // do other stuff
// }

public class CensusEthnicType {

  private int value;
  private final int MaxValue = 6;


  // Class constructor:
  public CensusEthnicType() {
    value = 0;  // "not set yet"
  }

  // Number of enumerated values:
  public int MaxValue() {
    return MaxValue;
  }

  // Current value (out of the legal ones):
  public int value() {
    return value;
  }

  // Returning integer values for the enumerated alternatives:
  // (would normally be kept 'secret')

  public int EuroAmerican() {
    return 1;
  }

  public int AfricanAmerican() {
    return 2;
  }

  public int AsianAmerican() {
    return 3;
  }

  public int HispanicAmerican() {
    return 4;
  }

  public int NativeAmerican() {
    return 5;
  }

  public int EskimoAmerican() {
    return 6;
  }

  public boolean setValue(int v) {
    if ((v < 1) || (v > MaxValue)) return false;
    value = v;
    return true;
  }

  // alternative method:
  public int setValue (int v) {
    if ((v < 1) || v > MaxValue)) return 0;
    value = v;
    return value;
  }

}



                 ==================================
  Swarm-Support is for discussion of the technical details of the day
  to day usage of Swarm.  For list administration needs (esp.
  [un]subscribing), please send a message to <address@hidden>
  with "help" in the body of the message.


*** * **** * ***** ********* ** ****** ***** *** ***** ********
Stephen C. Upton  TSA-5, MS F602  Z119895                       
(505) 667-9435 Voice                    (505) 665-2017 Fax                      
address@hidden
********* ******* ********* *** ** *** ******** **** ****** **
                 ==================================
  Swarm-Support is for discussion of the technical details of the day
  to day usage of Swarm.  For list administration needs (esp.
  [un]subscribing), please send a message to <address@hidden>
  with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]