Dmitrii
Dmitrii

Reputation: 624

How to get next in static enum

public enum Planet { 
    MERCURY(false),
    VENUS(false),
    EARTH(false),
    MARS(false),
    JUPITER(false),
    SATURN(false),
    URANUS(false),
    NEPTUNE(false); 
}

    public boolean isCurrent;

    Planet(boolean isCurrent){
       this.isCurrent = isCurrent;
    }

    public static void next(){
    if(planet == VENUS){
        VENUS.isCurrent = false;
        EARTH.isCurrent = true;
        MARS.isCurrent = false;
        JUPITER.isCurrent = false;
        SATURN.isCurrent = false;
        URANUS.isCurrent = false;
        NEPTUNE.isCurrent = false;
    }
    if(planet == EARTH){
        VENUS.isCurrent = false;
        EARTH.isCurrent = false;
        MARS.isCurrent = true;
        JUPITER.isCurrent = false;
        SATURN.isCurrent = false;
        URANUS.isCurrent = false;
        NEPTUNE.isCurrent = false;
    }
...  
}

i found this solution,

private enum Planet { MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE;
    public Planet getNext() {
        return this.ordinal() < Planet.values().length - 1
            ? Planet.values()[this.ordinal() + 1]
            : null;
    }
}

but i'm unable to use this cuz this enumeration is imported as static in other classes;

For now i use follow construction:

 public static Planet setNewCurrent(){
    for(Planet planet : Planet.values()){
        if(planet == VENUS){
            VENUS.isCurrent = false;
            EARTH.isCurrent = true;
            MARS.isCurrent = false;
            JUPITER.isCurrent = false;
            SATURN.isCurrent = false;
            URANUS.isCurrent = false;
            NEPTUNE.isCurrent = false;
        }
        if(planet == EARTH){
            VENUS.isCurrent = false;
            EARTH.isCurrent = false;
            MARS.isCurrent = true;
            JUPITER.isCurrent = false;
            SATURN.isCurrent = false;
            URANUS.isCurrent = false;
            NEPTUNE.isCurrent = false;
        }
...
    }
}   

Does anyone know some convenient way to getNextPlanet() like this

planet.getNext().isCurrent = true;

Upvotes: 1

Views: 2309

Answers (3)

Dmitrii
Dmitrii

Reputation: 624

    public enum Planet {
        EARTH,
        MARS;

        public static Planet current = EARTH;

        Planet() {
        }

        public Planet getNext() {
            return values()[(ordinal() + 1) % values().length];
        }

        public static void setNext(){
            current = current.getNext();
        }

        public static Planet getCurrent(){
            return current;
        }


    }

import static com.test.Planet.*;

    public class Main {
        public static void main(String[] args){
            getCurrent();
            setNext();
        }
    }

Upvotes: 0

EpicPandaForce
EpicPandaForce

Reputation: 81539

I hope you do realize that you could just have something like the following:

public enum Planet { 
    MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE; 

    public Planet getNext() {
        return this.ordinal() < Planet.values().length - 1
            ? Planet.values()[this.ordinal() + 1]
            : null;
    }
}

private Planet planet;

public void someFunction()
{
    planet = Planet.MARS;
    planet = planet.getNext();
    if(planet != null)
    {
        doStuff();
    }
}

And of course, you can just use a switch-case statement with the enum based on whichever is selected in the field variable: Java using enum with switch statement

A boolean variable to determine which one is "currently selected" isn't necessary at all.

EDIT: Based on your comment, you want a singleton instance of Planet. Luckily enough, you can use the enum itself for that:

public enum Planet { 
    MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE; 

    private static Planet currentPlanet = MERCURY;

    public static Planet getCurrentPlanet()
    {
        return currentPlanet;
    }

    public static boolean setToNext() {
        boolean retVal = this.ordinal() < Planet.values().length - 1;
        currentPlanet = this.ordinal() < Planet.values().length - 1
                ? Planet.values()[this.ordinal() + 1]
                : Planet.values()[0];
        return retVal; //returns false when it was the last element of the enum
    }
}


public void doSomething()
{
    Planet planet;
    do
    {
        planet = Planet.getCurrentPlanet();
        //do things with planet, like write out their names
        System.out.println(planet.name());
    }
    while(Planet.setToNext());
}

Upvotes: 3

Boann
Boann

Reputation: 50021

That's not the right way to use an enum. You should not have a boolean isCurrent on each enum constant.

Instead, have a Planet currentPlanet variable in one place (not on the enum):

Planet currentPlanet = Planet.MERCURY;

When you want to get the next one do:

currentPlanet = currentPlanet.getNext();

Upvotes: 2

Related Questions