Question on bounding generic with Enum

Consider an enum that implements some interface:
public enum Blah implements Bleh
//I can now make a class like this:
public class Foo<T extends Enum<T>>
// The compiler has no problem so far.
// But if I do this:
public class Foo<T extends Enum<T> & bleh>
The compiler gives this error:
classes cannot directly extend java.lang.Enum
What have I done wrong?
-Eric

It takes more than 10 minutes to show up. More like a
week. Be patient. I just cut and pasted this thread.You responded so fast I thought the bug was already known. I hope this
can be fixed in the next release because I think it will be a very useful
bounding technique for enums.
-Eric

Similar Messages

  • Problems with enums

    Hi, I am trying to build a card class where each card has a suit, rank and prime number. I am trying to do it with enums, but I am having problems initialising the class from my Deck. The Card class is like so:
    import java.util.Comparator;
    public class Card
         private final Rank rank;
         private final Suit suit;
         private final PrimeVal primeVal;
         public enum Rank
              Deuce(0),Three(1),Four(2),Five(3),Six(4),Seven(5), Eight(6),
              Nine(7), Ten(8), Jack(9), Queen(10), King(11), Ace(12);     
              private final Integer rankValue;
             Rank(int rankValue)
                 this.rankValue = rankValue;
             public Integer faceValue(){ return rankValue; };
         public enum PrimeVal
              Deuce(2),Three(3),Four(5),Five(7),Six(11),Seven(13), Eight(17),
              Nine(19), Ten(23), Jack(29), Queen(31), King(37), Ace(41);     
              private final Integer primeVal;
              PrimeVal (int primeVal)
                 this.primeVal = primeVal;
             public Integer primeVal(){ return primeVal; };
         public enum Suit
              Spades(0x1000), Hearts (0x2000), Diamonds(0x4000), Clubs(0x8000);
              private final Integer suitValue;
              Suit(int suitValue)
                   this.suitValue = suitValue;
              public Integer suitValue(){ return suitValue; };
         public Card(Rank rank, Suit suit, PrimeVal primeVal)
            this.rank = rank;
            this.suit = suit;
            this.primeVal = primeVal;
        public Rank rank() { return rank; }
        public Suit suit() { return suit; }
        public String toString() { return rank + " of " + suit; }
    }and my initialisation procedure in Deck is:
    static
             for (Card.Suit suit : Card.Suit.values())
                for (Card.Rank rank : Card.Rank.values() )
                    Deck.add(new Card(rank, suit));
        }This code is predominantly from a enum tutorial off of this site, my problem is, I am just learning, I don't know to keep the features and add in the prime number and get it to initialise correctly... which is why the constructor is wrong. Is this even a good way of doing it?
    Many thanks, Ron

    Kaj, thanks, i know I need a constructor, I just
    don't know how to add it in! That was the question,
    how to add it?
    Thanks, RonAre you kidding me?
    Here's your original code, trying to invoke the constructor:
    Deck.add(new Card(rank, suit)); You're passing 2 arguments to the Card constructor, but you need 3.
    Think.

  • What's the deal with enums?

    Hello,
    I'm trying to familiarise myself with the new Java features that project Tiger (Java 1.5) will introduce. I come from a C++ programming background. I very briefly studied enums whilst I learned C++ and never used them in my programs, so I was, quite frankly, relieved when I learned that Java didn't support them.
    But now it does.
    I'm wondering, from a software design and engineering perspective, what benefit enums will add to the Java language and software engineering in general. I have read some pleas and arguments for enums. As I understand those arguments, proponents feel that minor ledgibility improvements to switch statements (which some OO programmers don't use on the basis that it weakens the object-orientedness through its goto-esque break statements) necessitate the addition of a whole other structure.
    Out of curiosity, wouldn't it be more advantageous for Sun to put their efforts into supporting constant strings in switches in addition to primitive data types if people are so concerned with clarity?
    I fail to understand what enums will do that constant string arrays or constant values cannot. I would like to think that Sun wouldn't add C++ features unless if there was a reason to do so, so I believe that there are good reasons to introduce this feature.
    Please help clarify this confusion.
    Thank you!

    Comments on the previous posting inline. Note: I'll go off on a few tangents, but try to always return to the issue of enums. The fact that I go off on tangents (related initially to an enum type example) should show that enums help eliminate numerous problems.
    Fair enough, but wouldn't this accomplish the same thing in
    a slightly wordier wayPretty much. But you still have the validation code in there yourself:
    currentDirection = ( direction > 0 && direction < 5 ? direction : 0 );People can still accidentally enter an invalid value, and it won't get caught by the compiler. So, I could call set(Compass.WEST) and hope/expect that is equivalent to set(Direction.LEFT). This will only occur if both Compass.WEST and Direction.LEFT are the same int value. If they are not, then we are in trouble. Worse, still if Compass.WEST and Direction.RIGHT are the same value, the code will compile, and run without any illegal value flagging, but our traveller will go off in the wrong direction.
    Error handling tangent: Given the implementation, the error handling is not performed in set, rather the illegal value is captured as a zero, which must be dealt with at a later time - say in a switch statement that calls get. This is not a good idea - it increases the "distance" between the error occuring (bad call to set) and the error being detected (if xInt != 0). The larger the distance, the harder debugging is. If you throw an exception in set the bug is immediately flagged. With enums, none of this should be necessary.
    Implementation tangent: set, get and currentDirection are static. This is not a good idea - it only allows one direction. If you have multiple travellers, each would have a Direction object, but (sticking with constants) UP, etc. would still be static.
    I keep the switch statement in my enum workaround, but I believe that
    the second reply suggested that enum could altogether eliminate the
    use for switch statements. How exactly would it do that?enum in its simplest form would not remove switch. However, using an extension of the type-safe enum idiom (i.e. the current Java approach to enums), you can embed direction specific logic in each of the "enum" members. If I understand the enum-spec correcly, this will also be possible with the Java 1.5 enum language facility.
    So, for an example, sticking with the domain: Consider a class Location, with x and y elements, and a Direction. The non-OO, non-enum approach would be as follows:
    class Location {
        int x,y;
        void move (final int direction) {
            switch (direction) {
                case UP:
                    --y;
                    break;
                case DOWN:
                    ++y;
                    break;
                case LEFT;
                    --x;
                    break;
                case RIGHT:
                    ++x;
                    break;
                default;
                    throw new RuntimeException("Invalid direction " + direction);
    class Direction {
        public static final int UP = 1;
        public static final int DOWN = 2;
        public static final int LEFT = 3;
        public static final int RIGHT = 4;
    }Note that I've deliberately been loose with my visibility. All logic is in Location, and Direction is dumb. If we go with a type safe-enum idiom, we get:
    class Location {
        int x,y;
        void move (final Direction direction) {
            if (direction == Direction.UP) {
                --y;
            } else if (direction == Direction.DOWN) {
                ++y;
            } else if (direction == Direction.LEFT) {
                --x;
            } else if (direction == Direction.RIGHT) {
                ++x;
            } else { // direction is null
                throw new NullPointerException("null direction");
    class Direction {
        public static final Direction UP = new Direction();
        public static final Direction DOWN = new Direction();
        public static final Direction LEFT = new Direction();
        public static final Direction RIGHT = new Direction();
        private Direction () {} // the ctor is private to limit instances
    }This code is a rewrite of the int approach, but is "type-safe". We've moved from switch to if as we can't switch on object references. Note: there is still error handling code, as you're not "null-safe" (I don't know what the Java 1.5 spec says about this)
    Little else has changed - the responsibilities are still the same. We can address this by going OO, which removes the if (formerly a switch) statement, but does couple the classes.
    class Location {
        int x,y;
        void move (final Direction direction) {
            direction.move(this);
    class Direction {
        private final int xOffset, yOffset;
        private Direction (final int xOffset, final int yOffset) {
            this.xOffset = xOffset;
            this.yOffset = yOffset;
        public static final Direction UP = new Direction(0,1);
        public static final Direction DOWN = new Direction(0,-1);
        public static final Direction LEFT = new Direction(-1,0);
        public static final Direction RIGHT = new Direction(1,0);
        void move(final Location location) {
            location.x += xOffset;
            location.y += yOffset;
    }This removes any conditional logic, embedding (constructor parameterised) logic within Direction. It also eliminates the "null-safe" check, as direction.move(this) will throw an NPE if a null is passed.
    Also, I've read some things saying that enum reduces 'boilerplate'
    code. What exactly does that and 'boilerplate' mean?The Direction class is an example of the type-safe enum approach currently used in Java. It has been enhanced with logic for movement. Even if I dropped this logic, there is still functionality worth adding to the Direction class - a toString method that returns one of "UP", "DOWN", etc (shown next); a way to serialize the object and retain reference equality - you have to write a readResolve method (not shown).
    class Direction {
        private final String name;
        private Direction (final String name) {
            this.name = name;
        public static final Direction UP = new Direction("UP");
        public static final Direction DOWN = new Direction("DOWN");
        public static final Direction LEFT = new Direction("LEFT");
        public static final Direction RIGHT = new Direction("RIGHT");
        public String toString() {
            return name;
    }Once you've written this code for one type-safe enum, you've written them for them all. You copy-cut-paste, and rename the class/elements. All this code is predictable. It is "boiler plate". Now, if you gave me a compiler that generated all this code for me, when I present it with
    enum Direction {UP, DOWN, LEFT, RIGHT}; then I've got something that reduces my boiler plate code to zip.... Enter the Java 1.5 compiler.

  • Long story short....I restored my computer to an earlier point using time machine and now all of the previous back-ups are gone. My question has to do with itunes...is it possible to restore my itunes library to the most recent backup because the restore

    Long story short....I restored my computer to an earlier point using time machine and now all of the previous back-ups are gone. My question has to do with itunes...is it possible to restore my itunes library to the most recent backup because the restore that I did was in march and the latest backup was in may....the reason being is that there were app that i had downloaded that are in my most recent back-up, that were not in the backup I restored to. Is it possible to just restore one application ie)itunes?

    See this post.
    tt2

  • Generic with holdingtac report T Code : S_P00_07000134

    Hi,
    I am executing the generic with holding tac report RFIDYYWT ( T Code S_P00_07000134 ).
    the report should
    1) All vendor invoices paid
    2) All credit memos paid
    3) All open down payments i.e. down payment paid but not yet settled against a final invoice.
    But , I am getting only 1 & 2 documents ( above mentioned ) it means that I am not getting the All open down payments documents in the report output.
    please do the needful ASAP.
    Thanks in Advance
    Arjun

    Resolved by using Check Register

  • How to create a generic with iMovie for iPad?

    How do i create A generic with iMovie for iPad?

    luckyuis wrote:
    Hi All,
    > I have generated a report showing data for past 12 months and after that i need to include a chart showing Month on X axis with data for past 12 months ..for example if i run the report for June 2011 then on the chart it should take as june 11..>may 11..>april 11........so on june 10 with respect to other values
    >
    > fyi .. i am using type: Numeric Axis..> Date Axis line chart and created seperate formule on top of date field to get past 12 months data
    >
    > Please advise how can i do that on the chart
    >
    > Thanks much,
    > Lucky
    Once you pull the data, goup on the date monthly, most recent first ,and then graph the summaries in the group.

  • New here... question to start off with

    Hi everyone...
    New to the forum and excited to learn some things with Dreamweaver.
    A question to start off with.... How does one program Dreamweaver so that it will automatically format in different browsers (sizes).  Is this even possible?
    cheers...
    twingi

    Read post #3 again... the part about 100%
    Also... lots of tutorial links
    As well as the links below - another way to learn (including anyone else's bad habits) is to find a page you like, and then tell your web browser to show you the code view for the page and/or do a "Save As" and save the page to your computer to then copy & paste code segments into your file (in IE click View at the top, select Source from the options)
    HTML and/or DW Tutorials, and other information links that I have saved
    http://validator.w3.org/
    http://www.visibone.com/
    http://www.w3schools.com/
    http://www.hotscripts.com/
    http://webdesignledger.com/
    http://www.projectseven.com/
    http://www.adobe.com/devnet/
    http://www.scriptarchive.com/
    http://www.htmldog.com/guides/
    http://www.htmlcodetutorial.com/
    http://alistapart.com/topics/code
    http://www.how-to-build-websites.com/
    http://css.maxdesign.com.au/floatutorial/
    Download User Guide as PDF for easy search
    http://www.adobe.com/support/documentation
    http://www.ianr.unl.edu/internet/mailto.html
    http://lynda.com/ Hours of videos. (must pay)
    http://apptools.com/examples/pagelayout101.php
    http://www.thesitewizard.com/archive/css.shtml
    http://www.projectseven.com/tutorials/index.htm
    If not PDF (link above) an online guide to read
    http://livedocs.adobe.com/en_US/Dreamweaver/9.0/
    Nate's Forms http://www.mindpalette.com/scripts/
    Customizing the layouts that come with CS3 (VIDEO)
    http://www.adobe.com/designcenter/video_workshop/?id=vid0155
    FormMail http://www.bebosoft.com/products/formstogo/index.php
    For those using MySQL - Installing PHP and MySQL on Windows XP
    http://www.webassist.com/professional/products/solutionrecipes.asp
    Community MX lessons http://www.communitymx.com/abstract.cfm?cid=3D074
    http://www.adobe.com/cfusion/designcenter/search.cfm?product=Dreamweaver&go=Go
    The Contact Form Solution Pack is only $29.99. To learn more, visit
    http://www.webassist.com/go/cfsp
    Web advisor extension to DW
    Date and Time through Javascript http://www.mediacollege.com/internet/javascript/date-time/

  • Bounded Taskflows with Fragments And Return Activity

    Hi,
    I have Bounded Taskflow that requires-transaction and based on fragments, which means I should commit or rollback at the end of the taskflow. Since the documentation clearly states that you shouldn't add return activities to Bounded taskflows with fragments, I could call this taskflow from another taskflow and add it to the page as a region. Is it a best practice?
    Best Regards,
    Salim

    Hi,
    in the usecase you describe - yes
    Frank

  • How to use bounding theme with jdbc theme

    Hello
    I'm using oracle maps javascript API to locate geometry with jdbc theme based foi, so how to use bounding theme with jdbc theme? Give me help, thanks.

    Hi,
    You can find some information & code samples at this link :-
    http://download-west.oracle.com/docs/cd/B19306_01/java.102/b14355/xadistra.htm#g1077100
    Regards,
    Sandeep

  • I dont know my security questions and the email with those questione is not correct

    i dont know my security questions and the email with those questione is not correct

    I would contact Apple ID Support.
    Apple ID Support

  • Hi any body having configuration material on Generic with holding tax

    Hi SAP Guru's
    Any body having configuration material on Generic with holding tax
    Please help me on this because presently i am working UK porject when i am executing transaction code S_P00_07000134  getting no list genarated
    Please give me some inputs
    Regards,
    satya

    Hello abhijeeth,
    Sent!! chk u r mail for the notes.
    Regrads,
    shashikanth,
    Asssign points for usefull answer.

  • I forgot my iCloud password  also the questions that were associated with the account please is there anyway to resolve this

    i forgot my iCloud password and the questions that were associated with the account is there any way to resolve this

    No one here can help you.
    Contact Apple.

  • Hi. Quick question I am working with Panasonic GH3 footage and it is playing back in slow motion?

    Hi. Quick question I am working with Panasonic GH3 footage shot using .MOV wrapper with their IPB 50mbps and All-Intra 72MBPS codecs and they are playing back in slow motion in Sg CC. The footage works fine in PP CC and I have no clue on how to fix it. Is this a bug or do I a have a setting wrong some place?

    Yes it turns red this is silly PP and AE have no problem with playback for these codecs. So it is indeed Sg not handling the codec correctly and not my system?
    What if I transcoded it to something else other than the previous what would you reccommend?
    I was going to transcode to ProRes but 5DtoRGB does not work on Windows with GH3 codecs.
    What is the correct time warp seting under clip properties? I screwed with it and forgot the default setting.
    Is there a way to reset Sg back to defaults settings globaly. I tried to many settings trying to fix the playback and may have left something in the wrong state.
    Thanks...

  • A question about a method with generic bounded type parameter

    Hello everybody,
    Sorry, if I ask a question which seems basic, but
    I'm new to generic types. My problem is about a method
    with a bounded type parameter. Consider the following
    situation:
    abstract class A{     }
    class B extends A{     }
    abstract class C
         public abstract <T extends A>  T  someMethod();
    public class Test extends C
         public <T extends A>  T  someMethod()
              return new B();
    }What I want to do inside the method someMethod in the class Test, is to
    return an instance of the class B.
    Normally, I'm supposed to be able to do that, because an instance of
    B is also an instance of A (because B extends A).
    However I cannot compile this program, and here is the error message:
    Test.java:16: incompatible types
    found   : B
    required: T
                    return new B();
                           ^
    1 errorany idea?
    many thanks,

    Hello again,
    First of all, thank you very much for all the answers. After I posted the comment, I worked on the program
    and I understood that in fact, as spoon_ says the only returned value can be null.
    I'm agree that I asked you a very strange (and a bit stupid) question. Actually, during recent months,
    I have been working with cryptography API Core in Java. I understood that there are classes and
    interfaces for defining keys and key factories specification, such as KeySpec (interface) and
    KeyFactorySpi (abstract class). I wanted to have some experience with these classes in order to
    understand them better. So I created a class implementing the interface KeySpec, following by a
    corresponding Key subclass (with some XOR algorithm that I defined myself) and everything was
    compiled (JDK 1.6) and worked perfectly. Except that, when I wanted to implement a factory spi
    for my classes, I saw for the first time this strange method header:
    protected abstract <T extends KeySpec> T engineGetKeySpec
    (Key key, Class<T> keySpec) throws InvalidKeySpecExceptionThat's why yesterday, I gave you a similar example with the classes A, B, ...
    in order to not to open a complicated security discussion but just to explain the ambiguous
    part for me, that is, the use of T generic parameter.
    The abstract class KeyFactorySpi was defined by Sun Microsystem, in order to give to security
    providers, the possibility to implement cryptography services and algorithms according to a given
    RFC (or whatever technical document). The methods in this class are invoked inside the
    KeyFactory class (If you have installed the JDK sources provided by Sun, You can
    verify this, by looking the source code of the KeyFactory class.) So here the T parameter is a
    key specification, that is, a class that implements the interface KeySpec and this class is often
    defined by the provider and not Sun.
    stefan.schulz wrote:
    >
    If you define the method to return some bound T that extends A, you cannot
    return a B, because T would be declared externally at invocation time.
    The definition of T as is does not make sense at all.>
    He is absolutely right about that, but the problem is, as I said, here we are
    talking about the implementation and not the invocation. The implementation is done
    by the provider whereas the invocation is done by Sun in the class KeyFactory.
    So there are completely separated.
    Therefore I wonder, how a provider can finally impelment this method??
    Besides, dannyyates wrote
    >
    Find whoever wrote the signature and shoot them. Then rewrite their code.
    Actually, before you shoot them, ask them what they were trying to achieve that
    is different from my first suggestion!
    >
    As I said, I didn't choose this method header and I'm completely agree
    with your suggestion, the following method header will do the job very well
    protected abstract KeySpec engineGetKeySpec (Key key, KeySpec key_spec)
    throws InvalidKeySpecException and personally I don't see any interest in using a generic bounded parameter T
    in this method header definition.
    Once agin, thanks a lot for the answers.

  • Question about reading generic tables with keys

    Hello
    I'm wondering if it's possible to read a generic table with key statement:
    FIELD-SYMBOLS:
        <lv_key>         TYPE zconf_key,
        <lt_data_bi>     TYPE INDEX TABLE,
        <lt_data_ai>     TYPE INDEX TABLE,
        <ls_data_bi>     TYPE any,
        <ls_data_ai>     TYPE any.
    * create data types
      CREATE DATA lt_data_bi TYPE (ls_node-data_table_type).
      ASSIGN lt_data_bi->* TO <lt_data_bi>.
      CREATE DATA lt_data_ai TYPE (ls_node-data_table_type).
      ASSIGN lt_data_ai->* TO <lt_data_ai>.
    * do check
      LOOP AT <lt_data_ai> ASSIGNING <ls_data_ai>.
        ASSIGN COMPONENT 'KEY' OF STRUCTURE <ls_data_ai> TO <lv_key>.
        READ TABLE <lt_data_bi> WITH KEY key = <lv_key>
        ASSIGNING <ls_data_bi> BINARY SEARCH.
    I receive the message: "The specified type has no structure and therefore no component called KEY" in the read statement above.
    Does anyone has an idea what I can do here?
    Regards,
      Mathias

    Mathias Glockner wrote:
    >     READ TABLE <lt_data_bi> WITH KEY key = <lv_key>
    In your structure ls_data_bi there is no field called KEY . please check whether you have field KEY in structure
    a®s

Maybe you are looking for

  • Mac Pro turns itself on every day at the same time

    My Mac Pro suddenly decided to start turning itself on every day at 6:00:12pm local time (12:00:12AM GMT). It turns on at exactly 12 seconds after 6:00pm every day. I recently upgraded to OSX 10.5 and installed the point update. It did it before the

  • [865PE/G Neo2 Series] Why do I have a big ol' amount of space reserved for an MFT on my SATA drive?

    The hard drive is on the promise controller and for some reason it has a large amount of space used for the MFT when even my system drive and my other storage drive, both located on the INTEL SATA ports do not have this space set aside.

  • HP Laptop Power Cord Safety Recall and Replacemen​t

    Some power cords for HP and Compaq and mini notebook computers sold from September 2010 through June 2012 may be part of voluntary recall and replacement program launched on August 26, 2014. The impacted power cords have the potential to overheat, po

  • PO vendor not in company code

    Hi, We have two company code in SAP, these two company codes share the same purchasing org. For one vendor exist in company code:A, not exist in company code:B, it is still successful of using this vendor to create a PO of company code:B. It is stran

  • Problem in update snow leopard 10.6. 2 to version 10.6.4 on Imac 27 "I7

    Problem in update snow leopard 10.6. 2 to version 10.6.4 on Imac 27 "I7 When do the automatic update of version 10.6.2. to version 10.6.4 when it comes to "Apple" and the hourglass to "think" that moment appears on the screen too fast some risks. Doe