/* * @(#)XBoolean.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.parser.*; import java.beans.*; import java.text.ParseException; /** *

Object wrapper for the primitive * boolean type that also provides * {@link Stringable Stringable} capabilities. * * The default value for this class is * false.

* * @author Jeff Raab * @version 2.2 * @since 1.0 */ public class XBoolean extends XObject { /** The wrapped value of this object. */ private boolean value = false; /** * Constructs a wrapper for the default * boolean value. * * @see #XBoolean(boolean) * @see #XBoolean(String) */ public XBoolean() {} /** * Constructs a wrapper for the given * boolean value. * * @param b the value to be wrapped * @see #XBoolean() * @see #XBoolean(String) */ public XBoolean(boolean b) { value = b; } /** * Constructs a wrapper for the boolean value * whose state information is encapsulated * in the given String data. * * @param s a String representation * of the desired value * @throws ParseException if the data is malformed * @see #XBoolean() * @see #XBoolean(boolean) */ public XBoolean(String s) throws ParseException { fromStringData(s); } //////////////// // Stringable // //////////////// public void fromStringData(String data) throws ParseException { boolean oldValue = value; Parser p = ParserUtilities.getDefaultParser(); Object obj = p.parse(data); // store extracted value if (obj instanceof XBoolean) { XBoolean b = (XBoolean)obj; value = b.getValue(); } // otherwise throw an exception else { throw new ParseException( "Expected boolean value.", data.length()); } // report property change changeAdapter.firePropertyChange( VALUE, new Boolean(oldValue), data); } public String toStringData() { return value + ""; } //////////////// // Public API // //////////////// /** * Returns true if the wrapped object * is equal to the given object, and false * if it is not. * * @param other the object to be compared with the wrapped object */ public boolean equals(Object other) { if (other instanceof XBoolean) return getValue() == ((XBoolean)other).getValue(); return false; } /** * Returns an int hash code * appropriate for the wrapped object. */ public int hashCode() { return (new Boolean(getValue())).hashCode(); } /** * Returns a String representation * of the wrapped value. */ public String toString() { return value + ""; } /** * Sets the value wrapped by this object * to the given value. * * @param b the value to be wrapped * @see #getValue() */ public void setValue(boolean b) { boolean oldValue = value; value = b; // if the value has changed if (getValue() != oldValue) { // notify listeners of property change changeAdapter.firePropertyChange( VALUE, new Boolean(oldValue), new Boolean(getValue())); } } /** * Returns the value wrapped by this object. * * @see #setValue(boolean) */ public boolean getValue() { return value; } //////////////////// // Static methods // //////////////////// /** * Parses a boolean value from a String * using the current shared parser object. * * @param data the String data to parse * @return the extracted boolean value * @throws NumberFormatException if the data is malformed */ public static boolean parseBoolean(String data) throws NumberFormatException { try { return (new XBoolean(data)).value; } catch (ParseException e) { throw new NumberFormatException( formatErrorMessage(e, data)); } } /** * Returns an array of primitive boolean values * copied from the given array * of XBoolean objects.

* * @param x an array of XBooleans * @return the resulting array * of boolean values * @see #toXArray(boolean[]) */ public static boolean[] toPrimitiveArray(XBoolean[] x) { // return null array if appropriate if (x == null) return null; // otherwise perform the type translation boolean[] temp = new boolean[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 XBoolean objects * initialized from the given array * of boolean values. * * @param a an array of booleans * @return the resulting array * of XBoolean objects * @see #toPrimitiveArray(XBoolean[]) */ public static XBoolean[] toXArray(boolean[] a) { // return null array if appropriate if (a == null) return null; // otherwise perform the type translation XBoolean[] temp = new XBoolean[a.length]; for (int i = 0; i < temp.length; i++) temp[i] = new XBoolean(a[i]); return temp; } }