2 * Copyright (c) 2001-2007 Sun Microsystems, Inc. All rights reserved.
4 * The Sun Project JXTA(TM) Software License
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * 3. The end-user documentation included with the redistribution, if any, must
17 * include the following acknowledgment: "This product includes software
18 * developed by Sun Microsystems, Inc. for JXTA(TM) technology."
19 * Alternately, this acknowledgment may appear in the software itself, if
20 * and wherever such third-party acknowledgments normally appear.
22 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
23 * not be used to endorse or promote products derived from this software
24 * without prior written permission. For written permission, please contact
25 * Project JXTA at http://www.jxta.org.
27 * 5. Products derived from this software may not be called "JXTA", nor may
28 * "JXTA" appear in their name, without prior written permission of Sun.
30 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
32 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SUN
33 * MICROSYSTEMS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
36 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
39 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * JXTA is a registered trademark of Sun Microsystems, Inc. in the United
42 * States and other countries.
44 * Please see the license information page at :
45 * <http://www.jxta.org/project/www/license.html> for instructions on use of
46 * the license in source files.
48 * ====================================================================
50 * This software consists of voluntary contributions made by many individuals
51 * on behalf of Project JXTA. For more information on Project JXTA, please see
52 * http://www.jxta.org.
54 * This license is based on the BSD license adopted by the Apache Foundation.
56 package net.jxta.impl.id.binaryID;
60 import java.util.logging.Level;
61 import net.jxta.logging.Logging;
62 import java.util.logging.Logger;
63 import net.jxta.id.ID;
67 * This class implements a PeerGroup ID. Each peer group is assigned a unique
68 * peer id.BinaryID id are used to implement peer group id. Because this id is
69 * built with BinaryID, pulling the parent group requires a little work. The
70 * parent group is the first id, with the second following, separated by a
71 * dash '-' character.<p>
73 * @author Daniel Brookshier <a HREF="mailto:turbogeek@cluck.com">turbogeek@cluck.com</a>
75 * @see net.jxta.id.IDFactory
76 * @see net.jxta.peergroup.PeerGroupID
78 public final class PeerGroupBinaryID extends net.jxta.peergroup.PeerGroupID {
81 * LOG object for this class.
83 private final static transient Logger LOG = Logger.getLogger(PeerGroupBinaryID.class.getName());
86 * This is the id string used in the XML of the id. The format is TX0..Xn where T is the type and X0 through Xn are the base64 encoded id.
91 * Constructor for creating a new PeerGroupID with a unique ID and a parent.<p>
93 * Note that only the ID for the parent is obtained and not the
94 * parent and the grandparent.
96 * @param parent Parent peer group.
97 * @param data data byte array to be used as the id.
98 * @param lengthIncluded If true, the first byte in the data array is the length of the remaining bytes.
100 public PeerGroupBinaryID(net.jxta.peergroup.PeerGroupID parent, byte[] data, boolean lengthIncluded) {
103 String parentStr = IDFormat.childGroup(parent);
105 if (parentStr != null) {
106 id = BinaryIDFactory.newBinaryID(BinaryID.flagPeerGroupID, data, lengthIncluded).getID() + "."
107 + parentStr.replace('-', '.');
109 id = BinaryIDFactory.newBinaryID(BinaryID.flagPeerGroupID, data, lengthIncluded).getID();
114 * Creates a ID from a string. Note that the ID is not currently validated.
116 * @param id Value of ID.
119 protected PeerGroupBinaryID(String id) {
125 * Constructor for creating a new PeerGroupID with a unique ID and a parent.
127 * @param data DOCUMENT ME!
128 * @param lengthIncluded DOCUMENT ME!
130 public PeerGroupBinaryID(byte[] data, boolean lengthIncluded) {
132 id = BinaryIDFactory.newBinaryID(BinaryID.flagPeerGroupID, data, lengthIncluded).getID();
136 * Constructor for creating a new PeerGroupID. Note that this creates an
137 * invalid ID but is required for serialization.
139 public PeerGroupBinaryID() {
144 * Constructor. Intializes contents from provided ID. This PeerGroupID has
147 * @param id the ID data
149 public PeerGroupBinaryID(BinaryID id) {
151 this.id = id.getID();
158 public boolean equals(Object target) {
159 if (this == target) {
163 return target instanceof PeerGroupBinaryID && getUniqueValue().equals(((PeerGroupBinaryID) target).getUniqueValue());
170 public int hashCode() {
171 return getUniqueValue().hashCode();
178 public String getIDFormat() {
179 return IDFormat.INSTANTIATOR.getSupportedIDFormat();
186 public Object getUniqueValue() {
188 return ID.nullID.getUniqueValue();
191 return getIDFormat() + "-" + id;
197 public net.jxta.id.ID getPeerGroupID() {
198 // convert to the generic world PGID as necessary
199 return IDFormat.translateToWellKnown(this);
206 public net.jxta.peergroup.PeerGroupID getParentPeerGroupID() {
207 net.jxta.peergroup.PeerGroupID result = null;
211 result = (net.jxta.peergroup.PeerGroupID) net.jxta.id.ID.nullID;
214 int parentStart = idd.indexOf('.');
216 if (parentStart != -1) {
217 idd = idd.substring(parentStart + 1);
221 URI url = new URI("urn:jxta:" + idd.replace('.', '-'));
222 net.jxta.peergroup.PeerGroupID peerGroupID = (net.jxta.peergroup.PeerGroupID) net.jxta.id.IDFactory.fromURI(url);
224 result = (net.jxta.peergroup.PeerGroupID) IDFormat.translateToWellKnown(peerGroupID);
225 } catch (Exception e) {
226 if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
227 LOG.warning("cannot convert sub group. ID value = " + id);
232 // LOG.error("getParentPeerGroupID():"+result);
237 * returns the coded ID without the binaryid tag.
239 * @return The coded ID without the binaryid tag.
241 protected String getID() {