package org.btcollider.cnc.keysrv;

public class KeyTree {
	private boolean value;
	private boolean searched;

	private long inWorkSince;

	private KeyTree parent;

	private KeyTree left;
	private KeyTree right;

	public KeyTree(boolean value, KeyTree parent) {
		this.value = value;
		this.parent = parent;
		this.searched = false;
		this.inWorkSince = -1;
	}
	
	public boolean isFree() {
		return !isSearched() && !inWork();
	}

	public boolean isLeaf() {
		return left == null && right == null;
	}

	public boolean getValue() {
		return value;
	}

	public KeyTree getParent() {
		return parent;
	}

	public KeyTree getLeft() {
		return left;
	}

	public boolean hasLeft() {
		return left != null;
	}

	public void setLeft(KeyTree left) {
		this.left = left;
	}
	
	public KeyTree getRight() {
		return right;
	}
	
	public boolean hasRight() {
		return right != null;
	}

	public void setRight(KeyTree right) {
		this.right = right;
	}

	public Boolean isSearched() {
		return searched;
	}

	public void setSearched(boolean searched) {
		this.searched = searched;
	}

	public void setInWork(long since) {
		this.inWorkSince = since;
	}

	public void setInWork() {
		this.inWorkSince = System.currentTimeMillis();
	}
	
	public void clearInWork() {
		this.inWorkSince = -1;
	}

	public boolean inWork() {
		return inWorkSince != -1;
	}

	public long inWorkSince() {
		return this.inWorkSince;
	}
}
