KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > cmi > ClusterId


1 /*
2  * Copyright (C) 2002-2003, Simon Nieuviarts
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  */

19 package org.objectweb.carol.cmi;
20
21 import java.io.DataInput JavaDoc;
22 import java.io.DataOutput JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.Serializable JavaDoc;
25 import java.util.Arrays JavaDoc;
26
27 /**
28  * @author nieuviar
29  *
30  */

31 public class ClusterId implements Serializable JavaDoc {
32     private static WeakCache wc = new WeakCache();
33     private transient byte id[];
34     private transient int hash = 0;
35     private transient String JavaDoc str;
36
37     private ClusterId() {
38     }
39
40     private ClusterId(byte id[]) {
41         if (id.length > Short.MAX_VALUE) {
42             throw new IllegalArgumentException JavaDoc("Too long array for a cluster ID");
43         }
44         this.id = id;
45         redoHash();
46     }
47
48     public static ClusterId toClusterId(byte[] id) {
49         ClusterId cid = new ClusterId(id);
50         return (ClusterId) wc.getCached(cid);
51     }
52
53     private void redoHash() {
54         int h = 0;
55         int l = id.length;
56         int n = 1;
57         for (int i=0; i<l; i++) {
58             h += id[i] * n;
59             n *= 31;
60         }
61         hash = h;
62     }
63
64     public int hashCode() {
65         return hash;
66     }
67
68     public boolean match(byte ar[]) {
69         return Arrays.equals(id, ar);
70     }
71
72     public boolean equals(Object JavaDoc o) {
73         if (o instanceof ClusterId) {
74             ClusterId i = (ClusterId)o;
75             return match(i.id);
76         }
77         return false;
78     }
79
80     /**
81      * Readable format.
82      * @return cluster id in a human readable format
83      */

84     public String JavaDoc toString() {
85         if (str != null) return str;
86         String JavaDoc s = "";
87         int i;
88         for (i = 0; i < id.length; i++) {
89             int n = id[i];
90             if (n < 0) {
91                 n += 256;
92             }
93             if (i > 0) {
94                 s += "-";
95             }
96             s += n;
97         }
98         str = s;
99         return s;
100     }
101
102 // /**
103
// * @return a copy of the byte array of this Cluster Id
104
// */
105
// public byte[] toByteArray() {
106
// return (byte[]) id.clone();
107
// }
108
//
109
public void write(DataOutput JavaDoc out) throws IOException JavaDoc {
110         out.writeShort(id.length);
111         out.write(id);
112     }
113
114     public static ClusterId read(DataInput JavaDoc in) throws IOException JavaDoc {
115         int l = in.readShort();
116         byte[] a = new byte[l];
117         in.readFully(a);
118         ClusterId id = new ClusterId();
119         id.id = a;
120         id.redoHash();
121         return (ClusterId) wc.getCached(id);
122     }
123
124     private void writeObject(java.io.ObjectOutputStream JavaDoc out)
125         throws IOException JavaDoc {
126         out.writeShort(id.length);
127         out.write(id, 0, id.length);
128     }
129
130     private void readObject(java.io.ObjectInputStream JavaDoc in)
131         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
132         int l = in.readShort();
133         id = new byte[l];
134         in.readFully(id);
135         redoHash();
136     }
137 }
138
Popular Tags