Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #11378 > unrolled thread

enum paralellism

Started byRoedy Green <see_website@mindprod.com.invalid>
First post2012-01-16 06:16 -0800
Last post2012-01-17 10:34 -0800
Articles 6 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  enum paralellism Roedy Green <see_website@mindprod.com.invalid> - 2012-01-16 06:16 -0800
    Re: enum paralellism Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-01-16 10:33 -0500
      [OT] East and West in Oz (Was: enum paralellism) Lew <noone@lewscanon.com> - 2012-01-16 10:32 -0800
      Re: enum paralellism Roedy Green <see_website@mindprod.com.invalid> - 2012-01-17 10:08 -0800
        Re: enum paralellism Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-01-17 10:37 -0800
    Re: enum paralellism Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-01-17 10:34 -0800

#11378 — enum paralellism

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-01-16 06:16 -0800
Subjectenum paralellism
Message-ID<4tb8h7h2tul5rq4m5bk9sg485tq33qi394@4ax.com>
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.
-- 
Roedy Green Canadian Mind Products
http://mindprod.com
One of the most useful comments you can put in a program is 
"If you change this, remember to change ?XXX? too".
 

[toc] | [next] | [standalone]


#11380

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-01-16 10:33 -0500
Message-ID<jf1g0r$ua$1@dont-email.me>
In reply to#11378
On 1/16/2012 9:16 AM, Roedy Green wrote:
> 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?

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#11382 — [OT] East and West in Oz (Was: enum paralellism)

FromLew <noone@lewscanon.com>
Date2012-01-16 10:32 -0800
Subject[OT] East and West in Oz (Was: enum paralellism)
Message-ID<jf1qf6$2td$1@news.albasani.net>
In reply to#11380
Eric Sosman wrote:
> ...[snip]...
> lands of Oz and indicate their general locations (MUNCHKIN is East,
> despite much confusion) and in the other you associate each land

In Oz, East and West are reversed from our world.
<http://upload.wikimedia.org/wikipedia/commons/6/6d/Oz-and-surrounding-countrie.jpg>

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

[toc] | [prev] | [next] | [standalone]


#11414

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-01-17 10:08 -0800
Message-ID<dodbh7t37pqvn5teiu806s627h1hl0070v@4ax.com>
In reply to#11380
On Mon, 16 Jan 2012 10:33:46 -0500, Eric Sosman
<esosman@ieee-dot-org.invalid> wrote, quoted or indirectly quoted
someone who said :

>     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.
-- 
Roedy Green Canadian Mind Products
http://mindprod.com
One of the most useful comments you can put in a program is 
"If you change this, remember to change ?XXX? too".
 

[toc] | [prev] | [next] | [standalone]


#11419

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-01-17 10:37 -0800
Message-ID<LdjRq.963$744.783@newsfe10.iad>
In reply to#11414
On 1/17/12 10:08 AM, Roedy Green wrote:
> On Mon, 16 Jan 2012 10:33:46 -0500, Eric Sosman
> <esosman@ieee-dot-org.invalid>  wrote, quoted or indirectly quoted
> someone who said :
>
>>      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.
<snip>
> 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.

[toc] | [prev] | [next] | [standalone]


#11418

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-01-17 10:34 -0800
Message-ID<kbjRq.962$744.661@newsfe10.iad>
In reply to#11378
On 1/16/12 6:16 AM, Roedy Green wrote:
> 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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web