Skip to content

Commit

Permalink
AbstractYamlMapping and AbstractYamlSequence
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Feb 27, 2017
1 parent 27faf37 commit 05099b5
Show file tree
Hide file tree
Showing 15 changed files with 304 additions and 166 deletions.
137 changes: 137 additions & 0 deletions src/main/java/com/amihaiemil/camel/AbstractYamlMapping.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/**
* Copyright (c) 2016-2017, 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.camel;

import java.util.Iterator;
import java.util.Set;

/**
* AbstractYamlMapping implementing methods which should be the same across
* all final implementations of YamlMapping.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 1.0.0
*/
abstract class AbstractYamlMapping implements YamlMapping{

/**
* Return the keys' set of this mapping.
* @return Set of YamlNode keys.
*/
abstract Set<YamlNode> keys();

@Override
public int hashCode() {
int hash = 0;
for(final YamlNode key : this.keys()) {
hash += key.hashCode();
}
for(final YamlNode value : this.children()) {
hash += value.hashCode();
}
return hash;
}

/**
* Equals method for YamlMapping. It returns true if the compareTo(...)
* method returns 0.
* @param other The YamlMapping to which this is compared.
* @return True or false.
*/
@Override
public boolean equals(final Object other) {
final boolean result;
if (other == null || !(other instanceof YamlMapping)) {
result = false;
} else if (this == other) {
result = true;
} else {
result = this.compareTo((YamlMapping) other) == 0;
}
return result;
}

/**
* Compare this Mapping to another node.<br><br>
*
* A Mapping is always considered greater than a Scalar or a Sequence.<br>
*
* If o is a Mapping, their integer lengths are compared - the one with
* the greater length is considered greater. If the lengths are equal,
* then the 2 Mappings are equal if all elements are equal (K==K and V==V).
* If the elements are not identical, the comparison of the first unequal
* elements is returned.
*
* @param other The other AbstractNode.
* @checkstyle NestedIfDepth (100 lines)
* @checkstyle ExecutableStatementCount (100 lines)
* @return
* a value < 0 if this < o <br>
* 0 if this == o or <br>
* a value > 0 if this > o
*/
@Override
public int compareTo(final YamlNode other) {
int result = 0;
if (other == null || !(other instanceof YamlMapping)) {
result = 1;
} else if (this != other) {
final AbstractYamlMapping map = (AbstractYamlMapping) other;
final Set<YamlNode> keys = this.keys();
final Set<YamlNode> otherKeys = map.keys();
if(keys.size() > otherKeys.size()) {
result = 1;
} else if (keys.size() < otherKeys.size()) {
result = -1;
} else {
final Iterator<YamlNode> keysIt = keys.iterator();
final Iterator<YamlNode> otherKeysIt = otherKeys.iterator();
final Iterator<YamlNode> values = this.children().iterator();
final Iterator<YamlNode> otherVals = map.children().iterator();
int keysComparison;
int valuesComparison;
while(values.hasNext()) {
keysComparison = keysIt.next()
.compareTo(otherKeysIt.next());
valuesComparison = values.next()
.compareTo(otherVals.next());
if(keysComparison != 0) {
result = keysComparison;
break;
}
if(valuesComparison != 0) {
result = valuesComparison;
break;
}
}
}
}
return result;
}

}
118 changes: 118 additions & 0 deletions src/main/java/com/amihaiemil/camel/AbstractYamlSequence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
* Copyright (c) 2016-2017, 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.camel;

import java.util.Collection;
import java.util.Iterator;

/**
* AbstractYamlSequence implementing methods which should be the same across
* all final implementations of YamlSequence.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 1.0.0
*/
abstract class AbstractYamlSequence implements YamlSequence {

@Override
public int hashCode() {
int hash = 0;
for(final YamlNode node : this.children()) {
hash += node.hashCode();
}
return hash;
}

/**
* Equals method for YamlSequence. It returns true if the compareTo(...)
* method returns 0.
* @param other The YamlSequence to which this is compared.
* @return True or false
*/
@Override
public boolean equals(final Object other) {
final boolean result;
if (other == null || !(other instanceof YamlSequence)) {
result = false;
} else if (this == other) {
result = true;
} else {
result = this.compareTo((YamlSequence) other) == 0;
}
return result;
}

/**
* Compare this Sequence to another node.<br><br>
*
* A Sequence is always considered greater than a Scalar and less than
* a Mapping.<br>
*
* If o is a Sequence, their integer lengths are compared - the one with
* the greater length is considered greater. If the lengths are equal,
* then the 2 Sequences are equal if all elements are equal. If the
* elements are not identical, the comparison of the first unequal
* elements is returned.
*
* @param other The other AbstractNode.
* @checkstyle NestedIfDepth (100 lines)
* @return
* a value < 0 if this < o <br>
* 0 if this == o or <br>
* a value > 0 if this > o
*/
@Override
public int compareTo(final YamlNode other) {
int result = 0;
if (other == null || other instanceof Scalar) {
result = 1;
} else if (other instanceof YamlMapping) {
result = -1;
} else if (this != other) {
final Collection<YamlNode> nodes = this.children();
nodes.hashCode();
final Collection<YamlNode> others = other.children();
if(nodes.size() > others.size()) {
result = 1;
} else if (nodes.size() < others.size()) {
result = -1;
} else {
final Iterator<YamlNode> ietrator = others.iterator();
final Iterator<YamlNode> here = nodes.iterator();
while(ietrator.hasNext()) {
result = here.next().compareTo(ietrator.next());
if(result != 0) {
break;
}
}
}
}
return result;
}

}
14 changes: 8 additions & 6 deletions src/main/java/com/amihaiemil/camel/ReadYamlMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
* YamlMapping read from somewhere.
Expand All @@ -40,7 +41,7 @@
* @todo #73:30min/DEV Continue implementing and unit-testing the methods
* from this class one by one.
*/
final class ReadYamlMapping implements YamlMapping {
final class ReadYamlMapping extends AbstractYamlMapping {

/**
* Lines read.
Expand Down Expand Up @@ -134,11 +135,6 @@ public String indent(final int indentation) {
return null;
}

@Override
public int compareTo(final YamlNode other) {
return 0;
}

/**
* The YamlNode value associated with a String key.
* @param key String key.
Expand All @@ -163,4 +159,10 @@ private YamlNode nodeValue(final String key, final boolean map) {
}
return value;
}

@Override
Set<YamlNode> keys() {
// TODO Auto-generated method stub
return null;
}
}
12 changes: 1 addition & 11 deletions src/main/java/com/amihaiemil/camel/ReadYamlSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @todo #64:30min/DEV Continue implementing and unit testing this class,
* one method at a time.
*/
final class ReadYamlSequence implements YamlSequence {
final class ReadYamlSequence extends AbstractYamlSequence {

/**
* Lines read.
Expand All @@ -35,16 +35,6 @@ public String indent(final int indentation) {
return null;
}

@Override
public int compareTo(final YamlNode other) {
return 0;
}

@Override
public int size() {
return 0;
}

@Override
public YamlMapping yamlMapping(final int index) {
return null;
Expand Down

0 comments on commit 05099b5

Please sign in to comment.