From 1628bdb42cbc38c318a9a6c8e6cd708461ab77c9 Mon Sep 17 00:00:00 2001 From: Suseika Date: Thu, 20 Aug 2015 23:54:23 +0500 Subject: [PATCH] #99 Remove TextTemplateBuilder --- .../inflectible/TextTemplateBuilder.java | 160 ------------------ .../inflectible/TemplateBuilderTest.java | 129 -------------- 2 files changed, 289 deletions(-) delete mode 100644 src/main/java/org/tendiwa/inflectible/TextTemplateBuilder.java delete mode 100644 src/test/java/org/tendiwa/inflectible/TemplateBuilderTest.java diff --git a/src/main/java/org/tendiwa/inflectible/TextTemplateBuilder.java b/src/main/java/org/tendiwa/inflectible/TextTemplateBuilder.java deleted file mode 100644 index 0b1cc8c..0000000 --- a/src/main/java/org/tendiwa/inflectible/TextTemplateBuilder.java +++ /dev/null @@ -1,160 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2015 Georgy Vlasov - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package org.tendiwa.inflectible; - -import com.google.common.collect.ImmutableList; -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; -import org.tenidwa.collections.utils.Rethrowing; - -/** - * A builder used to create {@link Template}s. - * @author Georgy Vlasov (suseika@tendiwa.org) - * @version $Id$ - * @since 0.1 - */ -public final class TextTemplateBuilder { - /** - * Names of the arguments of a template. - */ - private final transient ImmutableList arguments; - - /** - * Parts of a template. Those can be either placeholders or plain text - * parts (which may be seen as constant-value placeholders, hence the type). - */ - private final transient List parts; - - /** - * If this builder has already been used to produce a template. - */ - private transient boolean used; - - /** - * Ctor. - * @param names Names of the arguments of a template - */ - TextTemplateBuilder(final ImmutableList names) { - this.arguments = names; - this.parts = new ArrayList<>( - TextTemplateBuilder.expectedPartsNumber(names) - ); - this.used = false; - } - - /** - * Adds a placeholder to the body of the constructed template. - * @param placeholder A placeholder - * @return This builder - */ - public TextTemplateBuilder addPlaceholder( - final Placeholder placeholder - ) { - this.parts.add(new PiPlaceholder(placeholder)); - return this; - } - - /** - * Adds plain text to the body of the constucted template. - * @param text Plain text - * @return This builder - */ - public TextTemplateBuilder addText(final String text) { - this.parts.add(new PiPlainText(text)); - return this; - } - - /** - * Builds a template. - * @return A new {@link Template} - */ - public Template build() { - if (this.used) { - throw new IllegalStateException( - "This builder has already been used" - ); - } - this.used = true; - return new BasicTemplate(this.arguments, this.parts); - } - - /** - * A good approximated size for an array list that will hold the parts of - * this text. - * @param arguments Arguments to this text. - * @return Good size for an array list. - */ - private static int expectedPartsNumber( - final ImmutableList arguments - ) { - return arguments.size() * 2 + 1; - } - - /** - * {@link Template} defined by its arguments' names and a - * heterogeneous list of its parts (placeholders and plain text chunks). - */ - private static class BasicTemplate implements Template { - /** - * Argument names. - */ - private final transient ImmutableList arguments; - /** - * Heterogeneous list of template's parts (placeholders and plain text - * chunks). - */ - private final transient List parts; - - /** - * Ctor. - * @param names Argument names - * @param pieces Heterogeneous list of template's parts (placeholders - * and plain text chunks) - */ - BasicTemplate( - final ImmutableList names, - final List pieces - ) { - this.arguments = names; - this.parts = pieces; - } - - @Override - public String fillUp( - final ImmutableList lexemes, - final Vocabulary vocabulary - ) throws Exception { - final ActualArguments actualArguments = - new BasicActualArguments(this.arguments, lexemes); - return this.parts.stream() - .map( - Rethrowing.rethrowFunction( - part -> part.fillUp(actualArguments, vocabulary) - ) - ) - .collect(Collectors.joining()); - } - } -} diff --git a/src/test/java/org/tendiwa/inflectible/TemplateBuilderTest.java b/src/test/java/org/tendiwa/inflectible/TemplateBuilderTest.java deleted file mode 100644 index 2c340e3..0000000 --- a/src/test/java/org/tendiwa/inflectible/TemplateBuilderTest.java +++ /dev/null @@ -1,129 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2015 Georgy Vlasov - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package org.tendiwa.inflectible; - -import com.google.common.collect.ImmutableList; -import java.util.stream.IntStream; -import org.hamcrest.CoreMatchers; -import org.hamcrest.MatcherAssert; -import org.junit.Ignore; -import org.junit.Test; - -/** - * Unit tests for {@link TextTemplateBuilder}. - * @author Georgy Vlasov (suseika@tendiwa.org) - * @version $Id$ - * @since 0.1 - */ -public final class TemplateBuilderTest { - /** - * TextTemplateBuilder can create a template. - * @throws Exception If fails - */ - @Test - public void createsTemplate() throws Exception { - final ArgumentName subject = new ArgumentName("subject"); - final ArgumentName object = new ArgumentName("object"); - MatcherAssert.assertThat( - new TextTemplateBuilder( - ImmutableList.of( - subject, - object - ) - ) - .addPlaceholder( - new PhFromArgument(subject) - ) - .addText(" immediately picks up an ") - .addPlaceholder( - new PhFromArgument(object) - ) - .build() - .fillUp( - ImmutableList.of( - new SingleFormLexeme("man"), - new SingleFormLexeme("apple") - ), - new BasicVocabulary() - ), - CoreMatchers.equalTo("man immediately picks up an apple") - ); - } - - /** - * TextTemplateBuilder can not be used twice to produce a template. - * @throws Exception If fails - */ - @Test(expected = IllegalStateException.class) - public void cantBeUsedTwice() throws Exception { - final ArgumentName name = new ArgumentName("actor"); - final TextTemplateBuilder builder = - new TextTemplateBuilder(ImmutableList.of(name)) - .addPlaceholder( - new PhFromArgument(name) - ); - IntStream.range(0, 2).forEach(i -> builder.build()); - } - - /** - * TextTemplateBuilder can consist of just text, with no placeholders. - * @throws Exception If fails - */ - @Test - public void canConsistOfJustText() throws Exception { - MatcherAssert.assertThat( - new TextTemplateBuilder(ImmutableList.of()) - .addText("Hey, ") - .addText("dude!") - .build() - .fillUp(ImmutableList.of(), new BasicVocabulary()), - CoreMatchers.equalTo("Hey, dude!") - ); - } - - /** - * TextTemplateBuilder can create texts that report wrong number of - * arguments. - * @throws Exception If can't fill up the template - */ - @Test(expected = Exception.class) - @Ignore - public void createdTextsReportWrongNumberOfArguments() throws Exception { - new TextTemplateBuilder( - ImmutableList.of( - new ArgumentName("someone"), - new ArgumentName("item") - ) - ) - .build() - .fillUp( - ImmutableList.of( - new SingleFormLexeme("Mary"), - new SingleFormLexeme("cock"), - new SingleFormLexeme("backstage") - ), - new BasicVocabulary() - ); - } -}