KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > clustering > test > TestObject


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * 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 JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.util.Random JavaDoc;
23 import java.util.zip.CRC32 JavaDoc;
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 JavaDoc {
35
36     private static final int SIZE = 50;
37
38     private byte[] bytes;
39
40     public TestObject() {
41         TestObject.RandomBinarySource source = new
42             TestObject.RandomBinarySource();
43         bytes = new byte[SIZE];
44         try {
45             for (int i=0; i < SIZE; i++) {
46                bytes[i] = (byte) source.read();
47             }
48         } catch (Exception JavaDoc e) {
49             // Random binary stream never throws exceptions
50
}
51     }
52
53     public int hashCode() {
54         return (new Long JavaDoc(getCrc())).hashCode();
55     }
56
57     public boolean equals(Object JavaDoc obj) {
58         return ((TestObject)obj).getCrc() == getCrc();
59     }
60
61     public long getCrc() {
62         CRC32 JavaDoc crc = new CRC32 JavaDoc();
63
64         crc.update(this.bytes);
65
66         return crc.getValue();
67
68     }
69
70
71
72         /**
73      * An helper class that implements java.io.InputStream and suplies and
74      * endless supply of Random bytes.
75      */

76     static class RandomBinarySource extends InputStream JavaDoc {
77         private Random JavaDoc rand;
78
79         public RandomBinarySource() {
80             super();
81             rand = new Random JavaDoc();
82         }
83
84         public int read()
85         throws java.io.IOException JavaDoc {
86             return (rand.nextInt(255));
87         }
88
89         public void close() {
90         }
91     }
92
93 }
94
Popular Tags