KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > util > GUID


1 package org.apache.ojb.broker.util;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.io.Serializable JavaDoc;
19 import java.net.InetAddress JavaDoc;
20 import java.net.UnknownHostException JavaDoc;
21 import java.rmi.server.UID JavaDoc;
22
23 /**
24  * simple GUID (Globally Unique ID) implementation.
25  * A GUID is composed of two parts:
26  * 1. The IP-Address of the local machine.
27  * 2. A java.rmi.server.UID
28  *
29  * @author Thomas Mahler
30  * @version $Id: GUID.java,v 1.7.2.1 2005/12/21 22:27:47 tomdz Exp $
31  */

32 public class GUID implements Serializable JavaDoc
33 {
34     static final long serialVersionUID = -6163239155380515945L; /**
35      * holds the hostname of the local machine.
36      */

37     private static String JavaDoc localIPAddress;
38
39     /**
40      * String representation of the GUID
41      */

42     private String JavaDoc guid;
43
44     /**
45      * compute the local IP-Address
46      */

47     static
48     {
49         try
50         {
51             localIPAddress = InetAddress.getLocalHost().getHostAddress();
52         }
53         catch (UnknownHostException JavaDoc e)
54         {
55             localIPAddress = "localhost";
56         }
57     }
58
59     /**
60      * public no args constructor.
61      */

62     public GUID()
63     {
64         UID JavaDoc uid = new UID JavaDoc();
65         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
66         buf.append(localIPAddress);
67         buf.append(":");
68         buf.append(uid.toString());
69         guid = buf.toString();
70     }
71  
72     /**
73      * public constructor.
74      * The caller is responsible to feed a globally unique
75      * String into the theGuidString parameter
76      * @param theGuidString a globally unique String
77      */

78     public GUID(String JavaDoc theGuidString)
79     {
80         guid = theGuidString;
81     }
82
83     /**
84      * returns the String representation of the GUID
85      */

86     public String JavaDoc toString()
87     {
88         return guid;
89     }
90     /**
91      * @see java.lang.Object#equals(Object)
92      */

93     public boolean equals(Object JavaDoc obj)
94     {
95         if (obj instanceof GUID)
96         {
97             if (guid.equals(((GUID) obj).guid))
98             {
99                 return true;
100             }
101         }
102         return false;
103     }
104
105     /**
106      * @see java.lang.Object#hashCode()
107      */

108     public int hashCode()
109     {
110         return guid.hashCode();
111     }
112
113 }
114
Popular Tags