com.ibm.commerce.promotion.uniquecode.generator

Class CodeGenerator

  • java.lang.Object
    • com.ibm.commerce.promotion.uniquecode.generator.CodeGenerator


  • public class CodeGenerator
    extends java.lang.Object
    This class generates the codes which satisfy the code pattern definition.

    After the class instance is created, the invoker needs to explicitly call the validateCodePattern method to do initial code pattern parse and validation.

    The code generation batch size is needed before the code generation starts.

    The class has a default Pattern registry. The default Pattern classes are SequencePattern, NumericPattern, AlphaUpperNumericPattern, AlphaLowerNumericPattern, AlphaMixNumericPattern, AlphabeticUpperPattern, AlphabeticLowerPattern and AlphabeticMixPattern.

    If there needs a new Pattern to register, user must following the steps to make the pattern work properly:

  • Extends this CodeGenerator class.
  • Overrides the patternStyleConverter and getCodeSpace methods to support the behavior of the new Patterns.
  • Creates a new instance of the class.
  • Uses the registerPattern method to register the new Patterns.
  • Sets the batch size, target total number and the finished number to the new class instance.
  • Uses the validateCodePattern method to parse and validate the pattern definition.
  • Uses the generateNextBatch method to generate code for the new patterns.
    • Constructor Summary

      Constructors 
      Constructor and Description
      CodeGenerator(java.lang.String pattern, java.lang.Long target)
      Construct the code generator
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      void checkCodespaceSize(java.lang.Long targetGenerated)
      This method validates the code space size.
      static void cleanCodeGeneratorForPromotion(java.lang.String promotionId)
      Clean the related code generator free for GC
      java.lang.String[] generateNextBatch()
      This method generates a batch of codes.
      int getBatchSize()
      This method gets the batch size.
      static CodeGenerator getCodeGeneratorForPromotion(java.lang.String promotionId)
      Get the promotionId related code generator
      java.math.BigDecimal getCodeSpace()
      This method calculates the code pattern space according to the list of parsed Pattern objects.
      long getErrorCodesSize()
      Get the error codes size.
      java.lang.Long getTargetTotal()
      This method gets the total number of the generated codes
      java.util.List<java.lang.String> patternStyleConverter(java.lang.String segment)
      This method divides the input string into a list of pattern segments and convert the segments into the Pattern objects.
      void registerPattern(Pattern p)
      This method registers an extra Pattern.
      void setBatchSize(int localBatchSize)
      This method sets the batch size.
      static void setCodeGeneratorForPromotion(java.lang.String promotionId, CodeGenerator cg)
      Put the code generator to cache
      void setCodePattern(java.lang.String pattern)
      This method sets the code pattern definition string.
      void setErrorCodesSize(long size)
      Set the error codes size.
      void setFinishedSize(long aFinishedSize)
      This method sets the total number of generate codes
      void setTargetTotal(java.lang.Long target)
      This method sets the total number of generate codes
      void validateCodePattern()
      This method parses the pattern string and checks if code space size is enough for the total number of generated codes.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CodeGenerator

        public CodeGenerator(java.lang.String pattern,
                             java.lang.Long target)
        Construct the code generator
        Parameters:
        pattern - The raw pattern definition.
        target - The total number of the codes to be generated for this pattern.
    • Method Detail

      • getCodeGeneratorForPromotion

        public static CodeGenerator getCodeGeneratorForPromotion(java.lang.String promotionId)
        Get the promotionId related code generator
        Parameters:
        promotionId - The target promotionId
        Returns:
        The code generator related to the promotionId
      • setCodeGeneratorForPromotion

        public static void setCodeGeneratorForPromotion(java.lang.String promotionId,
                                                        CodeGenerator cg)
        Put the code generator to cache
        Parameters:
        promotionId - The target promotionId
        cg - The related code generator
      • cleanCodeGeneratorForPromotion

        public static void cleanCodeGeneratorForPromotion(java.lang.String promotionId)
        Clean the related code generator free for GC
        Parameters:
        promotionId - The target promotionId
      • setCodePattern

        public void setCodePattern(java.lang.String pattern)
        This method sets the code pattern definition string.
        Parameters:
        pattern - The code pattern definition
      • setFinishedSize

        public void setFinishedSize(long aFinishedSize)
        This method sets the total number of generate codes
        Parameters:
        aFinishedSize - The finished size
      • setTargetTotal

        public void setTargetTotal(java.lang.Long target)
        This method sets the total number of generate codes
        Parameters:
        target - The total number
      • getTargetTotal

        public java.lang.Long getTargetTotal()
        This method gets the total number of the generated codes
        Returns:
        The total number
      • registerPattern

        public void registerPattern(Pattern p)
        This method registers an extra Pattern.

        This method allows another Pattern to register into this code generator.

        Parameters:
        p - The pattern that need to register
      • patternStyleConverter

        public java.util.List<java.lang.String> patternStyleConverter(java.lang.String segment)
        This method divides the input string into a list of pattern segments and convert the segments into the Pattern objects.
        Parameters:
        segment - The input pattern mask.
        Returns:
        The pattern segments
      • checkCodespaceSize

        public void checkCodespaceSize(java.lang.Long targetGenerated)
                                throws PatternDefineException
        This method validates the code space size.

        If the code space size is not enough, it will directly throw the PatternDefineException

        Parameters:
        targetGenerated - The target size to be generated
        Throws:
        PatternDefineException - If the pattern definition is wrong, then throws the definition error of the pattern.
      • validateCodePattern

        public void validateCodePattern()
                                 throws PatternDefineException
        This method parses the pattern string and checks if code space size is enough for the total number of generated codes.
        Throws:
        PatternDefineException - If the pattern definition is wrong, then throws the definition error of the pattern.
      • getCodeSpace

        public java.math.BigDecimal getCodeSpace()
        This method calculates the code pattern space according to the list of parsed Pattern objects.
        Returns:
        The total number of possible codes that could be generated
      • generateNextBatch

        public java.lang.String[] generateNextBatch()
        This method generates a batch of codes.
        Returns:
        The codes array.
      • getBatchSize

        public int getBatchSize()
        This method gets the batch size.
        Returns:
        An Integer value representing the batch size
      • setBatchSize

        public void setBatchSize(int localBatchSize)
        This method sets the batch size.
        Parameters:
        localBatchSize - An Integer value representing the batch size
      • setErrorCodesSize

        public void setErrorCodesSize(long size)
        Set the error codes size.
        Parameters:
        size - The size of the error codes.
      • getErrorCodesSize

        public long getErrorCodesSize()
        Get the error codes size.
        Returns:
        The size of the error codes.