/*
 * @(#)XBigInteger.java    1.2  30 May 2002
 *
 * Copyright 2004
 * College of Computer and Information Science
 * Northeastern University
 * Boston, MA  02115
 *
 * The Java Power Tools software may be used for educational
 * purposes as long as this copyright notice is retained intact
 * at the top of all source files.
 *
 * To discuss possible commercial use of this software, 
 * contact Richard Rasala at Northeastern University, 
 * College of Computer and Information Science,
 * 617-373-2462 or rasala@ccs.neu.edu.
 *
 * The Java Power Tools software has been designed and built
 * in collaboration with Viera Proulx and Jeff Raab.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either
 * in whole or in part without explicit permission.
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

package edu.neu.ccs;

import edu.neu.ccs.parser.*;
import java.beans.*;
import java.math.*;
import java.text.ParseException;

/**
 * <P>Object wrapper for the arbitrary precision 
 * <CODE>BigInteger</CODE> class that also provides
 * <CODE>{@link Stringable Stringable}</CODE> capabilities.
 *
 * The default value for this class is
 * the <CODE>BigInteger</CODE> representation of zero,
 * <CODE>{@link BigInteger#ZERO BigInteger.ZERO}</CODE>.</P>
 * 
 * @author  Jeff Raab
 * @author  Richard Rasala
 * @version 2.2
 * @since   1.0
 */
public class XBigInteger extends XNumber {
    
    /** The wrapped value of this object. */
    private BigInteger value = BigInteger.ZERO;
    
    /**
     * Constructs a wrapper for the default
     * <CODE>BigInteger</CODE> value of
     * <CODE>BigInteger.ZERO</CODE>.
     *
     * @see #XBigInteger(BigInteger)
     * @see #XBigInteger(String)
     */
    public XBigInteger() {}
    
    /**
     * Constructs a wrapper for the given 
     * <CODE>BigInteger</CODE> value.
     *
     * If the given value is <CODE>null</CODE>,
     * the wrapped value is set to 
     * <CODE>BigInteger.ZERO</CODE>.
     * 
     * @param b the value to be wrapped
     * @see #XBigInteger()
     * @see #XBigInteger(String)
     */
    public XBigInteger(BigInteger b) {
        setValue(b);
    }
    
    /**
     * Constructs a wrapper for the <CODE>BigInteger</CODE> value
     * whose state information is encapsulated
     * in the given <CODE>String</CODE> data.
     * 
     * @param s a <CODE>String</CODE> representation 
     *      of the desired value
     * @throws ParseException if the data is malformed
     * @see #XBigInteger()
     * @see #XBigInteger(BigInteger)
     */
    public XBigInteger(String s) throws ParseException {
        fromStringData(s);
    }
    
    ////////////////
    // Stringable //
    ////////////////

    public void fromStringData(String data) throws ParseException {
        // sanity check for null data
        if (data == null)
            throw new ParseException("Input was null.", -1);
        
        BigInteger oldValue = value;

        Parser p = ParserUtilities.getDefaultParser();
        Object obj = p.parse(data);

        // store extracted value
        if (obj instanceof XNumber) {
            XBigInteger x = ParserUtilities.toXBigInteger((XNumber) obj);
            value = x.getValue();
        }
        // otherwise throw an exception
        else {
            throw new ParseException(
                "Expected numeric value.", data.length());
        }
        
        // report property change
        changeAdapter.firePropertyChange(
            VALUE,
            oldValue,
            data);
    }
    
    public String toStringData() {
        return value.toString();
    }
    
    /////////////
    // XNumber //
    /////////////

    public byte byteValue() {
        BigInteger max = new BigInteger(Byte.MAX_VALUE + ""),
                   min = new BigInteger(Byte.MIN_VALUE + "");
    
        if ((getValue().compareTo(max) > 0) ||
            (getValue().compareTo(min) < 0))
        {
            throw new NumberFormatException(
                "Number beyond precision of a byte.");
        }
        
        return (byte)getValue().intValue();
    }

    public short shortValue() {
        BigInteger max = new BigInteger(Short.MAX_VALUE + ""),
                   min = new BigInteger(Short.MIN_VALUE + "");
    
        if ((getValue().compareTo(max) > 0) ||
            (getValue().compareTo(min) < 0))
        {
            throw new NumberFormatException(
                "Number beyond precision of a short.");
        }
        
        return (short)getValue().intValue();
    }

    public int intValue() {
        BigInteger max = new BigInteger(Integer.MAX_VALUE + ""),
                   min = new BigInteger(Integer.MIN_VALUE + "");
    
        if ((getValue().compareTo(max) > 0) ||
            (getValue().compareTo(min) < 0))
        {
            throw new NumberFormatException(
                "Number beyond precision of an int.");
        }
        
        return getValue().intValue();
    }

    public long longValue() {
        BigInteger max = new BigInteger(Long.MAX_VALUE + ""),
                   min = new BigInteger(Long.MIN_VALUE + "");
    
        if ((getValue().compareTo(max) > 0) ||
            (getValue().compareTo(min) < 0))
        {
            throw new NumberFormatException(
                "Number beyond precision of a long.");
        }
        
        return getValue().longValue();
    }
    
    // Overflow handled by IEEE use of +Infinity or -Infinity
    public float floatValue() {
        return getValue().floatValue();
    }

    // Overflow handled by IEEE use of +Infinity or -Infinity
    public double doubleValue() {
        return getValue().doubleValue();
    }

    ////////////////
    // Public API //
    ////////////////

    /**
     * Returns <CODE>true</CODE> if the wrapped object
     * is equal to the given object, and <CODE>false</CODE>
     * if it is not.
     *
     * @param the object to be compared with the wrapped object
     */
    public boolean equals(Object other) {
        if (other instanceof XBigInteger)
            return getValue().equals(((XBigInteger)other).getValue());
    
        return getValue().equals(other);
    }
    
    /**
     * Returns an <CODE>int</CODE> hash code 
     * appropriate for the wrapped object.
     */
    public int hashCode() {
        return getValue().hashCode();
    }

    /**
     * Returns a <CODE>String</CODE> representation 
     * of the wrapped value.
     */
    public String toString() {
        return value.toString();
    }
    
    /**
     * Sets the value wrapped by this object 
     * to the given value.
     *
     * If <CODE>null</CODE>, the value is set
     * to the default value.
     *
     * @param b the value to be wrapped
     * @see #getValue()
     */
    public void setValue(BigInteger b) {
        BigInteger oldValue = value;
        
        // check for null value
        if (b == null)
            b = BigInteger.ZERO;
        
        value = b;
        
        // if the value has changed
        if (!getValue().equals(oldValue)) {
        
            // notify listeners of property change
            changeAdapter.firePropertyChange(
                VALUE, 
                oldValue, 
                getValue());
        }
    }

    /**
     * Returns the value wrapped by this object.
     * 
     * @see #setValue(BigInteger)
     */
    public BigInteger getValue() {
        return value;
    }

    ////////////////////
    // Static methods //
    ////////////////////
    
    /**
     * Returns an array of <CODE>BigInteger</CODE> objects 
     * copied from the given array 
     * of <CODE>XBigInteger</CODE> objects.</P>
     * 
     * @param x an array of <CODE>XBigInteger</CODE>s
     * @return the resulting array 
     *      of <CODE>BigInteger</CODE> objects
     * @see #toXArray(BigInteger[])
     */
    public static BigInteger[] toPrimitiveArray(XBigInteger[] x) {
        
        // return null array if appropriate
        if (x == null)
            return null;
        
        // otherwise perform the type translation
        BigInteger[] temp = new BigInteger[x.length];
        for (int i = 0; i < temp.length; i++)
            if (x[i] != null)
                temp[i] = x[i].getValue();
        return temp;
    }

    /**
     * Returns an array of <CODE>XBigInteger</CODE> objects
     * initialized from the given array 
     * of <CODE>BigInteger</CODE> objects.
     * 
     * @param a an array of <CODE>BigInteger</CODE>s
     * @return the resulting array 
     *      of <CODE>XBigInteger</CODE> objects
     * @see #toPrimitiveArray(XBigInteger[])
     */
    public static XBigInteger[] toXArray(BigInteger[] a) {
        
        // return null array if appropriate
        if (a == null)
            return null;
        
        // otherwise perform the type translation
        XBigInteger[] temp = new XBigInteger[a.length];
        for (int i = 0; i < temp.length; i++)
            if (a[i] != null)
                temp[i] = new XBigInteger(a[i]);
        return temp;
    }
}
