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