From 0b2c506dbd88b39fdc09e33e2a88fb7cc26b2df8 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Wed, 21 May 2008 16:19:05 +0000 Subject: [PATCH] removed TimeUnit2, which is under GPL and hence incompatible. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@9497 71c3de6d-444a-0410-be80-ed276b4c234a --- .../hudson/slaves/ComputerRetentionWork.java | 3 +- .../java/hudson/slaves/RetentionStrategy.java | 6 +- core/src/main/java/hudson/util/TimeUnit2.java | 534 ------------------ 3 files changed, 3 insertions(+), 540 deletions(-) delete mode 100644 core/src/main/java/hudson/util/TimeUnit2.java diff --git a/core/src/main/java/hudson/slaves/ComputerRetentionWork.java b/core/src/main/java/hudson/slaves/ComputerRetentionWork.java index 9cb867cfc8..15b952237b 100644 --- a/core/src/main/java/hudson/slaves/ComputerRetentionWork.java +++ b/core/src/main/java/hudson/slaves/ComputerRetentionWork.java @@ -6,7 +6,6 @@ import java.util.WeakHashMap; import hudson.model.Computer; import hudson.model.Hudson; import hudson.triggers.SafeTimerTask; -import hudson.util.TimeUnit2; /** * Periodically checks the slaves and try to reconnect dead slaves. @@ -32,7 +31,7 @@ public class ComputerRetentionWork extends SafeTimerTask { // at the moment I don't trust strategies to wait more than 60 minutes // strategies need to wait at least one minute final long waitInMins = Math.min(1, Math.max(60, c.getRetentionStrategy().check(c))); - nextCheck.put(c, startRun + TimeUnit2.MINUTES.toMillis(waitInMins)); + nextCheck.put(c, startRun + waitInMins*1000*60 /*MINS->MILLIS*/); } } } diff --git a/core/src/main/java/hudson/slaves/RetentionStrategy.java b/core/src/main/java/hudson/slaves/RetentionStrategy.java index 155c4473c4..1448246e64 100644 --- a/core/src/main/java/hudson/slaves/RetentionStrategy.java +++ b/core/src/main/java/hudson/slaves/RetentionStrategy.java @@ -4,10 +4,8 @@ import hudson.ExtensionPoint; import hudson.Util; import hudson.model.*; import hudson.util.DescriptorList; -import hudson.util.TimeUnit2; import org.kohsuke.stapler.DataBoundConstructor; -import java.util.concurrent.TimeUnit; import java.util.logging.Logger; import java.util.logging.Level; @@ -153,7 +151,7 @@ public abstract class RetentionStrategy implements Describab public synchronized long check(SlaveComputer c) { if (c.isOffline()) { final long demandMilliseconds = System.currentTimeMillis() - c.getDemandStartMilliseconds(); - if (demandMilliseconds > TimeUnit2.MINUTES.toMillis(inDemandDelay)) { + if (demandMilliseconds > inDemandDelay*1000*60 /*MINS->MILLIS*/) { // we've been in demand for long enough logger.log(Level.INFO, "Launching computer {0} as it has been in demand for {1}", new Object[]{c.getNode().getNodeName(), Util.getTimeSpanString(demandMilliseconds)}); @@ -162,7 +160,7 @@ public abstract class RetentionStrategy implements Describab } } else if (c.isIdle()) { final long idleMilliseconds = System.currentTimeMillis() - c.getIdleStartMilliseconds(); - if (idleMilliseconds > TimeUnit2.MINUTES.toMillis(idleDelay)) { + if (idleMilliseconds > idleDelay*1000*60 /*MINS->MILLIS*/) { // we've been idle for long enough logger.log(Level.INFO, "Disconnecting computer {0} as it has been idle for {1}", new Object[]{c.getNode().getNodeName(), Util.getTimeSpanString(idleMilliseconds)}); diff --git a/core/src/main/java/hudson/util/TimeUnit2.java b/core/src/main/java/hudson/util/TimeUnit2.java deleted file mode 100644 index 3f0b40e526..0000000000 --- a/core/src/main/java/hudson/util/TimeUnit2.java +++ /dev/null @@ -1,534 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Sun designates this - * particular file as subject to the "Classpath" exception as provided - * by Sun in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, - * CA 95054 USA or visit www.sun.com if you need additional information or - * have any questions. - */ - -/* - * This file is available under and governed by the GNU General Public - * License version 2 only, as published by the Free Software Foundation. - * However, the following notice accompanied the original version of this - * file: - * - * Written by Doug Lea with assistance from members of JCP JSR-166 - * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain - */ - -package hudson.util; - -import java.util.concurrent.TimeUnit; - -/** - * Backport of TimeUnit from JDK 7. - * - * A 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 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. - *

- *

A 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: - *

- *

  Lock lock = ...;
- *  if ( lock.tryLock(50L, TimeUnit.MILLISECONDS) ) ...
- * 
- * while this code will timeout in 50 seconds: - *
- *  Lock lock = ...;
- *  if ( lock.tryLock(50L, TimeUnit.SECONDS) ) ...
- * 
- *

- * 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 TimeUnit. - * - * @author Doug Lea - * @since 1.5 - */ -public enum TimeUnit2 { - - 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 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, TimeUnit2 u) { - return u.toNanos(d); - } - public long convert(long d, TimeUnit u) { - return u.toNanos(d); - } - int excessNanos(long d, long m) { - return (int) (d - (m * C2)); - } - }, - 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 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, TimeUnit2 u) { - return u.toMicros(d); - } - public long convert(long d, TimeUnit u) { - return u.toMicros(d); - } - int excessNanos(long d, long m) { - return (int) ((d * C1) - (m * C2)); - } - }, - 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 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, TimeUnit2 u) { - return u.toMillis(d); - } - public long convert(long d, TimeUnit u) { - return u.toMillis(d); - } - int excessNanos(long d, long m) { - return 0; - } - }, - 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 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, TimeUnit2 u) { - return u.toSeconds(d); - } - public long convert(long d, TimeUnit u) { - return u.toSeconds(d); - } - int excessNanos(long d, long m) { - return 0; - } - }, - 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 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, TimeUnit2 u) { - return u.toMinutes(d); - } - public long convert(long d, TimeUnit u) { - return SECONDS.toMinutes(u.toSeconds(d)); - } - int excessNanos(long d, long m) { - return 0; - } - }, - 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 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, TimeUnit2 u) { - return u.toHours(d); - } - public long convert(long d, TimeUnit u) { - return SECONDS.toHours(u.toSeconds(d)); - } - int excessNanos(long d, long m) { - return 0; - } - }, - 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 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, TimeUnit2 u) { - return u.toDays(d); - } - public long convert(long d, TimeUnit u) { - return SECONDS.toDays(u.toSeconds(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. - - /** - * Convert the given time duration in the given unit to this unit. Conversions from finer to coarser granularities - * truncate, so lose precision. For example converting 999 milliseconds to seconds results in 0. - * Conversions from coarser to finer granularities with arguments that would numerically overflow saturate to - * Long.MIN_VALUE if negative or Long.MAX_VALUE if positive. - *

- *

For example, to convert 10 minutes to milliseconds, use: TimeUnit.MILLISECONDS.convert(10L, - * TimeUnit.MINUTES) - * - * @param sourceDuration the time duration in the given sourceUnit - * @param sourceUnit the unit of the sourceDuration argument - * - * @return the converted duration in this unit, or Long.MIN_VALUE if conversion would negatively overflow, - * or Long.MAX_VALUE if it would positively overflow. - */ - public long convert(long sourceDuration, TimeUnit2 sourceUnit) { - throw new AbstractMethodError(); - } - - /** - * Convert the given time duration in the given unit to this unit. Conversions from finer to coarser granularities - * truncate, so lose precision. For example converting 999 milliseconds to seconds results in 0. - * Conversions from coarser to finer granularities with arguments that would numerically overflow saturate to - * Long.MIN_VALUE if negative or Long.MAX_VALUE if positive. - *

- *

For example, to convert 10 minutes to milliseconds, use: TimeUnit.MILLISECONDS.convert(10L, - * TimeUnit.MINUTES) - * - * @param sourceDuration the time duration in the given sourceUnit - * @param sourceUnit the unit of the sourceDuration argument - * - * @return the converted duration in this unit, or Long.MIN_VALUE if conversion would negatively overflow, - * or Long.MAX_VALUE if it would positively overflow. - */ - public long convert(long sourceDuration, TimeUnit sourceUnit) { - throw new AbstractMethodError(); - } - - /** - * Equivalent to NANOSECONDS.convert(duration, this). - * - * @param duration the duration - * - * @return the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or - * Long.MAX_VALUE if it would positively overflow. - * - * @see #convert - */ - public long toNanos(long duration) { - throw new AbstractMethodError(); - } - - /** - * Equivalent to MICROSECONDS.convert(duration, this). - * - * @param duration the duration - * - * @return the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or - * Long.MAX_VALUE if it would positively overflow. - * - * @see #convert - */ - public long toMicros(long duration) { - throw new AbstractMethodError(); - } - - /** - * Equivalent to MILLISECONDS.convert(duration, this). - * - * @param duration the duration - * - * @return the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or - * Long.MAX_VALUE if it would positively overflow. - * - * @see #convert - */ - public long toMillis(long duration) { - throw new AbstractMethodError(); - } - - /** - * Equivalent to SECONDS.convert(duration, this). - * - * @param duration the duration - * - * @return the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or - * Long.MAX_VALUE if it would positively overflow. - * - * @see #convert - */ - public long toSeconds(long duration) { - throw new AbstractMethodError(); - } - - /** - * Equivalent to MINUTES.convert(duration, this). - * - * @param duration the duration - * - * @return the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or - * Long.MAX_VALUE if it would positively overflow. - * - * @see #convert - * @since 1.6 - */ - public long toMinutes(long duration) { - throw new AbstractMethodError(); - } - - /** - * Equivalent to HOURS.convert(duration, this). - * - * @param duration the duration - * - * @return the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or - * Long.MAX_VALUE if it would positively overflow. - * - * @see #convert - * @since 1.6 - */ - public long toHours(long duration) { - throw new AbstractMethodError(); - } - - /** - * Equivalent to DAYS.convert(duration, this). - * - * @param duration the duration - * - * @return the converted duration - * - * @see #convert - * @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 Object.wait using this time unit. This is a convenience method that converts timeout - * arguments into the form required by the Object.wait method. - *

- *

For example, you could implement a blocking poll method (see {@link java.util.concurrent.BlockingQueue#poll - * BlockingQueue.poll}) using: - *

- *

  public synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException {
-     *    while (empty) {
-     *      unit.timedWait(this, timeout);
-     *      ...
-     *    }
-     *  }
- * - * @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. - * @see Object#wait(long, int) - */ - 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 Thread.join using this time unit. This is a convenience method that converts time - * arguments into the form required by the 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. - * @see Thread#join(long, int) - */ - 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 Thread.sleep using this unit. This is a convenience method that converts time arguments into - * the form required by the 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. - * @see Thread#sleep - */ - public void sleep(long timeout) throws InterruptedException { - if (timeout > 0) { - long ms = toMillis(timeout); - int ns = excessNanos(timeout, ms); - Thread.sleep(ms, ns); - } - } - -}