2012-10-01 133 views
0

我露出一个枚举作为在序遍历树结构(迭代器使用这些枚举常数来决定如何遍历树)指南:隐藏的枚举常量

/** 
* The result type of an {@link IVisitor} implementation. 
* 
* @author Johannes Lichtenberger, University of Konstanz 
*/ 
public enum EVisitResult { 
    /** Continue without visiting the siblings of this node. */ 
    SKIPSIBLINGS, 

    /** Continue without visiting the descendants of this node. */ 
    SKIPSUBTREE, 

    /** Continue traversal. */ 
    CONTINUE, 

    /** Terminate traversal. */ 
    TERMINATE, 

    /** Pop from the right sibling stack. */ 
    SKIPSUBTREEPOPSTACK 
} 

但是最后枚举常量仅用于内部访问者,不应该从使用公共API的用户使用。任何想法如何我可以隐藏“SKIPSUBTREEPOPSTACK”?

+2

彼得的答案很可能是做的最简单的方法这个。但我的第一本能是看看是否有方法来重构代码,以避免需要私有枚举值。 – Alex

+0

那时候我真的想过添加常量,但它是迭代器/迭代器在删除子树时工作的唯一方法(并且可能在删除后还可能合并相邻的TextNode)。 – Johannes

回答

3

你所能做的就是不应该使用的文档。

另一种方法是使用一个接口

public interface EVisitResult { 
} 

public enum PublicEVisitResult implements EVisitResult { 
    /** Continue without visiting the siblings of this node. */ 
    SKIPSIBLINGS, 

    /** Continue without visiting the descendants of this node. */ 
    SKIPSUBTREE, 

    /** Continue traversal. */ 
    CONTINUE, 

    /** Terminate traversal. */ 
    TERMINATE, 
} 

enum LocalEVisitResult implements EVisitResult { 
    /** Pop from the right sibling stack. */ 
    SKIPSUBTREEPOPSTACK 
} 
+0

+1击败了我的接口解决方法:P – Gamb

+0

是的,只是想到了这种“可扩展的枚举”方法。嗯,我认为没有人会使用公共API来实现接口的想法(希望是;-)):D – Johannes

+0

或者你可以使值@Deprecated –

1

如果你想为公共API和内部实现枚举两个,可以有2个枚举

private enum InternalFoo 
    foo1 
    foo2 
    foox 

private void doFoo(InternalFoo foo) 
    switch(foo) 
     case foo1 
     ... 


----- 


public enum Foo 
    foo1(InternalFoo.foo1) 
    foo2(InternalFoo.foo2) 
    // no foox 

    InternalFoo internal; 
    Foo(InternalFoo internal){ this.internal=internal; } 

public void doFoo(Foo foo) 
    doFoo(foo.internal);