/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.util;

import java.io.Serializable;

/**
 * A mutable character class.
 *
 * @version <tt>$Revision: 1.1.4.2 $</tt>
 * @author  <a href="mailto:jason@planet57.com">Jason Dillon</a>
 */
public class MuCharacter
   implements Comparable, Cloneable, Serializable, Mutable
{
   /** <code>char</code> value */
   private char value = 0;

   /**
    * Construct a new mutable character.
    */
   public MuCharacter() {}

   /**
    * Construct a new mutable character.
    *
    * @param c    <code>char</code> value.
    */
   public MuCharacter(char c) {
      value = c;
   }

   /**
    * Construct a new mutable character.
    *
    * @param obj  Object to convert to a <code>char</code>.
    */
   public MuCharacter(Object obj) {
      setValue(obj);
   }

   /**
    * Set the value.
    *
    * @param c    <code>char</code> value.
    * @return     The previous value.
    */
   public char set(char c) {
      char old = value;
      value = c;
      return old;
   }

   /**
    * Get the current value.
    *
    * @return     The current value.
    */
   public char get() {
      return value;
   }

   /**
    * Return the <code>char</code> value of this mutable character.
    *
    * @return   <code>char</code> value.
    */
   public char charValue() {
      return value;
   }

   /**
    * Compares this object with the specified long for order.
    *
    * @param other   Value to compare with.
    * @return        A negative integer, zero, or a positive integer as
    *                this object is less than, equal to, or greater than
    *                the specified object.
    */
   public int compareTo(char other) {
      return (value < other) ? -1 : (value == other) ? 0 : 1;
   }

   /**
    * Compares this object with the specified object for order.
    *
    * @param other   Value to compare with.
    * @return        A negative integer, zero, or a positive integer as
    *                this object is less than, equal to, or greater than
    *                the specified object.
    *
    * @throws ClassCastException    Object is not a MuCharacter.
    */
   public int compareTo(Object obj) {
      return compareTo(((MuCharacter)obj).value);
   }

   /**
    * Convert this mutable character to a string.
    *
    * @return   String value.
    */
   public String toString() {
      return String.valueOf(value);
   }

   /**
    * Get the hash code of this mutable character.
    *
    * @return   Hash code.
    */
   public int hashCode() {
      return value;
   }

   /**
    * Test the equality of this mutable character and another object.
    *
    * @param obj    Qbject to test equality with.
    * @return       True if object is equal.
    */
   public boolean equals(Object obj) {
      if (obj == this) return true;

      if (obj != null && obj.getClass() == getClass()) {
         return value == ((MuCharacter)obj).charValue();
      }

      return false;
   }

   /**
    * Return a cloned copy of this mutable character.
    *
    * @return   Cloned mutable character.
    */
   public Object clone() {
      try {
         return super.clone();
      }
      catch (CloneNotSupportedException e) {
         throw new InternalError();
      }
   }


   /////////////////////////////////////////////////////////////////////////
   //                            Mutable Support                          //
   /////////////////////////////////////////////////////////////////////////

   /**
    * Set the value of this mutable character.
    *
    * @param obj  Object to convert to a <code>char</code>.
    *
    * @throws NotCoercibleException    Can not convert to <code>char</code>.
    */
   public void setValue(Object obj) {
      if (obj instanceof MuCharacter) {
         value = ((MuCharacter)obj).value;
      }
      else if (obj instanceof Character) {
         value = ((Character)obj).charValue();
      }
      else if (obj instanceof Number) {
         value = (char)((Number)obj).intValue();
      }
      else {
         throw new NotCoercibleException("can not convert to 'char': " + obj);
      }
   }

   /**
    * Return the char value of this mutable character.
    *
    * @return   <code>java.lang.Character</code> value.
    */
   public Object getValue() {
      return new Character(value);
   }
}

