KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > protocols > WanPipeAddress


1 // $Id: WanPipeAddress.java,v 1.6 2005/04/15 13:17:02 belaban Exp $
2

3 package org.jgroups.protocols;
4
5 import org.jgroups.Address;
6 import org.jgroups.util.Util;
7
8 import java.io.*;
9
10
11 /**
12  * Logical address for a WAN pipe (logical link)
13  */

14 public class WanPipeAddress implements Address {
15     String JavaDoc logical_name=null;
16
17
18     // Used only by Externalization
19
public WanPipeAddress() {
20     }
21
22
23     public WanPipeAddress(String JavaDoc logical_name) {
24         this.logical_name=logical_name;
25     }
26
27
28     public boolean isMulticastAddress() {
29         return true;
30     }
31
32     public int size() {
33         return logical_name != null? logical_name.length()+2 : 22;
34     }
35
36
37     /**
38      * Establishes an order between 2 addresses. Assumes other contains non-null WanPipeAddress.
39      *
40      * @return 0 for equality, value less than 0 if smaller, greater than 0 if greater.
41      */

42     public int compareTo(Object JavaDoc other) throws ClassCastException JavaDoc {
43         if(other == null) {
44             System.err.println("WanPipeAddress.compareTo(): other address is null !");
45             return -1;
46         }
47
48         if(!(other instanceof WanPipeAddress)) {
49             System.err.println("WanPipeAddress.compareTo(): other address is not of type WanPipeAddress !");
50             return -1;
51         }
52
53         if(((WanPipeAddress)other).logical_name == null) {
54             System.err.println("WanPipeAddress.compareTo(): other address is null !");
55             return -1;
56         }
57
58         return logical_name.compareTo(((WanPipeAddress)other).logical_name);
59     }
60
61
62     public boolean equals(Object JavaDoc obj) {
63         return compareTo(obj) == 0 ? true : false;
64     }
65
66
67     public int hashCode() {
68         return logical_name.hashCode();
69     }
70
71
72     public String JavaDoc toString() {
73         return logical_name;
74     }
75
76
77     public void writeExternal(ObjectOutput out) throws IOException {
78         out.writeObject(logical_name);
79     }
80
81
82     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
83         logical_name=(String JavaDoc)in.readObject();
84     }
85
86
87
88     public static void main(String JavaDoc args[]) {
89
90         WanPipeAddress a=new WanPipeAddress("daddy");
91         System.out.println(a);
92
93         WanPipeAddress b=new WanPipeAddress("daddy.nms.fnc.fujitsu.com");
94         System.out.println(b);
95
96
97         if(a.equals(b))
98             System.out.println("equals");
99         else
100             System.out.println("does not equal");
101     }
102
103
104     public void writeTo(DataOutputStream outstream) throws IOException {
105         Util.writeString(logical_name, outstream);
106     }
107
108     public void readFrom(DataInputStream instream) throws IOException, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
109         logical_name=Util.readString(instream);
110     }
111 }
112
Popular Tags