Search code examples
javaenumsrenderenum-map

Code Explanation: Enum and Rendering


So I came across this piece of code and being a beginner, i didn't really understand the code. I was wondering if someone could explain to me the code. Thanks!

private static final EnumMap<State, IRenderer> RENDERERS_MAP;
    static {
    RENDERERS_MAP= new EnumMap<State, IRenderer>(State.class);

   for (State state : State.values()) {
        RENDERERS_MAP.put(state, getRender());
   }
}

It's mainly this piece of code ^ code that i needed an explanation about because as a beginner I've never used "<>" and ":". Also I'm not sure what a EnumMap is. Preferably this one can be explained in full detail. What is IRenderer?

EDIT: I also would like what <> and : are called so i can search it up on the internet and read up on them. Links are welcome too :)

void render() {
    Renderer currentRenderer = RENDERERS_MAP.get(currentState);
    if (ren  != null) {
        currentRenderer.render();
     }
}

This is the piece of code that I think renders the code onto the screen.

public enum State {
    START_MENU() {
        @Override
        public Renderer getRenderer() {
            return new StartMenuRenderer();
        }
    },
    PLAYER_ONE_MENU() {
        @Override
        public Renderer getRenderer() {
            return new PlayerOneRenderer();
        }
    },
    PLAYER_TWO_MENU() {
        @Override
        public Renderer getRenderer() {
            return new PlayerTwoRenderer();
        }
    },
    WIN_SCREEN() {
        @Override
        public Renderer getRenderer() {
            return null;  //TODO: implement body
        }
    },
    LOSE_SCREEN() {
        @Override
        public Renderer getRenderer() {
            return null;  //TODO: implement body
        }
    },
    PLAY_SCREEN() {
        @Override
        public Renderer getRenderer() {
            return null;  //TODO: implement body
        }
    };

    public abstract Renderer getRenderer();
}

This is the enum State which is being used to organize all the states in the game.

Thank you for all your help and explanations!


Solution

  • It's mainly this piece of code ^ code that i needed an explanation about because as a beginner I've never used "<>" and ":". Also I'm not sure what a EnumMap is. Preferably this one can be explained in full detail.

    The <> is part of the generics declaration, and simply associates the types you declare to the generics class you are using. For a Map, there are two types, the Key and the Value, which by convention are declared as EnumMap<K,V>

    So when you say:

    RENDERERS_MAP= new EnumMap<State, IRenderer>(State.class);
    

    You are saying that you wish to create an EnumMap where the Map key is a State, and the Map value is an IRenderer. Once you declare your particular EnumMap with your desired types, Java then takes care of casting when retrieving or setting values on the Map.

    So you can say:

    IRenderer myRenderer = RENDERERS_MAP.get(myState);
    

    without explicitly casting.