开发者

How to nest an Enum inside the value of an Enum

开发者 https://www.devze.com 2022-12-27 09:04 出处:网络
I\'d like to know if it is possible in Java to nest Enums. Here what i\'d like to be able to do : Have an enum Species made of CAT and DOG wich would grant me access to sub enums of the available CA

I'd like to know if it is possible in Java to nest Enums.

Here what i'd like to be able to do :

Have an enum Species made of CAT and DOG wich would grant me access to sub enums of the available CAT and DOG breeds. For example, i'd like to be able to test if wether a CAT or a DOG and if an animal is a PERSAN CAT or a PITBULL DOG. CAT and DOG breeds must be distinct enums ,i.e a CatBreeds enum and a D开发者_开发知识库ogBreeds enum.

Here is an example of access pattern i'd like to use :

Species :

  • Species.CAT
  • Species.DOG

Breeds :

  • Species.CAT.breeds.PERSAN
  • Species.DOG.breeds.PITBULL


Wouldn't you be better doing this with inheritance? Cat and Dog would be subclasses of Species. Persion would be a subclass of Cat and Pitbull would be a subclass of Dog.

What are trying to achieve by wanting to use Enums?


one way to nest enums:

package nested2;
import java.util.*;
interface HasChildren {
    Set<Enum<? extends HasChildren>> children();
    Class parent();
    Enum<? extends HasChildren> p();
}
enum Citrus implements HasChildren {
    lemon, lime, orange;
    Enum<? extends HasChildren> p;
    Class parent = getDeclaringClass();
    Class children;
    public Enum<? extends HasChildren> p() {
        return p;
    }
    public Class parent() {
        return parent;
    }
    public Set<Enum<? extends HasChildren>> children() {
        return children != null ? EnumSet.allOf(children) : null;
    }
    enum Orange implements HasChildren {
        navel, valencia, blood;
        Enum p;
        Class parent = getDeclaringClass();
        Class children;
        public Enum<? extends HasChildren> p() {
            return p;
        }
        public Class<Enum<? extends HasChildren>> parent() {
            return parent;
        }
        public Set<Enum<? extends HasChildren>> children() {
            return children != null ? EnumSet.allOf(children) : null;
        }
        enum Navel implements HasChildren {
            washinton, lateLane, caraCaraPink;
            Enum<? extends HasChildren> p;
            Class parent = getDeclaringClass();
            Class children;
            public Enum<? extends HasChildren> p() {
                return p;
            }
            public Class parent() {
                return parent;
            }
            public Set<Enum<? extends HasChildren>> children() {
                return null;
            }
        }
        static {
            navel.children = Navel.class;
            for (Navel n : Navel.values())
                n.p = orange;
            navel.p = navel;
        }
        enum Blood implements HasChildren {
            moro, taroco;
            Enum<? extends HasChildren> p;
            Class parent = getDeclaringClass();
            Class children;
            public Enum<? extends HasChildren> p() {
                return p;
            }
            public Class parent() {
                return parent;
            }
            public Set<Enum<? extends HasChildren>> children() {
                return null;
            }
        }
        static {
            blood.children = Blood.class;
            for (Blood b : Blood.values())
                b.p = blood;
            blood.p = orange;
        }
    }
    static {
        for (Orange o : Orange.values())
            o.p = orange;
        orange.children = Orange.class;
    }
}
public class EnumTree {
    static void visit(Class<?> clazz) {
        Object[] enumConstants = clazz.getEnumConstants();
        if (enumConstants[0] instanceof HasChildren) for (Object o : enumConstants)
            visit((HasChildren) o, clazz.getName());
    }
    static void visit(HasChildren hasChildren, String prefix) {
        if (hasChildren instanceof Enum) {
            System.out.println(prefix + ' ' + hasChildren + " (" + hasChildren.parent() + ")" + " (" + hasChildren.p()
                    + " " + ((Enum) hasChildren).getDeclaringClass());
            if (hasChildren.children() != null) for (Object o : hasChildren.children())
                visit((HasChildren) o, prefix + ' ' + hasChildren);
        } else
            System.out.println("other " + hasChildren.getClass());
    }
    static <E extends Enum<E> & HasChildren> Set<E> foo() {
        return null;
    }
    public static void main(String[] args) {
        //looks like p is the right parent
        System.out.println(Citrus.Orange.Navel.washinton);
        System.out.println(Citrus.Orange.Navel.washinton.getClass().getDeclaredClasses());
        System.out.println(Citrus.Orange.Navel.washinton.getClass().getEnclosingClass());
        System.out.println(Citrus.Orange.Navel.washinton.getClass().getDeclaringClass());
        visit(Citrus.lemon, "");
        System.out.println("----------------------");
        visit(Citrus.orange, "");
        System.out.println("----------------------");
        visit(Citrus.class);
        System.out.println("----------------------");
    }
}


public enum A {

    a1, 
    a2;

    public enum B {
        b1, 
        b2
    }
}

and so on...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号