KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > pluto > om > common > ObjectIDImpl


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

16 package org.apache.cocoon.portal.pluto.om.common;
17
18 import java.io.IOException JavaDoc;
19 import java.io.ObjectInputStream JavaDoc;
20 import java.io.ObjectOutputStream JavaDoc;
21
22 /**
23  * Wraps around the internal Object IDs. By holding both
24  * the string and the integer version of an Object ID this class
25  * helps speed up the internal processing.
26  *
27  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
28  *
29  * @version CVS $Id: ObjectIDImpl.java 123407 2004-12-27 13:51:59Z cziegeler $
30  */

31 public class ObjectIDImpl
32 implements org.apache.pluto.om.common.ObjectID, java.io.Serializable JavaDoc {
33
34     private String JavaDoc stringOID;
35     private int intOID;
36
37     private ObjectIDImpl (int oid, String JavaDoc stringOID) {
38         this.stringOID = stringOID;
39         intOID = oid;
40     }
41
42     // internal methods.
43

44     private void readObject (ObjectInputStream JavaDoc stream) throws IOException JavaDoc {
45         intOID = stream.readInt ();
46
47         stringOID = String.valueOf (intOID);
48     }
49
50     private void writeObject (ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
51         stream.write (intOID);
52     }
53
54     
55     // addtional methods.
56

57     public boolean equals (Object JavaDoc object) {
58         boolean result = false;
59
60         if (object instanceof ObjectIDImpl) {
61             result = (intOID == ((ObjectIDImpl) object).intOID);
62         } else if (object instanceof String JavaDoc) {
63             result = stringOID.equals (object);
64         } else if (object instanceof Integer JavaDoc) {
65             result = (intOID == ((Integer JavaDoc)object).intValue());
66         }
67         return (result);
68     }
69
70     /* (non-Javadoc)
71      * @see java.lang.Object#hashCode()
72      */

73     public int hashCode () {
74         return intOID;
75     }
76
77     /* (non-Javadoc)
78      * @see java.lang.Object#toString()
79      */

80     public String JavaDoc toString () {
81         return stringOID;
82     }
83
84     public int intValue () {
85         return (intOID);
86     }
87
88     static public ObjectIDImpl createFromString(String JavaDoc idStr) {
89         char[] id = idStr.toCharArray();
90         int _id = 1;
91         for (int i=0; i<id.length; i++) {
92             if ((i%2)==0) _id *= id[i];
93             else _id ^= id[i];
94             _id = Math.abs(_id);
95         }
96         return new ObjectIDImpl(_id, idStr);
97     }
98 }
99
Popular Tags