key0-96/
key0-96/doc/key/
key0-96/doc/key/credits/
key0-96/doc/key/developers/
key0-96/doc/key/developers/resources/
key0-96/setup/caves/
key0-96/setup/help/
key0-96/setup/ruins/
key0-96/src/
key0-96/src/commands/
key0-96/src/events/
key0-96/src/hack/
key0-96/src/sql/
key0-96/src/swing/
key0-96/src/talker/forest/
key0-96/src/talker/objects/
key0-96/src/terminals/
package key;

import key.primitive.*;

import java.io.*;

/**
  *  A statistics class that keeps idle time
 */
public final class ConnectionStatistics extends Atom
{
	private static final long serialVersionUID = 4197321215561299181L;
	
	public static final AtomicElement[] ELEMENTS =
	{
			//  String getName();
		AtomicElement.construct( ConnectionStatistics.class, DateTime.class, "lastUnIdle",
			AtomicElement.PUBLIC_FIELD,
			"the time the last command was executed" ),
		AtomicElement.construct( ConnectionStatistics.class, Duration.class, "idleTime",
			AtomicElement.PUBLIC_ACCESSORS | AtomicElement.READ_ONLY,
			"the amount of time this connection has been idle" )
	};
	
	public static final AtomicStructure STRUCTURE = new AtomicStructure( Atom.STRUCTURE, ELEMENTS );
	
	/**
	  *  The time the last command was executed
	 */
	public DateTime lastUnIdle;
	
	public ConnectionStatistics()
	{
		unIdle();
	}
	
	public AtomicStructure getDeclaredStructure()
	{
		return( STRUCTURE );
	}

	public void loaded()
	{
		super.loaded();
		unIdle();
	}
	
	/**
	  *  Unidles the connection to the current time
	 */
	public void unIdle()
	{
		lastUnIdle = new DateTime();
	}
	
	public final Duration getIdleTime()
	{
		return( lastUnIdle.difference( new DateTime() ) );
	}
	
	public final Duration getIdleTime( DateTime currentTime )
	{
		return( lastUnIdle.difference( currentTime ) );
	}
}