001    /**
002     * Copyright 2003-2005 Arthur van Hoff, Rick Blair
003     *
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    package org.apache.activemq.jmdns;
020    
021    import java.util.ArrayList;
022    import java.util.logging.Logger;
023    
024    /**
025     * DNSState defines the possible states for services registered with JmDNS.
026     *
027     * @author Werner Randelshofer, Rick Blair
028     * @version 1.0  May 23, 2004  Created.
029     */
030    public class DNSState implements Comparable
031    {
032        private static Logger logger = Logger.getLogger(DNSState.class.toString());
033    
034        private final String name;
035    
036        /**
037         * Ordinal of next state to be created.
038         */
039        private static int nextOrdinal = 0;
040        /**
041         * Assign an ordinal to this state.
042         */
043        private final int ordinal = nextOrdinal++;
044        /**
045         * Logical sequence of states.
046         * The sequence is consistent with the ordinal of a state.
047         * This is used for advancing through states.
048         */
049        private final static ArrayList sequence = new ArrayList();
050    
051        private DNSState(String name)
052        {
053            this.name = name;
054            sequence.add(this);
055        }
056    
057        public final String toString()
058        {
059            return name;
060        }
061    
062        public static final DNSState PROBING_1 = new DNSState("probing 1");
063        public static final DNSState PROBING_2 = new DNSState("probing 2");
064        public static final DNSState PROBING_3 = new DNSState("probing 3");
065        public static final DNSState ANNOUNCING_1 = new DNSState("announcing 1");
066        public static final DNSState ANNOUNCING_2 = new DNSState("announcing 2");
067        public static final DNSState ANNOUNCED = new DNSState("announced");
068        public static final DNSState CANCELED = new DNSState("canceled");
069    
070        /**
071         * Returns the next advanced state.
072         * In general, this advances one step in the following sequence: PROBING_1,
073         * PROBING_2, PROBING_3, ANNOUNCING_1, ANNOUNCING_2, ANNOUNCED.
074         * Does not advance for ANNOUNCED and CANCELED state.
075         */
076        public final DNSState advance()
077        {
078            return (isProbing() || isAnnouncing()) ? (DNSState) sequence.get(ordinal + 1) : this;
079        }
080    
081        /**
082         * Returns to the next reverted state.
083         * All states except CANCELED revert to PROBING_1.
084         * Status CANCELED does not revert.
085         */
086        public final DNSState revert()
087        {
088            return (this == CANCELED) ? this : PROBING_1;
089        }
090    
091        /**
092         * Returns true, if this is a probing state.
093         */
094        public boolean isProbing()
095        {
096            return compareTo(PROBING_1) >= 0 && compareTo(PROBING_3) <= 0;
097        }
098    
099        /**
100         * Returns true, if this is an announcing state.
101         */
102        public boolean isAnnouncing()
103        {
104            return compareTo(ANNOUNCING_1) >= 0 && compareTo(ANNOUNCING_2) <= 0;
105        }
106    
107        /**
108         * Returns true, if this is an announced state.
109         */
110        public boolean isAnnounced()
111        {
112            return compareTo(ANNOUNCED) == 0;
113        }
114    
115        /**
116         * Compares two states.
117         * The states compare as follows:
118         * PROBING_1 &lt; PROBING_2 &lt; PROBING_3 &lt; ANNOUNCING_1 &lt;
119         * ANNOUNCING_2 &lt; RESPONDING &lt; ANNOUNCED &lt; CANCELED.
120         */
121        public int compareTo(Object o)
122        {
123            return ordinal - ((DNSState) o).ordinal;
124        }
125    }