KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > mobilitools > smi > api > Name


1 /*
2 * MobiliTools: an implementation of the Object Management Group's
3 * Mobile Agent Facility specification.
4 * Copyright (C) 2003 France Telecom R&D
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * MobiliTools $Name: $
21 *
22 * Contact: mobilitools-smi@lists.debian-sf.objectweb.org
23 *
24 * Authors: Bruno Dillenseger
25 */

26
27
28 package org.objectweb.mobilitools.smi.api;
29
30
31 import java.lang.Comparable JavaDoc;
32 import java.io.Serializable JavaDoc;
33
34
35 /**
36  * MobiliTools $Name: $, $Id: Name.java,v 1.1.1.1 2003/03/28 14:48:06 dillense Exp $
37  * <P>
38  * Wrapper class for MAF's Name class. Uniquely identies an agent or an agency.
39 */

40 public class Name implements Comparable JavaDoc, Serializable JavaDoc
41 {
42     byte[] my_authority;
43     byte[] my_identity;
44     short my_type;
45
46
47     /**
48         Duplicates a Name.
49         @param name the name to duplicate
50     */

51     public Name(Name name)
52     {
53         this(name.authority(), name.identity(), name.type());
54     }
55
56
57     /**
58         Creates a new Name object from a MAF name (IDL-defined type).
59         @param name the MAF representation of the name to duplicate
60     */

61     public Name(org.omg.CfMAF.Name name)
62     {
63         this(name.authority, name.identity, name.agent_system_type);
64     }
65
66
67     /**
68         Creates a new Name object from the given authority, identity
69         and agent system type identifier.
70         @param authority the authority of the agent or agency designated byt the new name
71         @param identity the identity of the agent or agency designated by the new name
72         @param type the agenct system type of the agent or agency designated by the new name
73     */

74     public Name(byte[] authority, byte[] identity, short type)
75     {
76         my_authority = authority;
77         my_identity = identity;
78         my_type = type;
79     }
80
81
82     /**
83         Creates a new Name object from the given authority and identity in
84         string representation, and agent system type identifier.
85         @param authority the authority of the agent or agency designated byt the new name
86         @param identity the identity of the agent or agency designated by the new name
87         @param type the agent system type of the agent or agency designated by the new name
88     */

89     public Name(String JavaDoc authority, String JavaDoc identity, short type)
90     {
91         this(authority.getBytes(), identity.getBytes(), type);
92     }
93
94
95     /**
96         Creates a new Name from a string representation of a MAF name.
97         @see #getStringRepresentation()
98     */

99     public Name(String JavaDoc str)
100     {
101         this(
102             new String JavaDoc(str.getBytes(), 1, str.getBytes()[0]),
103             new String JavaDoc(str.getBytes(), 2 + str.getBytes()[0], str.getBytes()[1 + str.getBytes()[0]]),
104             (short)Integer.parseInt(new String JavaDoc(
105                 str.getBytes(),
106                 str.getBytes()[1+str.getBytes()[0]] + str.getBytes()[0] + 2,
107                 str.getBytes().length - (str.getBytes()[1+str.getBytes()[0]] + str.getBytes()[0] + 2))));
108     }
109
110
111     /**
112         @return the MAF name (IDL-defined type).
113     */

114     public org.omg.CfMAF.Name getmafname()
115     {
116         return new org.omg.CfMAF.Name(my_authority, my_identity, my_type);
117     }
118
119
120     /**
121         @return a string representation of the agent idendity.
122     */

123     public String JavaDoc identity()
124     {
125         return new String JavaDoc(my_identity);
126     }
127
128
129     /**
130         @return a string representation of the agent's authority.
131     */

132     public String JavaDoc authority()
133     {
134         return new String JavaDoc(my_authority);
135     }
136
137
138     /**
139         @return the agent system type Id
140     */

141     public short type()
142     {
143         return my_type;
144     }
145
146
147     /**
148         @return a printable string (partial) representation of the name
149         using format "<I>identity</I> <B>(</B><I>authority</I><B>)</B>".
150     */

151     public String JavaDoc toString()
152     {
153         return identity() + " (" + authority() + ")";
154     }
155
156
157     /**
158         @return a string-coded full representation of the MAF name (not printable).
159     */

160     public String JavaDoc getStringRepresentation()
161     {
162         byte[] byteRep = new byte[1 + my_authority.length + 1 + my_identity.length];
163         int i, j;
164         byteRep[0] = (byte) my_authority.length;
165         for (i=0, j=1 ; i<my_authority.length ; ++i, ++j)
166         {
167             byteRep[j] = my_authority[i];
168         }
169         byteRep[j++] = (byte) my_identity.length;
170         for (i=0 ; i<my_identity.length ; ++i, ++j)
171         {
172             byteRep[j] = my_identity[i];
173         }
174         return new String JavaDoc(byteRep) + String.valueOf(my_type);
175     }
176
177
178     public boolean equals(Object JavaDoc other)
179     {
180         Name otherName = null;
181         if (other instanceof Name)
182         {
183             otherName = (Name)other;
184         }
185         else if (other instanceof org.omg.CfMAF.Name)
186         {
187             otherName = new Name((org.omg.CfMAF.Name)other);
188         }
189         return
190             otherName != null &&
191             identity().equals(otherName.identity()) &&
192             authority().equals(otherName.authority()) &&
193             my_type == otherName.type();
194     }
195
196
197     public int hashCode()
198     {
199         return getStringRepresentation().hashCode();
200     }
201
202
203     public int compareTo(Object JavaDoc other)
204     {
205         return getStringRepresentation().compareTo(((Name)other).getStringRepresentation());
206     }
207 }
208
Popular Tags