Skip to content

Commit

Permalink
#98 toCollection()
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jul 13, 2020
1 parent b046e68 commit 00c9b13
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
33 changes: 21 additions & 12 deletions src/main/java/org/xembly/Directives.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import lombok.EqualsAndHashCode;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
Expand Down Expand Up @@ -102,14 +101,13 @@ public final class Directives implements Iterable<Directive> {
/**
* List of directives.
*/
private final transient Collection<Directive> all =
new CopyOnWriteArrayList<Directive>();
private final transient Collection<Directive> all;

/**
* Public ctor.
*/
public Directives() {
this(Collections.<Directive>emptyList());
this(Collections.emptyList());
}

/**
Expand All @@ -125,8 +123,9 @@ public Directives(final String text) throws SyntaxException {
* Public ctor.
* @param dirs Directives
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
public Directives(final Iterable<Directive> dirs) {
this.append(dirs);
this.all = Directives.toCollection(dirs);
}

@Override
Expand Down Expand Up @@ -244,11 +243,9 @@ public static Iterable<Directive> copyOf(final Node node) {
* @since 0.11
*/
public Directives append(final Iterable<Directive> dirs) {
final Collection<Directive> list = new LinkedList<Directive>();
for (final Directive dir : dirs) {
list.add(dir);
synchronized (this.all) {
this.all.addAll(Directives.toCollection(dirs));
}
this.all.addAll(list);
return this;
}

Expand Down Expand Up @@ -575,11 +572,23 @@ private static Collection<Directive> parse(final String script)
final XemblyParser parser = new XemblyParser(tokens);
try {
return parser.directives();
} catch (final RecognitionException ex) {
throw new SyntaxException(script, ex);
} catch (final ParsingException ex) {
} catch (final RecognitionException | ParsingException ex) {
throw new SyntaxException(script, ex);
}
}

/**
* Iterable to collection.
* @param itr Iterable
* @param <T> The type
* @return Collection
*/
private static <T> Collection<T> toCollection(final Iterable<T> itr) {
final Collection<T> col = new LinkedList<>();
for (final T item : itr) {
col.add(item);
}
return col;
}

}
40 changes: 26 additions & 14 deletions src/test/java/org/xembly/DirectivesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,19 @@ public void performsFullScaleModifications() throws Exception {
public void copiesExistingNode() throws Exception {
final Document dom = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().newDocument();
new Xembler(
new Directives().add("dudes").append(
Directives.copyOf(
new XMLDocument(
StringUtils.join(
"<jeff name='Jeffrey'><first/><second/>",
"<?some-pi test?>",
"<file a='x'><f><name>\u20ac</name></f></file>",
"<!-- some comment -->",
"<x><![CDATA[hey you]]></x> </jeff>"
)
).node()
final Iterable<Directive> copy = Directives.copyOf(
new XMLDocument(
StringUtils.join(
"<jeff name='Jeffrey'><first/><second/>",
"<?some-pi test?>",
"<file a='x'><f><name>\u20ac</name></f></file>",
"<!-- some comment -->",
"<x><![CDATA[hey you]]></x> </jeff>"
)
)
).apply(dom);
).node()
);
MatcherAssert.assertThat(copy, Matchers.iterableWithSize(19));
new Xembler(new Directives().add("dudes").append(copy)).apply(dom);
MatcherAssert.assertThat(
XhtmlMatchers.xhtml(dom),
XhtmlMatchers.hasXPaths(
Expand Down Expand Up @@ -339,4 +337,18 @@ public void addsComments() throws Exception {
);
}

/**
* Directives can append.
* @throws Exception If some problem inside
*/
@Test
public void appendsDirs() throws Exception {
MatcherAssert.assertThat(
new Directives().add("0").append(
new Directives().add("1")
).add("2").toString(),
Matchers.equalTo("ADD \"0\";ADD \"1\";ADD \"2\";")
);
}

}

0 comments on commit 00c9b13

Please sign in to comment.