/*
 * @(#)XString.java    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 
 * <CODE>{@link String String}</CODE> class that also provides
 * <CODE>{@link Stringable Stringable}</CODE> capabilities.
 *
 * The default value for this class is
 * the <CODE>String</CODE> representation of epsilon,
 * <CODE>""</CODE>.</P>
 * 
 * @author  Jeff Raab
 * @version 2.2
 * @since   1.0
 */
public class XString extends XObject {

    /** The wrapped value of this object. */
    private String value = "";
    
    /**
     * Constructs a wrapper for the default
     * <CODE>String</CODE> value;
     *
     * @see #XString(String)
     */
    public XString() {}
    
    /**
     * Constructs a wrapper for the given
     * <CODE>String</CODE> value.
     * 
     * @param s the desired <CODE>String</CODE>
     * @see #XString()
     */
    public XString(String s) {
        setValue(s);
    }
    
    ////////////////
    // Stringable //
    ////////////////

    public void fromStringData(String s) {
        setValue(s);
    }
    
    public String toStringData() {
        return getValue();
    }
    
    ////////////////
    // Public API //
    ////////////////

    /**
     * Returns <CODE>true</CODE> if the wrapped <CODE>String</CODE>
     * is equal to the given object, and <CODE>false</CODE>
     * if it is not.
     *
     * @param other the object to be compared with 
     *      the wrapped <CODE>String</CODE>
     */
    public boolean equals(Object other) {
        if (other instanceof XString)
            return getValue().equals(((XString)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 the wrapped <CODE>String</CODE>.
     */
    public String toString() {
        return value;
    }
    
    /**
     * Sets the value wrapped by this object 
     * to the given value.
     *
     * If <CODE>null</CODE>, the value is set
     * to the empty string, <CODE>""</CODE>.
     *
     * @param s the value to be wrapped
     * @see #getValue()
     */
    public void setValue(String s) {
        String oldValue = value;
        
        // check for null value
        if (s == null)
            s = "";
        
        value = s;
        
        // if the value has changed
        if (!getValue().equals(oldValue)) {
        
            // notify listeners of property change
            changeAdapter.firePropertyChange(
                VALUE, 
                oldValue, 
                getValue());
        }
    }

    /**
     * Returns the wrapped value of this object.
     * 
     * @see #setValue(String)
     */
    public String getValue() {
        return value;
    }

    ////////////////////
    // Static methods //
    ////////////////////
    
    /**
     * Returns an array of <CODE>XString</CODE> objects
     * initialized from the given array 
     * of <CODE>String</CODE> objects.
     * 
     * @param a an array of <CODE>String</CODE>s
     * @return the resulting array 
     *      of <CODE>XString</CODE> objects
     */
    public static XString[] toXArray(String[] a) {
        
        // return a null array if appropriate
        if (a == null) 
            return null;

        // otherwise translate the types
        XString[] temp = new XString[a.length];
        for (int i = 0; i < temp.length; i++)
            if (a[i] != null)
                temp[i] = new XString(a[i]);
        return temp;
    }
}
