Skip to content
Snippets Groups Projects
Commit b977dcc8 authored by felmer's avatar felmer
Browse files

LMS-257 new method for Template: attemptToBind()

SVN: 4521
parent 19c4f4ae
No related branches found
No related tags found
No related merge requests found
...@@ -29,6 +29,7 @@ import java.util.Map; ...@@ -29,6 +29,7 @@ import java.util.Map;
* String text = template.createText(); * String text = template.createText();
* </pre> * </pre>
* The method {@link #bind(String, String)} throws an exception if the placeholder name is unknown. * The method {@link #bind(String, String)} throws an exception if the placeholder name is unknown.
* The method {@link #attemptToBind(String, String)} returns <code>false</code> if the placeholder name is unknown.
* The method {@link #createText()} throws an exception if not all placeholders have been bound. * The method {@link #createText()} throws an exception if not all placeholders have been bound.
* <p> * <p>
* Since placeholder bindings change the state of an instance of this class there is method {@link #createFreshCopy()} * Since placeholder bindings change the state of an instance of this class there is method {@link #createFreshCopy()}
...@@ -273,6 +274,20 @@ public class Template ...@@ -273,6 +274,20 @@ public class Template
* @throws IllegalArgumentException if placeholder is not known. * @throws IllegalArgumentException if placeholder is not known.
*/ */
public void bind(String placeholderName, String value) public void bind(String placeholderName, String value)
{
boolean successful = attemptToBind(placeholderName, value);
if (successful == false)
{
throw new IllegalArgumentException("Unknown variable '" + placeholderName + "'.");
}
}
/**
* Attempts to bind the specified value to the specified placeholder name.
*
* @return <code>true</code> if successful.
*/
public boolean attemptToBind(String placeholderName, String value)
{ {
assert placeholderName != null : "Unspecified placeholder name."; assert placeholderName != null : "Unspecified placeholder name.";
assert value != null : "Unspecified value for '" + placeholderName + "'"; assert value != null : "Unspecified value for '" + placeholderName + "'";
...@@ -280,9 +295,10 @@ public class Template ...@@ -280,9 +295,10 @@ public class Template
VariableToken variableToken = variableTokens.get(placeholderName); VariableToken variableToken = variableTokens.get(placeholderName);
if (variableToken == null) if (variableToken == null)
{ {
throw new IllegalArgumentException("Unknown variable '" + placeholderName + "'."); return false;
} }
variableToken.bind(value); variableToken.bind(value);
return true;
} }
/** /**
......
...@@ -128,6 +128,15 @@ public class TemplateTest ...@@ -128,6 +128,15 @@ public class TemplateTest
} }
} }
@Test
public void testAttemptToBind()
{
Template template = new Template("${greeting} ${name}!");
assertEquals(true, template.attemptToBind("greeting", "Hi"));
assertEquals(false, template.attemptToBind("blabla", "blub"));
assertEquals("Hi ${name}!", template.createText(false));
}
@Test @Test
public void testIncompleteBinding() public void testIncompleteBinding()
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment