1 /*
   2  * Copyright (c) 2007, 2017 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation. Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.jemmy.env;
  26 
  27 import org.jemmy.TimeoutExpiredException;
  28 import org.jemmy.JemmyException;
  29 
  30 /**
  31  * Represents one timeout.
  32  * @author Alexandre Iline (alexandre.iline@sun.com)
  33  */
  34 public class Timeout extends Object implements Cloneable {
  35 
  36     private String name;
  37     private long value;
  38     private long startTime;
  39 
  40     /**
  41      * Constructor.
  42      * @param name Timeout name.
  43      * @param value Timeout value in milliseconds.
  44      */
  45     public Timeout(String name, long value) {
  46         this.name = name;
  47         this.value = value;
  48     }
  49 
  50     /**
  51      * Returns timeout name.
  52      * @return timeout name.
  53      */
  54     public String getName() {
  55         return (name);
  56     }
  57 
  58     /**
  59      * Returns timeout value.
  60      * @return timeout value.
  61      */
  62     public long getValue() {
  63         return (value);
  64     }
  65 
  66     public void setValue(long value) {
  67         this.value = value;
  68     }
  69 
  70     /**
  71      * Sleeps for timeout value.
  72      */
  73     public void sleep() {
  74         if (getValue() > 0) {
  75             try {
  76                 Thread.sleep(getValue());
  77             } catch (InterruptedException e) {
  78                 throw (new JemmyException("Sleep " +
  79                         getName() +
  80                         " was interrupted!",
  81                         e));
  82             }
  83         }
  84     }
  85 
  86     /**
  87      * Starts timeout measuring.
  88      */
  89     public void start() {
  90         startTime = System.currentTimeMillis();
  91     }
  92 
  93     /**
  94      * Checks if timeout has been expired after start() invocation.
  95      * @return true if timeout has been expired.
  96      */
  97     public boolean expired() {
  98         return (System.currentTimeMillis() - startTime > getValue());
  99     }
 100 
 101     /**
 102      * Throws a TimeoutExpiredException exception if timeout has been expired.
 103      * @throws TimeoutExpiredException if timeout has been expired after start() invocation.
 104      */
 105     public void check() {
 106         if (expired()) {
 107             throw (new TimeoutExpiredException(getName() +
 108                     " timeout expired!"));
 109         }
 110     }
 111 
 112     /**
 113      *
 114      * @return
 115      */
 116     @Override
 117     public String toString() {
 118         return "Timeout [" + name + ", " + value + "]";
 119     }
 120 
 121     @Override
 122     public Timeout clone() {
 123         return new Timeout(name, value);
 124     }
 125 }