X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fservice%2FClusterUID.java;h=96cf83f2e4a4ed90d7138cae5f92f8c889678c9b;hb=c26409b1caf2f1e560d37c5befd11b442399c3fe;hp=adfaed44d4299e8c90cd0c3a73032500d7ce42d0;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db/src/org/simantics/db/service/ClusterUID.java b/bundles/org.simantics.db/src/org/simantics/db/service/ClusterUID.java index adfaed44d..96cf83f2e 100644 --- a/bundles/org.simantics.db/src/org/simantics/db/service/ClusterUID.java +++ b/bundles/org.simantics.db/src/org/simantics/db/service/ClusterUID.java @@ -1,101 +1,101 @@ -package org.simantics.db.service; - -import java.nio.ByteBuffer; -import java.nio.ByteOrder; - -public final class ClusterUID { - public static final ClusterUID Null = new ClusterUID(0, 0); - public static final ClusterUID Reserved = new ClusterUID(0, 1); - public static final ClusterUID Builtin = new ClusterUID(0, 2); - public static boolean isLegal(ClusterUID clusterUID) { - return !clusterUID.equals(Null) && !clusterUID.equals(Reserved); - } -// public final long first; - public final long second; -// public transient int arrayOffset = -1; - -// transient private static long firstCache1 = 0; -// transient private static long firstCache2 = 0; - transient private static long secondCache1 = 0; - transient private static long secondCache2 = 0; - transient private static ClusterUID clusterCache1 = null; - transient private static ClusterUID clusterCache2 = null; - - public ClusterUID() { -// first = 0; - second = 0; - } - - public ClusterUID(long first, long second) { - assert(first == 0); -// this.first = first; - this.second = second; - } - - public synchronized static ClusterUID make(long first, long second) { - - assert(first == 0); - - if(second == secondCache1) return clusterCache1; - else if(second == secondCache2) return clusterCache2; - else { - ClusterUID result = new ClusterUID(first, second); - secondCache2 = secondCache1; clusterCache2 = clusterCache1; - secondCache1 = second; clusterCache1 = result; - return result; - } - - } - - public static ClusterUID make(byte[] bytes, int offset) { - if (bytes.length < offset + 16) - throw new IllegalArgumentException("Too few bytes for ClusteUID. length=" + bytes.length + " offset=" + offset); - ByteBuffer bf = ByteBuffer.wrap(bytes, offset, 16); - bf.order(ByteOrder.LITTLE_ENDIAN); - return make(bf.getLong(),bf.getLong()); - } - - @Override - public String toString() { - return String.format("%x.%x", 0, second); - } - public static int getLongLength() { - return 2; - } - public int toByte(byte[] bytes, int offset) { - Bytes.writeLE(bytes, offset+0, 0); - Bytes.writeLE(bytes, offset+8, second); - return offset + 16; - } - public int toLong(long[] longs, int offset) { - longs[offset++] = 0; - longs[offset++] = second; - return offset; - } - public byte[] asBytes() { - byte[] bytes = new byte[16]; - toByte(bytes, 0); - return bytes; - } - public ResourceUID toRID(int resourceIndex) { - return new ResourceUID(0, second, resourceIndex); - } - @Override - public boolean equals(Object o) { - if (this == o) - return true; - else if (!(o instanceof ClusterUID)) - return false; - ClusterUID x = (ClusterUID)o; - return /*first == x.first &&*/ second == x.second; - } - @Override - public int hashCode() { - int result = 17; -// int f = (int)(0 ^ (0 >>> 32)); -// result = 31 * result + f; - int s = (int)(second ^ (second >>> 32)); - result = 31 * result + s; - return result; - } -} +package org.simantics.db.service; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +public final class ClusterUID { + public static final ClusterUID Null = new ClusterUID(0, 0); + public static final ClusterUID Reserved = new ClusterUID(0, 1); + public static final ClusterUID Builtin = new ClusterUID(0, 2); + public static boolean isLegal(ClusterUID clusterUID) { + return !clusterUID.equals(Null) && !clusterUID.equals(Reserved); + } +// public final long first; + public final long second; +// public transient int arrayOffset = -1; + +// transient private static long firstCache1 = 0; +// transient private static long firstCache2 = 0; + transient private static long secondCache1 = 0; + transient private static long secondCache2 = 0; + transient private static ClusterUID clusterCache1 = null; + transient private static ClusterUID clusterCache2 = null; + + public ClusterUID() { +// first = 0; + second = 0; + } + + public ClusterUID(long first, long second) { + assert(first == 0); +// this.first = first; + this.second = second; + } + + public synchronized static ClusterUID make(long first, long second) { + + assert(first == 0); + + if(second == secondCache1) return clusterCache1; + else if(second == secondCache2) return clusterCache2; + else { + ClusterUID result = new ClusterUID(first, second); + secondCache2 = secondCache1; clusterCache2 = clusterCache1; + secondCache1 = second; clusterCache1 = result; + return result; + } + + } + + public static ClusterUID make(byte[] bytes, int offset) { + if (bytes.length < offset + 16) + throw new IllegalArgumentException("Too few bytes for ClusteUID. length=" + bytes.length + " offset=" + offset); + ByteBuffer bf = ByteBuffer.wrap(bytes, offset, 16); + bf.order(ByteOrder.LITTLE_ENDIAN); + return make(bf.getLong(),bf.getLong()); + } + + @Override + public String toString() { + return String.format("%x.%x", 0, second); + } + public static int getLongLength() { + return 2; + } + public int toByte(byte[] bytes, int offset) { + Bytes.writeLE(bytes, offset+0, 0); + Bytes.writeLE(bytes, offset+8, second); + return offset + 16; + } + public int toLong(long[] longs, int offset) { + longs[offset++] = 0; + longs[offset++] = second; + return offset; + } + public byte[] asBytes() { + byte[] bytes = new byte[16]; + toByte(bytes, 0); + return bytes; + } + public ResourceUID toRID(int resourceIndex) { + return new ResourceUID(0, second, resourceIndex); + } + @Override + public boolean equals(Object o) { + if (this == o) + return true; + else if (!(o instanceof ClusterUID)) + return false; + ClusterUID x = (ClusterUID)o; + return /*first == x.first &&*/ second == x.second; + } + @Override + public int hashCode() { + int result = 17; +// int f = (int)(0 ^ (0 >>> 32)); +// result = 31 * result + f; + int s = (int)(second ^ (second >>> 32)); + result = 31 * result + s; + return result; + } +}