Reputation: 521
If I have some many constants like shortName="rule1"; descrition="description1"; rule2/description2; rule3 / description3
? and I need to find out the description for a certain shortName, given by a parameter, what would be the best way to do this?
Would it be a enum
like this?
public enum Description {
RULE1 ("rule1", "description1"),
RULE2 ("rule2", "description2"),
RULE3 ("rule3", "description3");
private final String shortName;
private final String description;
Description(String shortName, String description) {
this.shortName= shortName;
this.description = description;
}
}
But if I have a method like private String getDescription(String shortName)
how can I use the enum to get the description of a shortName declared in enum?
I can't use constants because I have ~200 definitions like this.
Upvotes: 2
Views: 132
Reputation: 340350
You asked:
and I need to find out the description for a certain shortName, given by a parameter, what would be the best way to do this?
Would it be a enum like this?
Yes, if those rule1, rule2, and such are all conceptually members of the same type.
For example:
public enum Pet{ DOG , CAT , BIRD , HAMSTER }
public enum Flavor{ VANILLA , CHOCOLATE, STRAWBERRY }
public enum Color{ BURLYWOOD , CORNFLOWER_BLUE, DARK_SLATE_GREY }
Using an enum such as those means you can write other code that is type-safe, ensures valid values, and is more self-documenting.
pictureMaker.displayIceCreamCone( Flavor.CHOCOLATE )
On the other hand, if your values are unrelated, just a hodgepodge of various strings for various purposes, I would use string constants. And if they are resources for localization, use specific localization tools.
You asked:
But if I have a method like private String getDescription(String shortName) how can I use the enum to get the description of a shortName declared in enum?
That question suggests you are passing around the text of the short name as a key to finding the description. But you should not be passing around some string, you should be passing around the enum object. Take, for example, java.time.DayOfWeek
enum. You should be passing around DayOfWeek.SATURDAY
rather than "SATURDAY"
.
But if you must, you could implement a static method on your enum to loop through all the enum objects to find one that matches.
// Utility method to loop all the enum objects until finding a match.
public static String getLongStringForShortName ( String shorty )
{
String result = null;
if ( RULE1.shortName.equals( shorty ) ) { result = RULE1.description; }
if ( RULE2.shortName.equals( shorty ) ) { result = RULE2.description; }
if ( RULE3.shortName.equals( shorty ) ) { result = RULE3.description; }
return result;
}
Or, in alternative syntax, use streams to softcode references to each and every enum object.
// Utility method to loop all the enum objects until finding a match.
public static String getLongStringForShortName ( String shorty )
{
String result = "";
Optional < Description > optionalDesc = Arrays.stream( Description.values() ).filter( ( Description d ) -> d.description.equals( shorty ) ).findFirst();
if ( optionalDesc.isPresent() ) { result = optionalDesc.get().description; }
return result;
}
Map
But that code has a smell about it. You likely have the wrong data structure if you do this often, or this is your main purpose. This looks like we are abusing the enum where instead should be using a Map
.
Map < String, String > descriptions =
Map.of(
"rule1" , "description1" ,
"rule2" , "description2" ,
"rule3" , "description3"
)
;
String longDesc = descriptions.get( "rule1" );
EnumMap
You could mix the concepts of enum and map. Your Question lacks the context to know if this is right for you or not. But FYI…
Change your enum class to just this:
package work.basil.example;
public enum Description
{
RULE1, RULE2, RULE3;
}
Use an EnumMap
to map each of these enum objects to some other object such as a string.
Map < Description, String > descriptionToLongForm = new EnumMap <>( Description.class );
descriptionToLongForm.put( Description.RULE1 , "description1" );
descriptionToLongForm.put( Description.RULE2 , "description2" );
descriptionToLongForm.put( Description.RULE3 , "description3" );
String longDesc = descriptionToLongForm.get( Description.RULE2 );
Or, in alternative syntax, using Map.of
. This produces a non-modifiable map.
Map < Description, String > descriptionToLongForm =
Map.of(
Description.RULE1 , "description1" ,
Description.RULE2 , "description2" ,
Description.RULE3 , "description3"
);
String longDesc = descriptionToLongForm.get( Description.RULE2 );
Upvotes: 4