4

I want to do a Country enum from which i can access to its states, how can i do that?

Something like this:

public enum SomeEnum {

     ARGENTINA {
       BUENOS_AIRES;
     }

     UNITED_STATES {
       CALIFORNIA, FLORIDA, NEW_YORK, ALASKA;
     }

}

SomeEnum state1 = SomeEnum.ARGENTINA.BUENOS_AIRES
SomeEnum state2 = SomeEnum.UNITED_STATES.CALIFORNIA;
Luis Veliz
  • 247
  • 4
  • 8

4 Answers4

7

You could use a interface like

interface Country {
    Country USA = Americas.USA;

    enum Asia implements Country {
        Indian,
        China,
        SriLanka
    }
    enum Americas implements Country {
        USA,
        Brazil
    }
    enum Europe implements Country {
        UK,
        Ireland,
        France
    }
}

and you can have

Country c = Country.USA;
c = Country.Asia.China;
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

You can't assign a nested enum to its parent like

SomeEnum state1 = SomeEnum.ARGENTINA.BUENOS_AIRES

What you can do is

class Test {
    SomeEnum someEnum = SomeEnum.SRILANKA;
    SomeEnum.UNITED_STATES us = SomeEnum.UNITED_STATES.CALIFORNIA;
    SomeEnum.ARGENTINA argentina = SomeEnum.ARGENTINA.BUENOS_AIRES;
}

enum SomeEnum {
    SRILANKA;

    enum ARGENTINA {
        BUENOS_AIRES;
    }

    enum UNITED_STATES {
        CALIFORNIA, FLORIDA, NEW_YORK, ALASKA;
    }
}

Here you can't assign UNITED_STATES and ARGENTINA to a type of SomeEnum

  • Depending on the requirements, the design can be elaborated on. For example, if you want t o be able to assign a state from any country to a variable `myState`, you may declare a marker `interface State {}` and have the inner enums implement it (`enum ARGENTINA implements State …`), so `State myState = SomeEnum.ARGENTINA.BUENOS_AIRES;` will work. – Ole V.V. Oct 05 '16 at 06:21
  • 1
    They could all implement the same `interface Country` – Peter Lawrey Oct 05 '16 at 07:20
1

I suggest you look for a different approach to achieving this. If you can think of a way of 'nesting' the states in the countries then you are going to have the issue of not having a single type for all states. You won't be able to have a state variable that can be assigned a state of the US or a state of Argentina.

Here is an alternative model you could consider:

public interface State {
}

private enum UnitedStates implements State {
    CALIFORNIA, ...;
}

private enum ArgentinaStates implements State {
    BUENOS_AIRES, ...;
}

public enum Country {
        SRILANKA(),
        US(UnitedStates.values()),
        ARGENTINA(ArgentinaStates.values());

    Country(State... states) {
        this.states = Arrays.toList(states);
    }

    private final List<State> states;

    public List<State> getStates() {
        return states;
    }
}

Then you can do:

State state;
state = ArgentinaStates.BUENOS_AIRES;
state = UnitedStates.CALIFORNIA;
sprinter
  • 27,148
  • 6
  • 47
  • 78
0

@Thusitha Thilina Dayaratne example was perfect! But if you still want to use the same time like you mentioned:

SomeEnum state1 = SomeEnum.ARGENTINA.BUENOS_AIRES SomeEnum state2 = SomeEnum.UNITED_STATES.CALIFORNIA;

Then just do like this:

    Object state1, state2, state3;

    state1 = SomeEnum.UNITED_STATES.CALIFORNIA;
    state2 = SomeEnum.ARGENTINA.BUENOS_AIRES;
    state3 = SomeEnum.SRILANKA;

    System.out.println(state1);
    System.out.println(state2);
    System.out.println(state3);

And you will get what exactly you wanted!