/* ************************************************************************
#
#  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.BigInteger;

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

public class BigIntegerStruct extends ScalarStruct {
	protected BigInteger value = null;

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

	public BigInteger getValue() {
		return this.value; 
	}
	
	public void setValue(BigInteger 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() {
		BigIntegerStruct n = new BigIntegerStruct();
		n.setValue(this.value);
		//n.setName(this.name);
		return n;
	}

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

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