001 /*
002 * Created on Oct 31, 2007
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 *
014 * Copyright @2007-2010 the original author or authors.
015 */
016 package org.fest.swing.timing;
017
018 import java.util.concurrent.TimeUnit;
019
020 /**
021 * Understands a timeout.
022 *
023 * @author Yvonne Wang
024 * @author Alex Ruiz
025 */
026 public final class Timeout {
027
028 private final long duration;
029
030 /**
031 * Creates a new <code>{@link Timeout}</code>.
032 * @param duration the duration of the timeout in milliseconds.
033 * @return the created <code>Timeout</code>.
034 */
035 public static Timeout timeout(long duration) {
036 return new Timeout(duration);
037 }
038
039 /**
040 * Creates a new <code>{@link Timeout}</code>.
041 * @param duration the duration of the timeout.
042 * @param timeUnit the unit of time of the timeout.
043 * @return the created <code>Timeout</code>.
044 * @throws NullPointerException if the given time unit is <code>null</code>.
045 */
046 public static Timeout timeout(long duration, TimeUnit timeUnit) {
047 if (timeUnit == null) throw new NullPointerException("Time unit should not be null");
048 return new Timeout(timeUnit.toMillis(duration));
049 }
050
051 private Timeout(long duration) {
052 this.duration = duration;
053 }
054
055 /**
056 * Returns the duration of the timeout in milliseconds.
057 * @return the duration of the timeout in milliseconds.
058 */
059 public long duration() { return duration; }
060 }