/* ************************************************************************
#
#  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 IntegerStruct extends ScalarStruct {
	protected Long value = null;

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

	public Long getValue() {
		return this.value; 
	}
	
	public void setValue(Long 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;
			case "Random":
				int from = 1;
				int to = 100;

				if (source.HasA("From")) from = Convert.ToInt32(proc.ResolveValueToString(inst, source.A("From")));
				if (source.HasA("To")) to = Convert.ToInt32(proc.ResolveValueToString(inst, source.A("To")));

				Value = new Random().Next(to - from + 1) + from;
				break;
			default:
				throw new IllegalArgumentException("Unknown operation: " + operation);
		}
		*/

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

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

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