• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

5 Valued Variable

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As booleans can have 2 values, true & false, my program needs a new type of variable that has 5 states. The criteria for this concept must be:

1. Easy to understand.

2. short.

3. Hard to make a mistake.

I have come up with the following:

Define an Interface



include the interface in the program, where multiValue is the package containing the interface


The program can then be written:


Does anyone have any comments on how to make the concept better according to the 3 criteria?

Thank you
 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why the interface? enum constitutes a type.
 
Saloon Keeper
Posts: 14700
331
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you should lose the interface and use the enum directly.
 
Richard Mittleman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The interface is used to have a standard line of code that can always be inserted in a program rather than remembering how the enum is defined.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you need to remember how the interface is defined and the enum within the interface instead?

Collecting several enums within a class may be viable, but use a class then and not an interface.
 
Marshal
Posts: 27536
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Mittleman wrote:The interface is used to have a standard line of code that can always be inserted in a program rather than remembering how the enum is defined.



I don't understand that. What "standard line of code" would you be inserting into a program?
 
Richard Mittleman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


would be inserted in any program that wants to use the "ExpandedBoolean".
 
Paul Clapham
Marshal
Posts: 27536
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the enum is in a package then you can import-static it directly. You don't need it to be in an interface. Thus:

instead of

If it isn't in a package then... well, it should be in a package. Only the most trivial things don't need to be in a package, and we aren't talking about trivialities here.
 
Greenhorn
Posts: 2
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not create a class to deal with it?
e.g.
Class ExpandedBoolean{
public enum Type {NO, RARE, MAYBE, OFTEN, YES}

methods to deal with the logic like below,

expandedBoolean doesThisHappen = expandedBoolean.NO;
if (doesThisHappen == expandedBoolean.NO)
System.out.println("Should be NO = " + doesThisHappen);

doesThisHappen = expandedBoolean.RARE;
if (doesThisHappen == expandedBoolean.RARE)
System.out.println("Should be RARE = " + doesThisHappen);

doesThisHappen = expandedBoolean.MAYBE;
if (doesThisHappen == expandedBoolean.MAYBE)
System.out.println("Should be MAYBE = " + doesThisHappen);

doesThisHappen = expandedBoolean.OFTEN;
if (doesThisHappen == expandedBoolean.OFTEN)
System.out.println("Should be OFTEN = " + doesThisHappen);

doesThisHappen = expandedBoolean.YES;
if (doesThisHappen == expandedBoolean.YES)
System.out.println("Should be YES = " + doesThisHappen);

}
 
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have problems with the name ExpandedBoolean. It makes me expect a value FileNotFound. I'd go for the following:
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote: It makes me expect a value FileNotFound.



I was thinking the exact same thing -- you beat me to the punch. TRUE, FALSE, and FILE_NOT_FOUND.
 
Richard Mittleman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone,

I used the import of the enum rather than the interface.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic