2012-10-01 231 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);