-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AbstractYamlMapping and AbstractYamlSequence
- Loading branch information
1 parent
27faf37
commit 05099b5
Showing
15 changed files
with
304 additions
and
166 deletions.
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
src/main/java/com/amihaiemil/camel/AbstractYamlMapping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
118
src/main/java/com/amihaiemil/camel/AbstractYamlSequence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.