This is an automated email from the ASF dual-hosted git repository. okram pushed a commit to branch tp4 in repository https://gitbox.apache.org/repos/asf/tinkerpop.git
The following commit(s) were added to refs/heads/tp4 by this push: new 43ab07f added hashCode() and equals() to Instruction and SourceInstruction. Wrote unit tests cases validating methods and hash/equals. 43ab07f is described below commit 43ab07f5cd525897ae81184e8cdd75421f5cb4ff Author: Marko A. Rodriguez <okramma...@gmail.com> AuthorDate: Fri Mar 22 14:12:13 2019 -0600 added hashCode() and equals() to Instruction and SourceInstruction. Wrote unit tests cases validating methods and hash/equals. --- .../tinkerpop/machine/bytecode/Bytecode.java | 16 +++++- .../tinkerpop/machine/bytecode/Instruction.java | 16 ++++++ .../machine/bytecode/SourceInstruction.java | 17 +++++- .../machine/bytecode/InstructionTest.java | 61 ++++++++++++++++++++++ .../machine/bytecode/SourceInstructionTest.java | 57 ++++++++++++++++++++ 5 files changed, 164 insertions(+), 3 deletions(-) diff --git a/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/bytecode/Bytecode.java b/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/bytecode/Bytecode.java index b0bf82f..bfcb930 100644 --- a/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/bytecode/Bytecode.java +++ b/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/bytecode/Bytecode.java @@ -26,10 +26,10 @@ import java.util.List; /** * @author Marko A. Rodriguez (http://markorodriguez.com) */ -public final class Bytecode<C> implements Cloneable { +public final class Bytecode<C> implements Cloneable { // todo: serializable? - private List<Instruction<C>> instructions = new ArrayList<>(); private List<SourceInstruction> sourceInstructions = new ArrayList<>(); + private List<Instruction<C>> instructions = new ArrayList<>(); public void addSourceInstruction(final String op, final Object... args) { this.sourceInstructions.add(new SourceInstruction(op, args)); @@ -60,6 +60,18 @@ public final class Bytecode<C> implements Cloneable { } @Override + public int hashCode() { + return this.sourceInstructions.hashCode() ^ this.instructions.hashCode(); + } + + @Override + public boolean equals(final Object object) { + return object instanceof Bytecode && + this.instructions.equals(((Bytecode) object).instructions) && + this.sourceInstructions.equals(((Bytecode) object).sourceInstructions); + } + + @Override public String toString() { return this.instructions.toString(); } diff --git a/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/bytecode/Instruction.java b/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/bytecode/Instruction.java index 4136d2e..1c08a58 100644 --- a/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/bytecode/Instruction.java +++ b/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/bytecode/Instruction.java @@ -73,6 +73,22 @@ public final class Instruction<C> { } @Override + public int hashCode() { + return this.coefficient.hashCode() ^ this.op.hashCode() ^ Arrays.hashCode(this.args) ^ this.labels.hashCode(); + } + + @Override + public boolean equals(final Object object) { + if (!(object instanceof Instruction)) + return false; + final Instruction other = (Instruction) object; + return this.op.equals(other.op) && + Arrays.equals(this.args, other.args) && + this.coefficient.equals(other.coefficient) && + this.labels.equals(other.labels); + } + + @Override public String toString() { return StringFactory.makeInstructionString(this); } diff --git a/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/bytecode/SourceInstruction.java b/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/bytecode/SourceInstruction.java index ce6c944..6b484fc 100644 --- a/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/bytecode/SourceInstruction.java +++ b/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/bytecode/SourceInstruction.java @@ -20,6 +20,8 @@ package org.apache.tinkerpop.machine.bytecode; import org.apache.tinkerpop.machine.util.StringFactory; +import java.util.Arrays; + /** * @author Marko A. Rodriguez (http://markorodriguez.com) */ @@ -28,7 +30,7 @@ public final class SourceInstruction { private final String op; private final Object[] args; - public SourceInstruction(final String op, final Object[] args) { + public SourceInstruction(final String op, final Object... args) { this.op = op; this.args = args; } @@ -42,6 +44,19 @@ public final class SourceInstruction { } @Override + public int hashCode() { + return this.op.hashCode() ^ Arrays.hashCode(this.args); + } + + @Override + public boolean equals(final Object object) { + if (!(object instanceof SourceInstruction)) + return false; + final SourceInstruction other = (SourceInstruction) object; + return this.op.equals(other.op) && Arrays.equals(this.args, other.args); + } + + @Override public String toString() { return StringFactory.makeSourceInstructionString(this); } diff --git a/java/machine/machine-core/src/test/java/org/apache/tinkerpop/machine/bytecode/InstructionTest.java b/java/machine/machine-core/src/test/java/org/apache/tinkerpop/machine/bytecode/InstructionTest.java new file mode 100644 index 0000000..63736e9 --- /dev/null +++ b/java/machine/machine-core/src/test/java/org/apache/tinkerpop/machine/bytecode/InstructionTest.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tinkerpop.machine.bytecode; + +import org.apache.tinkerpop.machine.coefficient.LongCoefficient; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +/** + * @author Marko A. Rodriguez (http://markorodriguez.com) + */ +class InstructionTest { + + private final Instruction<Long> a = new Instruction<>(LongCoefficient.create(10L), "atest", new Object[]{1, 2, 3}); + private final Instruction<Long> b = new Instruction<>(LongCoefficient.create(10L), "atest", new Object[]{1, 2, 3}); + private final Instruction<Long> c = new Instruction<>(LongCoefficient.create(10L), "atest", new Object[]{1, 2}); + private final Instruction<Long> d = new Instruction<>(LongCoefficient.create(10L), "btest", new Object[]{1, 2, 3}); + private final Instruction<Long> e = new Instruction<>(LongCoefficient.create(1L), "btest", new Object[]{1, 2, 3}); + + @Test + void testMethods() { + assertEquals("atest", a.op()); + assertEquals("btest", d.op()); + assertEquals(1, a.args()[0]); + assertEquals(3, d.args()[2]); + assertEquals(2, c.args()[1]); + } + + @Test + void testHashEquals() { + assertEquals(a, b); + assertEquals(a.hashCode(), b.hashCode()); + assertNotEquals(a, c); + assertNotEquals(b, c); + assertNotEquals(a.hashCode(), c.hashCode()); + assertNotEquals(a, d); + assertNotEquals(c, d); + assertNotEquals(a.hashCode(), d.hashCode()); + assertNotEquals(c.hashCode(), d.hashCode()); + assertNotEquals(d, e); + assertNotEquals(d.hashCode(), e.hashCode()); + } +} diff --git a/java/machine/machine-core/src/test/java/org/apache/tinkerpop/machine/bytecode/SourceInstructionTest.java b/java/machine/machine-core/src/test/java/org/apache/tinkerpop/machine/bytecode/SourceInstructionTest.java new file mode 100644 index 0000000..50b4c87 --- /dev/null +++ b/java/machine/machine-core/src/test/java/org/apache/tinkerpop/machine/bytecode/SourceInstructionTest.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tinkerpop.machine.bytecode; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +/** + * @author Marko A. Rodriguez (http://markorodriguez.com) + */ +class SourceInstructionTest { + + private final SourceInstruction a = new SourceInstruction("atest", new Object[]{1, 2, 3}); + private final SourceInstruction b = new SourceInstruction("atest", new Object[]{1, 2, 3}); + private final SourceInstruction c = new SourceInstruction("atest", new Object[]{1, 2}); + private final SourceInstruction d = new SourceInstruction("btest", new Object[]{1, 2, 3}); + + @Test + void testMethods() { + assertEquals("atest", a.op()); + assertEquals("btest", d.op()); + assertEquals(1, a.args()[0]); + assertEquals(3, d.args()[2]); + assertEquals(2, c.args()[1]); + } + + @Test + void testHashEquals() { + assertEquals(a, b); + assertEquals(a.hashCode(), b.hashCode()); + assertNotEquals(a, c); + assertNotEquals(b, c); + assertNotEquals(a.hashCode(), c.hashCode()); + assertNotEquals(a, d); + assertNotEquals(c, d); + assertNotEquals(a.hashCode(), d.hashCode()); + assertNotEquals(c.hashCode(), d.hashCode()); + } +}