|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectde.marcelsauer.tokenreplacer.Toky
public class Toky
Toky is a token replacer for Strings. It will replace the found token with a
provided static value or a dynamically generated value created by a
Generator. Toky itself IS NOT THREAD SAFE. so handling Toky in
a multi threaded environment should be synchronized by the client.
simplest use case, only static values
TokenReplacer toky = new Toky().register("number", "123");
toky.substitute("i can count to {number}");
is same as registering an explicit Token
toky = new Toky().register(new Token("number").replacedBy("123"));
toky.substitute("i can count to {number}");
we can also use a Generator to dynamically get the
value (which here does not really make sense ;-)
toky = new Toky().register(new Token("number").replacedBy(new Generator() {
@Override
public void inject(String[] args) {
// not relevant here
}
@Override
public String generate() {
return "123";
}
}));
here we use a generator and pass the arguments "a,b,c" to it, they
will be injected via Generator.inject(String[] args) before the call
to Generator.generate() is done. it is up to the generator to decide
what to do with them. this feature makes handling tokens pretty powerful
because you can write very dynamic generators.
toky.substitute("i can count to {number(a,b,c)}");
if you prefer to use index based tokens, you can also use this:
toky.register(new String[] { "one", "two", "three" });
toky.substitute("abc {0} {1} {2} def")); // will produce "abc one two three def"
of course you can replace all default delimiters with your preferred ones, just make sure start and end are different.
toky.withTokenStart("*"); // default is '{'
toky.withTokenEnd("#"); // default is '}'
toky.withArgumentDelimiter(";"); // default is ','
toky.withArgumentStart("["); // default is '('
toky.withArgumentEnd("]"); // default is ')'
by default Toky will throw IllegalStateExceptions if there was no matching value or generator found for a token. you can enable/disable generating exceptions.
toky.doNotIgnoreMissingValues(); // which is the DEFAULT
will turn error reporting for missing values OFF
toky.ignoreMissingValues();
you can enable/disable generator caching. if you enable caching once a
generator for a token returned a value this value will be used for all
subsequent tokens with the same name otherwise the generator will be called
once for every token.
e.g. {counter}{counter}{counter}
with a registered generator will result in 3 calls to the generator
(resulting in poorer performance). so, if you know your generator will always
return the same value enable caching.
toky.enableGeneratorCaching(); toky.disableGeneratorCaching();
| Constructor Summary | |
|---|---|
Toky()
constructs a new TokenReplacer. |
|
Toky(TokenReplacer impl)
allows clients of Toky to provided their own implementation of
the TokenReplacer that will be called. |
|
| Method Summary | |
|---|---|
TokenReplacer |
disableGeneratorCaching()
turns generator caching OFF. |
TokenReplacer |
doNotIgnoreMissingValues()
tells the TokenReplacer to report any tokens that can not be
replaced. |
TokenReplacer |
enableGeneratorCaching()
turns generator caching ON. |
TokenReplacer |
ignoreMissingValues()
tells the TokenReplacer to IGNORE any tokens that can not be
replaced. |
TokenReplacer |
register(java.lang.String[] replacements)
registers an array of replacements for a string based in indexed tokens. |
TokenReplacer |
register(java.lang.String token,
Generator Generator)
registers a Token that will be replaced by the given
Generator. |
TokenReplacer |
register(java.lang.String token,
java.lang.String value)
registers a static value for a given token. |
TokenReplacer |
register(Token token)
registers a Token that needs to be replaced. |
java.lang.String |
substitute(java.lang.String toSubstitute)
replaces all Token with one of the following:
the provided static values set via TokenReplacer.register(String, String)
the token registered via TokenReplacer.register(Token)
the generator registered via TokenReplacer.register(String, Generator)
|
TokenReplacer |
withArgumentDelimiter(java.lang.String argsSep)
|
TokenReplacer |
withArgumentEnd(java.lang.String argsEnd)
|
TokenReplacer |
withArgumentStart(java.lang.String argsStart)
|
TokenReplacer |
withTokenEnd(java.lang.String tokenEnd)
|
TokenReplacer |
withTokenStart(java.lang.String tokenStart)
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public Toky(TokenReplacer impl)
Toky to provided their own implementation of
the TokenReplacer that will be called. use this if you know what
you are doing :-)
otherwise always use the normal Toky() constructor that will use
the correct implementation!
impl - public Toky()
TokenReplacer.
| Method Detail |
|---|
public TokenReplacer register(java.lang.String token,
java.lang.String value)
TokenReplacer#register(Generator). same as registering a token via
TokenReplacer.register(Token) and supplying a replacement value via
Token.replacedBy(String).
register in interface TokenReplacertoken - the name of the token to be replaced e.g. for ${date} ->
"date" would be the token, must not be null or emptyvalue - the static value that will be used when replacing the token,
must not be null or empty
TokenReplacer to allow method chainingpublic TokenReplacer register(Token token)
TokenReplacerToken that needs to be replaced.
register in interface TokenReplacertoken - the Token, must not be null, the token must have a
valid value or generator associated with it which was set via
Token.replacedBy(String) or
Token.replacedBy(String)
#TokenReplacer to allow method chainingpublic java.lang.String substitute(java.lang.String toSubstitute)
TokenReplacerToken with one of the following:
TokenReplacer.register(String, String)
TokenReplacer.register(Token)
TokenReplacer.register(String, Generator)
substitute in interface TokenReplacertoSubstitute - the string that contains the tokens, will be returned as-is in
case of null or empty string
public TokenReplacer withArgumentDelimiter(java.lang.String argsSep)
withArgumentDelimiter in interface TokenReplacerargsSep - changes the delimiter of the arguments to the given value e.g.
{dynamic(1;2;3)} -> ';' would be the delimiter, must not be
null or empty
#TokenReplacer to allow method chainingpublic TokenReplacer withArgumentEnd(java.lang.String argsEnd)
withArgumentEnd in interface TokenReplacerargsEnd - sets the argument end identifier to the given value e.g.
{dynamic[1;2;3]} -> ']' would be the delimiter e.g. ']', must
not be null or empty
#TokenReplacer to allow method chainingpublic TokenReplacer withArgumentStart(java.lang.String argsStart)
withArgumentStart in interface TokenReplacerargsStart - sets the argument start identifier to the given value e.g.
{dynamic[1;2;3]} -> '[' would be the delimiter e.g. '[', must
not be null or empty
#TokenReplacer to allow method chainingpublic TokenReplacer withTokenEnd(java.lang.String tokenEnd)
withTokenEnd in interface TokenReplacertokenEnd - sets the token end identifier to the given value e.g.
[dynamic] -> ']' would be the end identifier, e.g. '[', must
not be null or empty
#TokenReplacer to allow method chainingpublic TokenReplacer withTokenStart(java.lang.String tokenStart)
withTokenStart in interface TokenReplacertokenStart - sets the token start identifier to the given value e.g.
[dynamic] -> '[' would be the start identifier, e.g. '[', must
not be null or empty
#TokenReplacer to allow method chaining
public TokenReplacer register(java.lang.String token,
Generator Generator)
TokenReplacerToken that will be replaced by the given
Generator. same as registering a token via
TokenReplacer.register(Token) and supplying a generator via
Token.replacedBy(Generator)
register in interface TokenReplacertoken - the name of the token to be replaced e.g. for ${date} ->
"date" would be the token, must not be null or empty
#TokenReplacer to allow method chainingpublic TokenReplacer doNotIgnoreMissingValues()
TokenReplacerTokenReplacer to report any tokens that can not be
replaced. if turned on an IllegalStateException will be thrown
during token replacement. reporting errors is turned ON by DEFAULT.
doNotIgnoreMissingValues in interface TokenReplacer#TokenReplacer to allow method chainingpublic TokenReplacer ignoreMissingValues()
TokenReplacerTokenReplacer to IGNORE any tokens that can not be
replaced. if turned OFF no Exceptions will be thrown during token
replacement. reporting errors is turned ON by DEFAULT.
ignoreMissingValues in interface TokenReplacer#TokenReplacer to allow method chainingpublic TokenReplacer enableGeneratorCaching()
TokenReplacerGenerator all remaining values with the same token name will be
replaced by the cached version. use TokenReplacer.disableGeneratorCaching() to
turn caching off.
enableGeneratorCaching in interface TokenReplacer#TokenReplacer to allow method chainingpublic TokenReplacer disableGeneratorCaching()
TokenReplacerTokenReplacer.enableGeneratorCaching() to
turn caching on.
disableGeneratorCaching in interface TokenReplacer#TokenReplacer to allow method chainingpublic TokenReplacer register(java.lang.String[] replacements)
TokenReplacer
toky.register(new String[] { "one", "two", "three" });
toky.substitute("{0} {1} {2}")); // will result in "one two three"
register in interface TokenReplacerreplacements - the array of replacements that will be used when replacing an
indexed strings, must not be null but can be empty
#TokenReplacer to allow method chaining
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||