Thread: TimeUnit class with support for the 600ms game tick

Results 1 to 9 of 9
  1. #1 TimeUnit class with support for the 600ms game tick 
    Registered Member
    Join Date
    May 2011
    Age
    29
    Posts
    2,246
    Thanks given
    2,469
    Thanks received
    1,120
    Rep Power
    943
    It was useful for my project, figured someone else would have uses for it as well.

    I haven't really tested it beside one method, but unless I'm a complete retard it should work as intended.

    Code:
    /**
     * A {@code TimeUnit} represents time durations at a given unit of
     * granularity and provides utility methods to convert across units,
     * and to perform timing and delay operations in these units.  A
     * {@code TimeUnit} does not maintain time information, but only
     * helps organize and use time representations that may be maintained
     * separately across various contexts.  A nanosecond is defined as one
     * thousandth of a microsecond, a microsecond as one thousandth of a
     * millisecond, a millisecond as one thousandth of a second, a minute
     * as sixty seconds, an hour as sixty minutes, and a day as twenty four
     * hours.
     * <p>
     * <p>A {@code TimeUnit} is mainly used to inform time-based methods
     * how a given timing parameter should be interpreted. For example,
     * the following code will timeout in 50 milliseconds if the {@link
     * java.util.concurrent.locks.Lock lock} is not available:
     * <p>
     * <pre> {@code
     * Lock lock = ...;
     * if (lock.tryLock(50L, TimeUnit.MILLISECONDS)) ...}</pre>
     * <p>
     * while this code will timeout in 50 seconds:
     * <pre> {@code
     * Lock lock = ...;
     * if (lock.tryLock(50L, TimeUnit.SECONDS)) ...}</pre>
     * <p>
     * Note however, that there is no guarantee that a particular timeout
     * implementation will be able to notice the passage of time at the
     * same granularity as the given {@code TimeUnit}.
     *
     * @author Doug Lea
     * @since 1.5
     */
    public enum TickTime {
        /**
         * Time unit representing one thousandth of a microsecond
         */
        NANOSECONDS {
            public long toNanos(long d) {
                return d;
            }
    
            public long toMicros(long d) {
                return d / (C1 / C0);
            }
    
            public long toMillis(long d) {
                return d / (C2 / C0);
            }
    
            public long toGameTicks(long d) {
                return (long) (d / (C3 / C0) / 0.6);
            }
    
            public long toSeconds(long d) {
                return d / (C3 / C0);
            }
    
            public long toMinutes(long d) {
                return d / (C4 / C0);
            }
    
            public long toHours(long d) {
                return d / (C5 / C0);
            }
    
            public long toDays(long d) {
                return d / (C6 / C0);
            }
    
            public long convert(long d, TickTime u) {
                return u.toNanos(d);
            }
    
            int excessNanos(long d, long m) {
                return (int) (d - (m * C2));
            }
        },
        /**
         * Time unit representing one thousandth of a millisecond
         */
        MICROSECONDS {
            public long toNanos(long d) {
                return x(d, C1 / C0, MAX / (C1 / C0));
            }
    
            public long toMicros(long d) {
                return d;
            }
    
            public long toMillis(long d) {
                return d / (C2 / C1);
            }
    
            public long toGameTicks(long d) {
                return (long) (d / (C3 / C1) / 0.6);
            }
    
            public long toSeconds(long d) {
                return d / (C3 / C1);
            }
    
            public long toMinutes(long d) {
                return d / (C4 / C1);
            }
    
            public long toHours(long d) {
                return d / (C5 / C1);
            }
    
            public long toDays(long d) {
                return d / (C6 / C1);
            }
    
            public long convert(long d, TickTime u) {
                return u.toMicros(d);
            }
    
            int excessNanos(long d, long m) {
                return (int) ((d * C1) - (m * C2));
            }
        },
        /**
         * Time unit representing one thousandth of a second
         */
        MILLISECONDS {
            public long toNanos(long d) {
                return x(d, C2 / C0, MAX / (C2 / C0));
            }
    
            public long toMicros(long d) {
                return x(d, C2 / C1, MAX / (C2 / C1));
            }
    
            public long toMillis(long d) {
                return d;
            }
    
            public long toGameTicks(long d) {
                return (long) (d / (C3 / C2) / 0.6);
            }
    
            public long toSeconds(long d) {
                return d / (C3 / C2);
            }
    
            public long toMinutes(long d) {
                return d / (C4 / C2);
            }
    
            public long toHours(long d) {
                return d / (C5 / C2);
            }
    
            public long toDays(long d) {
                return d / (C6 / C2);
            }
    
            public long convert(long d, TickTime u) {
                return u.toMillis(d);
            }
    
            int excessNanos(long d, long m) {
                return 0;
            }
        },
        GAMETICKS {
            public long toNanos(long d) {
                return d * 600000000;
            }
    
            public long toMicros(long d) {
                return d * 600000;
            }
    
            public long toMillis(long d) {
                return d * 600;
            }
    
            public long toGameTicks(long d) {
                return d;
            }
    
            public long toSeconds(long d) {
                return (long) (d / 0.6);
            }
    
            public long toMinutes(long d) {
                return (long) (d / 60 / 0.6);
            }
    
            public long toHours(long d) {
                return (long) (d / 60 / 60 / 0.6);
            }
    
            public long toDays(long d) {
                return (long) (d / 24 / 60 / 60 / 0.6);
            }
    
            public long convert(long d, TickTime u) {
                return u.toGameTicks(d);
            }
    
            int excessNanos(long d, long m) {
                return 0;
            }
    
        },
        /**
         * Time unit representing one second
         */
        SECONDS {
            public long toNanos(long d) {
                return x(d, C3 / C0, MAX / (C3 / C0));
            }
    
            public long toMicros(long d) {
                return x(d, C3 / C1, MAX / (C3 / C1));
            }
    
            public long toMillis(long d) {
                return x(d, C3 / C2, MAX / (C3 / C2));
            }
    
            public long toGameTicks(long d) {
                return (long) (d / 0.6);
            }
    
            public long toSeconds(long d) {
                return d;
            }
    
            public long toMinutes(long d) {
                return d / (C4 / C3);
            }
    
            public long toHours(long d) {
                return d / (C5 / C3);
            }
    
            public long toDays(long d) {
                return d / (C6 / C3);
            }
    
            public long convert(long d, TickTime u) {
                return u.toSeconds(d);
            }
    
            int excessNanos(long d, long m) {
                return 0;
            }
        },
        /**
         * Time unit representing sixty seconds
         */
        MINUTES {
            public long toNanos(long d) {
                return x(d, C4 / C0, MAX / (C4 / C0));
            }
    
            public long toMicros(long d) {
                return x(d, C4 / C1, MAX / (C4 / C1));
            }
    
            public long toMillis(long d) {
                return x(d, C4 / C2, MAX / (C4 / C2));
            }
    
            public long toGameTicks(long d) {
                return (long) (x(d, C4 / C3, ((MAX / (C4 / C3)))) / 0.6);
            }
    
            public long toSeconds(long d) {
                return x(d, C4 / C3, MAX / (C4 / C3));
            }
    
            public long toMinutes(long d) {
                return d;
            }
    
            public long toHours(long d) {
                return d / (C5 / C4);
            }
    
            public long toDays(long d) {
                return d / (C6 / C4);
            }
    
            public long convert(long d, TickTime u) {
                return u.toMinutes(d);
            }
    
            int excessNanos(long d, long m) {
                return 0;
            }
        },
        /**
         * Time unit representing sixty minutes
         */
        HOURS {
            public long toNanos(long d) {
                return x(d, C5 / C0, MAX / (C5 / C0));
            }
    
            public long toMicros(long d) {
                return x(d, C5 / C1, MAX / (C5 / C1));
            }
    
            public long toMillis(long d) {
                return x(d, C5 / C2, MAX / (C5 / C2));
            }
    
            public long toGameTicks(long d) {
                return (long) (x(d, C5 / C3, MAX / (C5 / C3)) / 0.6);
            }
    
            public long toSeconds(long d) {
                return x(d, C5 / C3, MAX / (C5 / C3));
            }
    
            public long toMinutes(long d) {
                return x(d, C5 / C4, MAX / (C5 / C4));
            }
    
            public long toHours(long d) {
                return d;
            }
    
            public long toDays(long d) {
                return d / (C6 / C5);
            }
    
            public long convert(long d, TickTime u) {
                return u.toHours(d);
            }
    
            int excessNanos(long d, long m) {
                return 0;
            }
        },
        /**
         * Time unit representing twenty four hours
         */
        DAYS {
            public long toNanos(long d) {
                return x(d, C6 / C0, MAX / (C6 / C0));
            }
    
            public long toMicros(long d) {
                return x(d, C6 / C1, MAX / (C6 / C1));
            }
    
            public long toMillis(long d) {
                return x(d, C6 / C2, MAX / (C6 / C2));
            }
    
            public long toGameTicks(long d) {
                return (long) (x(d, C6 / C3, MAX / (C6 / C3)) / 0.6);
            }
    
            public long toSeconds(long d) {
                return x(d, C6 / C3, MAX / (C6 / C3));
            }
    
            public long toMinutes(long d) {
                return x(d, C6 / C4, MAX / (C6 / C4));
            }
    
            public long toHours(long d) {
                return x(d, C6 / C5, MAX / (C6 / C5));
            }
    
            public long toDays(long d) {
                return d;
            }
    
            public long convert(long d, TickTime u) {
                return u.toDays(d);
            }
    
            int excessNanos(long d, long m) {
                return 0;
            }
        };
        // Handy constants for conversion methods
        static final long C0 = 1L;
        static final long C1 = C0 * 1000L;
        static final long C2 = C1 * 1000L;
        static final long C3 = C2 * 1000L;
        static final long C4 = C3 * 60L;
        static final long C5 = C4 * 60L;
        static final long C6 = C5 * 24L;
        static final long MAX = Long.MAX_VALUE;
    
        /**
         * Scale d by m, checking for overflow.
         * This has a short name to make above code more readable.
         */
        static long x(long d, long m, long over) {
            if (d > over)
                return Long.MAX_VALUE;
            if (d < -over)
                return Long.MIN_VALUE;
            return d * m;
        }
    
        // To maintain full signature compatibility with 1.5, and to improve the
        // clarity of the generated javadoc (see 6287639: Abstract methods in
        // enum classes should not be listed as abstract), method convert
        // etc. are not declared abstract but otherwise act as abstract methods.
    
        /**
         * Converts the given time duration in the given unit to this unit.
         * Conversions from finer to coarser granularities truncate, so
         * lose precision. For example, converting {@code 999} milliseconds
         * to seconds results in {@code 0}. Conversions from coarser to
         * finer granularities with arguments that would numerically
         * overflow saturate to {@code Long.MIN_VALUE} if negative or
         * {@code Long.MAX_VALUE} if positive.
         * <p>
         * <p>For example, to convert 10 minutes to milliseconds, use:
         * {@code TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)}
         *
         * @param sourceDuration the time duration in the given {@code sourceUnit}
         * @param sourceUnit     the unit of the {@code sourceDuration} argument
         * @return the converted duration in this unit,
         * or {@code Long.MIN_VALUE} if conversion would negatively
         * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
         */
        public long convert(long sourceDuration, TickTime sourceUnit) {
            throw new AbstractMethodError();
        }
    
        /**
         * Equivalent to
         * {@link #convert(long, TickTime) NANOSECONDS.convert(duration, this)}.
         *
         * @param duration the duration
         * @return the converted duration,
         * or {@code Long.MIN_VALUE} if conversion would negatively
         * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
         */
        public long toNanos(long duration) {
            throw new AbstractMethodError();
        }
    
        /**
         * Equivalent to
         * {@link #convert(long, TickTime) MICROSECONDS.convert(duration, this)}.
         *
         * @param duration the duration
         * @return the converted duration,
         * or {@code Long.MIN_VALUE} if conversion would negatively
         * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
         */
        public long toMicros(long duration) {
            throw new AbstractMethodError();
        }
    
        /**
         * Equivalent to
         * {@link #convert(long, TickTime) MILLISECONDS.convert(duration, this)}.
         *
         * @param duration the duration
         * @return the converted duration,
         * or {@code Long.MIN_VALUE} if conversion would negatively
         * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
         */
        public long toMillis(long duration) {
            throw new AbstractMethodError();
        }
    
        public long toGameTicks(long duration) {
            throw new AbstractMethodError();
        }
    
        /**
         * Equivalent to
         * {@link #convert(long, TickTime) SECONDS.convert(duration, this)}.
         *
         * @param duration the duration
         * @return the converted duration,
         * or {@code Long.MIN_VALUE} if conversion would negatively
         * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
         */
        public long toSeconds(long duration) {
            throw new AbstractMethodError();
        }
    
        /**
         * Equivalent to
         * {@link #convert(long, TickTime) MINUTES.convert(duration, this)}.
         *
         * @param duration the duration
         * @return the converted duration,
         * or {@code Long.MIN_VALUE} if conversion would negatively
         * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
         * @since 1.6
         */
        public long toMinutes(long duration) {
            throw new AbstractMethodError();
        }
    
        /**
         * Equivalent to
         * {@link #convert(long, TickTime) HOURS.convert(duration, this)}.
         *
         * @param duration the duration
         * @return the converted duration,
         * or {@code Long.MIN_VALUE} if conversion would negatively
         * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
         * @since 1.6
         */
        public long toHours(long duration) {
            throw new AbstractMethodError();
        }
    
        /**
         * Equivalent to
         * {@link #convert(long, TickTime) DAYS.convert(duration, this)}.
         *
         * @param duration the duration
         * @return the converted duration
         * @since 1.6
         */
        public long toDays(long duration) {
            throw new AbstractMethodError();
        }
    
        /**
         * Utility to compute the excess-nanosecond argument to wait,
         * sleep, join.
         *
         * @param d the duration
         * @param m the number of milliseconds
         * @return the number of nanoseconds
         */
        abstract int excessNanos(long d, long m);
    
        /**
         * Performs a timed {@link Object#wait(long, int) Object.wait}
         * using this time unit.
         * This is a convenience method that converts timeout arguments
         * into the form required by the {@code Object.wait} method.
         * <p>
         * <p>For example, you could implement a blocking {@code poll}
         * method (see {@link BlockingQueue#poll BlockingQueue.poll})
         * using:
         * <p>
         * <pre> {@code
         * public synchronized Object poll(long timeout, TimeUnit unit)
         *     throws InterruptedException {
         *   while (empty) {
         *     unit.timedWait(this, timeout);
         *     ...
         *   }
         * }}</pre>
         *
         * @param obj     the object to wait on
         * @param timeout the maximum time to wait. If less than
         *                or equal to zero, do not wait at all.
         * @throws InterruptedException if interrupted while waiting
         */
        public void timedWait(Object obj, long timeout) throws InterruptedException {
            if (timeout > 0) {
                long ms = toMillis(timeout);
                int ns = excessNanos(timeout, ms);
                obj.wait(ms, ns);
            }
        }
    
        /**
         * Performs a timed {@link Thread#join(long, int) Thread.join}
         * using this time unit.
         * This is a convenience method that converts time arguments into the
         * form required by the {@code Thread.join} method.
         *
         * @param thread  the thread to wait for
         * @param timeout the maximum time to wait. If less than
         *                or equal to zero, do not wait at all.
         * @throws InterruptedException if interrupted while waiting
         */
        public void timedJoin(Thread thread, long timeout) throws InterruptedException {
            if (timeout > 0) {
                long ms = toMillis(timeout);
                int ns = excessNanos(timeout, ms);
                thread.join(ms, ns);
            }
        }
    
        /**
         * Performs a {@link Thread#sleep(long, int) Thread.sleep} using
         * this time unit.
         * This is a convenience method that converts time arguments into the
         * form required by the {@code Thread.sleep} method.
         *
         * @param timeout the minimum time to sleep. If less than
         *                or equal to zero, do not sleep at all.
         * @throws InterruptedException if interrupted while sleeping
         */
        public void sleep(long timeout) throws InterruptedException {
            if (timeout > 0) {
                long ms = toMillis(timeout);
                int ns = excessNanos(timeout, ms);
                Thread.sleep(ms, ns);
            }
        }
    
    }
    In case you're not familiar with this class already, it will convert a specified amount of days or seconds or whatever to gameticks or vise versa.

    Want to know how many game ticks are in 3 days?
    Code:
    long 3days = TickTime.DAYS.toGameTicks(3);
    Want to know how many seconds are in 3 game ticks?
    Code:
    long 3gameticks = TickTime.GAMETICKS.toSeconds(3);
    Reply With Quote  
     

  2. Thankful users:


  3. #2  
    Registered Member

    Join Date
    May 2013
    Age
    27
    Posts
    414
    Thanks given
    215
    Thanks received
    200
    Rep Power
    137
    Looks nice, will definitely use. Thanks for your contribution.
    Reply With Quote  
     

  4. #3  
    Donator

    Jason's Avatar
    Join Date
    Aug 2009
    Posts
    6,092
    Thanks given
    2,402
    Thanks received
    2,823
    Rep Power
    4550
    I can see the appeal, good job.
    Reply With Quote  
     

  5. #4  
    Donator


    Join Date
    Jul 2013
    Posts
    1,233
    Thanks given
    1
    Thanks received
    493
    Rep Power
    0
    Guess its helpful for the game ticks. still would rather use calendar/date for majority of things long term
    Reply With Quote  
     

  6. #5  
    Registered Member
    Join Date
    Dec 2013
    Posts
    419
    Thanks given
    127
    Thanks received
    85
    Rep Power
    349
    Look at how TimeUnit does things.
    Also, a lot of these methods already exists e.g. TimeUnit.NANOSECONDS.toSeconds(1000000000000L);

    No need to reinvent the wheel here.
    The only unique feature that I can see from skimming is toGameTicks(x) which don't help much anyway because I can't specify what a game tick is without having to edit your code directly.

    I read your comments but it justifies nothing.
    Reply With Quote  
     

  7. Thankful user:


  8. #6  
    Registered Member
    shitposter's Avatar
    Join Date
    May 2016
    Posts
    147
    Thanks given
    50
    Thanks received
    169
    Rep Power
    1261
    dont listenn to hater u did good i like
    This signature requires a rune-server™ extreme donator account.
    If you have not purchased extreme donator, you may not view this signature.
    Reply With Quote  
     

  9. #7  
    Java Programmer
    _Jon's Avatar
    Join Date
    Jan 2015
    Age
    30
    Posts
    206
    Thanks given
    36
    Thanks received
    63
    Rep Power
    47
    Quote Originally Posted by avestaX3owner View Post
    dont listenn to hater u did good i like
    It really is pretty pointless the TimeUnit class is already in play, plus it goes hand in hand with the already existing Executor Framework.
    Github - Here
    Reply With Quote  
     

  10. #8  
    Registered Member
    Tyluur's Avatar
    Join Date
    Jun 2010
    Age
    26
    Posts
    5,103
    Thanks given
    1,818
    Thanks received
    1,767
    Rep Power
    2438
    this is a useful release. he's not reinventing the engine lmao he just took the TimeUnit class and added support for ticks. dunno why the hate. gj
    Quote Originally Posted by blakeman8192 View Post
    Keep trying. Quitting is the only true failure.
    Spoiler for skrrrrr:

    Attached image
    Reply With Quote  
     

  11. Thankful user:


  12. #9  
    G Herbo #NLMB


    Join Date
    Jul 2014
    Age
    27
    Posts
    1,445
    Thanks given
    236
    Thanks received
    236
    Rep Power
    84
    Quote Originally Posted by Tyluur View Post
    this is a useful release. he's not reinventing the engine lmao he just took the TimeUnit class and added support for ticks. dunno why the hate. gj
    :0 I thought it was you who posted this thread. Your names are too similar

    OT: Nice contribution tylurr
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 0
    Last Post: 03-19-2014, 09:29 PM
  2. Someone can't wait for the Olympic Games
    By Dunhel in forum Humor
    Replies: 3
    Last Post: 07-12-2012, 02:55 PM
  3. for the nubs having problems with "dclaws = 3;"
    By Silicity in forum Snippets
    Replies: 3
    Last Post: 02-14-2009, 11:51 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •