BinaryWidget.java :  » Database-ORM » TJDO » com » triactive » jdo » test » Java Open Source

Java Open Source » Database ORM » TJDO 
TJDO » com » triactive » jdo » test » BinaryWidget.java
/*
 * Copyright 2004 (C) TJDO.
 * All rights reserved.
 *
 * This software is distributed under the terms of the TJDO License version 1.0.
 * See the terms of the TJDO License in the documentation provided with this software.
 *
 * $Id: BinaryWidget.java,v 1.1 2004/03/22 04:58:13 jackknifebarber Exp $
 */

package com.triactive.jdo.test;

import java.util.Arrays;


public class BinaryWidget extends Widget
{
    private byte[] fixedLengthBinary;
    private byte[] normalBinary;
    private byte[] hugeBinary;


    public BinaryWidget()
    {
        super();
    }


    public byte[] getFixedLengthBinary()
    {
        return fixedLengthBinary;
    }


    public byte[] getNormalBinary()
    {
        return normalBinary;
    }


    public byte[] getHugeBinary()
    {
        return hugeBinary;
    }


    public void setNormalBinary(byte[] normalBinary)
    {
        this.normalBinary = normalBinary;
    }


    /**
     * Fills all of the object's fields with random data values.  Any non-
     * primitive fields (with the exception of <code>id</code>) will also be
     * assigned <code>null</code> on a random basis.
     */

    public void fillRandom()
    {
        super.fillRandom();

        fixedLengthBinary = nextNull() ? null : nextBinary(20);
        normalBinary      = nextNull() ? null : nextBinary(r.nextInt(21));
        hugeBinary        = nextNull() ? null : nextBinary(r.nextInt(101));
    }


    /**
     * Indicates whether some other object is "equal to" this one.  By comparing
     * against an original copy of the object, <code>compareTo()</code> can be
     * used to verify that the object has been written to a database and read
     * back correctly.
     *
     * @param   obj     the reference object with which to compare
     *
     * @return  <code>true</code> if this object is equal to the obj argument;
     *          <code>false</code> otherwise.
     */

    public boolean compareTo(Object obj)
    {
        if (obj == this)
            return true;

        if (!(obj instanceof BinaryWidget) || !super.compareTo(obj))
            return false;

        BinaryWidget w = (BinaryWidget)obj;

        if (fixedLengthBinary == null) { if (w.fixedLengthBinary != null) return false; }
        else if (!Arrays.equals(fixedLengthBinary, w.fixedLengthBinary)) return false;

        if (normalBinary == null) { if (w.normalBinary != null) return false; }
        else if (!Arrays.equals(normalBinary, w.normalBinary)) return false;

        if (hugeBinary == null) { if (w.hugeBinary != null) return false; }
        else if (!Arrays.equals(hugeBinary, w.hugeBinary)) return false;

        return true;
    }


    /**
     * Returns a string representation for this object.  All of the field
     * values are included in the string for debugging purposes.
     *
     * @return  a string representation for this object.
     */

    public String toString()
    {
        StringBuffer s = new StringBuffer(super.toString());

        s.append("  fixedLengthBinary = ").append(toHexString(fixedLengthBinary));
        s.append('\n');
        s.append("  normalBinary = ").append(toHexString(normalBinary));
        s.append('\n');
        s.append("  hugeBinary = ").append(toHexString(hugeBinary));
        s.append('\n');

        return s.toString();
    }


    /**
     * Returns a hex string representation of a given byte array.
     *
     * @param ba     the byte array to convert to string.
     *
     * @return  a hex string representation of the given byte array.
     */

    private static String toHexString(byte[] ba)
    {
        if (ba == null)
            return "null";

        StringBuffer s = new StringBuffer();

        for (int i = 0; i < ba.length; ++i)
            s.append(Integer.toHexString((int)ba[i] & 0xff));

        return s.toString();
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.