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

How to code this fine: returning an ArrayList containing a part of an enum's members  RSS feed

 
Dóra Takács
Ranch Hand
Posts: 39
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers!

I've ran into a bit of a block where I dont want to solve a problem by chaining 10 or so of   myList.add(first).add(second)...add(thirteenth);    into a method, or even write them out on separate lines. I haven't been able to come up with any useful or not complicated ideas to solve this, so here is my enum:



The whole thing is because I store all of these in the same database table. For various complicated reasons, this has to stay like this. Thus, to separate the special work times that a vehicle has (in service, in rent, out of service) and the special work times that a person/user has (holiday, sickleave etc) I need these two static methods to be able to conviniently retrieve them.

My question is, how to do the methods more handsomely? Adding the enum members one-by-one is pretty unfriendly for future expansion of the enum.
My thought was to somehow make them into a loop, but I couldn't find an enum attribute that would let me to. Maybe make an individual ID for each, in their constructor? But then I would need an instance of each?
Making a private static int counter burnt in it would be risky of mistakes, too. I'm out of ideas!

Is there a common way to do this that I have overlooked?
I looked on stack overflow but couldn't find an example for this, please rescue my newbie soul!

Best regards and thank you for your time, Dóri
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that a bunch of add() statements would be inelegant, but I did come up with a way to do this in one loop -- but it's a little complex.

It involves adding an argument to the enum, then creating a constructor that sets that argument to a field, then using that field in a static block to create two (or more) lists separated by the value of the field.

So, without giving too much away, you can create you enum like this:

...then write a constructor like this:

See if you can get the rest.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get an array of all elements of an enum with one of its methods, and use a method of the Arrays class (I think) to convert that to a List.
 
Dóra Takács
Ranch Hand
Posts: 39
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for the answers! I wasn't aware of the .values() method that enums have, and giving types was a great idea too. Although I have settled with a way of not-adding vehicle types from the array values() gives back, and only adding that few ones to the other one, I definitely learnt something from the idea, and I might use it in an other enum I have.

Thank you for your times!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!