Source code: com/fetish/directory/tool/NeighborRegistry.java
1 package com.fetish.directory.tool;
2
3 import java.util.*;
4 import net.jini.core.lookup.ServiceID;
5
6 /**
7 * Data structure to store neighbors
8 * @author SUN MicroSystems
9 */
10
11 public class NeighborRegistry {
12
13 /**
14 * Tree to look for neighbors through their url
15 */
16 private TreeMap neighbors;
17
18 /**
19 * Tree to look for neighbors through their ServiceID
20 */
21 private TreeMap serviceIDs;
22
23 /**
24 * Comparator for ServiceIDs
25 */
26 private class SidCmp implements java.util.Comparator {
27 public int compare( Object o1, Object o2 ) throws ClassCastException {
28 if( ( o1 instanceof ServiceID ) && ( o2 instanceof ServiceID ) ) {
29 long hi1 = ( ( ServiceID )o1 ).getMostSignificantBits();
30 long lo1 = ( ( ServiceID )o1 ).getLeastSignificantBits();
31 long hi2 = ( ( ServiceID )o2 ).getMostSignificantBits();
32 long lo2 = ( ( ServiceID )o2 ).getLeastSignificantBits();
33 if( hi1 == hi2 ) {
34 if( lo1 > lo2 ) {
35 return -1;
36 } else if( lo1 == lo2 ) {
37 return 0;
38 } else {
39 return 1;
40 }
41 } else {
42 if( hi1 > hi2 ) {
43 return -1;
44 } else if( hi1 == hi2 ) {
45 return 0;
46 } else {
47 return 1;
48 }
49 }
50 } else {
51 throw new ClassCastException( "Not a ServiceID" );
52 }
53 }
54 public boolean equals( Object o1, Object o2 ) {
55 if( ( o1 instanceof ServiceID ) && ( o2 instanceof ServiceID ) ) {
56 return ( ( ServiceID )o1 ).equals( ( ServiceID )o2 );
57 } else {
58 return false;
59 }
60 }
61 }
62
63 /**
64 * Creates an empty registry of neighbors
65 */
66 public NeighborRegistry() {
67 neighbors = new TreeMap();
68 serviceIDs = new TreeMap( new SidCmp() );
69 }
70
71 /**
72 * Adds a new pair to the structure
73 * @param neighborUrl The url of the new neighbor to be added
74 * @param neighborUrl The ServiceID of the new neighbor to be added
75 */
76 public synchronized void put( String neighborUrl, ServiceID sid ) {
77 if( !this.neighbors.containsKey( ( Object )neighborUrl ) && ! this.serviceIDs.containsKey( ( Object )sid ) ) {
78 this.neighbors.put( ( Object )neighborUrl, ( Object )sid );
79 this.serviceIDs.put( ( Object )sid, ( Object )neighborUrl );
80 }
81 }
82
83 /**
84 * Checks the existence of a neighbor url
85 * @param The url of the neighbor whose existence is desired to check
86 */
87 public synchronized boolean contains( String neighborUrl ) {
88 return this.neighbors.containsKey( ( Object )neighborUrl );
89 }
90
91 /**
92 * Checks the existence of a neighbor ServiceID
93 * @param The ServiceID of the neighbor whose existence is desired to check
94 */
95 public synchronized boolean contains( ServiceID sid ) {
96 return this.serviceIDs.containsKey( ( Object )sid );
97 }
98
99 /**
100 * Obtains a neighbor url through its ServiceID
101 * @param The ServiceID of the neighbor to be retrieved
102 */
103 public synchronized ServiceID get( ServiceID sid ) {
104 return ( ServiceID )this.serviceIDs.get( ( Object )sid );
105 }
106
107 /**
108 * Obtains a neighbor ServiceID through its url
109 * @param The url of the neighbor to be retrieved
110 */
111 public synchronized String get( String neighborUrl ) {
112 return ( String )this.neighbors.get( ( Object )neighborUrl );
113 }
114
115 /**
116 * Delete a neighbor identified by its ServiceID
117 * @param The ServiceID of the neighbor to remove
118 */
119 public synchronized void remove( ServiceID sid ) {
120 if( this.contains( sid ) ) {
121 String url = ( String )this.serviceIDs.get( ( Object )sid );
122 this.serviceIDs.remove( ( Object )sid );
123 this.neighbors.remove( ( Object )url );
124 }
125 }
126
127 /**
128 * Delete a neighbor identified by its url
129 * @param The url of the neighbor to remove
130 */
131 public synchronized void remove( String url ) {
132 if( this.contains( url ) ) {
133 ServiceID sid = ( ServiceID )this.neighbors.get( ( Object )url );
134 this.neighbors.remove( ( Object )url );
135 this.serviceIDs.remove( ( Object )sid );
136 }
137 }
138
139 /**
140 * Obtain the whole list of neighbor ServiceIDs
141 * @returns An array of ServiceID containing the ServiceIDs for all known neighbors
142 */
143 public synchronized ServiceID[] getServiceIDs() {
144 ServiceID[] sids = new ServiceID[ this.serviceIDs.size() ];
145 Object[] objectSids = this.serviceIDs.keySet().toArray();
146 for( int i=0; i<sids.length; i++ ) {
147 sids[ i ] = ( ServiceID )objectSids[ i ];
148 }
149 return sids;
150 }
151
152 /**
153 * Obtain the whole list of neighbor urls
154 * @returns An array of String containing the urls for all known neighbors
155 */
156 public synchronized String[] getUrls() {
157 String[] urls = new String[ this.neighbors.size() ];
158 Object[] objectUrls = this.neighbors.keySet().toArray();
159 for( int i=0; i<urls.length; i++ ) {
160 urls[ i ] = ( String )objectUrls[ i ];
161 }
162 return urls;
163 }
164
165 /**
166 * Destroys and reinitializes the structure
167 */
168 public synchronized void clear() {
169 this.neighbors = new TreeMap();
170 this.serviceIDs = new TreeMap( new SidCmp() );
171 }
172
173 /**
174 * Return the element count on the registry
175 * @returns The number of elements in the registry
176 */
177 public int size() {
178 return this.neighbors.size();
179 }
180 }