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