Skip to content

Commit

Permalink
Update equals() method to account for the type of T
Browse files Browse the repository at this point in the history
Also reflects the equals() change to hashCode() and adds tests for both
implementations.
  • Loading branch information
bryanhuhta committed Dec 16, 2024
1 parent 58da7a0 commit f477f44
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.pyroscope.labels;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;

class Ref<T> {
Expand All @@ -16,13 +17,16 @@ public Ref(T val, Long id) {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Ref<T> valueRef = (Ref<T>) o;
return id.equals(valueRef.id);

Ref<?> valueRef = (Ref<?>) o;
if (val.getClass() != o.getClass()) return false;

return id.equals(valueRef.id) && val.equals(valueRef.val);
}

@Override
public int hashCode() {
return id.hashCode();
return Objects.hash(id, val);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.pyroscope.labels;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class RefTest {
@Test
void testCreateRef() {
Ref<String> ref = new Ref<>("test", 1L);
assertEquals("test", ref.val);
assertEquals(1L, ref.id);
assertEquals(1L, ref.refCount.get());
}

@Test
@SuppressWarnings("unlikely-arg-type")
void testEquals() {
Ref<String> ref1 = new Ref<>("test", 1L);
assertTrue(ref1.equals(ref1));
assertFalse(ref1.equals(null));
assertFalse(ref1.equals(new Integer(3)));
assertFalse(ref1.equals(new Ref<Integer>(3, 1L)));
}

@Test
void testHashCode() {
Ref<String> ref1 = new Ref<>("test", 1L);
assertEquals(3557490, ref1.hashCode());
}
}

0 comments on commit f477f44

Please sign in to comment.