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 
  26 package org.jemmy.timing;
  27 
  28 import org.jemmy.JemmyException;
  29 import org.jemmy.TimeoutExpiredException;
  30 import org.jemmy.env.Timeout;
  31 
  32 /**
  33  *
  34  * @author shura
  35  */
  36 public class Waiter {
  37     /**
  38      *
  39      */
  40     public static final Timeout DEFAULT_DELTA = new Timeout("default.wait.delta", 100);
  41     private long waitTime;
  42     private long delta;
  43 
  44     /**
  45      *
  46      * @param waitTime
  47      * @param delta
  48      */
  49     public Waiter(Timeout waitTime, Timeout delta) {
  50         this.waitTime = waitTime.getValue();
  51         this.delta = delta.getValue();
  52     }
  53 
  54     /**
  55      *
  56      * @param waitTime
  57      */
  58     public Waiter(Timeout waitTime) {
  59         this.waitTime = waitTime.getValue();
  60         this.delta = DEFAULT_DELTA.getValue();
  61     }
  62 
  63     /**
  64      *
  65      * @param <T>
  66      * @param state
  67      * @return
  68      */
  69     public <T> T waitState(State<T> state) {
  70         long start = System.currentTimeMillis();
  71         T res;
  72         while( System.currentTimeMillis() < start + waitTime) {
  73             res = state.reached();
  74             if(res != null) {
  75                 return res;
  76             }
  77             try {
  78                 Thread.sleep(delta);
  79             } catch (InterruptedException ex) {
  80                 throw new JemmyException("Wait interrupted for: ", state);
  81             }
  82         }
  83         return null;
  84     }
  85 
  86     /**
  87      *
  88      * @param <T>
  89      * @param value
  90      * @param state
  91      * @return
  92      */
  93     public <T> T waitValue(final T value, final State<T> state) {
  94         State<T> st = new State<T>() {
  95             public T reached() {
  96                 T res = state.reached();
  97                 if(res != null && res.equals(value)) {
  98                     return res;
  99                 } else {
 100                     return null;
 101                 }
 102             }
 103         };
 104         return waitState(st);
 105     }
 106 
 107     /**
 108      *
 109      * @param <T>
 110      * @param state
 111      * @return
 112      */
 113     public <T> T ensureState(State<T> state) {
 114         T res = waitState(state);
 115         if(res == null) {
 116             throw new TimeoutExpiredException("State '" + state + "' has not been reached in " + waitTime + " milliseconds");
 117         }
 118         return res;
 119     }
 120     /**
 121      *
 122      * @param <T>
 123      * @param value
 124      * @param state
 125      * @return
 126      */
 127     public <T> T ensureValue(T value, State<T> state) {
 128         T res = waitValue(value, state);
 129         if (res == null) {
 130             throw new TimeoutExpiredException("State '" + state + "' has not been reached in " + waitTime + " milliseconds");
 131         }
 132         return res;
 133     }
 134 }