• 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

Right way to design enums

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a no sql database, it is advised to have a type parameter in all the objects/documents that are saved. This type param is like the table name. So I created the following enum


And when saving the a document, I HAVE do a Type.CUSTOMER.getName(). All this works but I wanted to know if there is a better way to do this.
Pros: standard enum pattern followed everywhere.
Cons: Nothing preventing you to have the same name for 2 different enum values which in this scenario is wrong.(2 tables with same name)
Also if I am using a jackson object mapper, I have to create some kind of custom mapping so that what is saved to database is "customer" and not "CUSTOMER"

If I want to save "CUSTOMER" in the database and not "customer", then all is good as I can simply remove the "name" attribute and we are done. But if I want to store in lower caps one other option is simply to have this:

Much much simpler and solves all the problems listed above, but the thing is that I am not using the standard conventions of using CAPS for enums.
What do you think is the better approach?
Also I though this is a design question so did not post it in "OTHER BIG DATA" forums



 
Marshal
Posts: 4513
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had a similar situation where I needed to be able to store attributes and values for a number of different types of entities in a single table (I was using a SQL database but in a NoSQL way). I didn't want to add an additional field to the entity class which contained non-entity data, and I didn't want to have an Enum class which I would need to update as the entities which were getting persisted changed.

I ended using Annotations and Reflection to add metadata to the entity classes and read the metadata when storing to and retrieving from the data store. Same as how you markup your classes with Json-related information for Jackson.

For example - this a an entity class with a @StorageEntity annotation to indicate the type to use with the data store, and @StorageAttribute annotations indicating the name the data store should use for the field. I also have a @Metadata annotation which is used to by the data store to validate the data before persisting it, and by the web server service to provide resource metadata. I also use reflection to determine the Java data type for each field then convert to/from the text form which is used by the data store (everything is stored as a text representation).


The @StorageEntity annotation looks like this:


A method to read the StorageEntity value could look like this:

 
andy kumar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For a simple requirement like mine, I am planning to just create an enum with lower camel case. The only issue is that it is not the standard way to code enum. Any other suggestions/ comments? If not I will simple go with my solutions of creating enums the non standard way
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Introduce a Decorator and have it either get the String to use from the Enum or map Enum values to corresponding string values to use. I think I'd prefer the latter since that allows me to keep the Enum code to the bare minimum and concentrate everything about the display string in the decorator.

Doing this allows you to create multiple decorators for different purposes. Say JSON expects a different convention for the names it uses, or say you want to convert to Python convention using all lower case and underscores.

Honestly, I don't know if Decorator is the right name for this but that's what's coming to mind right now. Somebody correct me if I'm wrong, please.

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sure that if you create multiple decorators, you could probably spot a pattern and extract generalizations and parameterize variations. That's left to you as an exercise.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic