KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > protocols > pbcast > Gossip


1 // $Id: Gossip.java,v 1.4 2004/09/15 16:21:10 belaban Exp $
2

3 package org.jgroups.protocols.pbcast;
4
5 import org.jgroups.Address;
6
7 import java.io.Serializable JavaDoc;
8 import java.util.Vector JavaDoc;
9
10
11 public class Gossip implements Serializable JavaDoc {
12     Address sender=null;
13     long id=-1;
14     Digest digest=null;
15     Vector JavaDoc not_seen=null; // members who haven't seen this specific gossip yet
16
Vector JavaDoc seen=new Vector JavaDoc(11); // members who have seen the gossip already
17

18
19     public Gossip(Address obj, long id) {
20         sender=obj;
21         this.id=id;
22     }
23
24     public Gossip(Address obj, long id, Digest d, Vector JavaDoc not_seen) {
25         sender=obj;
26         this.id=id;
27         digest=d;
28         this.not_seen=not_seen;
29     }
30
31
32     /**
33      * Removes obj from not_seen list
34      */

35     public void removeFromNotSeenList(Address mbr) {
36         if(not_seen != null && mbr != null)
37             not_seen.removeElement(mbr);
38     }
39
40     public void addToSeenList(Address mbr) {
41         if(mbr != null && !seen.contains(mbr))
42             seen.addElement(mbr);
43     }
44
45
46     public int sizeOfNotSeenList() {
47         return not_seen == null ? 0 : not_seen.size();
48     }
49
50
51     public Vector JavaDoc getNotSeenList() {
52         return not_seen;
53     }
54
55     public Vector JavaDoc getSeenList() {
56         return seen;
57     }
58
59
60     public boolean equals(Object JavaDoc o) {
61         Gossip other=null;
62
63         if(sender != null && o != null) {
64             other=(Gossip)o;
65             return sender.equals(other.sender) && id == other.id;
66         }
67         return false;
68     }
69
70
71     public int hashCode() {
72         if(sender != null)
73             return sender.hashCode() + (int)id;
74         else
75             return (int)id;
76     }
77
78
79     public Gossip copy() {
80         Gossip ret=new Gossip(sender, id);
81         if(digest != null)
82             ret.digest=digest.copy();
83         if(not_seen != null)
84             ret.not_seen=(Vector JavaDoc)not_seen.clone();
85         if(seen != null)
86             ret.seen=(Vector JavaDoc)seen.clone();
87         return ret;
88     }
89
90
91     public String JavaDoc toString() {
92         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
93         sb.append("sender=");
94         if(sender != null)
95             sb.append(sender);
96         else
97             sb.append("<null>");
98         if(digest != null) sb.append(", digest=" + digest);
99         if(not_seen != null) sb.append(", not_seen=" + not_seen);
100         if(seen != null) sb.append(", seen=" + seen);
101         sb.append(", id=" + id);
102         return sb.toString();
103     }
104
105
106     public String JavaDoc shortForm() {
107         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
108         if(sender != null)
109             sb.append(sender);
110         else
111             sb.append("<null>");
112         sb.append("#" + id);
113         return sb.toString();
114     }
115
116
117     public static void main(String JavaDoc[] args) {
118         Gossip id1, id2;
119
120         id1=new Gossip(new org.jgroups.stack.IpAddress("daddy", 4567), 23);
121         id2=new Gossip(new org.jgroups.stack.IpAddress("133.164.130.19", 4567), 23);
122
123         System.out.println(id1.equals(id2));
124     }
125 }
126
Popular Tags