This project can be used by you to generate some Java Enums.
These enums are generated by a Groovy script
called java-enums-generator.groovy that reads a configuration file named config.groovy
with has a sample (config.sample.groovy) that you can you a base.
Java programmers don’t like to waste their time writing enums. Especially if these enums are big and their data comes from a database. You can search for projects with this intention and you will find things like these:
So, for example, as a programmer, you have a challenge to create two (2) separated Java enums to represent the Brazilian states and regions. Obviously, you can create a Java enum to Brazilian regions by your hands with minimal effort because there are only five (5) regions in Brazil. But, things come annoying when you want to create a Java Enum to the Brazilian states cause there are 26 states plus one federal district. And, you want to link each state in its enum with the corresponding region enum.
You think that you go through this problem only one time and it’s ok to write theses Java enums by hand
because they are almost immutable (changes almost never happen).
But, your boss wants that you do the same, again, for the American states …
Hahaha 😄! You will lose your time again!
A better solution to this problem is to build your Java enums from existing and officially APIs (made by government institutions). A Brazilian institution called IBGE provide two URLs that we use in our configuration example to get the regions and states:
Based on the response of these APIs and the configuration written in a Groovy semantics we can build our Java enums in a smart way as you will see through the code of this project.
A personal motivation: I wanted to improve my skills in Groovy programming language by exploring it more.
Any of the following alternatives will build some Java enums inside your current directory.
Try the following commands and you will get a bunch of Java code inside a directory tree com/example/JavaProject/enums
.
$ groovy java-enums-generator.groovy Generating output in directory "sample.test/src/main/java" ... Creating enum com.example.JavaProject.enums.BRRegion ... Creating interface com.example.JavaProject.enums.Region ... Creating enum com.example.JavaProject.enums.BRState ... Creating interface com.example.JavaProject.enums.State ...
You can see, compile and test the generated Java classes with the following commands:
$ tree -a . |-- .gitignore |-- build.gradle |-- gradle | `-- wrapper | |-- gradle-wrapper.jar | `-- gradle-wrapper.properties |-- gradlew |-- gradlew.bat `-- src |-- main | `-- java | `-- com | `-- example | `-- JavaProject | `-- enums | |-- BRRegion.java | |-- BRState.java | |-- Region.java | `-- State.java `-- test `-- groovy `-- com `-- example `-- JavaProject |-- HelloWorldSpec.groovy `-- enums `-- BRStateSpec.groovy 15 directories, 12 files
$ ./gradlew clean test $ open build/reports/tests/test/index.html
Your configuration of the generated Java enums needs to be written in Groovy
in a file called config.groovy
or the file config.sample.groovy
will be used.
After writing it, you can validate its contents by running the following script:
$ groovy config.test.groovy Testing "config.sample.groovy" filled with a content sample ... Configuration should define a map of properties ... ok! Configuration should have a keySet equals to [package_name, set] ... ok! Configuration should have at least one set ... ok!