1 19 20 package com.lutris.airsent.business.delivery; 21 22 import com.lutris.airsent.spec.delivery.Update; 23 import com.lutris.airsent.business.AirSentBusinessException; 24 25 32 public class UpdateImpl implements Update { 33 private long updateState; 34 35 41 public UpdateImpl() {} 42 43 51 public UpdateImpl(long initialState) { 52 updateState = initialState; 53 } 54 55 66 public long needUpdate(long browserState, long wait) throws AirSentBusinessException { 67 waitForChange(browserState, wait); 68 69 return getUpdateState(); 70 } 71 72 80 public void setUpdateState(long updateState) { 81 this.updateState = updateState; 82 } 83 84 92 public long getUpdateState() { 93 return updateState; 94 } 95 96 105 private void waitForChange(long browserState, long wait) 106 throws AirSentBusinessException { 107 108 if ((browserState != updateState) || (wait <= 0)) { 109 110 112 return; 113 } 114 115 long now = System.currentTimeMillis(); 116 117 long giveUpTime = now + (wait * 1000); 118 119 while (browserState == updateState) { 121 long leftToGo = giveUpTime - now; 122 123 if (leftToGo <= 0) { 124 125 127 break; 128 } 129 130 try { 131 Thread.sleep(leftToGo); 132 } catch (InterruptedException e) { 133 throw new AirSentBusinessException("Exception in waitForChange: " + e); 134 } 135 136 now = System.currentTimeMillis(); 137 } 138 } 139 140 } 141 142 | Popular Tags |