swarm-support
[Top][All Lists]
Advanced

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

Re: 'typedef enum' in Java?


From: Miles Parker
Subject: Re: 'typedef enum' in Java?
Date: Fri, 21 Jan 2000 13:17:44 -0500

Just want to agree with what Nick and Stephen have said. There has been a lot 
of discussion about enumerated types in Java, and not much consensus on a best 
solution with type safety, most seem to involve some kind of trade off. Most of 
us, including myself, go with what Nick describes..there is a problem with type 
saftey, but in practice it usually really isn't an issue. It does require a 
little extra care, and is something that the languaage should provide built in, 
but seems to work fine.

btw, a little style suggestion...the Java naming convention is generally to 
name these guys like

        public static final int ERROR = -1;
        public static final int ASIAN_AMERICAN = 0;
        public static final int CAUCASIAN = 1;

Really for any static finals, but especially these pseudo-enumerated dealies. 
Avoids confusion with Class, method, and variable names, and makes their static 
final nature clear.

-Miles



Miles T. Parker
Software Engineer
The Brookings Institution  1775 Mass. Ave. NW  Washington, DC  20036
http://www.brook.edu/es/dynamics/models/ascape
mailto:address@hidden  voice 202.797.6136  fax 202.797.6319

>>> Nick Collier <address@hidden> 01/21/00 12:50PM >>>
I don't think it gets at what you want as it lists static finals, but I
usually do something like:

public interface Ethnicity {
        public static final int Error = -1;
        public static final int AsianAmerican = 0;
        public static final int Caucasian = 1;
        ...
}

This way any class that implements this interface has access to the
enumeration and all the enumerations are in one place. This is as close
as you can get to plain ol' typedef enums I think.

Nick    


"Sven N. Thommesen" 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.

-- 
Nick Collier
Social Science Research Computing
University of Chicago

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.


                  ==================================
   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]