Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Cleanup T2 equals() and add test
Browse files Browse the repository at this point in the history
t2gran committed Nov 29, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 05023d1 commit 7850a61
Showing 2 changed files with 35 additions and 16 deletions.
19 changes: 3 additions & 16 deletions src/main/java/org/opentripplanner/common/model/T2.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.opentripplanner.common.model;

import java.io.Serializable;
import java.util.Objects;

/**
* An ordered pair of objects of potentially different types
@@ -25,22 +26,8 @@ public int hashCode() {
@Override
public boolean equals(Object object) {
if (!(object instanceof T2)) { return false; }

var other = (T2) object;

if (first == null) {
if (other.first != null) { return false; }
} else {
if (!first.equals(other.first)) { return false; }
}

if (second == null) {
if (other.second != null) { return false; }
} else {
if (!second.equals(other.second)) { return false; }
}

return true;
var other = (T2<?,?>) object;
return Objects.equals(first, other.first) && Objects.equals(second, other.second);
}

@Override
32 changes: 32 additions & 0 deletions src/test/java/org/opentripplanner/common/model/T2Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.opentripplanner.common.model;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import org.junit.jupiter.api.Test;

class T2Test {

@Test
void testEquals() {
var subject = new T2<>("Alf", 1);

assertEquals(new T2<>("Alf", 1), subject);
assertEquals(new T2<>("Alf", 1).hashCode(), subject.hashCode());

// first is different
assertNotEquals(new T2<>("Alfi", 1), subject);

// second is different
assertNotEquals(new T2<>("Alf", 2), subject);

// Different types, should not fail with exception
assertNotEquals(new T2<>(1, "Alf"), subject);
}

@Test
void testToString() {
var subject = new T2<>("Alf", 1);
assertEquals("T2(Alf, 1)", subject.toString());
}
}

0 comments on commit 7850a61

Please sign in to comment.