]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/api/src/net/jxta/peergroup/PeerGroupID.java
5835fff907dbad86e1807a00c33ceda1ca375a34
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / api / src / net / jxta / peergroup / PeerGroupID.java
1 /*
2  * Copyright (c) 2001-2007 Sun Microsystems, Inc.  All rights reserved.
3  *  
4  *  The Sun Project JXTA(TM) Software License
5  *  
6  *  Redistribution and use in source and binary forms, with or without 
7  *  modification, are permitted provided that the following conditions are met:
8  *  
9  *  1. Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *  
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.
15  *  
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.
21  *  
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.
26  *  
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.
29  *  
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.
40  *  
41  *  JXTA is a registered trademark of Sun Microsystems, Inc. in the United 
42  *  States and other countries.
43  *  
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.
47  *  
48  *  ====================================================================
49  *  
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.
53  *  
54  *  This license is based on the BSD license adopted by the Apache Foundation. 
55  */
56 package net.jxta.peergroup;
57
58
59 import java.net.URI;
60
61 import net.jxta.id.ID;
62 import net.jxta.id.IDFactory;
63
64
65 /**
66  *  This class implements a PeerGroup ID. Each peer group is assigned a
67  *  unique id.
68  *
69  *  @see         net.jxta.id.ID
70  *  @see         net.jxta.id.IDFactory
71  *  @see         net.jxta.peer.PeerID
72  *
73  * @since JXTA 1.0
74  */
75 public abstract class PeerGroupID extends ID {
76     
77     /**
78      * Creates an ID by parsing the given URI.
79      *
80      * <p>This convenience factory method works as if by invoking the
81      * {@link net.jxta.id.IDFactory#fromURI(URI)} method; any 
82      * {@link java.net.URISyntaxException} thrown is caught and wrapped in a 
83      * new {@link IllegalArgumentException} object, which is then thrown.  
84      *
85      * <p> This method is provided for use in situations where it is known that
86      * the given string is a legal ID, for example for ID constants declared
87      * within in a program, and so it would be considered a programming error
88      * for the URI not to parse as such.  The {@link net.jxta.id.IDFactory}, 
89      * which throws {@link java.net.URISyntaxException} directly, should be used 
90      * situations where a ID is being constructed from user input or from some 
91      * other source that may be prone to errors. 
92      *
93      * @param  fromURI   The URI to be parsed into an ID
94      * @return The new ID
95      *
96      * @throws  NullPointerException If {@code fromURI} is {@code null}.
97      * @throws  IllegalArgumentException If the given URI is not a valid ID.
98      */
99     public static PeerGroupID create(URI fromURI) {
100         return (PeerGroupID) ID.create(fromURI);
101     }
102     
103     /**
104      *  {@inheritDoc}
105      */
106     public PeerGroupID intern() {
107         return (PeerGroupID) super.intern();
108     }
109     
110     /**
111      * The well known Unique Identifier of the world peergroup.
112      * This is a singleton within the scope of a VM.
113      */
114     public final static PeerGroupID worldPeerGroupID = (new WorldPeerGroupID()).intern();
115     
116     /**
117      * The well known Unique Identifier of the net peergroup.
118      * This is a singleton within the scope of this VM.
119      */
120     public final static PeerGroupID defaultNetPeerGroupID = (new NetPeerGroupID()).intern();
121     
122     /**
123      *  Returns the parent peer group id of this peer group id, if any.
124      *
125      *  @return the id of the parent peergroup or null if this group has no
126      *  parent group.
127      */
128     public abstract PeerGroupID getParentPeerGroupID();
129 }
130
131
132 final class WorldPeerGroupID extends PeerGroupID {
133     
134     /**
135      * The name associated with this ID Format.
136      */
137     final static String JXTAFormat = "jxta";
138     
139     private static final String UNIQUEVALUE = "WorldGroup";
140     
141     /**
142      *  WorldPeerGroupID is not intended to be constructed. You should use the 
143      *  {@link PeerGroupID.worldPeerGroupID} constant instead.
144      */
145     WorldPeerGroupID() {}
146     
147     /**
148      *  {@inheritDoc}
149      */
150     @Override
151     public boolean equals(Object target) {
152         return (this == target); // worldPeerGroupID is only itself.
153     }
154     
155     /**
156      * deserialization has to point back to the singleton in this VM
157      */
158     private Object readResolve() {
159         return PeerGroupID.worldPeerGroupID;
160     }
161     
162     /**
163      *  {@inheritDoc}
164      */
165     @Override
166     public String getIDFormat() {
167         return JXTAFormat;
168     }
169     
170     /**
171      *  {@inheritDoc}
172      */
173     @Override
174     public Object getUniqueValue() {
175         return getIDFormat() + "-" + UNIQUEVALUE;
176     }
177     
178     /**
179      *  {@inheritDoc}
180      */
181     @Override
182     public PeerGroupID getParentPeerGroupID() {
183         return null;
184     }
185 }
186
187
188 final class NetPeerGroupID extends PeerGroupID {
189
190     /**
191      * The name associated with this ID Format.
192      */
193     final static String JXTAFormat = "jxta";
194     
195     private static final String UNIQUEVALUE = "NetGroup";
196     
197     /**
198      *  NetPeerGroupID is not intended to be constructed. You should use the 
199      *  {@link PeerGroupID.defaultNetPeerGroupID} constant instead.
200      */
201     NetPeerGroupID() {}
202     
203     /**
204      *  {@inheritDoc}
205      */
206     @Override
207     public boolean equals(Object target) {
208         return (this == target); // netPeerGroupID is only itself.
209     }
210     
211     /**
212      * deserialization has to point back to the singleton in this VM
213      */
214     private Object readResolve() {
215         return PeerGroupID.defaultNetPeerGroupID;
216     }
217     
218     /**
219      *  {@inheritDoc}
220      */
221     @Override
222     public String getIDFormat() {
223         return JXTAFormat;
224     }
225     
226     /**
227      *  {@inheritDoc}
228      */
229     @Override
230     public Object getUniqueValue() {
231         return getIDFormat() + "-" + UNIQUEVALUE;
232     }
233     
234     /**
235      *  {@inheritDoc}
236      */
237     @Override
238     public PeerGroupID getParentPeerGroupID() {
239         return PeerGroupID.worldPeerGroupID;
240     }
241 }