package edu.unl.dCache.namespace;

// Statistics structures

class StatEntry {
	
		private long _starttime; 
		private String _start = null;
		private String _finish = null;
		private long _finishtime;
		private long _status = -1;
		
		public StatEntry( Object startObj ) {
			if (startObj != null) {
				_start = startObj.toString();
			} else {
				_start = "null";
			}
			_starttime = System.currentTimeMillis();
		}
		
		public void failRequest( ) {
			failRequest( null );
		}
		
		public void failRequest( Object finishObj ) {
			if (finishObj != null) {
				_finish = finishObj.toString();
			} else {
				_finish = "null";
			}
			_status = 1;
			_finishtime = System.currentTimeMillis();
		}
		
		public void finishRequest( ) {
			finishRequest( null );
		}
		
		public void finishRequest( Object finishObj ) {
			if (finishObj != null) {
				_finish = finishObj.toString();				
			} else {
				_finish = "null";
			}
			_status = 0;
			_finishtime = System.currentTimeMillis();
		}

		public long getLength() throws Exception {
			if (_status >= 0) {
				return _finishtime - _starttime;
			}
			throw new Exception( "Tried to get execution length of an unfinished stat item!" );
		}
		
		public Object getStart() {
			return _start;
		}
		
		public Object getFinish() throws Exception {
			if (_status >= 0) {
				return _finish;
			}
			throw new Exception( "Tried to get the finish object of an unfinished stat item!" );
		}
		
		public long getStartTime() {
			return _starttime;
		}
		
		public long getFinishTime() throws Exception {
			if (_status >= 0) {
				return _finishtime;
			}
			throw new Exception( "Tried to get a finish time before finishing stat item!" );
		}
		
		public String toString(){
			int length = _start.length() + _finish.length();
			StringBuffer sb = new StringBuffer(36 + length);
			sb.append(_starttime).append(", ").append(_finishtime).append("; ");
			sb.append(_status).append("; ");
			sb.append(_start).append("; ").append(_finish);
			return sb.toString();

		}
		
}
