Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Subclassing EnumSet to add an interface?

Reply
Thread Tools

Subclassing EnumSet to add an interface?

 
 
Eric Smith
Guest
Posts: n/a
 
      05-12-2007
I'd like to create a subclass of EnumSet to implement the Comparable
interface (using my own arbitrary ordering, so that I can use the
subclass as a key in a dictionary), but I can't seem to figure
out how to do it.

I tried:

import java.util.EnumSet;

public abstract class Foo<E extends Enum<E>> extends EnumSet<E>
implements Comparable<Foo>
{
public int compareTo (Foo o)
{
return 1; // dummy value for now
}
}


The compiler says:

Foo.java:3: cannot find symbol
symbol : constructor EnumSet()
location: class java.util.EnumSet<E>
public abstract class Foo<E extends Enum<E>> extends EnumSet<E>
^
1 error


I don't understand why it thinks there should be an EnumSet()
constructor, since I'm subclassing it as an abstract class.
Any hints or suggestions?

Thanks!
Eric
 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      05-12-2007

"Eric Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I'd like to create a subclass of EnumSet to implement the Comparable
> interface (using my own arbitrary ordering, so that I can use the
> subclass as a key in a dictionary), but I can't seem to figure
> out how to do it.
>
> I tried:
>
> import java.util.EnumSet;
>
> public abstract class Foo<E extends Enum<E>> extends EnumSet<E>
> implements Comparable<Foo>
> {
> public int compareTo (Foo o)
> {
> return 1; // dummy value for now
> }
> }
>
>
> The compiler says:
>
> Foo.java:3: cannot find symbol
> symbol : constructor EnumSet()
> location: class java.util.EnumSet<E>
> public abstract class Foo<E extends Enum<E>> extends EnumSet<E>
> ^
> 1 error


You're not specifying a constructor, so one is being created for you, which
looks like

public Foo()
{
super();
}

The compiler is complaining that the constructor "super()" is attempting to
call doesn't exist. In fact, since EnumSet has no public constructors, it
cannot be subclassed (other than, perhaps, within its package.)


 
Reply With Quote
 
 
 
 
Eric Smith
Guest
Posts: n/a
 
      05-12-2007
Mike Schilling
> You're not specifying a constructor, so one is being created for you, which
> looks like
>
> public Foo()
> {
> super();
> }


Yes, I had tried doing that explicitly as well.

> In fact, since EnumSet has no public constructors, it
> cannot be subclassed (other than, perhaps, within its package.)


Thanks, I was afraid that might be the case but wasn't sure.

Ugh. I'll have to write my own EnumSet class.

I never cease to be amazed at how often the standard Java
classes do 95% of what I want, but *cannot* be coerced into
letting me implement that last 5%.
 
Reply With Quote
 
Richard Reynolds
Guest
Posts: n/a
 
      05-12-2007

"Eric Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Mike Schilling
>> You're not specifying a constructor, so one is being created for you,
>> which
>> looks like
>>
>> public Foo()
>> {
>> super();
>> }

>
> Yes, I had tried doing that explicitly as well.
>
>> In fact, since EnumSet has no public constructors, it
>> cannot be subclassed (other than, perhaps, within its package.)

>
> Thanks, I was afraid that might be the case but wasn't sure.
>
> Ugh. I'll have to write my own EnumSet class.
>
> I never cease to be amazed at how often the standard Java
> classes do 95% of what I want, but *cannot* be coerced into
> letting me implement that last 5%.


Could you write your own class that implements Comparable and just delegates
the EumSet methods to a contained EnumSet class? maybe that's what you meant
anyway?
Richard.


 
Reply With Quote
 
Tom Hawtin
Guest
Posts: n/a
 
      05-12-2007
Eric Smith wrote:
>
> Ugh. I'll have to write my own EnumSet class.


No. Just use Comparator rather than Comparable.

> I never cease to be amazed at how often the standard Java
> classes do 95% of what I want, but *cannot* be coerced into
> letting me implement that last 5%.


You just have to use it correctly.

Tom Hawtin
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      05-12-2007
Eric Smith <(E-Mail Removed)> writes:

> I'd like to create a subclass of EnumSet to implement the Comparable
> interface (using my own arbitrary ordering, so that I can use the
> subclass as a key in a dictionary), but I can't seem to figure
> out how to do it.


As others have pointed out, EnumSet cannot be subclassed.

Two approaches spring to mind, if all you need are keys based on
sets of enum values:

Make an adapter key object containing the EnumSet:

class MyDictionaryKey<T extends Enum>
implements Comparable<MyDictionaryKey<T>> {
private final EnumSet<T> enumSet;
public MyDictionaryKey(EnumSet<T> enumSet) {
this.enumSet = enumSet;
}
public EnumSet<T> getEnumSet() {
return enumSet;
}
public int compareTo(MyDictionaryKey<T> other) {
/// ...your impl
}
}

and use it for keys in your dictionary.

Or, create a Comparator<EnumSet<MyEnum>> and use a dictionary
that allows a comparator for the keys.

/L
--
Lasse Reichstein Nielsen - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      05-12-2007
Richard Reynolds wrote:
> "Eric Smith" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Mike Schilling
>>> You're not specifying a constructor, so one is being created for you,
>>> which
>>> looks like
>>>
>>> public Foo()
>>> {
>>> super();
>>> }

>> Yes, I had tried doing that explicitly as well.
>>
>>> In fact, since EnumSet has no public constructors, it
>>> cannot be subclassed (other than, perhaps, within its package.)

>> Thanks, I was afraid that might be the case but wasn't sure.
>>
>> Ugh. I'll have to write my own EnumSet class.
>>
>> I never cease to be amazed at how often the standard Java
>> classes do 95% of what I want, but *cannot* be coerced into
>> letting me implement that last 5%.

>
> Could you write your own class that implements Comparable and just delegates
> the EumSet methods to a contained EnumSet class? maybe that's what you meant
> anyway?


As Joshua Bloch advised in /Effective Java/, "prefer composition to inheritance."

--
Lew
 
Reply With Quote
 
Eric Smith
Guest
Posts: n/a
 
      05-12-2007
"Richard Reynolds" <(E-Mail Removed)> writes:
> Could you write your own class that implements Comparable and just delegates
> the EumSet methods to a contained EnumSet class? maybe that's what you meant
> anyway?


If I'm going to the trouble of implementing it myself, I'm going to
implement it in terms of bitmaps stored as ints or longs, to make the
compareTo function efficent.

But thanks for the idea! Using delegation like that may well solve
other problems I face.

Eric
 
Reply With Quote
 
Eric Smith
Guest
Posts: n/a
 
      05-12-2007
Lew <(E-Mail Removed)> writes:
> As Joshua Bloch advised in /Effective Java/, "prefer composition to inheritance."


Even when you only want to add one simple method?
 
Reply With Quote
 
Eric Smith
Guest
Posts: n/a
 
      05-12-2007
I wrote:
> I never cease to be amazed at how often the standard Java
> classes do 95% of what I want, but *cannot* be coerced into
> letting me implement that last 5%.


Tom Hawtin wrote:
> You just have to use it correctly.


I originally learned object-oriented programming in Smalltalk.
Perhaps Smalltalk taught me to do things incorrectly, though
at the time I didn't seem to have trouble with it.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Enumset Roedy Green Java 10 09-01-2007 03:11 PM
EnumSet + contains: strange behavior Ulrich Scholz Java 10 06-04-2006 10:56 AM
EnumSet Generics puzzle Roedy Green Java 11 08-22-2005 02:27 PM
EnumSet--what the...? George Cherry Java 0 07-09-2005 08:05 PM
EnumSet, what the ? Roedy Green Java 6 07-09-2005 07:18 PM



Advertisments