blob: 422cb98f73551135f3297fd1ba0b5f0b5684a25b [file] [log] [blame]
/*
* Copyright 2003,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
*/
package org.apache.pluto.driver.util;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* * Wraps around the internal Object IDs. By holding both * the string and the
* integer version of an Object ID this class * helps speed up the internal
* processing.
*/
public class ObjectIdImpl implements org.apache.pluto.om.ObjectID,
java.io.Serializable {
private String stringOID = null;
private int intOID;
private ObjectIdImpl(int oid) {
stringOID = String.valueOf(oid);
intOID = oid;
}
private ObjectIdImpl(int oid, String stringOID) {
this.stringOID = stringOID;
intOID = oid;
}
// internal methods.
private void readObject(ObjectInputStream stream) throws IOException,
ClassNotFoundException {
intOID = stream.readInt();
stringOID = String.valueOf(intOID);
}
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.write(intOID);
}
// addtional methods.
public boolean equals(Object object) {
boolean result = false;
if (object instanceof ObjectIdImpl) {
result = (intOID == ((ObjectIdImpl) object).intOID);
} else if (object instanceof String) {
result = stringOID.equals(object);
} else if (object instanceof Integer) {
result = (intOID == ((Integer) object).intValue());
}
return (result);
}
public int hashCode() {
return (intOID);
}
public String toString() {
return (stringOID);
}
public int intValue() {
return (intOID);
}
static public ObjectIdImpl createFromString(String idStr) {
char[] id = idStr.toCharArray();
int _id = 1;
for (int i = 0; i < id.length; i++) {
if ((i % 2) == 0) {
_id *= id[i];
} else {
_id ^= id[i];
}
_id = Math.abs(_id);
}
return new ObjectIdImpl(_id, idStr);
}
}