Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

EnumSet + contains: strange behavior

Thread view: 
Ulrich Scholz - 31 May 2006 16:05 GMT
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
   }
}
}
Oliver Wong - 31 May 2006 16:34 GMT
> Dear all,
>
[quoted text clipped - 41 lines]
> }
> }

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

   - Oliver
Ulrich Scholz - 31 May 2006 16:37 GMT
Oops!  Thanks

>     There are semicolons after the if statement. Get rid of them.
>
>     - Oliver
Thomas Hawtin - 31 May 2006 19:29 GMT
>>    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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Dale King - 01 Jun 2006 04:00 GMT
>>>    if(result.contains(EnumTest.ONE));
>>>    {
[quoted text clipped - 3 lines]
> 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.
Signature

 Dale King

Oliver Wong - 01 Jun 2006 15:30 GMT
>>>>    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
Ulrich Scholz - 02 Jun 2006 09:58 GMT
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
Patricia Shanahan - 02 Jun 2006 14:01 GMT
> 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
Dale King - 02 Jun 2006 14:48 GMT
>> 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
[quoted text clipped - 10 lines]
>   // 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

Signature

 Dale King

Christian Kaufhold - 04 Jun 2006 11:56 GMT
> 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;

> }
Oliver Wong - 02 Jun 2006 14:52 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.