Giter VIP home page Giter VIP logo

java-enums-generator's Introduction

Java Enums Generator

About

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.

Motivation

One

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.

Two

A personal motivation: I wanted to improve my skills in Groovy programming language by exploring it more.

Actually, today I could produce the same results gained here by using my current knowledge in Python and Jinja or even using my recent skills in Dhall. Later, my goal is to produce some posts by comparing this solution with others that I will produce with these alternatives.

Alternative ways to execute this project

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.

If you have Groovy installed and have cloned this project

$ 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

If you have Docker installed

$ docker run -it -v "$PWD":/mnt paulojeronimo/java-enums-generator
Warning
This does not exist yet. It will be available in the next git commits.

Configuration

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!

java-enums-generator's People

Contributors

paulojeronimo avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.