This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Typed parameters  RSS feed

 
Mohamed Farouk
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • 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
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sun's information on enums
 
Mohamed Farouk
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • 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..
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • 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
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • 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.)

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!