Better database backed-up enum

After the code that uses javassist to generate bytecode for the enum that can be loaded from the database, I created full java solution for enum that is partially defined in code and partially read from the database. So here is the code:

import java.util.HashMap;
import java.util.Map;

public class Sport {

    private int id;
    private SportName name;
    private String additionalProp;

    public int getId(){
        return this.id;
    }

    public SportName getName(){
        return this.name;
    }

    public String getAdditionalProp(){
        return this.additionalProp;
    }

    public Sport(int id, String name, String additionalProp){
        this.id = id;
        this.name = Name.getOrCreateSportName(id, name);
        this.additionalProp = additionalProp;
    }

    private interface SportName{}

    private static class SportNameImpl implements SportName{
        private final String name;

        protected SportNameImpl(String name){
            this.name = name;
        }

        @Override
        public String toString() {
            return this.name;
        }
    }

    public enum Name implements SportName{
        SOCCER(1, "Soccer"),
        BASKETBALL(2, "Basketball"),
        TENNIS_CLAY(3, "Tennis"),   // 1.GroupClay -> 3.Tennis (in case 2 sports can have same name)
        TENNIS_GRASS(4, "Tennis");  // 2.GroupGrass -> 4.Tennis (in case 2 sports can have same name)

        private static final Map<Integer, Name> sportIdToSportNames = new HashMap<>();

        static{
            for(Name sportName : Name.values()){
                sportIdToSportNames.put(sportName.id, sportName);
            }
        }

        private final int id;
        private final String name;

        Name(int id, String name){
            this.id = id;
            this.name = name;
        }

        private static SportName getOrCreateSportName(int id, String name){
            SportName sportName = sportIdToSportNames.get(id);
            if(sportName == null){
                sportName = new SportNameImpl(name);
            }
            return sportName;
        }

        @Override
        public String toString() {
            return this.name;
        }
    }

}

import java.util.Arrays;
import java.util.List;

public class noviEnumProg {

    public static void main(String[] args) {
        Sport sport = new Sport(1, "Soccer", "additionalpro");
        Sport s4 = new Sport(100, "badminton", "badmintondodatni");
        Sport s2 = new Sport(2, "BASKABALLAAA", "dodatnozabasket");
        Sport s3 = new Sport(3, "HOKEJ", "HOKEJD");

        List<Sport> sports = Arrays.asList(sport, s2, s3, s4);

        if(sport.getName() == Sport.Name.SOCCER){
            System.out.println("this is soccer!");
        }

        if(s2.getName() == Sport.Name.BASKETBALL){
            System.out.println("this is basket since id is same");
        }

        if(s3.getName() == Sport.Name.SOCCER){
            System.out.println("this is never true");
        }

        for(Sport sp : sports){
            System.out.println(sp.getName() + " + " + sp.getAdditionalProp());
        }
    }

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s