View Javadoc

1   /*
2    * Copyright 2003,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  /* 
17  
18   */
19  
20  package org.apache.pluto.portalImpl.util;
21  
22  import java.io.IOException;
23  import java.io.ObjectInputStream;
24  import java.io.ObjectOutputStream;
25  
26  /***
27   ** Wraps around the internal Object IDs. By holding both
28   ** the string and the integer version of an Object ID this class
29   ** helps speed up the internal processing.
30   **/
31  
32  public class ObjectID implements org.apache.pluto.om.common.ObjectID, java.io.Serializable
33  {
34  
35      private String  stringOID;
36      private int     intOID;
37  
38      private ObjectID (int oid, String stringOID)
39      {
40          this.stringOID = stringOID;
41          intOID    = oid;
42      }   
43  
44      // internal methods.
45      
46      private void readObject (ObjectInputStream stream) throws IOException, ClassNotFoundException
47      {
48          intOID = stream.readInt ();
49  
50          stringOID = String.valueOf (intOID);
51      }
52  
53      private void writeObject (ObjectOutputStream stream) throws IOException
54      {
55          stream.write (intOID);
56      }
57  
58      
59      // addtional methods.
60      
61      public boolean equals (Object object)
62      {
63          boolean result = false;
64  
65          if (object instanceof ObjectID)            
66              result = (intOID == ((ObjectID) object).intOID);  
67          else if (object instanceof String)
68              result = stringOID.equals (object);
69          else if (object instanceof Integer)
70              result = (intOID == ((Integer)object).intValue());        
71          return (result);
72      }
73  
74      public int hashCode ()
75      {
76          return (intOID);
77      }
78  
79      public String toString ()
80      {
81          return (stringOID);
82      }
83  
84      public int intValue ()
85      {
86          return (intOID);
87      }
88  
89      static public ObjectID createFromString(String idStr)
90      {
91          char[] id = idStr.toCharArray();
92          int _id  = 1;
93          for (int i=0; i<id.length; i++)
94          {
95              if ((i%2)==0)   _id *= id[i];
96              else            _id ^= id[i];
97              _id = Math.abs(_id);
98          }
99          return new ObjectID(_id, idStr);
100     }
101 }