Class UniqueIdentifier.Generator

java.lang.Object
com.github.yellowstonegames.core.UniqueIdentifier.Generator
Enclosing class:
UniqueIdentifier

public static final class UniqueIdentifier.Generator extends Object
The type used as a factory to produce UniqueIdentifiers that are actually unique for a given Generator. This is used in UniqueIdentifier.GENERATOR, and can be used independently via generate().
  • Constructor Details

    • Generator

      public Generator()
      Creates a new Generator with a and b states derived from the current time in nanoseconds, and one of (2 to the 48) possible random initial c and d states pulled from Math.random(). This means that unless trillions of Generator instances are created before the nanosecond time changes, this cannot have the same initial state as another Generator. Eventually, all valid generators will enter all valid states, but this should really only be a concern after calling UniqueIdentifier.next() for over a year continuously.
    • Generator

      public Generator(long stateA, long stateB)
      Creates a new Generator given two long values for state.
      Parameters:
      stateA - may be any long
      stateB - may be any long unless both states are 0, in which case this is treated as 1
    • Generator

      public Generator(int a, int b, int c, int d)
      Creates a new Generator using the given 4 states verbatim, unless they are all 0 (then it treats d as 1).
      Parameters:
      a - may be any int unless all are 0
      b - may be any int unless all are 0
      c - may be any int unless all are 0
      d - may be any int unless all are 0
    • Generator

      public Generator(String serialized)
      Given a String containing the output of stringSerialize(), this creates a new UniqueIdentifier.Generator with the same data as the UniqueIdentifier.Generator that was serialized. This requires a 35-char String at minimum with four 8-hex-digit sections, but the delimiters between sections are permitted to be anything (such as '_' for UniqueIdentifier or '$' for this).
      Parameters:
      serialized - a String almost always produced by stringSerialize()
  • Method Details

    • generate

      public UniqueIdentifier generate()
      Creates a new UniqueIdentifier, advancing the state of this Generator in the process.
      Returns:
      a new UniqueIdentifier that will not occur again from this Generator unless (2 to the 128) - 1 more identifiers are generated
    • getA

      public int getA()
    • getB

      public int getB()
    • getC

      public int getC()
    • getD

      public int getD()
    • stringSerialize

      public String stringSerialize()
    • stringDeserialize

      public UniqueIdentifier.Generator stringDeserialize(String data)
      Loads the state of another serialized Generator (or UniqueIdentifier) into this. This requires a 35-char String at minimum with four 8-hex-digit sections, but the delimiters between sections are permitted to be anything (such as '_' for UniqueIdentifier or '$' for this).
      Parameters:
      data - a String almost always produced by stringSerialize()
      Returns:
      this Generator, after deserializing