Question

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!

Was it helpful?

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.

OTHER TIPS

1,EnumMap is a Map whose key is an Enum;

2, for(State state : State.values())... is an convenient form of iterating all item in State enum;

3,<> is generic type in Java,like template in C++,It means the specific Type will be descided in runtime,

4,the last piece of code looks like State pattern:different state,different implementations

EnumMap is a specialized Map implementation for use with enum type keys. All of the keys in an enum map must come from a single enum type that is specified, explicitly or implicitly. Here the Enum is State.

Now, in this line

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

You create an object of type EnumMap where State Enum is used as key. It is enclosed within <> because State could be replaced with any other Enum also. <> Suggests that that type is Generic. You can use any other enum in place of State.

Next,

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

This For loop is used for iterating all the EnumMap values One by one just as we iterate array elements one by one. ":" is used for iterating in many languages. Its a part of Syntax of Java foreach loop. In this loop Key and values are assigned for each index of RENDERERS_MAP using the put Method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top