Skip to content

Commit

Permalink
Sequence code + unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Jan 15, 2017
1 parent e53fc94 commit 9c3a2da
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 26 deletions.
1 change: 0 additions & 1 deletion checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@
<module name="IllegalInstantiation"/>
<module name="IllegalTokenText" />
<module name="InnerAssignment"/>
<module name="MagicNumber"/>
<module name="MissingSwitchDefault"/>
<module name="ModifiedControlVariable"/>
<module name="SimplifyBooleanExpression"/>
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/amihaiemil/camel/AbstractNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
* @version $Id$
* @since 1.0.0
* @see http://yaml.org/spec/1.2/spec.html#node//
* @todo #7:1h/DEV Implement and unit test Sequence node.
* See specification: http://yaml.org/spec/1.2/spec.html#sequence//
* @todo #7:1h/DEV Implement and unit test Mapping node.
* See specification: http://yaml.org/spec/1.2/spec.html#mapping//
*/
abstract class AbstractNode implements Comparable<AbstractNode> {

Expand Down
25 changes: 20 additions & 5 deletions src/main/java/com/amihaiemil/camel/Mapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,28 @@ final class Mapping extends AbstractNode {
*/
private final Map<AbstractNode, AbstractNode> mappings =
new TreeMap<AbstractNode, AbstractNode>();

/**
* Ctor.
* @param parent The parent node of this mapping.
* @param entries Entries contained in this mapping.
*/
Mapping(final AbstractNode parent) {
Mapping(
final AbstractNode parent,
final Map<AbstractNode, AbstractNode> entries
) {
super(parent);
this.mappings.putAll(entries);
}

/**
* Fetch a value from this mapping.
* @param key Key of the entry.
* @return Corresponding node or null if there is no entry
* for the specified key.
*/
public AbstractNode get(final AbstractNode key) {
return this.mappings.get(key);
}

@Override
Expand All @@ -49,9 +64,9 @@ public Collection<AbstractNode> children() {
* @checkstyle NestedIfDepth (100 lines)
* @checkstyle ExecutableStatementCount (100 lines)
* @return
* -1 if this < o <br>
* a value < 0 if this < o <br>
* 0 if this == o or <br>
* 1 if this > o
* a value > 0 if this > o
*/
@Override
public int compareTo(final AbstractNode other) {
Expand All @@ -60,7 +75,7 @@ public int compareTo(final AbstractNode other) {
result = 1;
} else if (other instanceof Mapping) {
result = -1;
} else {
} else if (this != other) {
Mapping map = (Mapping) other;
if(this.mappings.size() > map.mappings.size()) {
result = 1;
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/com/amihaiemil/camel/Scalar.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,25 @@ public int hashCode() {
*
* @param other The other AbstractNode.
* @return
* -1 if this < o <br>
* a value < 0 if this < o <br>
* 0 if this == o or <br>
* 1 if this > o
* a value > 0 if this > o
*/
@Override
public int compareTo(final AbstractNode other) {
int result = -1;
if (other == null) {
if (this == other) {
result = 0;
} else if (other == null) {
result = 1;
} else if (other instanceof Scalar) {
result = this.value.compareTo(((Scalar) other).value);
}
return result;
}

@Override
public String toString() {
return this.value;
}
}
17 changes: 10 additions & 7 deletions src/main/java/com/amihaiemil/camel/Sequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,25 @@ final class Sequence extends AbstractNode{
/**
* Nodes in this sequence.
*/
private final List<AbstractNode> nodes =
new LinkedList<AbstractNode>();
private final List<AbstractNode> nodes = new LinkedList<>();

/**
* Ctor.
* @param parent The parent node of this sequence.
* @param elements Elements of this sequence.
*/
Sequence(final AbstractNode parent) {
Sequence(
final AbstractNode parent, final Collection<AbstractNode> elements
) {
super(parent);
this.nodes.addAll(elements);
Collections.sort(this.nodes);
}

@Override
public Collection<AbstractNode> children() {
final List<AbstractNode> children = new LinkedList<>();
children.addAll(this.nodes);
Collections.sort(children);
return children;
}

Expand All @@ -78,9 +81,9 @@ public Collection<AbstractNode> children() {
* @param other The other AbstractNode.
* @checkstyle NestedIfDepth (100 lines)
* @return
* -1 if this < o <br>
* a value < 0 if this < o <br>
* 0 if this == o or <br>
* 1 if this > o
* a value > 0 if this > o
*/
@Override
public int compareTo(final AbstractNode other) {
Expand All @@ -89,7 +92,7 @@ public int compareTo(final AbstractNode other) {
result = 1;
} else if (other instanceof Mapping) {
result = -1;
} else {
} else if (this != other) {
Sequence seq = (Sequence) other;
if(this.nodes.size() > seq.nodes.size()) {
result = 1;
Expand Down
63 changes: 57 additions & 6 deletions src/test/java/com/amihaiemil/camel/ScalarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
package com.amihaiemil.camel;

import static org.hamcrest.CoreMatchers.is;

import java.util.HashMap;
import java.util.LinkedList;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
Expand Down Expand Up @@ -63,12 +67,13 @@ public void hasNoChildren() {
scl.children(), Matchers.emptyIterable()
);
}

/**
* Scalar throws ISE if no parents are specified.
* Scalar throws ISE because the parent cannot be a Scalar.
* Scalars cannot have children!
*/
@Test (expected = IllegalStateException.class)
public void orphanForbidden() {
public void scalarParent() {
new Scalar(
new Scalar(Mockito.mock(AbstractNode.class), "a"), "orphan"
);
Expand Down Expand Up @@ -101,16 +106,62 @@ public void equalsAndHashCode() {
}

/**
* Scalar can compare itself to other Scalar.
* Scalar can compare itself to another Scalar.
*/
@Test
public void comparesToScalar() {
final Scalar first = new Scalar(
Scalar first = new Scalar(
Mockito.mock(AbstractNode.class), "java"
);
final Scalar second = new Scalar(
Scalar second = new Scalar(
Mockito.mock(AbstractNode.class), "java"
);
Scalar digits = new Scalar(
Mockito.mock(AbstractNode.class), "123"
);
Scalar otherDigits = new Scalar(
Mockito.mock(AbstractNode.class), "124"
);
MatcherAssert.assertThat(first.compareTo(first), Matchers.equalTo(0));
MatcherAssert.assertThat(first.compareTo(second), Matchers.equalTo(0));
MatcherAssert.assertThat(
first.compareTo(digits), Matchers.greaterThan(0)
);
MatcherAssert.assertThat(
first.compareTo(null), Matchers.greaterThan(0)
);
MatcherAssert.assertThat(
digits.compareTo(otherDigits), Matchers.lessThan(0)
);
}

/**
* Scalar can compare itself to a Mapping.
*/
@Test
public void comparesToMapping() {
Scalar first = new Scalar(
Mockito.mock(AbstractNode.class), "java"
);
Mapping map = new Mapping(
Mockito.mock(AbstractNode.class),
new HashMap<AbstractNode, AbstractNode>()
);
MatcherAssert.assertThat(first.compareTo(map), Matchers.lessThan(0));
}

/**
* Scalar can compare itself to a Sequence.
*/
@Test
public void comparesToSequence() {
Scalar first = new Scalar(
Mockito.mock(AbstractNode.class), "java"
);
Sequence seq = new Sequence(
Mockito.mock(AbstractNode.class),
new LinkedList<AbstractNode>()
);
MatcherAssert.assertThat(first.compareTo(seq), Matchers.lessThan(0));
}
}

0 comments on commit 9c3a2da

Please sign in to comment.