Commit 3c350780 by Harish Butani

change id to String to allow for Guids

parent 34e7f58a
......@@ -31,22 +31,31 @@ import java.util.Date;
public class Id implements ITypedReferenceableInstance {
public final long id;
public final String id;
public final String className;
public final int version;
public Id(long id, int version, String className) {
public Id(String id, int version, String className) {
this.id = id;
this.className = className;
this.version = version;
}
public Id(long id, int version, String className) {
this("" + id, version, className);
}
public Id(String className) {
this(-System.nanoTime(), 0, className);
this("" + (-System.nanoTime()), 0, className);
}
public boolean isUnassigned() {
return id < 0;
try {
long l = Long.parseLong(id);
return l < 0;
} catch(NumberFormatException ne) {
return false;
}
}
public String toString() {
......@@ -56,20 +65,20 @@ public class Id implements ITypedReferenceableInstance {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || !ITypedReferenceableInstance.class.isAssignableFrom(o.getClass())) return false;
if (o == null || getClass() != o.getClass()) return false;
Id id1 = ((ITypedReferenceableInstance)o).getId();
Id id1 = (Id) o;
if (id != id1.id) return false;
if (version != id1.version) return false;
if (!className.equals(id1.className)) return false;
if (!id.equals(id1.id)) return false;
return true;
}
@Override
public int hashCode() {
int result = (int) (id ^ (id >>> 32));
int result = id.hashCode();
result = 31 * result + className.hashCode();
result = 31 * result + version;
return result;
......
......@@ -63,7 +63,7 @@ public class MemRepository implements IRepository {
@Override
public Id newId(String typeName) {
return new Id(ID_SEQ.incrementAndGet(), 0, typeName);
return new Id("" + ID_SEQ.incrementAndGet(), 0, typeName);
}
/**
......
......@@ -52,12 +52,12 @@ class IdSerializer extends CustomSerializer[Id](format => ( {
JField("version", JInt(version)) :: Nil) => new Id(id.toLong, version.toInt, className)
case JObject(JField("id", JString(id)) ::
JField(Serialization.STRUCT_TYPE_FIELD_NAME, JString(className)) ::
JField("version", JString(version)) :: Nil) => new Id(id.toLong, version.toInt, className)
JField("version", JString(version)) :: Nil) => new Id(id, version.toInt, className)
case JObject(JField(Serialization.STRUCT_TYPE_FIELD_NAME, JString(className)) ::
JField("id", JString(id)) ::
JField("version", JString(version)) :: Nil) => new Id(id.toLong, version.toInt, className)
JField("version", JString(version)) :: Nil) => new Id(id, version.toInt, className)
}, {
case id: Id => JObject(JField("id", JInt(id.id)),
case id: Id => JObject(JField("id", JString(id.id)),
JField(Serialization.STRUCT_TYPE_FIELD_NAME, JString(id.className)),
JField("version", JInt(id.version)))
}
......@@ -106,6 +106,9 @@ class TypedReferenceableInstanceSerializer(val typSystem : Option[MetadataServic
case JObject(JField("id", JInt(id)) ::
JField(Serialization.STRUCT_TYPE_FIELD_NAME, JString(className)) ::
JField("version", JInt(version)) :: Nil) => new Id(id.toLong, version.toInt, className)
case JObject(JField("id", JString(id)) ::
JField(Serialization.STRUCT_TYPE_FIELD_NAME, JString(className)) ::
JField("version", JInt(version)) :: Nil) => new Id(id, version.toInt, className)
case JObject(fs) =>
var typField : Option[JField] = None
var idField : Option[JField] = None
......@@ -205,7 +208,7 @@ object Serialization {
Extraction.extract[ITypedReferenceableInstance](value)
}
def serializeId(id : Id) = JObject(JField("id", JInt(id.id)),
def serializeId(id : Id) = JObject(JField("id", JString(id.id)),
JField(Serialization.STRUCT_TYPE_FIELD_NAME, JString(id.className)),
JField("version", JInt(id.version)))
......@@ -256,6 +259,9 @@ object Serialization {
case JObject(JField("id", JInt(id)) ::
JField(Serialization.STRUCT_TYPE_FIELD_NAME, JString(className)) ::
JField("version", JInt(version)) :: Nil) => new Id(id.toLong, version.toInt, className)
case JObject(JField("id", JString(id)) ::
JField(Serialization.STRUCT_TYPE_FIELD_NAME, JString(className)) ::
JField("version", JInt(version)) :: Nil) => new Id(id, version.toInt, className)
}
def toJson(value : ITypedReferenceableInstance) : String = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment