Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/cz/jirutka/rsql/parser/ast/Arity.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ public interface Arity {
*/
int max();

/**
* Compares this object with the specified object for equality. Two objects are considered equal if they represent
* the same Arity, meaning their min and max are equal.
*
* @param o The object to be compared for equality with this object.
* @return {@code true} if the specified object is equal to this object; {@code false} otherwise.
*/
@Override
boolean equals(Object o);

/**
* Returns the hash code value for this object. The hash code is computed based on the properties of the object and
* ensures that equal objects have the same hash code.
*
* @return The hash code value for this object.
*/
@Override
int hashCode();

/**
* Creates arity with given {@code min} and {@code max}.
*
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/cz/jirutka/rsql/parser/ast/DynamicArity.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,19 @@ public int min() {
public int max() {
return max;
}

@Override
public boolean equals(Object o) {
if (!(o instanceof Arity)) {
return false;
}

Arity that = (Arity) o;
return min == that.min() && max == that.max();
}

@Override
public int hashCode() {
return Integer.hashCode(min) + (31 * Integer.hashCode(max));
}
}
15 changes: 15 additions & 0 deletions src/main/java/cz/jirutka/rsql/parser/ast/NAry.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,19 @@ public int min() {
public int max() {
return n;
}

@Override
public boolean equals(Object o) {
if (!(o instanceof Arity)) {
return false;
}

Arity that = (Arity) o;
return n == that.min() && n == that.max();
}

@Override
public int hashCode() {
return Integer.hashCode(n);
}
}
18 changes: 18 additions & 0 deletions src/test/groovy/cz/jirutka/rsql/parser/ast/DynamicAritySpec.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cz.jirutka.rsql.parser.ast

import nl.jqno.equalsverifier.EqualsVerifier
import spock.lang.Specification

class DynamicAritySpec extends Specification {
Expand Down Expand Up @@ -30,4 +31,21 @@ class DynamicAritySpec extends Specification {
actual.min() == min
actual.max() == max
}

def 'Should be equal with n-ary'() {
expect:
//noinspection GrEqualsBetweenInconvertibleTypes
new DynamicArity(0, 0) == new NAry(0)
}

def 'Should not be equal with n-ary'() {
expect:
//noinspection GrEqualsBetweenInconvertibleTypes
new DynamicArity(0, 0) != new NAry(1)
}

def 'Should implement equals and hashCode'() {
expect:
EqualsVerifier.forClass(DynamicArity).verify()
}
}
21 changes: 21 additions & 0 deletions src/test/groovy/cz/jirutka/rsql/parser/ast/NArySpec.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cz.jirutka.rsql.parser.ast

import nl.jqno.equalsverifier.EqualsVerifier
import spock.lang.Specification

class NArySpec extends Specification {
Expand All @@ -23,4 +24,24 @@ class NArySpec extends Specification {
where:
n << [0, 1, 2, 32]
}

def 'Should be equal with dynamic arity'() {
expect:
//noinspection GrEqualsBetweenInconvertibleTypes
new NAry(0) == new DynamicArity(0, 0)
}

def 'Should be not equal with dynamic arity'() {
expect:
//noinspection GrEqualsBetweenInconvertibleTypes
new NAry(0) != a

where:
a << [new DynamicArity(0, 1), new DynamicArity(1, 1)]
}

def 'Should implement equals and hashCode'() {
expect:
EqualsVerifier.forClass(NAry).verify()
}
}
Loading