diff --git a/src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java b/src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java index 94b2cf7f..45e2d081 100644 --- a/src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java +++ b/src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java @@ -174,7 +174,7 @@ private YamlNode mappingSequenceOrPlainScalar(final YamlLine prev) { } else if (matcher.group(4) != null) { node = new ReadYamlMapping(prev.number(), prev, this); } - } else if (this.original().size() == 1) { + } else if (this.lines.size() == 1) { node = new ReadPlainScalar(this, first); } } diff --git a/src/main/java/com/amihaiemil/eoyaml/FlowOnOneLine.java b/src/main/java/com/amihaiemil/eoyaml/FlowOnOneLine.java deleted file mode 100644 index a094b637..00000000 --- a/src/main/java/com/amihaiemil/eoyaml/FlowOnOneLine.java +++ /dev/null @@ -1,116 +0,0 @@ -/** - * Copyright (c) 2016-2024, Mihai Emil Andronache - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ -package com.amihaiemil.eoyaml; - -/** - * Some YAML flow lines merged into one. - * @author Mihai Andronache (amihaiemil@gmail.com) - * @version $Id$ - * @since 6.0.0 - */ -final class FlowOnOneLine implements YamlLine { - - /** - * Merged line. - */ - private final YamlLine merged; - - /** - * Ctor. - * @param opening Opening bracket. - * @param closing Closing bracket. - * @param lines Lines to fold. - */ - FlowOnOneLine( - final FoldedFlowLines.Bracket opening, - final FoldedFlowLines.Bracket closing, - final YamlLines lines - ) { - final StringBuilder flowLine = new StringBuilder(); - int startIndex = opening.getIndex(); - for(final YamlLine line : lines) { - int endIndex; - if(line.number() < closing.getLine().number()) { - endIndex = line.trimmed().length(); - } else { - endIndex = closing.getIndex() + 1; - } - for (int i = startIndex; i < endIndex; i++) { - flowLine.append(line.trimmed().charAt(i)); - } - if(line.number() == closing.getLine().number()) { - break; - } - startIndex = 0; - } - this.merged = new RtYamlLine( - flowLine.toString(), - opening.getLine().number() - ); - } - - @Override - public String value() { - return this.merged.value(); - } - - @Override - public String trimmed() { - return this.merged.trimmed(); - } - - @Override - public String comment() { - return this.merged.comment(); - } - - @Override - public int number() { - return this.merged.number(); - } - - @Override - public int indentation() { - return this.merged.indentation(); - } - - @Override - public boolean requireNestedIndentation() { - return false; - } - - @Override - public int compareTo(final YamlLine other) { - return this.merged.compareTo(other); - } - - @Override - public String toString() { - return this.merged.toString(); - } -} diff --git a/src/main/java/com/amihaiemil/eoyaml/FoldedFlowLines.java b/src/main/java/com/amihaiemil/eoyaml/FoldedFlowLines.java deleted file mode 100644 index 4324a5b2..00000000 --- a/src/main/java/com/amihaiemil/eoyaml/FoldedFlowLines.java +++ /dev/null @@ -1,197 +0,0 @@ -/** - * Copyright (c) 2016-2024, Mihai Emil Andronache - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ -package com.amihaiemil.eoyaml; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; - -/** - * A flow YAML node which may span multiple lines. - * The iterator will fold all the lines into a single line. - * @author Mihai Andronache (amihaiemil@gmail.com) - * @version $Id$ - * @since 8.0.0 - */ -@Deprecated -final class FoldedFlowLines implements YamlLines { - - /** - * The flow's opening bracket. - */ - private final char openingBracket; - - /** - * The flow's closing bracket. - */ - private final char closingBracket; - - /** - * Lines to fold. - */ - private final YamlLines lines; - - /** - * Ctor. - * @param lines Lines to fold. - * @param openingBracket Opening. - * @param closingBracket Closing. - */ - @Deprecated - FoldedFlowLines( - final YamlLines lines, - final char openingBracket, - final char closingBracket - ) { - this.lines = lines; - this.openingBracket = openingBracket; - this.closingBracket = closingBracket; - } - - @Override - public Collection original() { - return this.lines.original(); - } - - @Override - public YamlNode nextYamlNode(final YamlLine prev) { - return this.lines.nextYamlNode(prev); - } - - @Override - public Iterator iterator() { - final YamlLine first = this.lines.iterator().next(); - final int openBracketIndex = first - .trimmed().indexOf(this.openingBracket); - if(openBracketIndex == -1) { - throw new IllegalStateException( - "Expected opening " - + this.openingBracket - + " on line " + (first.number() + 1) - ); - } - final Bracket closing = this.findClosingBracketIndex(this.lines); - if(closing.getIndex() == -1) { - throw new IllegalStateException( - "Did not find the closing bracket for opening bracket " - + "on line " + (first.number() + 1) - ); - } - final List one = new ArrayList<>(); - one.add( - new FlowOnOneLine( - new Bracket(openBracketIndex, first), - closing, - this.lines - ) - ); - return one.iterator(); - } - - /** - * Find the closing bracket within the given YamlLines. - * @param all Yaml lines. - * @return ClosingBracket. - */ - private Bracket findClosingBracketIndex(final YamlLines all) { - - final YamlLine first = all.iterator().next(); - int index = first.trimmed().indexOf(this.openingBracket); - int bracketCount = 1; - - final Bracket closing = new Bracket(index, first); - - for(final YamlLine line : all) { - String text = line.trimmed(); - for (int i = index + 1; i < text.length(); i++) { - if (text.charAt(i) == this.openingBracket) { - bracketCount++; - } else if (text.charAt(i) == this.closingBracket) { - bracketCount--; - } - - if (bracketCount == 0) { - index = i; - closing.setLine(line); - break; - } - } - if(bracketCount == 0) { - break; - } - index = -1; - } - - closing.setIndex(index); - return closing; - } - - /** - * Index and line of a bracket. - * @checkstyle JavadocMethod (100 lines) - */ - static class Bracket { - - /** - * Line where the bracket is. - */ - private YamlLine foundLine; - - /** - * Index of the bracket. - */ - private int idx; - - /** - * Ctor. - * @param index Index. - * @param line Line. - */ - Bracket(final int index, final YamlLine line) { - this.foundLine = line; - this.idx = index; - } - - public YamlLine getLine() { - return this.foundLine; - } - - public void setLine(final YamlLine line) { - this.foundLine = line; - } - - public int getIndex() { - return this.idx; - } - - public void setIndex(final int index) { - this.idx = index; - } - } -} diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadFlowMapping.java b/src/main/java/com/amihaiemil/eoyaml/ReadFlowMapping.java index cdff9853..49e69c8b 100644 --- a/src/main/java/com/amihaiemil/eoyaml/ReadFlowMapping.java +++ b/src/main/java/com/amihaiemil/eoyaml/ReadFlowMapping.java @@ -43,10 +43,8 @@ * @since 8.0.0 * @todo #601:30min Modify the yaml printing visitor, so it prints the flow * nodes in Flow style instead of block. - * @todo #607:60min Refactor this class to not use FoldedFlowLines anymore. - * FoldedFlowLines is deprecated and the collapsing/folding logic is now - * implemented with {@link CollapsedFlowLines}. ReadFlowSequence needs the - * same refactoring after that. + * @todo #615:60min Implement the comment() method properly (at the moment + * it always assumes there is no comment). */ final class ReadFlowMapping extends BaseYamlMapping { @@ -72,10 +70,11 @@ final class ReadFlowMapping extends BaseYamlMapping { * Ctor. * @param previous Line just before the start of this flow mapping. * @param lines All lines of the YAML document. + * @checkstyle AvoidInlineConditionals (30 lines) */ ReadFlowMapping(final YamlLine previous, final AllYamlLines lines) { this( - new FoldedFlowLines( + new CollapsedFlowLines( new Skip( lines, line -> line.number() <= previous.number(), @@ -87,7 +86,7 @@ final class ReadFlowMapping extends BaseYamlMapping { ), '{', '}' - ).iterator().next() + ).line(previous.number() < 0 ? 0 : previous.number() + 1) ); } @@ -98,6 +97,7 @@ final class ReadFlowMapping extends BaseYamlMapping { */ ReadFlowMapping(final YamlLine folded) { this.entries = new StringEntries(folded); + System.out.println("FLOW LINE: " + folded.value()); this.folded = folded; } diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadFlowSequence.java b/src/main/java/com/amihaiemil/eoyaml/ReadFlowSequence.java index 4fa75865..1ebec367 100644 --- a/src/main/java/com/amihaiemil/eoyaml/ReadFlowSequence.java +++ b/src/main/java/com/amihaiemil/eoyaml/ReadFlowSequence.java @@ -62,10 +62,11 @@ final class ReadFlowSequence extends BaseYamlSequence { * Ctor. * @param previous Line just before the start of this flow sequence. * @param lines All lines of the YAML document. + * @checkstyle AvoidInlineConditionals (30 lines) */ ReadFlowSequence(final YamlLine previous, final AllYamlLines lines) { this( - new FoldedFlowLines( + new CollapsedFlowLines( new Skip( lines, line -> line.number() <= previous.number(), @@ -77,7 +78,7 @@ final class ReadFlowSequence extends BaseYamlSequence { ), '[', ']' - ).iterator().next() + ).line(previous.number() < 0 ? 0 : previous.number() + 1) ); } diff --git a/src/main/java/com/amihaiemil/eoyaml/YamlLines.java b/src/main/java/com/amihaiemil/eoyaml/YamlLines.java index 8b88a8a9..95c9a5ae 100644 --- a/src/main/java/com/amihaiemil/eoyaml/YamlLines.java +++ b/src/main/java/com/amihaiemil/eoyaml/YamlLines.java @@ -44,7 +44,9 @@ interface YamlLines extends Iterable { * the call to this method should always be delegated down to the * base method, with no changes. * @return The original YamlLines as a Collection. + * @todo #615:30min Remove this method, it is not needed anymore. */ + @Deprecated Collection original(); /** @@ -68,19 +70,21 @@ interface YamlLines extends Iterable { * @return YamlLine or throws {@link IndexOutOfBoundsException}. */ default YamlLine line(final int number) { - final Collection lines = this.original(); - if(number < 0 && lines.size() > 0) { - return lines.iterator().next(); + int linesNr = 0; + final Iterator iterator = this.iterator(); + if(number < 0 && iterator.hasNext()) { + return iterator.next(); } - for(final YamlLine line : lines){ + for(final YamlLine line : this){ if(line.number() == number) { return line; } + linesNr++; } throw new IllegalArgumentException( "Couldn't find line " + number + ". Pay attention, there are " - + this.original().size() + " lines!"); + + linesNr + " lines!"); } } diff --git a/src/test/java/com/amihaiemil/eoyaml/FlowOnOneLineTestCase.java b/src/test/java/com/amihaiemil/eoyaml/FlowOnOneLineTestCase.java deleted file mode 100644 index 2e99f846..00000000 --- a/src/test/java/com/amihaiemil/eoyaml/FlowOnOneLineTestCase.java +++ /dev/null @@ -1,156 +0,0 @@ -/** - * Copyright (c) 2016-2024, Mihai Emil Andronache - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ -package com.amihaiemil.eoyaml; - -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; - -/** - * Unit tests for {@link FlowOnOneLine}. - * @author Mihai Andronache (amihaiemil@gmail.com) - * @version $Id$ - * @since 6.0.0 - */ -public final class FlowOnOneLineTestCase { - - /** - * FlowOnOneLine can fold empty lines. - */ - @Test - public void foldsEmptyLines() { - final YamlLine folded = new FlowOnOneLine( - new FoldedFlowLines.Bracket(0, new RtYamlLine("[", 0)), - new FoldedFlowLines.Bracket(0, new RtYamlLine("]", 1)), - new AllYamlLines(new ArrayList<>()) - ); - MatcherAssert.assertThat( - folded.trimmed(), - Matchers.isEmptyString() - ); - } - - /** - * FlowOnOneLine can fold a single line. - */ - @Test - public void foldsOneLine() { - final YamlLine original = new RtYamlLine("- [a, b, c]", 0); - final YamlLine folded = new FlowOnOneLine( - new FoldedFlowLines.Bracket(2, original), - new FoldedFlowLines.Bracket(10, original), - new AllYamlLines(Arrays.asList(original)) - ); - MatcherAssert.assertThat( - folded.trimmed(), - Matchers.equalTo("[a, b, c]") - ); - } - - /** - * FlowOnOneLine can fold only the first line - * (the whole interval is there). - */ - @Test - public void foldsFirstLine() { - final YamlLine first = new RtYamlLine("seq: [a, b, c]", 0); - final YamlLine folded = new FlowOnOneLine( - new FoldedFlowLines.Bracket(5, first), - new FoldedFlowLines.Bracket(13, first), - new AllYamlLines( - Arrays.asList( - first, - new RtYamlLine("key: [value, ada]", 1), - new RtYamlLine("key2: value2", 2) - ) - ) - ); - MatcherAssert.assertThat( - folded.trimmed(), - Matchers.equalTo("[a, b, c]") - ); - } - - /** - * FlowOnOneLine can fold a few lines if the interval spans on - * more of them. - */ - @Test - public void foldsSomeLines() { - final YamlLine first = new RtYamlLine("seq: [a, ", 0); - final YamlLine last = new RtYamlLine("c]", 2); - final YamlLine folded = new FlowOnOneLine( - new FoldedFlowLines.Bracket(5, first), - new FoldedFlowLines.Bracket(1, last), - new AllYamlLines( - Arrays.asList( - first, - new RtYamlLine(" b,", 1), - last, - new RtYamlLine("key: [value, ada]", 3), - new RtYamlLine("key2: value2", 4) - ) - ) - ); - MatcherAssert.assertThat( - folded.trimmed(), - Matchers.equalTo("[a,b,c]") - ); - } - - /** - * FlowOnOneLine can fold all the lines, if the interval - * spans on all of them. - */ - @Test - public void foldsAllLines() { - final YamlLine first = new RtYamlLine("seq: [a, ", 0); - final YamlLine last = new RtYamlLine("g]", 4); - final YamlLine folded = new FlowOnOneLine( - new FoldedFlowLines.Bracket(5, first), - new FoldedFlowLines.Bracket(1, last), - new AllYamlLines( - Arrays.asList( - first, - new RtYamlLine(" b,", 1), - new RtYamlLine(" [c, d],", 2), - new RtYamlLine("{e: f},", 3), - last - ) - ) - ); - MatcherAssert.assertThat( - folded.trimmed(), - Matchers.equalTo("[a,b,[c, d],{e: f},g]") - ); - } - -}