Class IDGenerator

java.lang.Object
org.chenile.utils.entity.service.IDGenerator

public abstract class IDGenerator extends Object
Generates IDs given a request context. This class is useful to generate predictable IDs for entities given a request ID. This is essential for replicating systems i.e. systems that replicate the same data in multiple machines by replaying the same end user request in these machines. E.g., let us say that we want to generate identical Orders in a store machine and a cloud machine. The IDs of the Order cannot differ between multiple machines.
If you use this IDGenerator, then the IDs will be the same since they all are based out of a request ID that is shared between the replicating machines.
Class needs to be used statically since it is designed to be called by entities in their JPA \@PrePersist and \@PreUpdate methods.
  • Field Details

  • Constructor Details

    • IDGenerator

      public IDGenerator()
  • Method Details

    • generateID

      public static String generateID(String prefix)
      This method generates a new unique ID for the given prefix. The ID is based out of request ID. A counter is added in case multiple IDs are required for the same entity.
      Parameters:
      prefix - Prefix that needs to be used. Typically, each entity will have its own prefix.
      Returns:
      the ID that can be used to generate new records for the given entity.
    • obtainCounter

      private static int obtainCounter(String prefix)
    • obtainIdMap

      private static Map<String,Integer> obtainIdMap()