Java枚舉類如何關聯常值?

 如下兩個類,EventType和KeeperState,是ZooKeeper源碼中枚舉類的實現方法:

 public enum EventType { // 事件類型
            // 無
            None (-1),
            // 結點創建
            NodeCreated (1),
            // 結點刪除
            NodeDeleted (2),
            // 結點數據變化
            NodeDataChanged (3),
            // 結點子節點變化
            NodeChildrenChanged (4);

            // 代表事件類型的整形 
            private final int intValue;     // Integer representation of value
                                            // for sending over wire

            // 構造函數
            EventType(int intValue) {
                this.intValue = intValue;
            }

            // 返回整形
            public int getIntValue() {
                return intValue;
            }

            // 從整形構造相應的事件
            public static EventType fromInt(int intValue) {
                switch(intValue) {
                    case -1: return EventType.None;
                    case  1: return EventType.NodeCreated;
                    case  2: return EventType.NodeDeleted;
                    case  3: return EventType.NodeDataChanged;
                    case  4: return EventType.NodeChildrenChanged;

                    default:
                        throw new RuntimeException("Invalid integer value for conversion to EventType");
                }
            }           
        }
    }
        public enum KeeperState { // 事件發生時Zookeeper的狀態
            /** Unused, this state is never generated by the server */
            @Deprecated
            // 未知狀態,不再使用,服務器不會產生此狀態
            Unknown (-1), 

            /** The client is in the disconnected state - it is not connected
             * to any server in the ensemble. */
            // 斷開
            Disconnected (0),

            /** Unused, this state is never generated by the server */
            @Deprecated
            // 未同步連接,不再使用,服務器不會產生此狀態
            NoSyncConnected (1),

            /** The client is in the connected state - it is connected
             * to a server in the ensemble (one of the servers specified
             * in the host connection parameter during ZooKeeper client
             * creation). */
            // 同步連接狀態
            SyncConnected (3),

            /**
             * Auth failed state
             */
            // 認證失敗狀態
            AuthFailed (4),

            /**
             * The client is connected to a read-only server, that is the
             * server which is not currently connected to the majority.
             * The only operations allowed after receiving this state is
             * read operations.
             * This state is generated for read-only clients only since
             * read/write clients aren't allowed to connect to r/o servers.
             */
            // 只讀連接狀態
            ConnectedReadOnly (5),

            /**
              * SaslAuthenticated: used to notify clients that they are SASL-authenticated,
              * so that they can perform Zookeeper actions with their SASL-authorized permissions.
              */
            // SASL認證通過狀態
            SaslAuthenticated(6),

            /** The serving cluster has expired this session. The ZooKeeper
             * client connection (the session) is no longer valid. You must
             * create a new client connection (instantiate a new ZooKeeper
             * instance) if you with to access the ensemble. */
            // 過期狀態
            Expired (-112);

            // 代表狀態的整形值
            private final int intValue;     // Integer representation of value
                                            // for sending over wire

                                            
            // 構造函數
            KeeperState(int intValue) {
                this.intValue = intValue;
            }

            // 返回整形值
            public int getIntValue() {
                return intValue;
            }

            // 從整形值構造相應的狀態
            public static KeeperState fromInt(int intValue) {
                switch(intValue) {
                    case   -1: return KeeperState.Unknown;
                    case    0: return KeeperState.Disconnected;
                    case    1: return KeeperState.NoSyncConnected;
                    case    3: return KeeperState.SyncConnected;
                    case    4: return KeeperState.AuthFailed;
                    case    5: return KeeperState.ConnectedReadOnly;
                    case    6: return KeeperState.SaslAuthenticated;
                    case -112: return KeeperState.Expired;

                    default:
                        throw new RuntimeException("Invalid integer value for conversion to KeeperState");
                }
            }
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章