我知道可以使用B作为接口来声明A.B.C
样式的3级枚举,最后使用C作为实现此接口的枚举成员。
但我想嵌套几个枚举来映射具有固定数量成员的常量树结构。当然,像Tree.A.Leaf.B.Node.C.Something.D
或简单A.B.C.D
这样的东西看起来不错。
可能吗?找不到任何实现方法。
谢谢。
更新(结果解决方案):
示例代码作为我自己的答案,以保持问题清晰。 希望这会对其他人有所帮助。
答案 0 :(得分:1)
enum Foods{
drinks, eats;
enum Drinks {
apple_juice, cola;
}
enum Eats{
potatoe, rice;
}
}
尝试打印:Foods.Eats.rice
但它看起来很糟糕并且味道很糟糕!
答案 1 :(得分:0)
最终解决方案代码(通过一些简化以保持清晰)以及使用它的说明(查看main()方法)。这就是我要搜索的内容。
package com.test;
/*
* Umbrella class which defines whole DB schema structure and some of global operations.
**/
public class Schema {
/**
* Schema elements are open for those who need access to them on appropriate level
* (HBase API). It should be encapsulated by business logics as development progresses
* but there's always something which is not covered so we keep them open.
*
* Schema elements are obviously something which gives
* you access to its string name and name prepared to be used by HBase.
*/
public interface NamedEntityInterface {
/**
* Just receive name as it is used by DB.
* @return DB level name as string.
*/
public abstract String getName();
/**
* @return DB level name to be used for HBase API calls.
*/
public abstract byte[] getNameBytes();
}
/**
* NamedEntity class provides most generic implementation for NamedEntityInterface
* widely used through all the schema.
*/
public abstract static class NamedEntity implements NamedEntityInterface {
private final String name;
private final byte[] nameBytes;
private NamedEntity(String name) {
this.name = name;
this.nameBytes = name.getBytes();
}
@Override
public String getName() {
return name;
}
@Override
public byte [] getNameBytes() {
return nameBytes;
}
}
/**
* Column abstraction.
*/
public static class Column extends NamedEntity {
private Column(String name) {
super(name);
}
}
/**
* Column family abstraction.
*/
public static class Family extends NamedEntity {
static final String NAME_DEFAULT = "d";
private Family(String name) {
super(name);
}
}
/**
* Table abstraction.
*/
public static class Table extends NamedEntity {
private Table(String name) {
super(Schema.class.getPackage().getName() + '.' + name);
}
}
public static class TableA extends Table {
public static class FamilyDefault extends Family {
private FamilyDefault() {
super(Family.NAME_DEFAULT);
}
public static final Column a = new Column("b");
public static final Column b = new Column("a");
}
private TableA() {
super("table.A");
}
public static final FamilyDefault familyDefault = new FamilyDefault();
}
public static class TableB extends Table {
public static class FamilyA extends Family {
public static final Column a = new Column("a");
public static final Column b = new Column("b");
private FamilyA() {
super(NAME_DEFAULT);
}
}
public static class FamilyB extends Family {
private FamilyB() {
super("f");
}
}
private TableB() {
super("table.B");
}
public static final FamilyA familyA = new FamilyA();
public static final FamilyB familyB = new FamilyB();
}
static public TableA tableA = new TableA();
static public TableB tableB = new TableB();
public static void main(String [] args) {
String tableName = Schema.tableA.getName();
Family someFamily = Schema.tableA.familyDefault;
byte [] column = Schema.tableA.familyDefault.a.getNameBytes();
String tableBFamilyBName = Schema.tableB.familyB.getName();
System.out.println("name: " + tableBFamilyBName);
}
}