1// Copyright 2003-2005 Arthur van Hoff, Rick Blair
2// Licensed under Apache License version 2.0
3// Original license LGPL
4
5package javax.jmdns.impl.constants;
6
7/**
8 * DNSState defines the possible states for services registered with JmDNS.
9 *
10 * @author Werner Randelshofer, Rick Blair, Pierre Frisch
11 */
12public enum DNSState {
13
14    /**
15     *
16     */
17    PROBING_1("probing 1", StateClass.probing),
18    /**
19    *
20    */
21    PROBING_2("probing 2", StateClass.probing),
22    /**
23    *
24    */
25    PROBING_3("probing 3", StateClass.probing),
26    /**
27    *
28    */
29    ANNOUNCING_1("announcing 1", StateClass.announcing),
30    /**
31    *
32    */
33    ANNOUNCING_2("announcing 2", StateClass.announcing),
34    /**
35    *
36    */
37    ANNOUNCED("announced", StateClass.announced),
38    /**
39    *
40    */
41    CANCELING_1("canceling 1", StateClass.canceling),
42    /**
43    *
44    */
45    CANCELING_2("canceling 2", StateClass.canceling),
46    /**
47    *
48    */
49    CANCELING_3("canceling 3", StateClass.canceling),
50    /**
51    *
52    */
53    CANCELED("canceled", StateClass.canceled),
54    /**
55     *
56     */
57    CLOSING("closing", StateClass.closing),
58    /**
59     *
60     */
61    CLOSED("closed", StateClass.closed);
62
63    private enum StateClass {
64        probing, announcing, announced, canceling, canceled, closing, closed
65    }
66
67    // private static Logger logger = Logger.getLogger(DNSState.class.getName());
68
69    private final String     _name;
70
71    private final StateClass _state;
72
73    private DNSState(String name, StateClass state) {
74        _name = name;
75        _state = state;
76    }
77
78    @Override
79    public final String toString() {
80        return _name;
81    }
82
83    /**
84     * Returns the next advanced state.<br/>
85     * In general, this advances one step in the following sequence: PROBING_1, PROBING_2, PROBING_3, ANNOUNCING_1, ANNOUNCING_2, ANNOUNCED.<br/>
86     * or CANCELING_1, CANCELING_2, CANCELING_3, CANCELED Does not advance for ANNOUNCED and CANCELED state.
87     *
88     * @return next state
89     */
90    public final DNSState advance() {
91        switch (this) {
92            case PROBING_1:
93                return PROBING_2;
94            case PROBING_2:
95                return PROBING_3;
96            case PROBING_3:
97                return ANNOUNCING_1;
98            case ANNOUNCING_1:
99                return ANNOUNCING_2;
100            case ANNOUNCING_2:
101                return ANNOUNCED;
102            case ANNOUNCED:
103                return ANNOUNCED;
104            case CANCELING_1:
105                return CANCELING_2;
106            case CANCELING_2:
107                return CANCELING_3;
108            case CANCELING_3:
109                return CANCELED;
110            case CANCELED:
111                return CANCELED;
112            case CLOSING:
113                return CLOSED;
114            case CLOSED:
115                return CLOSED;
116            default:
117                // This is just to keep the compiler happy as we have covered all cases before.
118                return this;
119        }
120    }
121
122    /**
123     * Returns to the next reverted state. All states except CANCELED revert to PROBING_1. Status CANCELED does not revert.
124     *
125     * @return reverted state
126     */
127    public final DNSState revert() {
128        switch (this) {
129            case PROBING_1:
130            case PROBING_2:
131            case PROBING_3:
132            case ANNOUNCING_1:
133            case ANNOUNCING_2:
134            case ANNOUNCED:
135                return PROBING_1;
136            case CANCELING_1:
137            case CANCELING_2:
138            case CANCELING_3:
139                return CANCELING_1;
140            case CANCELED:
141                return CANCELED;
142            case CLOSING:
143                return CLOSING;
144            case CLOSED:
145                return CLOSED;
146            default:
147                // This is just to keep the compiler happy as we have covered all cases before.
148                return this;
149        }
150    }
151
152    /**
153     * Returns true, if this is a probing state.
154     *
155     * @return <code>true</code> if probing state, <code>false</code> otherwise
156     */
157    public final boolean isProbing() {
158        return _state == StateClass.probing;
159    }
160
161    /**
162     * Returns true, if this is an announcing state.
163     *
164     * @return <code>true</code> if announcing state, <code>false</code> otherwise
165     */
166    public final boolean isAnnouncing() {
167        return _state == StateClass.announcing;
168    }
169
170    /**
171     * Returns true, if this is an announced state.
172     *
173     * @return <code>true</code> if announced state, <code>false</code> otherwise
174     */
175    public final boolean isAnnounced() {
176        return _state == StateClass.announced;
177    }
178
179    /**
180     * Returns true, if this is a canceling state.
181     *
182     * @return <code>true</code> if canceling state, <code>false</code> otherwise
183     */
184    public final boolean isCanceling() {
185        return _state == StateClass.canceling;
186    }
187
188    /**
189     * Returns true, if this is a canceled state.
190     *
191     * @return <code>true</code> if canceled state, <code>false</code> otherwise
192     */
193    public final boolean isCanceled() {
194        return _state == StateClass.canceled;
195    }
196
197    /**
198     * Returns true, if this is a closing state.
199     *
200     * @return <code>true</code> if closing state, <code>false</code> otherwise
201     */
202    public final boolean isClosing() {
203        return _state == StateClass.closing;
204    }
205
206    /**
207     * Returns true, if this is a closing state.
208     *
209     * @return <code>true</code> if closed state, <code>false</code> otherwise
210     */
211    public final boolean isClosed() {
212        return _state == StateClass.closed;
213    }
214
215}
216