• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Typed parameters

 
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends

Say in an java class we have a method with signature like this
Results getResult(parameter)
How will i make sure that parameter values are only one of those
"OPEN"
"CLOSED"
"BLOKED"
and the client will not be able to pass any other value to the method

There by avoid extra validations when the parameter is just a string

I think Java 5 has new options like Enums and generics but would some one be kind enough to tell how to implement the parameter typing using pre java 5 and post java 5 with an example

Thanks
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the parameter has to be a String, you cannot restrict, at compile time, what values of String can be passed.

If you can use any type of parameter to indicate your three choices (OPEN, CLOSED, BLOCKED), then you should consider using an "enum".

If you are on Java 1.4 or earlier, you can't use "enum", and would probably just use an int.
 
Mohamed Farouk
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your quick reply and The parameter need not be string
I just need an example of implementation using enum and without using enum

thanks..
 
Peter Chase
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sun's information on enums
 
Mohamed Farouk
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your quick reply and The parameter need not be string
I just need an example of implementation using enum and without using enum

thanks..
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mohamed,

if you use int constants, you could do something like this:

That's the way many Swing classes handle constants.

Hope this helps,
Guido
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mohamed Farouk:
Thanks for your quick reply and The parameter need not be string
I just need an example of implementation using enum and without using enum

thanks..



Here's an example of an enum implementation. (But not it's use.)

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill, if the conversion of spaces to underscores isn't really important then your convertString() method isn't really necessary for an enum - each enum has a built-in valueOf() method to convert a string to an enum value, and a toString() method to convert an enum value to a String.

If you pass a string to valueOf() for which there is no corresponding enum value, an IllegalArgumentException is thrown.
 
reply
    Bookmark Topic Watch Topic
  • New Topic