final field of an enum to refer to later-named instance.

This is a discussion on final field of an enum to refer to later-named instance. within the Java forums in Programming Languages category; [This is a supercede, just to correct a broken Subject] I've got an enum that contains Things that come in pairs. I'd like each Thing to have a *final* reference to it's mate, that in half of the cases appears later in the list of Thing-instances. e.g.: enum Thing { T1, T2; final Thing mate; } What could I do, to make T2 "final"ly the mate of T1 and vice versa ? I know some alternatives, myself, like just making the reference non-final (and initialize it in the enum's static {...} block) or defining an abstract method in the enum ...

Go Back   Application Development Forum > Programming Languages > Java

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-25-2008, 08:51 AM
Andreas Leitgeb
Guest
 
Default final field of an enum to refer to later-named instance.

[This is a supercede, just to correct a broken Subject]

I've got an enum that contains Things that come in pairs.

I'd like each Thing to have a *final* reference to
it's mate, that in half of the cases appears later
in the list of Thing-instances.

e.g.:
enum Thing {
T1, T2;
final Thing mate;
}

What could I do, to make T2 "final"ly the mate of T1
and vice versa ?

I know some alternatives, myself, like just making the
reference non-final (and initialize it in the enum's
static {...} block) or defining an abstract method in
the enum like "abstract Thing getMate();" and implement
it for each instance to return its mate...

Please only followup, if you know a trick for final fields,
or if you know that it is strictly impossible that way.
Reply With Quote
  #2  
Old 08-27-2008, 04:56 PM
Kenneth P. Turvey
Guest
 
Default Re: final field of an enum to refer to later-named instance.

On Mon, 25 Aug 2008 12:51:30 +0000, Andreas Leitgeb wrote:

> [This is a supercede, just to correct a broken Subject]
>
> I've got an enum that contains Things that come in pairs.
>
> I'd like each Thing to have a *final* reference to it's mate, that in
> half of the cases appears later in the list of Thing-instances.
>
> e.g.:
> enum Thing {
> T1, T2;
> final Thing mate;
> }

[Snip]

This is a problem because you really want to create a pair at a time not
a single thing at a time. You need to initialize the two mates at
creation, but you can't since the other thing hasn't been created yet.

I would suggest having a class called pair instead of a class called
Thing. It would make these problems go away. Otherwise you're going to
have to give up on the finality of mate.



--
Kenneth P. Turvey <kt-usenet@squeakydolphin.com>
http://www.electricsenator.net

I took the initiative in creating the Internet.
-- Al Gore
Reply With Quote
  #3  
Old 08-27-2008, 08:19 PM
Mike Schilling
Guest
 
Default Re: final field of an enum to refer to later-named instance.

Andreas Leitgeb wrote:
> [This is a supercede, just to correct a broken Subject]
>
> I've got an enum that contains Things that come in pairs.
>
> I'd like each Thing to have a *final* reference to
> it's mate, that in half of the cases appears later
> in the list of Thing-instances.
>
> e.g.:
> enum Thing {
> T1, T2;
> final Thing mate;
> }
>
> What could I do, to make T2 "final"ly the mate of T1
> and vice versa ?
>
> I know some alternatives, myself, like just making the
> reference non-final (and initialize it in the enum's
> static {...} block) or defining an abstract method in
> the enum like "abstract Thing getMate();" and implement
> it for each instance to return its mate...
>
> Please only followup, if you know a trick for final fields,
> or if you know that it is strictly impossible that way.


Not having played with enums, I don't know what games you can play with
their constructors, but this kind of thing will work for a "normal" class.
(The trick is to construct the mate if and only if it doesn't already
exist.)

import java.util.Map;
import java.util.HashMap;

public class Mates
{
public final Mates myMate;
private final int value;

private static Map<Integer, Mates> instances = new HashMap<Integer,
Mates>();

public Mates(int i)
{
value = i;
instances.put(i, this);
int mateValue = (i % 2 == 0) ? i + 1 : i - 1;
if (instances.get(mateValue) == null)
new Mates(mateValue);
myMate = instances.get(mateValue);
}

public String toString()
{
return Integer.valueOf(value).toString();
}


public static void main(String[] args)
{
Mates[] arr = new Mates[10];
for (int i = 0; i < 10; i++)
arr[i] = new Mates(i);

for (int i = 0; i < 10; i++)
{
System.out.println(arr[i] + " is the mate of " + arr[i].myMate);
}
}
}


Reply With Quote
  #4  
Old 08-28-2008, 04:48 AM
Andreas Leitgeb
Guest
 
Default Re: final field of an enum to refer to later-named instance.

Mike Schilling <mscottschilling@hotmail.com> wrote:
> Not having played with enums, I don't know what games you can play with
> their constructors,


Unfortunately, not enough :-(
The "next" enum-instance isn't even allocated, while the
current instance's constructor is not completed.

As it seems there really is no way.

In my case at hand, I've now changed my code to not really use
the enum-instances as such, but in the enum's static initializer
I create a few arrays, that are filled with return values of
method calls on the instances.
Later I do no longer touch the instances, but only read
the tables to get the infos I need.

Since the pairs were subsequent and I now use int's instead
of Thing-references, accessing the mate's results is now just
a matter of indexing with "i^1" instead of "i".

Thanks to all. Especially the trick for non-enums with
pair-wise creation (posted by a few) was new to me.

PS: the question about that "final" field was rather of
educational purpose. Even if it had worked, I'd have
switched to int-based processing at some time.

PPS: doing a switch on the index turned out to be noticably
faster than overriding an abstract method in the enum.
I really tried it, and wrote 40 anonymous subclasses for
my 40 instances. I still wouldn't claim that this always
needs to be the case. (Yes, I did the profiling first,
and it did indicate, that it would pay to optimize.)

PPPS: since an enum's .values() always does a .clone on it's
internally kept inventory-array ($VALUES), I also added a
static field:
private static final Thing things[]=Thing.values();
to help me get the Instance by index without cloning the
array each time. To my surprise I also didn't find a
method like valueOf(int).

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 12:42 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.