001    /*
002    Copyright (c) 1996-2011, Damon Hart-Davis
003    All rights reserved.
004    
005    Redistribution and use in source and binary forms, with or without
006    modification, are permitted provided that the following conditions are
007    met:
008    
009      * Redistributions of source code must retain the above copyright
010        notice, this list of conditions and the following disclaimer.
011    
012      * Redistributions in binary form must reproduce the above copyright
013        notice, this list of conditions and the following disclaimer in the
014        documentation and/or other materials provided with the
015        distribution.
016    
017    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
018    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
019    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
020    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
021    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
022    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
023    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
024    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
025    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
027    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028    */
029    package org.hd.d.pg2k.svrCore.vars;
030    
031    /**
032     * Created by IntelliJ IDEA.
033     * User: Damon
034     * Date: 30-Dec-2004
035     * Time: 18:03:11
036     */
037    
038    /**An enumeration of the available event periods from (very) short to (very) long.
039     * The actual event interval in milliseconds is available from the period value,
040     * as are some subsidiary methods.
041     * <p>
042     * Our on-the-wire tunnel (inter-peer) protocol may depend on the value ordinals,
043     * so be very careful if altering this enum in any way.
044     */
045    public enum EventPeriod
046        {
047        VSHORT(SystemVariables.EVENT_INTERVAL_VSHORT_TERM_MS),
048        SHORT(SystemVariables.EVENT_INTERVAL_SHORT_TERM_MS),
049        MEDIUM(SystemVariables.EVENT_INTERVAL_MEDIUM_TERM_MS),
050        LONG(SystemVariables.EVENT_INTERVAL_LONG_TERM_MS),
051        VLONG(SystemVariables.EVENT_INTERVAL_VLONG_TERM_MS);
052    
053        private EventPeriod(final int intervalMs) { this.intervalMs = intervalMs; }
054    
055        /**The interval for this event period in milliseconds; strictly positive. */
056        private final int intervalMs;
057    
058        /**Get the interval for this event period in milliseconds; strictly positive. */
059        public final int getIntervalMs() { return(intervalMs); }
060    
061        /**Compute the interval number given a time in milliseconds within that interval.
062         * The time passed must be positive.
063         */
064        public final long getIntervalNumber(final long time)
065            {
066            if(time <= 0) { throw new IllegalArgumentException(); }
067            return(time / intervalMs);
068            }
069    
070        /**Get start time (ms) of specified interval number.
071         * The interval time passed must be positive,
072         * and small enough to avoid overflow in the returned time value.
073         */
074        public final long getIntervalStartTime(final long intervalNumber)
075            {
076            if(intervalNumber <= 0) { throw new IllegalArgumentException(); }
077            if(Long.MAX_VALUE / intervalMs < intervalNumber) { throw new IllegalArgumentException(); }
078            return(intervalMs * intervalNumber);
079            }
080        }