001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.pluto.om.common;
018:
019: import java.io.IOException;
020: import java.io.ObjectInputStream;
021: import java.io.ObjectOutputStream;
022:
023: /**
024: * Wraps around the internal Object IDs. By holding both
025: * the string and the integer version of an Object ID this class
026: * helps speed up the internal processing.
027: *
028: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
029: *
030: * @version CVS $Id: ObjectIDImpl.java 433543 2006-08-22 06:22:54Z crossley $
031: */
032: public class ObjectIDImpl implements
033: org.apache.pluto.om.common.ObjectID, java.io.Serializable {
034:
035: private String stringOID;
036: private int intOID;
037:
038: private ObjectIDImpl(int oid, String stringOID) {
039: this .stringOID = stringOID;
040: intOID = oid;
041: }
042:
043: // internal methods.
044:
045: private void readObject(ObjectInputStream stream)
046: throws IOException {
047: intOID = stream.readInt();
048:
049: stringOID = String.valueOf(intOID);
050: }
051:
052: private void writeObject(ObjectOutputStream stream)
053: throws IOException {
054: stream.write(intOID);
055: }
056:
057: // addtional methods.
058:
059: public boolean equals(Object object) {
060: boolean result = false;
061:
062: if (object instanceof ObjectIDImpl) {
063: result = (intOID == ((ObjectIDImpl) object).intOID);
064: } else if (object instanceof String) {
065: result = stringOID.equals(object);
066: } else if (object instanceof Integer) {
067: result = (intOID == ((Integer) object).intValue());
068: }
069: return (result);
070: }
071:
072: /* (non-Javadoc)
073: * @see java.lang.Object#hashCode()
074: */
075: public int hashCode() {
076: return intOID;
077: }
078:
079: /* (non-Javadoc)
080: * @see java.lang.Object#toString()
081: */
082: public String toString() {
083: return stringOID;
084: }
085:
086: public int intValue() {
087: return (intOID);
088: }
089:
090: static public ObjectIDImpl createFromString(String idStr) {
091: char[] id = idStr.toCharArray();
092: int _id = 1;
093: for (int i = 0; i < id.length; i++) {
094: if ((i % 2) == 0)
095: _id *= id[i];
096: else
097: _id ^= id[i];
098: _id = Math.abs(_id);
099: }
100: return new ObjectIDImpl(_id, idStr);
101: }
102: }
|