/*
 * @(#)XChar.java    1.1.1   2 May 2006
 *
 * Copyright 2006
 * 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.console.*;
import java.beans.*;
import java.text.ParseException;

/**
 * <P>Object wrapper for the primitive 
 * <CODE>char</CODE> type that also provides
 * <CODE>{@link Stringable Stringable}</CODE> capabilities.
 *
 * The default value for this class is
 * the <CODE>char</CODE> 0.</P>
 * 
 * @author  Jeff Raab
 * @version 2.2
 * @since   1.0
 */
public class XChar extends XObject {
    
    /** The wrapped value of this object. */
    private char value = 0;
    
    /**
     * Constructs a wrapper for the default
     * <CODE>char</CODE> value.
     *
     * @see #XChar(char)
     * @see #XChar(String)
     */
    public XChar() {}
    
    /**
     * Constructs a wrapper for the given
     * <CODE>char</CODE> value.
     * 
     * @param c the value to be wrapped
     * @see #XChar()
     * @see #XChar(String)
     */
    public XChar(char c) {
        value = c;
    }
    
    /**
     * Constructs a wrapper for the <CODE>char</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 #XChar()
     * @see #XChar(char)
     */
    public XChar(String s) throws ParseException {
        fromStringData(s);
    }
    
    ////////////////
    // Stringable //
    ////////////////

    public void fromStringData(String data) throws ParseException {
        char oldValue = value;
    
        if (data == null)
            throw new ParseException("Input was null.", -1);
            
        if (data.length() == 0)
            throw new ParseException("No input provided. ", -1);
            
        if (data.length() > 1)
            throw new ParseException("Provide one character only. ", 1);
            
        value = data.charAt(0);
            
        // report property change
        changeAdapter.firePropertyChange(
            VALUE,
            new Character(oldValue),
            data);
    }
    
    public String toStringData() {
        return value + "";
    }
    
    ////////////////
    // 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 other the object to be compared with the wrapped object
     */
    public boolean equals(Object other) {
        if (other instanceof XChar)
            return getValue() == ((XChar)other).getValue();
            
        return false;
    }
    
    /**
     * Returns an <CODE>int</CODE> hash code 
     * appropriate for the wrapped object.
     */
    public int hashCode() {
        return (new Character(getValue())).hashCode();
    }

    /**
     * Returns a <CODE>String</CODE> representation 
     * of the wrapped value.
     */
    public String toString() {
        return value + "";
    }
    
    /**
     * Sets the value wrapped by this object 
     * to the given value.
     *
     * @param c the value to be wrapped
     * @see #getValue()
     */
    public void setValue(char c) {
        char oldValue = value;
        value = c;
        
        // if the value has changed
        if (getValue() != oldValue) {
        
            // notify listeners of property change
            changeAdapter.firePropertyChange(
                VALUE, 
                new Character(oldValue), 
                new Character(getValue()));
        }
    }

    /**
     * Returns the value wrapped by this object.
     * 
     * @see #setValue(char)
     */
    public char getValue() {
        return value;
    }

    ////////////////////
    // Static methods //
    ////////////////////
    
    /**
     * Parses a <CODE>char</CODE> value from a <CODE>String<CODE>
     * using the current shared parser.
     *
     * @param s the <CODE>String</CODE> data to parse
     * @return the extracted <CODE>char</CODE> value
     * @throws NumberFormatException if the data is malformed
     */
    public static char parseChar(String s)
        throws NumberFormatException 
    {
    	try {
    		return (new XChar(s)).value;
    	}
    	catch (ParseException e) {
    		throw new NumberFormatException(
    		    formatErrorMessage(e, s));
    	} 
    }
    
    /**
     * Returns an array of primitive <CODE>char</CODE> values 
     * copied from the given array 
     * of <CODE>XChar</CODE> objects.</P>
     * 
     * @param x an array of <CODE>XChar</CODE>s
     * @return the resulting array 
     *      of <CODE>char</CODE> values
     * @see #toXArray(char[])
     */
    public static char[] toPrimitiveArray(XChar[] x) {
        
        // return null array if appropriate
        if (x == null)
            return null;
        
        // otherwise perform the type translation
        char[] temp = new char[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>XChar</CODE> objects
     * initialized from the given array 
     * of <CODE>char</CODE> values.
     * 
     * @param a an array of <CODE>char</CODE>s
     * @return the resulting array 
     *      of <CODE>XChar</CODE> objects
     * @see #toPrimitiveArray(XChar[])
     */
    public static XChar[] toXArray(char[] a) {
        
        // return null array if appropriate
        if (a == null)
            return null;
        
        // otherwise perform the type translation
        XChar[] temp = new XChar[a.length];
        for (int i = 0; i < temp.length; i++)
            temp[i] = new XChar(a[i]);
        return temp;
    }
}
