summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/lookup/ItemPath.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/c2kernel/lookup/ItemPath.java')
-rw-r--r--src/main/java/com/c2kernel/lookup/ItemPath.java143
1 files changed, 43 insertions, 100 deletions
diff --git a/src/main/java/com/c2kernel/lookup/ItemPath.java b/src/main/java/com/c2kernel/lookup/ItemPath.java
index 17e5659..2219c8a 100644
--- a/src/main/java/com/c2kernel/lookup/ItemPath.java
+++ b/src/main/java/com/c2kernel/lookup/ItemPath.java
@@ -10,9 +10,11 @@
package com.c2kernel.lookup;
-import java.util.ArrayList;
+import java.nio.ByteBuffer;
+import java.util.UUID;
import com.c2kernel.common.ObjectNotFoundException;
+import com.c2kernel.common.SystemKey;
/**
@@ -23,38 +25,29 @@ import com.c2kernel.common.ObjectNotFoundException;
**/
public class ItemPath extends Path
{
- // no of components in a syskey
- public static final int elementNo = 3;
- // no of digits in a syskey component
- public static final int elementLen = 3;
- // maximum possible int syskey
- public static final int maxSysKey = (int)Math.pow(10, elementNo*elementLen)-1;
- protected static String mTypeRoot;
- /*
- * From syskey int
- * Note no EntityPath constructors allow setting of CONTEXT or ENTITY:
- * The object decides that for itself from the number of components
- */
-
- public ItemPath(int syskey) throws InvalidItemPathException {
- super();
- setSysKey(syskey);
+
+ public ItemPath() {
+ this(UUID.randomUUID());
}
- /*
- */
- public ItemPath()
- {
- super();
+ public ItemPath(UUID uuid) {
+ setSysKey(uuid);
+ mPath = new String[1];
+ mPath[0] = mUUID.toString();
}
+ public ItemPath(SystemKey syskey) {
+ setSysKey(syskey);
+ mPath = new String[1];
+ mPath[0] = mUUID.toString();
+ }
/*
*/
public ItemPath(String[] path) throws InvalidItemPathException
{
- super(path, Path.CONTEXT); // dummy - it will get replaced in checkSysPath()
- checkSysPath();
+ super(path, Path.CONTEXT);
+ getSysKeyFromPath();
}
/*
@@ -62,16 +55,19 @@ public class ItemPath extends Path
public ItemPath(String path) throws InvalidItemPathException
{
super(path, Path.CONTEXT);
- checkSysPath();
+ getSysKeyFromPath();
}
- /*
- */
- public ItemPath(ItemPath parent, String child) throws InvalidItemPathException {
- super(parent, child);
- checkSysPath();
+ private void getSysKeyFromPath() throws InvalidItemPathException {
+ if (mPath.length > 0) {
+ try {
+ setSysKey(UUID.fromString(mPath[0]));
+ } catch (IllegalArgumentException ex) {
+ throw new InvalidItemPathException(mPath[0]+" is not a valid UUID");
+ }
+ }
}
-
+
// EntityPaths root in /entity
@Override
public String getRoot() {
@@ -79,81 +75,28 @@ public class ItemPath extends Path
}
@Override
- public ItemPath getEntity() throws ObjectNotFoundException {
+ public ItemPath getItemPath() throws ObjectNotFoundException {
return this;
}
public byte[] getOID() {
- if (mSysKey == Path.INVALID) return null;
- return String.valueOf(mSysKey).getBytes();
+ if (mType == Path.CONTEXT) return null;
+ ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
+ bb.putLong(mUUID.getMostSignificantBits());
+ bb.putLong(mUUID.getLeastSignificantBits());
+ return bb.array();
}
-
- /*************************************************************************/
-
- /** Returns int form of syskey (if possible)
- */
- @Override
- public int getSysKey() {
- if (mSysKey == Path.INVALID && mType == Path.ENTITY)
- try {
- if (mPath.length != elementNo)
- throw new InvalidItemPathException("Incorrect number of components for a system key");
- mSysKey = 0;
- for (String keypart : mPath) {
- if (keypart.length()!=elementLen+1)
- throw new InvalidItemPathException("Component '"+keypart+"' is not the correct size for a system key");
- for(int j=1; j<elementLen+1; j++) // skip the 'd' prefix
- {
- char c = keypart.charAt(j);
- if((c >= '0') && (c <='9'))
- mSysKey = mSysKey*10 + c - '0';
- else
- throw new InvalidItemPathException("Component '"+keypart+"' contained a non-numeric char (excluding first char 'd').");
-
- }
- }
- } catch (InvalidItemPathException ex) {
- mSysKey = Path.INVALID;
- }
- return mSysKey;
- }
-
- /** Sets path from int syskey
- */
- public void setSysKey(int sysKey) throws InvalidItemPathException
- {
- if (sysKey < 0 || sysKey > maxSysKey)
- throw new InvalidItemPathException("System key "+sysKey+" out of range");
- String stringPath = Integer.toString(sysKey);
- ArrayList<String> newKey = new ArrayList<String>();
- for (int i=0; i<elementNo; i++) {
- StringBuffer nextComponent = new StringBuffer("d");
- int start = stringPath.length()-elementLen;
- if (start < 0) {
- nextComponent.append("000000".substring(0,Math.abs(start))).append(stringPath);
- stringPath = "";
- }
- else {
- nextComponent.append(stringPath.substring(start));
- stringPath = stringPath.substring(0, start);
- }
- newKey.add(0, nextComponent.toString());
- }
-
- mPath = (newKey.toArray(mPath));
- mSysKey = sysKey;
- mStringPath = null;
- mType = Path.ENTITY;
- checkSysPath();
+
+ protected void setSysKey(UUID uuid) {
+ mUUID = uuid;
+ mSysKey = new SystemKey(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits());
+ mType = Path.ENTITY;
}
-
- public void checkSysPath() throws InvalidItemPathException {
- if (mPath.length > elementNo)
- throw new InvalidItemPathException("EntityPath cannot have more than "+elementNo+" components: "+toString());
- if (mPath.length == elementNo)
- mType = Path.ENTITY;
- else
- mType = Path.CONTEXT;
+
+ protected void setSysKey(SystemKey sysKey) {
+ mSysKey = sysKey;
+ mUUID = new UUID(sysKey.msb, sysKey.lsb);
+ mType = Path.ENTITY;
}
}