From: www on
Jeff Higgins wrote:

>
> Can you use an EnumSet?
> You don't say how you intend to use your hierarchy of enums.
>
Thank you. I rephrase my question as following:

I need to have two Enum types. Most elements of the two are the same,
but there are some different ones.

enum CarModelOfFactor1
{
CarModelA, CarModelB, CarModelC, CarModelM
}

enum CarModelOfFactor2
{
CarModelA, CarModelB, CarModelC, CarModelX, CarModelW
}


I just hate to code "CarModelA, CarModelB, CarModelC" twice inside the
two enums. I am wondering if there is a better way to do it.
From: markspace on
www wrote:

> My original posting was too simplified. A, B, C, D, M, or P all are
> related, unlike your example above. Maybe I should call them CarModelA,
> CarModelB, ..., CarModelP etc.


Well, if they're related, then you need to implement something that
reflects how they are related. If enums don't do that, then don't use them.

It sounds like you need to extend or add to existing constant class.
You might make your own class with a factory pattern (not compiled or
tested):

public class CarModel {

private final HashMap<String,CarModel> models = new
HashMap<String,CarModel>();

public static CarModel getID( String car ) {
return models.get( car );
}

public static void setID( String car, CarModel ID ) {
// constant == don't change existing values
if( ! models.contailsKey( car ) ) {
models.add( car, ID );
}
// TODO: else throw exception?
}

static { // set default cars
models.add( "A", new CarModel() );
models.add( "B", new CarModel() );
models.add( "C", new CarModel() );
models.add( "D", new CarModel() );
}

}
From: Eric Sosman on
On 4/29/2010 2:54 PM, www wrote:
> Jeff Higgins wrote:
>
>>
>> Can you use an EnumSet?
>> You don't say how you intend to use your hierarchy of enums.
>>
> Thank you. I rephrase my question as following:
>
> I need to have two Enum types. Most elements of the two are the same,
> but there are some different ones.
>
> enum CarModelOfFactor1
> {
> CarModelA, CarModelB, CarModelC, CarModelM
> }
>
> enum CarModelOfFactor2
> {
> CarModelA, CarModelB, CarModelC, CarModelX, CarModelW
> }
>
>
> I just hate to code "CarModelA, CarModelB, CarModelC" twice inside the
> two enums. I am wondering if there is a better way to do it.

My first thought (and maybe my second, too) would be to
wonder why you need two separate enums for what looks to be
two subsets of values of one enum. In other words, why not
just have one "enum CarModel" with all six constants?

(Vague general impression: enums were underused before they
showed up in Java, but have been overused ever since.)

--
Eric Sosman
esosman(a)ieee-dot-org.invalid
From: Jeff Higgins on

> I need to have two Enum types.
Why?

Without a description of your use case ...

From: Tom Anderson on
On Thu, 29 Apr 2010, www wrote:

> Jeff Higgins wrote:
>
>> Can you use an EnumSet? You don't say how you intend to use your
>> hierarchy of enums.
>
> Thank you. I rephrase my question as following:
>
> I need to have two Enum types. Most elements of the two are the same, but
> there are some different ones.
>
> enum CarModelOfFactor1
> {
> CarModelA, CarModelB, CarModelC, CarModelM
> }
>
> enum CarModelOfFactor2
> {
> CarModelA, CarModelB, CarModelC, CarModelX, CarModelW
> }
>
> I just hate to code "CarModelA, CarModelB, CarModelC" twice inside the
> two enums. I am wondering if there is a better way to do it.

Why do there have to be multiple enums? What are they used for? Could you
have one big enum which had all the values?

tom

--
Formal logical proofs, and therefore programs - formal logical proofs
that particular computations are possible, expressed in a formal system
called a programming language - are utterly meaningless. To write a
computer program you have to come to terms with this, to accept that
whatever you might want the program to mean, the machine will blindly
follow its meaningless rules and come to some meaningless conclusion. --
Dehnadi and Bornat