001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    
021    package org.apache.directory.server.ntp.messages;
022    
023    
024    import java.util.Arrays;
025    import java.util.Collections;
026    import java.util.List;
027    
028    
029    /**
030     * Stratum: This is a eight-bit unsigned integer indicating the stratum
031     * level of the local clock, with values defined as follows:
032     *
033     *    Stratum  Meaning
034     *    ----------------------------------------------
035     *    0        unspecified or unavailable
036     *    1        primary reference (e.g., radio clock)
037     *    2-15     secondary reference (via NTP or SNTP)
038     *    16-255   reserved
039     * 
040     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041     * @version $Rev: 586763 $, $Date: 2007-10-20 19:26:29 +0200 (Sat, 20 Oct 2007) $
042     */
043    public final class StratumType implements Comparable<StratumType>
044    {
045        /**
046         * Constant for the "Unspecified or unavailable" stratum type.
047         */
048        public static final StratumType UNSPECIFIED = new StratumType( 0, "Unspecified or unavailable." );
049    
050        /**
051         * Constant for the "Primary reference" stratum type.
052         */
053        public static final StratumType PRIMARY_REFERENCE = new StratumType( 1, "Primary reference." );
054    
055        /**
056         * Constant for the "Secondary reference" stratum type.
057         */
058        public static final StratumType SECONDARY_REFERENCE = new StratumType( 2, "Secondary reference." );
059    
060        /**
061         * Array for building a List of VALUES.
062         */
063        private static final StratumType[] values =
064            { UNSPECIFIED, PRIMARY_REFERENCE, SECONDARY_REFERENCE };
065    
066        /**
067         * A list of all the stratum type constants.
068         */
069        public static final List<StratumType> VALUES = Collections.unmodifiableList( Arrays.asList( values ) );
070    
071        /**
072         * The name of the stratum type.
073         */
074        private final String name;
075    
076        /**
077         * The value/code for the stratum type.
078         */
079        private final int ordinal;
080    
081    
082        /**
083         * Private constructor prevents construction outside of this class.
084         */
085        private StratumType( int ordinal, String name )
086        {
087            this.ordinal = ordinal;
088            this.name = name;
089        }
090    
091    
092        /**
093         * Returns the stratum type when specified by its ordinal.
094         *
095         * @param type
096         * @return The stratum type.
097         */
098        public static StratumType getTypeByOrdinal( int type )
099        {
100            for ( int ii = 0; ii < values.length; ii++ )
101            {
102                if ( values[ii].ordinal == type )
103                {
104                    return values[ii];
105                }
106            }
107    
108            return UNSPECIFIED;
109        }
110    
111    
112        /**
113         * Returns the number associated with this stratum type.
114         *
115         * @return The stratum type ordinal.
116         */
117        public int getOrdinal()
118        {
119            return ordinal;
120        }
121    
122    
123        public int compareTo( StratumType that )
124        {
125            return ordinal - that.ordinal;
126        }
127    
128    
129        public String toString()
130        {
131            return name;
132        }
133    }