enum paralellism

R

Roedy Green

What if you have two enum classes that have similar structure, e.g.
similar method names, similar instance variables or similar enum
constants.

Is there any way to specify that similarity in one place or to use
interfaces, abstract classes, EnumSets or inheritance to enforce the
parallel structure? I have not found a way.
 
E

Eric Sosman

What if you have two enum classes that have similar structure, e.g.
similar method names, similar instance variables or similar enum
constants.

Is there any way to specify that similarity in one place or to use
interfaces, abstract classes, EnumSets or inheritance to enforce the
parallel structure? I have not found a way.

Is this parallelism a result of using classes from different
sources, each with its own enum to represent the same set of things?
For example, do you have two different enums with constants MUNCHKIN,
QUADLING, WINKIE, and GILLIKIN, in class libraries obtained from
independent authors of Oz software? If so, I think your best bet may
be to invent an enum of your own with references to the corresponding
"foreign" instances:

package com.mindprod.oz;
import com.baum.oz.Country;
import net.fandom.oz.OzLands;
enum OzTerritories {
MUNCHKIN(Country.MUNCHKIN, OzLands.MUNCHKIN),
QUADLING(Country.QUADLING, OzLands.QUADLING),
WINKIE(Country.WINKIE, OzLands.WINKIE),
GILLIKIN(Country.GILLIKIN, OzLands.GILLIKIN);

private final Country baumEnum;
private final OzLands fandomEnum;
private OzTerritories(Country baum, OzLands fandom) {
baumEnum = baum;
fandomEnum = fandom;
}
...
}

On the other hand, if the parallelism is in your own classes
and of your own invention, perhaps the parallel enums should really
just be one omnibus enum. For example, if in one enum you list the
lands of Oz and indicate their general locations (MUNCHKIN is East,
despite much confusion) and in the other you associate each land
with its proper witch (Glinda the Good rules in GILLIKIN), maybe
you should combine the now-parallel enums into a single enum that
embodies all the attributes of interest. Alternatively, you might
settle for a single "bare" enum with no (or few) attributes of its
own, plus a bunch of EnumMap's to hold the associations.

On the gripping hand, perhaps I've completely failed to grasp
(pun intended) the nature and origins of the parallelism you face.
Could you describe it further?
 
R

Roedy Green

Is this parallelism a result of using classes from different
sources, each with its own enum to represent the same set of things?

Is is a general question hoping to collect techniques that might apply
in different circumstances. However, what triggered it is I have
three different enums:

1. online bookstores where you look things up by ISBN. I probe the
bookstore to see if they have a given book in stock by looking for
snippets in the response e.g.

AMAZONCOM( "amazon.com",
"www.amazon.com",
CountryFlag.USA,
Global.configuration.getAccountForAmazonCom(),
ISBNsInStock.NO_DELAY,
new String[] {
">these sellers<",
"Available from",
"In Stock",
"In stock",
"Limited Availability",
"This title has not yet been released",
"Usually ships within",
"left in stock" },

new String[] {
"Currently unavailable",
"Looking for something?",
"Sign up to be notified when this item becomes
available",
"out of stock", "did not match any products"
},

new String[] { }, 1789, 9325 )
{
int doesThisBookstoreCarry( final String isbn13 )
{
return doesGenericAmazonCarry( this, isbn13 );
}
},

There are similar enums for DVDs stores that look up by UPC, and
Amazon stores that look up by ASIN. The other way to deal with it
would be to combine them in to one big enum with parms to keep track
of the subtype.
 
D

Daniel Pitts

What if you have two enum classes that have similar structure, e.g.
similar method names, similar instance variables or similar enum
constants.

Is there any way to specify that similarity in one place or to use
interfaces, abstract classes, EnumSets or inheritance to enforce the
parallel structure? I have not found a way.

Enums can implement interfaces:

public enum MyEnum implements Runnable {
A { public void run() { System.out.println("A Runs!"); } },
B { public void run() { System.out.println("B is an also ran."); } }
;
}

But they can not extend other classes. Enum *constants* however
automatically extend the (automatically abstract) Enum class. So you can
have abstract methods in the base enum class.

public enum MyEnum {
A { public void run() { System.out.println("A Runs!"); } },
B { public void run() { System.out.println("B is an also ran."); } }
;

public abstract void run();
}

It sounds like you want a combination of abstract methods and interfaces.
 
D

Daniel Pitts

Is is a general question hoping to collect techniques that might apply
in different circumstances. However, what triggered it is I have
three different enums:

1. online bookstores where you look things up by ISBN. I probe the
bookstore to see if they have a given book in stock by looking for
snippets in the response e.g.
There are similar enums for DVDs stores that look up by UPC, and
Amazon stores that look up by ASIN. The other way to deal with it
would be to combine them in to one big enum with parms to keep track
of the subtype.

While an interesting approach, I would personally look into
externalizing that into a configuration file, rather than inlining it as
enums. That way you don't need to recompile when you update your settings.

In any case, see my other message about how to achieve what you're asking.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top