public class CodeGenerator
extends java.lang.Object
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:
CodeGenerator
class.patternStyleConverter
and getCodeSpace
methods to support the behavior of the new Pattern
s.registerPattern
method to register the new Pattern
s.validateCodePattern
method to parse and validate the pattern definition.generateNextBatch
method to generate code for the new patterns.Constructor and Description |
---|
CodeGenerator(java.lang.String pattern,
java.lang.Long target)
Construct the code generator
|
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.
|
public CodeGenerator(java.lang.String pattern, java.lang.Long target)
pattern
- The raw pattern definition.target
- The total number of the codes to be generated for this pattern.public static CodeGenerator getCodeGeneratorForPromotion(java.lang.String promotionId)
promotionId
- The target promotionIdpublic static void setCodeGeneratorForPromotion(java.lang.String promotionId, CodeGenerator cg)
promotionId
- The target promotionIdcg
- The related code generatorpublic static void cleanCodeGeneratorForPromotion(java.lang.String promotionId)
promotionId
- The target promotionIdpublic void setCodePattern(java.lang.String pattern)
pattern
- The code pattern definitionpublic void setFinishedSize(long aFinishedSize)
aFinishedSize
- The finished sizepublic void setTargetTotal(java.lang.Long target)
target
- The total numberpublic java.lang.Long getTargetTotal()
public void registerPattern(Pattern p)
Pattern
.
This method allows another Pattern
to register into this code generator.
p
- The pattern that need to registerpublic java.util.List<java.lang.String> patternStyleConverter(java.lang.String segment)
Pattern
objects.segment
- The input pattern mask.public void checkCodespaceSize(java.lang.Long targetGenerated) throws PatternDefineException
If the code space size is not enough, it will directly throw the PatternDefineException
targetGenerated
- The target size to be generatedPatternDefineException
- If the pattern definition is wrong, then throws the definition error of the pattern.public void validateCodePattern() throws PatternDefineException
PatternDefineException
- If the pattern definition is wrong, then throws the definition error of the pattern.public java.math.BigDecimal getCodeSpace()
Pattern
objects.public java.lang.String[] generateNextBatch()
public int getBatchSize()
Integer
value representing the batch sizepublic void setBatchSize(int localBatchSize)
localBatchSize
- An Integer
value representing the batch sizepublic void setErrorCodesSize(long size)
size
- The size of the error codes.public long getErrorCodesSize()