Source code: net/jxta/impl/rendezvous/rpv/PeerViewDestination.java
1 /*
2 * Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. The end-user documentation included with the redistribution,
17 * if any, must include the following acknowledgment:
18 * "This product includes software developed by the
19 * Sun Microsystems, Inc. for Project JXTA."
20 * Alternately, this acknowledgment may appear in the software itself,
21 * if and wherever such third-party acknowledgments normally appear.
22 *
23 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and
24 * "Project JXTA" must not be used to endorse or promote products
25 * derived from this software without prior written permission.
26 * For written permission, please contact Project JXTA at
27 * http://www.jxta.org.
28 *
29 * 5. Products derived from this software may not be called "JXTA",
30 * nor may "JXTA" appear in their name, without prior written
31 * permission of Sun.
32 *
33 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 * DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR
37 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * SUCH DAMAGE.
45 * ====================================================================
46 *
47 * This software consists of voluntary contributions made by many
48 * individuals on behalf of Project JXTA. For more
49 * information on Project JXTA, please see
50 * <http://www.jxta.org/>.
51 *
52 * This license is based on the BSD license adopted by the Apache
53 * Foundation.
54 *
55 * $Id: PeerViewDestination.java,v 1.1 2004/07/22 21:35:55 jice Exp $
56 */
57 package net.jxta.impl.rendezvous.rpv;
58
59 import net.jxta.endpoint.EndpointAddress;
60 import net.jxta.id.ID;
61
62 /**
63 * This class contains only the comparable portion of PeerViewElement, so that it is possible
64 * to search for elements in the sorted set that the local PeerView is, without having
65 * enough information to create a valid PeerViewElement.
66 **/
67 public class PeerViewDestination implements Comparable {
68
69 /**
70 * An explicit endpoint address. This is normally a peerID based address but we happen
71 * to need it more often in the address form (it has also occured in the past that it could
72 * usefully be a real transport address).
73 **/
74 private EndpointAddress destAddress = null;
75
76 /**
77 * Constructs a PeerViewDestination from the given endpoint address.
78 **/
79 public PeerViewDestination(EndpointAddress addr) {
80 destAddress = addr;
81 }
82
83 /**
84 * Constructs a PeerViewDestination from a (peer)ID.
85 **/
86 public PeerViewDestination(ID peerId) {
87 destAddress = new EndpointAddress("jxta", peerId.getUniqueValue().toString(), null, null);
88 }
89
90 /**
91 * returns the destination address.
92 **/
93 public EndpointAddress getDestAddress() {
94 return destAddress;
95 }
96
97 /**
98 * {@inheritDoc}
99 *
100 * Note that only the protocol address and at a lower order the protocol name are considered
101 * for comparision.
102 **/
103 public int compareTo(Object other) {
104 PeerViewDestination pve = (PeerViewDestination) other;
105
106 int result = destAddress.getProtocolAddress().compareTo(pve.destAddress.getProtocolAddress());
107
108 if (result != 0) {
109 return result;
110 }
111
112 return destAddress.getProtocolName().compareTo(pve.destAddress.getProtocolName());
113 }
114
115 /**
116 * {@inheritDoc}
117 **/
118 public boolean equals(Object other) {
119
120 if (null == other) {
121 return false;
122 }
123
124 if (this == other) {
125 return true;
126 }
127
128 return 0 == compareTo(other);
129 }
130
131 /**
132 * {@inheritDoc}
133 **/
134 public int hashCode() {
135 return destAddress.hashCode();
136 }
137 }