Simple Id Generator : UUID GUID « Development Class « Java






Simple Id Generator

     
// Copyright (c) 2003-2009, Jodd Team (jodd.org). All Rights Reserved.


/**
 * Simple synchronized int ids sequence generator. It takes the positive sequence range (boundaries are included).
 * Optionally, it supports cycling, when counter reaches the max value. Otherwise an exception is thrown.
 * @see SimpleLongIdGenerator
 */
public class SimpleIdGenerator {

  protected volatile int value;

  protected int initialValue;
  protected int maxValue;
  protected boolean cycle;

  /**
   * Creates a new default cycled id generator. Starts from 1 and counts up to max int value.
   */
  public SimpleIdGenerator() {
    this(1, Integer.MAX_VALUE, true);
  }

  /**
   * Creates a new cycled id generator with specified initial value.
   */
  public SimpleIdGenerator(int initialValue) {
    this(initialValue, Integer.MAX_VALUE, true);
  }

  /**
   * Creates a new cycled id generator with specified range.
   */
  public SimpleIdGenerator(int initialValue, int maxValue) {
    this(initialValue, maxValue, true);
  }

  /**
   * Creates a new id generator with specified range and cycling flag.
   */
  public SimpleIdGenerator(int initialValue, int maxValue, boolean cycle) {
    if (initialValue < 0) {
      throw new IllegalArgumentException("Initial value '" + initialValue + "' must be a positive number.");
    }
    if (maxValue <= initialValue) {
      throw new IllegalArgumentException("Max value '" + maxValue + "' is less or equals to initial value '" + initialValue + "'.");
    }
    this.initialValue = this.value = initialValue;
    this.maxValue = maxValue;
    this.cycle = cycle;
  }

  /**
   * Returns the next value from the sequence. Thread-safe.
   */
  public synchronized int next() {
    int id = value;

    value++;
    if ((value > maxValue) || (value < 0)) {
      if (cycle == false) {
        throw new IllegalStateException("Max value already reached.");
      }
      value = initialValue;
    }
    return id;
  }
}

   
    
    
    
    
  








Related examples in the same category

1.Random GUID
2.Algorithm for generating Random GUID
3.Get a unique identifier Using java.rmi.dgc.VMID
4.Using java.util.UUID
5.Create your own basic UUID
6.Session ID generator
7.UUID generator from Sun Microsystems
8.UUID generator from http://www1.ics.uci.edu
9.Using java.util.concurrent.AtomicLong: A numerical id, start at zero and increment by one.
10.A 32 byte GUID generator
11.A unique identifier
12.Generate pseudo-GUID sequences
13.Generates a UUID
14.Generates random UUIDs
15.Generator for Globally unique Strings
16.ID generator
17.UUID generation
18.UUID generator of 32 bytes long values
19.Simple Long Id Generator
20.Long Sequence Generator
21.UUID generator
22.Thread-safe version of the Axiom UUIDGenerator
23.Convert an array of bytes into a List of Strings using UTF-8.
24.Your own UUID
25.Your own UUID 2
26.Create GUIDs according to the principles at http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt.
27.Get Unique Id