Last active
October 1, 2016 23:39
-
-
Save kentyeh/ca37337a1eeac6917a480b73b7687353 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigInteger; | |
import java.util.UUID; | |
/** Code from | |
* https://github.com/cchigoriwa/jaxcsd/blob/master/jaxcsd-api/src/main/java/zw/co/hitrac/jaxcsd/api/util/UUIDBasedOID.java | |
*/ | |
public class UUIDBasedOID { | |
protected static final String OID_PREFIX = "2.25"; // {joint-iso-itu-t uuid(25) <uuid-single-integer-value>} | |
protected UUID uuid; | |
protected String oid; | |
/** | |
* random generate a 2.25 oid | |
*/ | |
public UUIDBasedOID() { | |
uuid = UUID.randomUUID(); | |
oid = createOIDFromUUIDCanonicalHexString(uuid.toString()); | |
} | |
public UUIDBasedOID(String uuid) { | |
oid = createOIDFromUUIDCanonicalHexString(uuid); | |
} | |
/** | |
* generate a 2.25 oid from a given uuid | |
* @param uuid | |
*/ | |
public String getOID() { | |
return oid; | |
} | |
public UUID getUUID() { | |
return uuid; | |
} | |
public static BigInteger makeBigIntegerFromUnsignedLong(long unsignedLongValue) { | |
BigInteger bigValue; | |
if (unsignedLongValue < 0) { | |
unsignedLongValue = unsignedLongValue & Long.MAX_VALUE; | |
bigValue = BigInteger.valueOf(unsignedLongValue); | |
bigValue = bigValue.setBit(63); | |
} else { | |
bigValue = BigInteger.valueOf(unsignedLongValue); | |
} | |
//System.err.println("makeBigIntegerFromUnsignedLong(): bigValue = "+com.pixelmed.utils.HexDump.dump(bigValue.toByteArray())); | |
return bigValue; | |
} | |
public static String createOIDFromUUIDCanonicalHexString(String hexString) throws IllegalArgumentException { | |
UUID uuid = UUID.fromString(hexString); | |
long leastSignificantBits = uuid.getLeastSignificantBits(); | |
long mostSignificantBits = uuid.getMostSignificantBits(); | |
BigInteger decimalValue = makeBigIntegerFromUnsignedLong(mostSignificantBits); | |
decimalValue = decimalValue.shiftLeft(64); | |
BigInteger bigValueOfLeastSignificantBits = makeBigIntegerFromUnsignedLong(leastSignificantBits); | |
decimalValue = decimalValue.or(bigValueOfLeastSignificantBits); // not add() ... do not want to introduce question of signedness of long | |
return OID_PREFIX + "." + decimalValue.toString(); | |
} | |
public static final void main(String arg[]) { | |
//隨機建立 2.25 OID | |
System.out.println(new UUIDBasedOID().getOID()); | |
//根據已有之guid建立2.25 OID | |
System.out.println(new UUIDBasedOID("4dfce014-02d4-4b54-ad2d-d70c847a040f").getOID()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment