• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to allow both text and numeric range in enum  RSS feed

 
Tarun Jain
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a java enum already created with certain values as

ABC("abc"), DEF("def");

Now i have a requirement to include numeric value between 0-255 in above enum.
like 0("0"), 1("1")... and so on along with above values.

But writing all these values could be hectic as the max value could be large and not decided yet.

Can we somehow add a range like 0-255 which returns same output along with text fields already there.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Tarun Jain wrote:Can we somehow add a range like 0-255 which returns same output along with text fields already there.

You need not do anything. When you invoke the toString() on 1("1") it will return "1". Even toString() on 1("42") will return "1"*
Of course this is presuming you have not explicitly overridden the toString()
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot create an enum like this:-… because identifiers cannot start with a number.

Please explain more of what you want; an enum with 255 elements would be awkward to use, and really difficult for maintenance if you ever change the required range. What if you decide you need values up to 65335? Or even worse values in the range −32768…32767
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And yes, Welcome to the Ranch!!!
 
Tarun Jain
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a filed named protocol it can take value like tcp,udp etc and also accepts numbers.
Currently numbers are not supported and enum is used for text values.
Now i have to support numbers.
So can i use same enum to support numbers also.
I need values for 0-255 right now
and i aslo find it difficult to maintain for all numbers ad hence tried to ask for suggestions here.
Thanks for reply all.
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do the numbers represent? TCP/UDP port numbers?
 
Tarun Jain
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No It represents the protocol numbers like
1 - UDP
6 - TCP
112 - VRRP etc
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would use you the ENUM? Are you just looking for a way to map the protocol numbers to their menonics?

There are around 150 L4 protocols regsitered, but only a handful of them are commonly used. Do you need to include all of them?
 
Tarun Jain
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am checking the value of protocol from this enum at various places and this enum object is passed in later functions to perform processing.
Right now it does not support any numeric value.
Current requirement is to support only single numeric value but it can change with time to multiple values.
So wondering if it can be achieved easily using present enum only
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't use the protocol names or abbreviations instead, can you?

And why do they have to be in an enum? There must be other ways to record protocols.
 
Tarun Jain
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell Ritchie : Yes i completely agree with you but as it was already used in the design thats why i am checking if it can be done using this enum only else i have to change a lot of things to make it work and that might be not acceptable also.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you take out a contract on whoever wrote that code ?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!