Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > EnumSet + contains: strange behavior

Reply
Thread Tools

EnumSet + contains: strange behavior

 
 
Ulrich Scholz
Guest
Posts: n/a
 
      05-31-2006
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
}
}
}

 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      05-31-2006

"Ulrich Scholz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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

 
Reply With Quote
 
 
 
 
Ulrich Scholz
Guest
Posts: n/a
 
      05-31-2006
Oops! Thanks

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


 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      05-31-2006
Oliver Wong wrote:
>
> "Ulrich Scholz" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>>
>> 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/
 
Reply With Quote
 
Dale King
Guest
Posts: n/a
 
      06-01-2006
Thomas Hawtin wrote:
> Oliver Wong wrote:
>>
>> "Ulrich Scholz" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) oups.com...
>>>
>>> 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/cs...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
 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      06-01-2006

"Dale King" <"DaleWKing [at]gmail [dot] com"> wrote in message
news:(E-Mail Removed)...
> Thomas Hawtin wrote:
>> Oliver Wong wrote:
>>>
>>> "Ulrich Scholz" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed) oups.com...
>>>>
>>>> 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

 
Reply With Quote
 
Ulrich Scholz
Guest
Posts: n/a
 
      06-02-2006
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

 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      06-02-2006
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
 
Reply With Quote
 
Dale King
Guest
Posts: n/a
 
      06-02-2006
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/co...tml#NeedBraces
http://checkstyle.sourceforge.net/co...EmptyStatement
http://checkstyle.sourceforge.net/co...tml#EmptyBlock

--
Dale King
 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      06-02-2006

"Ulrich Scholz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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

 
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
Subclassing EnumSet to add an interface? Eric Smith Java 19 05-17-2007 02:42 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