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     * Leap Indicator (LI): This is a two-bit code warning of an impending
031     * leap second to be inserted/deleted in the last minute of the current
032     * day, with bit 0 and bit 1, respectively, coded as follows:
033     *
034     *    LI       Value     Meaning
035     *    -------------------------------------------------------
036     *    00       0         no warning
037     *    01       1         last minute has 61 seconds
038     *    10       2         last minute has 59 seconds)
039     *    11       3         alarm condition (clock not synchronized)
040     * 
041     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042     * @version $Rev: 586763 $, $Date: 2007-10-20 19:26:29 +0200 (Sat, 20 Oct 2007) $
043     */
044    public final class LeapIndicatorType implements Comparable<LeapIndicatorType>
045    {
046        /**
047         * Constant for the "No leap second warning" leap indicator type.
048         */
049        public static final LeapIndicatorType NO_WARNING = new LeapIndicatorType( 0, "No leap second warning." );
050    
051        /**
052         * Constant for the "Last minute has 61 seconds" leap indicator type.
053         */
054        public static final LeapIndicatorType POSITIVE_LEAP_SECOND = new LeapIndicatorType( 1,
055            "Last minute has 61 seconds." );
056    
057        /**
058         * Constant for the "Last minute has 59 seconds" leap indicator type.
059         */
060        public static final LeapIndicatorType NEGATIVE_LEAP_SECOND = new LeapIndicatorType( 2,
061            "Last minute has 59 seconds." );
062    
063        /**
064         * Constant for the "Alarm condition (clock not synchronized)" leap indicator type.
065         */
066        public static final LeapIndicatorType ALARM_CONDITION = new LeapIndicatorType( 3,
067            "Alarm condition (clock not synchronized)." );
068    
069        /**
070         * Array for building a List of VALUES.
071         */
072        private static final LeapIndicatorType[] values =
073            { NO_WARNING, POSITIVE_LEAP_SECOND, NEGATIVE_LEAP_SECOND, ALARM_CONDITION };
074    
075        /**
076         * A list of all the leap indicator type constants.
077         */
078        public static final List<LeapIndicatorType> VALUES = Collections.unmodifiableList( Arrays.asList( values ) );
079    
080        /**
081         * The name of the leap indicator type.
082         */
083        private final String name;
084    
085        /**
086         * The value/code for the leap indicator type.
087         */
088        private final int ordinal;
089    
090    
091        /**
092         * Private constructor prevents construction outside of this class.
093         */
094        private LeapIndicatorType( int ordinal, String name )
095        {
096            this.ordinal = ordinal;
097            this.name = name;
098        }
099    
100    
101        /**
102         * Returns the leap indicator type when specified by its ordinal.
103         *
104         * @param type
105         * @return The leap indicator type.
106         */
107        public static LeapIndicatorType getTypeByOrdinal( int type )
108        {
109            for ( int ii = 0; ii < values.length; ii++ )
110            {
111                if ( values[ii].ordinal == type )
112                {
113                    return values[ii];
114                }
115            }
116    
117            return NO_WARNING;
118        }
119    
120    
121        /**
122         * Returns the number associated with this leap indicator type.
123         *
124         * @return The leap indicator type ordinal.
125         */
126        public int getOrdinal()
127        {
128            return ordinal;
129        }
130    
131    
132        public int compareTo( LeapIndicatorType that )
133        {
134            return ordinal - that.ordinal;
135        }
136    
137    
138        public String toString()
139        {
140            return name;
141        }
142    }