/** * A sequence to generate safe keys. * * Author: Joachim Zobel * Copyright Outcome Unternehmensberatung GmbH 2006 * */ package Basic; import java.math.BigInteger; import java.util.Random; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * Create safe ids (that can not be guessed). * The ids are big integers. This has the advantage that * they have an ordering without an upper bound. * They are ordered in the sense that each call to getNext * gives a larger number than any call before. */ public class Sequence { /** * The number of bytes of random that are used. */ private static final int BYTES_SECURITY = 5; private BigInteger sum; private final Lock lock; private final Random rand; /** * byte[] to BigInteger conversion * @param dig - bytearray * @return the BigInt */ private static BigInteger bigInt(byte [] dig) { BigInteger rtn = BigInteger.ZERO; for (int i=0; iSequence */ public Sequence() { rand = new Random(); sum = BigInteger.ZERO; lock = new ReentrantLock(); } /** * Bean Accesssor * @return the next sequence value */ public BigInteger getNext() { byte []current = new byte[BYTES_SECURITY]; lock.lock(); rand.nextBytes(current); sum = sum.add(bigInt(current)); lock.unlock(); return sum; } public void setNext(BigInteger s) {/* Dummy */}; }