1
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;
Jonas
  • 121,568
  • 97
  • 310
  • 388
Dmitrii
  • 604
  • 2
  • 9
  • 30
  • Java enums are not equivalent to enums in other languages. They are not meant to have a particular "order" and thus no concept of the "next" one. – Sinkingpoint Aug 15 '14 at 09:17
  • 1
    @Quirliom http://stackoverflow.com/questions/3820149/enum-values-is-an-order-of-returned-enums-deterministic – EpicPandaForce Aug 15 '14 at 09:22
  • @Zhuiden yes the .values method exists but also uses Reflection and thus I wouldn't consider it a good way to do thing. – Sinkingpoint Aug 15 '14 at 09:28

3 Answers3

3

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());
}
Community
  • 1
  • 1
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • thx, but someFunction() is not static and i still can't use this without creating an instance of this enum. this code sample just snippet from all enum, and there is too much of logic in application based on static import. – Dmitrii Aug 15 '14 at 10:22
  • `Planet.MARS` is an instance of the enum, and if you're out of ideas, then so is `Planet.values()[0]` – EpicPandaForce Aug 15 '14 at 10:50
  • i know about this, i mean in outer classes i use simple construction setNext() and then every class, which imported this enum as static, will be get current state via getCurrent(). – Dmitrii Aug 15 '14 at 11:17
  • 1
    oh you want a singleton selected field for the enum. You can hack that in easily by making the `Planet planet;` be a field variable of the enum itself – EpicPandaForce Aug 15 '14 at 11:20
  • Well I had to make that static, but I think it works pretty okay. – EpicPandaForce Aug 15 '14 at 11:40
  • 1
    nice hack, changed return values()[(ordinal() + 1) % values().length]; and it works like a champ, ty. – Dmitrii Aug 15 '14 at 12:01
2

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();
Boann
  • 48,794
  • 16
  • 117
  • 146
  • in my case there is no way in creating an instance of enum. – Dmitrii Aug 15 '14 at 10:30
  • @Dmitrii You already have an instance. In fact you have 8 instances: one for each planet. That's what enum constants are: instances of the enum's class. – Boann Aug 15 '14 at 10:36
  • i use this enum from outer class: import static Planet.*; setNextCurrent(); all i need from here to see what exactly currentPlanet is – Dmitrii Aug 15 '14 at 10:41
  • @Dmitrii It doesn't matter that the import is static. Each constant is still an instance of the enum class, even though it's a static *member* of the class. Just try it and you'll see it works: `Planet p = MERCURY; p = p.getNext(); System.out.println(p)`. – Boann Aug 15 '14 at 10:43
0
    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();
        }
    }
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Dmitrii
  • 604
  • 2
  • 9
  • 30
  • yeah, that's pretty much what I added in my edit, but I also made it possible to iterate through all enum values using the setNext() function so that there would be an indicator that you've processed every element. – EpicPandaForce Aug 16 '14 at 21:01