/*
 * @(#)HexXLong.java    2.3  23 June 2003
 *
 * 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.util.*;
import java.text.*;

/**
 * <P>Class <CODE>HexXLong</CODE> extends <CODE>XLong</CODE>
 * by replacing the three methods:
 * <CODE>fromStringData</CODE>,
 * <CODE>toStringData</CODE>,
 * and <CODE>toString</CODE>,
 * to use hexadecimal digits rather than decimal digits.<P>
 *
 * @author  Richard Rasala
 * @version 2.3
 * @since   2.3
 */
public class HexXLong extends XLong {
    
    /**
     * Constructs a wrapper for the default <CODE>long</CODE> value.
     *
     * @see #HexXLong(long)
     * @see #HexXLong(String)
     */
    public HexXLong() { }
    
    
    /**
     * Constructs a wrapper for the given <CODE>long</CODE> value.
     *
     * @param x the value to be wrapped
     * @see #HexXLong()
     * @see #HexXLong(String)
     */
    public HexXLong(long x) { super(x); }
    
    
    /**
     * Constructs a wrapper for the long value whose state information
     * is encapsulated in the given hexadecimal <CODE>String</CODE> data
     * of length at most 16.
     *
     * @param s a hexadecimal <CODE>String</CODE> representation of the
     *          desired value
     * @see #HexXLong()
     * @see #HexXLong(long)
     * @see #fromStringData(String)
     * @throws ParseException if s is not hexadecimal with at most 16 digits
     */
    public HexXLong(String s) throws ParseException { super(s); }
    
    
    /**
     * <P>Sets the state of this object using information contained in the
     * given hexadecimal <CODE>String</CODE> of length at most 16.</P>
     *
     * <P>Uses <CODE>Hex.hexToLong</CODE> internally.</P>
     *
     * @param s a hexadecimal <CODE>String</CODE> representation of the
     *          desired value
     * @see Hex#hexToLong(String)
     * @throws ParseException if s is not hexadecimal with at most 16 digits
     */
    public void fromStringData(String s)
        throws ParseException
    {
        setValue(Hex.hexToLong(s));
    }
    
    
    /**
     * <P>Returns a hexadecimal <CODE>String</CODE> encapsulation of this object
     * that contains the information needed to set the state of this object at a
     * later time.</P>
     *
     * <P>Uses <CODE>Hex.longToHex</CODE> internally.</P>
     *
     * @return a hexadecimal <CODE>String</CODE> encapsulation of this object
     * @see Hex#longToHex(long)
     */
    public String toStringData() {
        return Hex.longToHex(getValue());
    }
    
    
    /**
     * <P>Returns <CODE>"0"</CODE> if the value of this object is zero; otherwise
     * returns the same <CODE>String</CODE> as <CODE>toStringData</CODE> with the
     * leading zeroes removed.</P>
     *
     * @return a brief hexadecimal <CODE>String</CODE> encapsulation of this object
     * @see #toStringData()
     */
    public String toString() {
        long x = getValue();
        
        if (x == 0)
            return "0";
        
        String s = Hex.longToHex(x);
        
        int length = s.length();
        
        for (int i = 0; i < length; i++)
            if (s.charAt(i) != '0')
                return s.substring(i);
        
        return s;
    }
    
}
