Java EnumSet Usage rotateEnum(T ce, boolean backwards, EnumSet ValidOptions)

Here you can find the source of rotateEnum(T ce, boolean backwards, EnumSet ValidOptions)

Description

rotate Enum

License

Open Source License

Declaration

public static <T extends Enum> T rotateEnum(T ce, boolean backwards, EnumSet ValidOptions) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.EnumSet;

public class Main {
    public static <T extends Enum> T rotateEnum(T ce, boolean backwards, EnumSet ValidOptions) {
        do {//ww  w .ja  v a  2s.c om
            if (backwards) {
                ce = prevEnum(ce);
            } else {
                ce = nextEnum(ce);
            }
        } while (!ValidOptions.contains(ce));
        return ce;
    }

    public static <T extends Enum> T prevEnum(T ce) {
        EnumSet valList = EnumSet.allOf(ce.getClass());
        int pLoc = ce.ordinal() - 1;
        if (pLoc < 0) {
            pLoc = valList.size() - 1;
        }
        if (pLoc < 0 || pLoc >= valList.size()) {
            pLoc = 0;
        }
        int pos = 0;
        for (Object g : valList) {
            if (pos == pLoc) {
                //noinspection unchecked
                return (T) g;
            }
            pos++;
        }
        return null;
    }

    public static <T extends Enum> T nextEnum(T ce) {
        EnumSet valList = EnumSet.allOf(ce.getClass());
        int pLoc = ce.ordinal() + 1;
        if (pLoc >= valList.size()) {
            pLoc = 0;
        }
        if (pLoc < 0 || pLoc >= valList.size()) {
            pLoc = 0;
        }
        int pos = 0;
        for (Object g : valList) {
            if (pos == pLoc) {
                //noinspection unchecked
                return (T) g;
            }
            pos++;
        }
        return null;
    }
}

Related

  1. getDataInEnumClass(String enumClassName)
  2. getEnumFromString(Class enumClass, String enumValue, boolean compareByValue)
  3. nativeLoadEnumDefaultValues(Class enumType)
  4. nextEnum(T ce)
  5. possibilities(Class enumClass)
  6. setOnly(EnumSet theSet, E flag)
  7. valueOfIgnoreCase(String text, Class cls)
  8. valueOfOrNull(Class enumType, String name)
  9. valueOfOrNull(Class enumType, String name, boolean checkCase)