001    /*
002     * Created on Oct 29, 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.util;
017    
018    import static java.lang.System.currentTimeMillis;
019    
020    /**
021     * Understands a time counter with a timeout.
022     *
023     * @author Alex Ruiz
024     */
025    public final class TimeoutWatch {
026    
027      private final long timeout;
028      private long startTime;
029      
030      /**
031       * Creates and starts a new <code>{@link TimeoutWatch}</code> with the given timeout.
032       * @param timeout the given timeout.
033       * @return the new timeout watch.
034       */
035      public static TimeoutWatch startWatchWithTimeoutOf(long timeout) {
036        TimeoutWatch watch = new TimeoutWatch(timeout);
037        watch.start();
038        return  watch;
039      }
040      
041      private TimeoutWatch(long timeout) {
042        this.timeout = timeout;
043      }
044      
045      void start() {
046        startTime = currentTimeMillis();
047      }
048    
049      public boolean isTimeOut() {
050        long timePassed = currentTimeMillis() - startTime;
051        return timePassed >= timeout;
052      }
053    }