swarm-support
[Top][All Lists]
Advanced

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

Re: 'typedef enum' in Java?


From: Nick Collier
Subject: Re: 'typedef enum' in Java?
Date: Fri, 21 Jan 2000 11:50:13 -0600

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.



reply via email to

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