【发布时间】:2015-02-03 12:09:00
【问题描述】:
我在我的项目中找到了用于生成 PK 的代码,但我根本无法理解该代码的作用。
谁能给我一些指导或想法或解释?
Integer typecode = ((AbstractEntity)object).getTypeCode();//this is unique number for every type
Long counter = Long.valueOf(this.fetchNextCounter(typecode.intValue())); //this returns a counter for each type
Integer clusterid = Integer.valueOf(0);
Integer millicnt = Integer.valueOf(0);
Long creationtime = Long.valueOf((new DateTime()).getMillis());
//here is where the magic starts and I cant simply understand a thing
if(typecode.intValue() >= 0 && typecode.intValue() <= 32767) {
long longValue = counter.longValue() << 15 | (long)typecode.intValue() << 48 & -281474976710656L;
longValue += ((long)clusterid.intValue() & 15L) << 44 & 263882790666240L;
longValue += creationtime.longValue() - 788914800000L << 4 & 17592186044400L;
longValue += (long)(clusterid.intValue() >> 2) & 12L;
longValue += (long)millicnt.intValue() & 3L;
longValue &= -8796093022209L;
return Long.valueOf(longValue);
} else {
throw new RuntimeException("illegal typecode : " + typecode + ", allowed range: 0-" + 32767);
}
我会感谢所有可以提供帮助的人?我们遇到的问题是我们的类型代码大于 32767,并且算法显示这不起作用,但为什么以及如何更改它?
【问题讨论】:
标签: java algorithm bit-manipulation uniqueidentifier unique-key