001/* 002 * $Id$ 003 */ 004 005package edu.jas.kern; 006 007 008import java.util.concurrent.Callable; 009 010 011/** 012 * Run-time status, defines local status and handling for local run time limits. 013 * @see edu.jas.kern.TimeStatus 014 * @author Heinz Kredel 015 */ 016 017public class LocalTimeStatus { 018 019 020 /** 021 * Local status flag. 022 */ 023 private boolean allowTime = false; 024 025 026 /** 027 * Local run-time limit in milliseconds. 028 */ 029 private long limitTime = Long.MAX_VALUE; 030 031 032 /** 033 * Local run-time limit in milliseconds. 034 */ 035 private long startTime = System.currentTimeMillis(); 036 037 038 /** 039 * Local call back method. true means continue, false means throw exception. 040 */ 041 private Callable<Boolean> callBack = null; 042 043 044 /** 045 * Public constructor. 046 */ 047 public LocalTimeStatus() { 048 this(false, Long.MAX_VALUE, false); 049 } 050 051 052 /** 053 * Public constructor. 054 * @param a true for active, false for inactive 055 * @param d time limit before exception 056 * @param r true for continue, false for exception 057 */ 058 public LocalTimeStatus(boolean a, long d, boolean r) { 059 allowTime = a; 060 limitTime = d; 061 callBack = new LocalTimeStatus.TSCall(r); 062 startTime = System.currentTimeMillis(); 063 } 064 065 066 /** 067 * To String. 068 * @return String representation of this. 069 */ 070 @Override 071 public String toString() { 072 StringBuffer sb = new StringBuffer("LocalTimeStatus("); 073 sb.append("" + allowTime); 074 sb.append(", " + limitTime); 075 try { 076 sb.append(", " + (callBack == null ? "null" : callBack.call())); 077 } catch (Exception e) { 078 } 079 sb.append(", " + startTime); 080 sb.append(")"); 081 return sb.toString(); 082 } 083 084 085 /** 086 * isActive. 087 * @return true, if run-time interruption is active, else false. 088 */ 089 public synchronized boolean isActive() { 090 return allowTime; 091 } 092 093 094 /** 095 * setAllow, set run-time interruption to allowed status. 096 */ 097 public synchronized void setActive() { 098 allowTime = true; 099 } 100 101 102 /** 103 * setNotActive, set run-time interruption to not active status. 104 */ 105 public synchronized void setNotActive() { 106 allowTime = false; 107 } 108 109 110 /** 111 * setLimit, set run-time limit in milliseconds. 112 */ 113 public synchronized void setLimit(long t) { 114 limitTime = t; 115 } 116 117 118 /** 119 * Restart timer, set run-time to current time. 120 */ 121 public synchronized void restart() { 122 startTime = System.currentTimeMillis(); 123 } 124 125 126 /** 127 * set call back, set the Callabe object. 128 */ 129 public synchronized void setCallBack(Callable<Boolean> cb) { 130 callBack = cb; 131 } 132 133 134 /** 135 * Check for exceeded time, test if time has exceeded and throw an exception 136 * if so. 137 * @param msg the message to be send with the exception. 138 */ 139 public synchronized void checkTime(String msg) { 140 if (!allowTime) { 141 return; 142 } 143 if (limitTime == Long.MAX_VALUE) { 144 return; 145 } 146 long tt = (System.currentTimeMillis() - startTime - limitTime); 147 //System.out.println("tt = " + (limitTime+tt)); 148 if (tt <= 0L) { 149 return; 150 } 151 if (callBack != null) { 152 try { 153 boolean t = callBack.call(); 154 if (t) { 155 return; 156 } 157 } catch (Exception e) { 158 } 159 } 160 if (msg != null) { 161 msg = msg + ", "; 162 } 163 throw new TimeExceededException(msg + "elapsed time >= " + (limitTime + tt) + " ms"); 164 } 165 166 167 /** 168 * A default call back class. 169 */ 170 public static class TSCall implements Callable<Boolean> { 171 172 173 boolean flag = true; 174 175 176 public TSCall(boolean b) { 177 flag = b; 178 } 179 180 181 public Boolean call() { 182 return flag; 183 } 184 185 } 186}