package info.bukova.isspst.filters;

import org.jfree.util.Log;

public class TristateBoolean
{
	private static final String TAG = TristateBoolean.class.getSimpleName();

	public static final int NULL = -1;
	public static final int FALSE = 0;
	public static final int TRUE = 1;

	public static final int getValidValue(final int value)
	{
		if ((value != TristateBoolean.NULL) && (value != TristateBoolean.FALSE) && (value != TristateBoolean.TRUE))
		{
			Log.warn(TAG + "\nBad tristate boolean value.");
			return TristateBoolean.NULL;
		}

		return value;
	}

	public TristateBoolean()
	{
		this.setValue(TristateBoolean.NULL);
	}

	public TristateBoolean(boolean value)
	{
		this.setValue(value ? TristateBoolean.TRUE : TristateBoolean.FALSE);
	}

	public TristateBoolean(int value)
	{
		this.setValue(value);
	}

	protected int value;

	public int getValue()
	{
		return value;
	}

	public void setValue(int value)
	{
		this.value = TristateBoolean.getValidValue(value);
	}

	public Boolean getBoolean()
	{
		switch (this.value)
		{
		case TristateBoolean.FALSE:
			return new Boolean(false);

		case TristateBoolean.TRUE:
			return new Boolean(true);

		default:
			return null;
		}
	}

	@Override
	public boolean equals(Object obj)
	{
		if (obj != null)
		{
			if (obj instanceof TristateBoolean)
			{
				TristateBoolean item = (TristateBoolean) obj;
				int thisValue = this.getValue();
				int itemValue = item.getValue();
				return (thisValue == itemValue);
			}
		}

		return false;
	}

	public boolean equals(int value)
	{
		return (this.getValue() == value);
	}
}
