Visando fixar mais esse padrão fiz um código extra e gostaria que vcs dessem um olhada.
package br.com.schimidtsolutions.estudo.interpreter;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;
import br.com.schimidtsolutions.estudo.interpreter.TextExpression;
import br.com.schimidtsolutions.estudo.interpreter.TextLocatorBuilder;
public class TextLocatorBuilderTest {
private TextLocatorBuilder builder;
@Before
public void setUp() {
builder = new TextLocatorBuilder();
}
@Test
public void deveEncontrarTextoSimplesDentroDeUmaFrase() {
builder.findText("Dênis");
TextExpression expression = builder.build();
assertThat(expression.evaluate("O meu nome é Dênis Schimidt"), equalTo(true));
}
@Test
public void naoDeveEncontrarNenhumTextoSimplesDentroDeUmaFrase() {
builder.findText("Fulano");
TextExpression expression = builder.build();
assertThat(expression.evaluate("O meu nome é Dênis Schimidt"), equalTo(false));
}
@Test
public void deveEncontrarTextoCombinadoDentroDeUmaFrase() {
builder.findText("tênis").or("Schimidt").and("Dênis");
TextExpression expression = builder.build();
assertThat(expression.evaluate("O meu nome é Dênis Schimidt"), equalTo(true));
}
@Test
public void naoDeveEncontrarNenhumTextoCombinadoComOrDentroDeUmaFrase() {
builder.findText("Ciclano").or("Fulano");
TextExpression expression = builder.build();
assertThat(expression.evaluate("O meu nome é Dênis Schimidt"), equalTo(false));
}
@Test
public void naoDeveEncontrarNenhumTextoCombinadoComAndDentroDeUmaFrase() {
builder.findText("nomenclatura").and("minha");
TextExpression expression = builder.build();
assertThat(expression.evaluate("O meu nome é Dênis Schimidt"), equalTo(false));
}
@Test
public void naoDeveEncontrarNenhumTextoCombinadoComOrAndDentroDeUmaFrase() {
builder.findText("nomenclatura").or("classe").and("Dênis");
TextExpression expression = builder.build();
assertThat(expression.evaluate("O meu nome é Dênis Schimidt"), equalTo(false));
}
}
package br.com.schimidtsolutions.estudo.interpreter;
public interface TextExpression {
boolean evaluate(String completeText);
}
package br.com.schimidtsolutions.estudo.interpreter;
import java.util.Scanner;
public class TextFinder implements TextExpression {
private final String targetText;
public TextFinder(final String targetText) {
this.targetText = targetText;
}
@Override
public boolean evaluate(final String completeText) {
Scanner scanner = new Scanner(completeText);
try {
scanner.useDelimiter(" ");
while (scanner.hasNext()) {
String nextString = scanner.next().trim();
if (nextString.equals(targetText)) {
return true;
}
}
return false;
} finally {
scanner.close();
}
}
}
package br.com.schimidtsolutions.estudo.interpreter;
public class OrExpression implements TextExpression {
private final TextExpression leftExpression;
private final TextExpression rightExpression;
public OrExpression(final TextExpression leftExpression, final TextExpression rightExpression) {
this.leftExpression = leftExpression;
this.rightExpression = rightExpression;
}
@Override
public boolean evaluate(final String completeText) {
return leftExpression.evaluate(completeText) || rightExpression.evaluate(completeText);
}
}
package br.com.schimidtsolutions.estudo.interpreter;
public class AndExpression implements TextExpression {
private final TextExpression leftExpression;
private final TextExpression rightExpression;
public AndExpression(final TextExpression leftExpression, final TextExpression rightExpression) {
this.leftExpression = leftExpression;
this.rightExpression = rightExpression;
}
@Override
public boolean evaluate(final String completeText) {
return leftExpression.evaluate(completeText) && rightExpression.evaluate(completeText);
}
}
package br.com.schimidtsolutions.estudo.interpreter;
public class TextLocatorBuilder {
private TextExpression expressionResult;
public TextLocatorBuilder findText(final String targetText) {
expressionResult = new TextFinder(targetText);
return this;
}
public TextLocatorBuilder and(final String targetText) {
expressionResult = new AndExpression(expressionResult, new TextFinder(targetText));
return this;
}
public TextLocatorBuilder or(final String targetText) {
expressionResult = new OrExpression(expressionResult, new TextFinder(targetText));
return this;
}
public TextExpression build() {
return expressionResult;
}
}