EnumSet + contains: strange behavior

Discussion in 'Java' started by Ulrich Scholz, May 31, 2006.

  1. Dear all,

    The following example of EnumTest seems to be inconsitent with the
    interface definition of contains: It executes the lines marked with
    XXX, which clearly is an error. Does anyone have an explanation?

    Thank you,

    Ulrich


    import java.util.*;

    public class EnumSetTest
    {

    public enum EnumTest
    {
    ONE,
    TWO,
    }

    public final void testEnumSet()
    {
    final EnumSet<EnumTest> result = EnumSet.noneOf(EnumTest.class);

    if(result.isEmpty())
    {
    System.out.println("empty"); // enters this branch: correct
    }
    else
    {
    System.out.println("not empty");
    }

    if(result.contains(EnumTest.ONE));
    {
    System.out.println("error"); // XXX
    }

    if(result.contains(EnumTest.TWO));
    {
    System.out.println("error"); // XXX
    }
    }
    }
     
    Ulrich Scholz, May 31, 2006
    #1
    1. Advertising

  2. Ulrich Scholz

    Oliver Wong Guest

    "Ulrich Scholz" <> wrote in message
    news:...
    > Dear all,
    >
    > The following example of EnumTest seems to be inconsitent with the
    > interface definition of contains: It executes the lines marked with
    > XXX, which clearly is an error. Does anyone have an explanation?
    >
    > Thank you,
    >
    > Ulrich
    >
    >
    > import java.util.*;
    >
    > public class EnumSetTest
    > {
    >
    > public enum EnumTest
    > {
    > ONE,
    > TWO,
    > }
    >
    > public final void testEnumSet()
    > {
    > final EnumSet<EnumTest> result = EnumSet.noneOf(EnumTest.class);
    >
    > if(result.isEmpty())
    > {
    > System.out.println("empty"); // enters this branch: correct
    > }
    > else
    > {
    > System.out.println("not empty");
    > }
    >
    > if(result.contains(EnumTest.ONE));
    > {
    > System.out.println("error"); // XXX
    > }
    >
    > if(result.contains(EnumTest.TWO));
    > {
    > System.out.println("error"); // XXX
    > }
    > }
    > }
    >


    There are semicolons after the if statement. Get rid of them.

    - Oliver
     
    Oliver Wong, May 31, 2006
    #2
    1. Advertising

  3. Oops! Thanks

    > There are semicolons after the if statement. Get rid of them.
    >
    > - Oliver
     
    Ulrich Scholz, May 31, 2006
    #3
  4. Oliver Wong wrote:
    >
    > "Ulrich Scholz" <> wrote in message
    > news:...
    >>
    >> if(result.contains(EnumTest.ONE));
    >> {


    > There are semicolons after the if statement. Get rid of them.


    I once spent a day or two trying to optimise some embedded code before I
    realised I had a semicolon in the middle of an if statement (pessimising
    the code would have actually made it sound better).

    So, always put opening braces where god intended (and as instructed by
    the Java coding standard). And never treat them as optional in
    if/else/for/do/while statements (except in the else-if chain
    idiom).</code-style-police>

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, May 31, 2006
    #4
  5. Ulrich Scholz

    Dale King Guest

    Thomas Hawtin wrote:
    > Oliver Wong wrote:
    >>
    >> "Ulrich Scholz" <> wrote in message
    >> news:...
    >>>
    >>> if(result.contains(EnumTest.ONE));
    >>> {

    >
    >> There are semicolons after the if statement. Get rid of them.

    >
    > I once spent a day or two trying to optimise some embedded code before I
    > realised I had a semicolon in the middle of an if statement


    Lucky you. Early in my career I spent a week trying to debug a problem
    like this (and I use that term loosely because the only debugging tool I
    had was printf and this was in an interrupt so I couldn't call printf).
    After a week I decided to scrap it and rewrite it without the
    interrupts. It wasn't until I was half-way done rewriting it that I
    found the semicolon. And of course I didn't keep a copy of the old code.
    Doh!

    > (pessimising the code would have actually made it sound better).


    Did you ever read the famous "Pessimal algorithms and simplexity
    analysis" paper? It is a must read:

    http://www.cs.uiuc.edu/class/fa05/cs473ug/resources/pessimal-algorithms.pdf

    > So, always put opening braces where god intended (and as instructed by
    > the Java coding standard). And never treat them as optional in
    > if/else/for/do/while statements (except in the else-if chain
    > idiom).</code-style-police>


    And using a tool like checkstyle to enforce it.
    --
    Dale King
     
    Dale King, Jun 1, 2006
    #5
  6. Ulrich Scholz

    Oliver Wong Guest

    "Dale King" <"DaleWKing [at]gmail [dot] com"> wrote in message
    news:...
    > Thomas Hawtin wrote:
    >> Oliver Wong wrote:
    >>>
    >>> "Ulrich Scholz" <> wrote in message
    >>> news:...
    >>>>
    >>>> if(result.contains(EnumTest.ONE));
    >>>> {

    >>
    >>> There are semicolons after the if statement. Get rid of them.

    [...]
    >> So, always put opening braces where god intended (and as instructed by
    >> the Java coding standard). And never treat them as optional in
    >> if/else/for/do/while statements (except in the else-if chain
    >> idiom).</code-style-police>

    >
    > And using a tool like checkstyle to enforce it.


    FWIW, here's how I discovered the problem: I took the OP's SSCCE, copied
    and pasted it into Eclipse, and hit CTRL-F to have Eclipse automatically
    format the code for me. I always do this before actually reading the SSCCE,
    because USENET tends to mangle the indentation. Eclipse formatted the code
    as:

    if(result.contains(EnumTest.ONE))
    ;
    {
    bla bla bla;
    }

    so it was immediately obvious to me what the problem was.

    - Oliver
     
    Oliver Wong, Jun 1, 2006
    #6
  7. My suggestion is to have Eclipse mark empty block the same way as,
    e.g., unused variables are marked. I had a short look at the Eclipse
    homepage but I couldn't find the proper place to place this suggestion.
    Any ideas?

    Ulrich
     
    Ulrich Scholz, Jun 2, 2006
    #7
  8. Ulrich Scholz wrote:
    > My suggestion is to have Eclipse mark empty block the same way as,
    > e.g., unused variables are marked. I had a short look at the Eclipse
    > homepage but I couldn't find the proper place to place this suggestion.
    > Any ideas?
    >
    > Ulrich
    >


    If they do that, it needs to be balanced by some annotation or other
    convention to indicate that the programmer really meant the empty block.
    Before annotations, I used:

    while(some_complex_condition){
    // EMPTY
    }

    Patricia
     
    Patricia Shanahan, Jun 2, 2006
    #8
  9. Ulrich Scholz

    Dale King Guest

    Patricia Shanahan wrote:
    > Ulrich Scholz wrote:
    >> My suggestion is to have Eclipse mark empty block the same way as,
    >> e.g., unused variables are marked. I had a short look at the Eclipse
    >> homepage but I couldn't find the proper place to place this suggestion.
    >> Any ideas?
    >>
    >> Ulrich
    >>

    >
    > If they do that, it needs to be balanced by some annotation or other
    > convention to indicate that the programmer really meant the empty block.
    > Before annotations, I used:
    >
    > while(some_complex_condition){
    > // EMPTY
    > }


    And this is why I suggested checkstyle which has an eclipse plug-in. You
    can configure it to generate warnings or errors in Eclipse if you are
    missing braces, have empty statements (standalone ;), or empty blocks.
    The empty block check can be configured to require an actual statement
    or just text (i.e. a comment). You can also configure it differently
    based on what type of block (e.g. allow only comments for catch but
    require a statement for if and else).

    See:
    http://checkstyle.sourceforge.net/config_blocks.html#NeedBraces
    http://checkstyle.sourceforge.net/config_coding.html#EmptyStatement
    http://checkstyle.sourceforge.net/config_blocks.html#EmptyBlock

    --
    Dale King
     
    Dale King, Jun 2, 2006
    #9
  10. Ulrich Scholz

    Oliver Wong Guest

    "Ulrich Scholz" <> wrote in message
    news:...
    > My suggestion is to have Eclipse mark empty block the same way as,
    > e.g., unused variables are marked. I had a short look at the Eclipse
    > homepage but I couldn't find the proper place to place this suggestion.
    > Any ideas?


    https://bugs.eclipse.org/bugs/enter_feature.cgi

    - Oliver
     
    Oliver Wong, Jun 2, 2006
    #10
  11. Patricia Shanahan <> wrote:

    > If they do that, it needs to be balanced by some annotation or other
    > convention to indicate that the programmer really meant the empty block.


    > while(some_complex_condition){


    continue;

    > }
     
    Christian Kaufhold, Jun 4, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green

    EnumSet, what the ?

    Roedy Green, Jul 8, 2005, in forum: Java
    Replies:
    6
    Views:
    1,944
    George Cherry
    Jul 9, 2005
  2. George Cherry

    EnumSet--what the...?

    George Cherry, Jul 9, 2005, in forum: Java
    Replies:
    0
    Views:
    2,397
    George Cherry
    Jul 9, 2005
  3. Roedy Green

    EnumSet Generics puzzle

    Roedy Green, Aug 18, 2005, in forum: Java
    Replies:
    11
    Views:
    1,595
    Thomas Hawtin
    Aug 22, 2005
  4. Eric Smith
    Replies:
    19
    Views:
    2,482
    Eric Smith
    May 17, 2007
  5. Roedy Green

    Enumset.contains

    Roedy Green, Nov 9, 2012, in forum: Java
    Replies:
    6
    Views:
    508
    Joshua Cranmer
    Nov 11, 2012
Loading...

Share This Page