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