java id生成器_id生成器

在android的sqlite中对于INTEGERPRIMARYKEY, 如果插入数据A,B,C,它们的_id为1,2,3,那么如果把他们都删除了,再插入一条数据,那么它的id为1而不是4。 因此我就写了这个工具来生成id,它能让生成相同的id相隔尽量的远。

SerialManager.java文件

packagecom.teleca;

publicclass

SerialManager

{

Noderoot=null;

longmin=Long.MIN_VALUE;

longmax=Long.MAX_VALUE;

longcursor=1;

public

SerialManager

() { } publicSerialManager(longmin,longmax) { if(min>max) { longtemp=min; min=max; max=temp; } this.min=min; this.max=max; }

publicvoid

setSerial(longid,booleanflag) { longtemp=0; Nodenode=findNode(id);

if

(flag) { if(node==null) { node=findNode(id-1);

if

(node!=null) { node.data.end=id; Nodenode2=findNode(id+1);

if

(node2!=null) { node.data.end=node2.data.end; this.removeNode(node2); } } else { Nodenode2=findNode(id+1); if(node2!=null) { node2.data.start=id; } else addNewNode(id,id); } } return; } else { if(node==null) return; Blockdata=node.data; if(data.start==data.end) { removeNode(node); } elseif(id==data.start) data.start=id+1; elseif(id==data.end) data.end=id-1; else { temp=data.end; data.end=id-1; addNewNode(id+1,temp); } } } publicvoidsetSerial(longid1,longid2) { longtemp=0; if(id1>id2) { temp=id1; id1=id2; id2=temp; } Nodenode1=findNode(id1); Nodenode2=null; if(node1==null) { node1=findNode(id1-1); if(node1!=null) { node1.data.end=id2; node2=findNode(id2+1); if(node2!=null) { node1.data.end=node2.data.end; this.removeNode(node2); } return; } else { node2=findNode(id2); if(node2==null) { node2=findNode(id2+1); if(node2!=null) node2.data.start=id1; else addNewNode(id1,id2); return; } else { node2.data.start=id1; return; } } } else { Blockdata=node1.data; if(id2<=data.end) { return; } else { node2=findNode(id2); if(node2==null) { data.end=id2; return; } else { data.end=node2.data.end; removeNode(node2); } } } } publiclonggetSerial() { Nodenode=findNode(cursor); longstart=cursor; while(node!=null) { cursor=node.data.end+1; if(cursor>max) cursor=min; elseif(cursor==0) cursor++; if(cursor==start) { return0; } node=findNode(cursor); } longres=cursor; cursor++; if(cursor>max) cursor=min; elseif(cursor==0) cursor++; returnres; } publicbooleanisKeyUsed(longid) { returnfindNode(id)!=null; } privateNodefindNode(longid) { Nodenode=null; NodetempNode=root; Blockblock=null; while(tempNode!=null) { block=tempNode.data; if(block.start<=id&&id<=block.end) { node=tempNode; break; } tempNode=tempNode.next; } returnnode; } privatevoidaddNewNode(longid1,longid2) { Nodenode=newNode(); node.data=newBlock(id1,id2); addNode(node); } privatevoidaddNode(Nodenode) { if(root==null) { root=node; node.prev=null; node.next=null; return; } NodetempNode=root; while(tempNode!=null) { if(tempNode.data.start>node.data.end) { if(tempNode==root) { node.prev=null; node.next=root; tempNode.prev=node; root=node; } else { node.prev=tempNode.prev; node.next=tempNode; tempNode.prev.next=node; tempNode.prev=node; } break; } elseif(tempNode.next==null) { tempNode.next=node; node.prev=tempNode; node.next=null; break; } tempNode=tempNode.next; } } privatevoidremoveNode(Nodenode) { Nodeprev=node.prev; if(prev==null) { root=node.next; } else { prev.next=node.next; } if(node.next!=null) node.next.prev=prev; node.prev=null; node.next=null; } publicvoidclear() { NodetempNode=root; Nodenode=null; while(tempNode!=null) { node=tempNode; tempNode=tempNode.next; node.prev=null; node.next=null; } root=null; cursor=1; } publicvoidprintln() { NodetempNode=root; while(tempNode!=null) { System.out.println("start:"+tempNode.data.start+"end:"+tempNode.data.end); tempNode=tempNode.next; } } } classNode { Nodeprev=null; Blockdata; Nodenext=null; } classBlock { longstart=0; longend=0; Block(longid1,longid2) { start=id1; end=id2; } publiclonggetStart(){ returnstart; } publicvoidsetStart(longstart){ this.start=start; } publiclonggetEnd(){ returnend; } publicvoidsetEnd(longend){ this.end=end; } } 测试程序如下: importjava.util.Random; importcom.teleca.SerialManager; publicclasstestimplementsRunnable{ staticRandomrandom=newRandom(System.currentTimeMillis()); staticlongbuffer[]=newlong[10]; staticintindex=0; staticlongcounter[]=newlong[100]; staticintcountIndex=0; staticSerialManagerserialManager=newSerialManager(Short.MIN_VALUE,Short.MAX_VALUE); publicvoidrun() { inti=0; for(i=0;i<100;i++) serialManager.setSerial(random.nextLong()%Short.MAX_VALUE,true); longtemp=0; booleanblErro=false; booleanblRun=true; while(blRun) { temp=serialManager.getSerial(); for(intj=0;j=Short.MAX_VALUE) { countIndex++; System.out.println("countIndex:"+countIndex); if(countIndex>=counter.length) { blRun=false; countIndex=counter.length-1; } } if(counter[countIndex]%1000==0) { System.out.println(temp+"countIndex:"+countIndex+"num:"+counter[countIndex]); try{ Thread.sleep(1); }catch(Exceptione) { e.printStackTrace(); } } } if(blErro) { System.out.println("erro:---------"+temp); serialManager.println(); } else System.out.println("Itpassedthetest!"); } /** *@paramargs */ publicstaticvoidmain(String[]args){ //TODOAuto-generatedmethodstub Threadt=newThread(newtest()); t.start(); } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值