Skip to content

Commit

Permalink
Ensure hashCode and equivalence tests are order independent.
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Syme <[email protected]>
  • Loading branch information
robsyme committed Aug 25, 2024
1 parent b28eeea commit 1daf22a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
25 changes: 23 additions & 2 deletions modules/nextflow/src/main/groovy/nextflow/util/ArrayBag.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,35 @@ class ArrayBag<E> implements Bag<E>, List<E>, KryoSerializable {

@Override
int hashCode() {
target.hashCode()
int hash = 0
target.each { element ->
hash ^= (element != null ? element.hashCode() : 0)
}
return hash
}

@Override
boolean equals(Object o) {
target.equals(o)
if (this.is(o)) return true
if (!(o instanceof ArrayBag)) return false

ArrayBag other = (ArrayBag) o
def thisFrequencyMap = createFrequencyMap(this.target)
def otherFrequencyMap = createFrequencyMap(other.target)

return thisFrequencyMap == otherFrequencyMap
}

private static Map<Object, Integer> createFrequencyMap(List list) {
Map<Object, Integer> frequencyMap = [:]
list.each { element ->
if (element != null) {
frequencyMap[element] = frequencyMap.getOrDefault(element, 0) + 1
}
}
return frequencyMap
}

// E getAt( int index ) {
// target.get(index)
// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,20 @@ class JoinOpTest extends Specification {
!result.isEmpty()
}

def 'should differentiate nonidentical ArrayBags join key' () {
given:
def key1 = new ArrayBag(["key", "key", "quay"])
def key2 = new ArrayBag(["quay", "quay", "key"])
def ch1 = Channel.of([key1, "foo"])
def ch2 = Channel.of([key2, "bar"])

when:
def op = new JoinOp(ch1 as DataflowReadChannel, ch2 as DataflowReadChannel)
List result = op.apply().toList().getVal()

then:
result.isEmpty()
}

def 'should not fail on mismatches' () {
given:
Expand Down

0 comments on commit 1daf22a

Please sign in to comment.