/* ************************************************************************
#
#  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 java.math.BigDecimal;

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

public class DecimalStruct extends ScalarStruct {
	protected BigDecimal value = null;

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

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

		/*
		switch (operation)
		{
			case "Inc":
				Value++;
				break;
			case "Dec":
				Value--;
				break;
			case "Copy":
				Value = iv.Value;
				break;
			case "Add":
				Value += iv.Value;
				break;
			case "Subtract":
				Value -= iv.Value;
				break;
			case "Multiply":
				Value *= iv.Value;
				break;
			case "Divide":
				Value /= iv.Value;
				break;
			case "Min":
				Value = (iv.Value < Value) ? iv.Value : Value;
				break;
			case "Max":
				Value = (iv.Value > Value) ? iv.Value : Value;
				break;
			case "Abs":
				Value = Math.Abs(iv.Value);
				break;
			default:
				throw new IllegalArgumentException("Unknown operation: " + operation);
		}
		*/

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

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

	@Override
	public int compare(Object y) {
		return DecimalStruct.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 DecimalStruct) && (y instanceof DecimalStruct)) {
			BigDecimal xv = ((DecimalStruct)x).getValue();
			BigDecimal yv = ((DecimalStruct)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;
	}
}
