KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > test > BinaryWidget


1 /*
2  * Copyright 2004 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: BinaryWidget.java,v 1.1 2004/03/22 04:58:13 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.test;
12
13 import java.util.Arrays JavaDoc;
14
15
16 public class BinaryWidget extends Widget
17 {
18     private byte[] fixedLengthBinary;
19     private byte[] normalBinary;
20     private byte[] hugeBinary;
21
22
23     public BinaryWidget()
24     {
25         super();
26     }
27
28
29     public byte[] getFixedLengthBinary()
30     {
31         return fixedLengthBinary;
32     }
33
34
35     public byte[] getNormalBinary()
36     {
37         return normalBinary;
38     }
39
40
41     public byte[] getHugeBinary()
42     {
43         return hugeBinary;
44     }
45
46
47     public void setNormalBinary(byte[] normalBinary)
48     {
49         this.normalBinary = normalBinary;
50     }
51
52
53     /**
54      * Fills all of the object's fields with random data values. Any non-
55      * primitive fields (with the exception of <code>id</code>) will also be
56      * assigned <code>null</code> on a random basis.
57      */

58
59     public void fillRandom()
60     {
61         super.fillRandom();
62
63         fixedLengthBinary = nextNull() ? null : nextBinary(20);
64         normalBinary = nextNull() ? null : nextBinary(r.nextInt(21));
65         hugeBinary = nextNull() ? null : nextBinary(r.nextInt(101));
66     }
67
68
69     /**
70      * Indicates whether some other object is "equal to" this one. By comparing
71      * against an original copy of the object, <code>compareTo()</code> can be
72      * used to verify that the object has been written to a database and read
73      * back correctly.
74      *
75      * @param obj the reference object with which to compare
76      *
77      * @return <code>true</code> if this object is equal to the obj argument;
78      * <code>false</code> otherwise.
79      */

80
81     public boolean compareTo(Object JavaDoc obj)
82     {
83         if (obj == this)
84             return true;
85
86         if (!(obj instanceof BinaryWidget) || !super.compareTo(obj))
87             return false;
88
89         BinaryWidget w = (BinaryWidget)obj;
90
91         if (fixedLengthBinary == null) { if (w.fixedLengthBinary != null) return false; }
92         else if (!Arrays.equals(fixedLengthBinary, w.fixedLengthBinary)) return false;
93
94         if (normalBinary == null) { if (w.normalBinary != null) return false; }
95         else if (!Arrays.equals(normalBinary, w.normalBinary)) return false;
96
97         if (hugeBinary == null) { if (w.hugeBinary != null) return false; }
98         else if (!Arrays.equals(hugeBinary, w.hugeBinary)) return false;
99
100         return true;
101     }
102
103
104     /**
105      * Returns a string representation for this object. All of the field
106      * values are included in the string for debugging purposes.
107      *
108      * @return a string representation for this object.
109      */

110
111     public String JavaDoc toString()
112     {
113         StringBuffer JavaDoc s = new StringBuffer JavaDoc(super.toString());
114
115         s.append(" fixedLengthBinary = ").append(toHexString(fixedLengthBinary));
116         s.append('\n');
117         s.append(" normalBinary = ").append(toHexString(normalBinary));
118         s.append('\n');
119         s.append(" hugeBinary = ").append(toHexString(hugeBinary));
120         s.append('\n');
121
122         return s.toString();
123     }
124
125
126     /**
127      * Returns a hex string representation of a given byte array.
128      *
129      * @param ba the byte array to convert to string.
130      *
131      * @return a hex string representation of the given byte array.
132      */

133
134     private static String JavaDoc toHexString(byte[] ba)
135     {
136         if (ba == null)
137             return "null";
138
139         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
140
141         for (int i = 0; i < ba.length; ++i)
142             s.append(Integer.toHexString((int)ba[i] & 0xff));
143
144         return s.toString();
145     }
146 }
147
Popular Tags