/* ************************************************************************
#
#  DivConq
#
#  http://divconq.com/
#
#  Copyright:
#    Copyright 2012 eTimeline, LLC. All rights reserved.
#
#  License:
#    See the license.txt file in the project's top-level directory for details.
#
#  Authors:
#    * Andy White
#
************************************************************************ */
package divconq.struct.scalar;

import divconq.struct.ScalarStruct;
import divconq.struct.Struct;

public class BooleanStruct extends ScalarStruct {
	protected Boolean value = null;

	@Override
	public Object getGenericValue() {
		return this.value;
	}
	
	@Override
	public void adaptValue(Object v) {
		this.value = Struct.objectToBoolean(v);
	}

	public Boolean getValue() {
		return this.value; 
	}
	
	public void setValue(Boolean v) { 
		this.value = v; 
	}
	
	@Override
	public boolean isEmpty() {
		return (this.value == null);
	}

		/*
			switch (operation)
			{
				case "Copy":
					Value = iv.Value;
					break;
				default:
					throw new ArgumentException();
			}
		*/

	@Override
	public Object clone() {
		BooleanStruct n = new BooleanStruct();
		n.setValue(this.value);
		//n.setName(this.name);
		return n;
	}

	@Override
	public boolean equals(Object obj) {
		return (BooleanStruct.comparison(this, obj) == 0);
	}

	@Override
	public int compare(Object y) {
		return BooleanStruct.comparison(this, y);
	}

	@Override
	public int hashCode() {
		return (this.value == null) ? 0 : this.value.hashCode();
	}
	
	@Override
	public String toString() {
		return (this.value == null) ? "null" : this.value.toString();
	}

	public static int comparison(Object x, Object y)
	{
		if ((y == null) && (x == null))
			return 0;

		if (y == null)
			return 1;

		if (x == null)
			return -1;

		if ((x instanceof BooleanStruct) && (y instanceof BooleanStruct)) {
			Boolean xv = ((BooleanStruct)x).getValue();
			Boolean yv = ((BooleanStruct)y).getValue();

			if ((yv == null) && (xv == null))
				return 0;

			if (yv == null)
				return 1;

			if (xv == null)
				return -1;

			return xv.compareTo(yv);
		}
		
		// TODO add support for other numeric types

		return 0;
	}
}
