01: /*
02: * The contents of this file are subject to the Sapient Public License
03: * Version 1.0 (the "License"); you may not use this file except in compliance
04: * with the License. You may obtain a copy of the License at
05: * http://carbon.sf.net/License.html.
06: *
07: * Software distributed under the License is distributed on an "AS IS" basis,
08: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
09: * the specific language governing rights and limitations under the License.
10: *
11: * The Original Code is The Carbon Component Framework.
12: *
13: * The Initial Developer of the Original Code is Sapient Corporation
14: *
15: * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16: */
17:
18: package org.sape.carbon.services.clustering.test;
19:
20: import java.io.InputStream;
21: import java.io.Serializable;
22: import java.util.Random;
23: import java.util.zip.CRC32;
24:
25: /**
26: * <p>This is the template for Classes.</p>
27: *
28: *
29: * @since carbon 1.0
30: * @author Greg Hinkle, January 2002
31: * @version $Revision: 1.3 $($Author: dvoet $ / $Date: 2003/05/05 21:21:09 $)
32: * @copyright 2002 Sapient
33: */
34: public class TestObject implements Serializable {
35:
36: private static final int SIZE = 50;
37:
38: private byte[] bytes;
39:
40: public TestObject() {
41: TestObject.RandomBinarySource source = new TestObject.RandomBinarySource();
42: bytes = new byte[SIZE];
43: try {
44: for (int i = 0; i < SIZE; i++) {
45: bytes[i] = (byte) source.read();
46: }
47: } catch (Exception e) {
48: // Random binary stream never throws exceptions
49: }
50: }
51:
52: public int hashCode() {
53: return (new Long(getCrc())).hashCode();
54: }
55:
56: public boolean equals(Object obj) {
57: return ((TestObject) obj).getCrc() == getCrc();
58: }
59:
60: public long getCrc() {
61: CRC32 crc = new CRC32();
62:
63: crc.update(this .bytes);
64:
65: return crc.getValue();
66:
67: }
68:
69: /**
70: * An helper class that implements java.io.InputStream and suplies and
71: * endless supply of Random bytes.
72: */
73: static class RandomBinarySource extends InputStream {
74: private Random rand;
75:
76: public RandomBinarySource() {
77: super ();
78: rand = new Random();
79: }
80:
81: public int read() throws java.io.IOException {
82: return (rand.nextInt(255));
83: }
84:
85: public void close() {
86: }
87: }
88:
89: }
|