-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Minor QoL clean up #175
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/.idea/ | ||
/.vscode/ | ||
|
||
# Ignore Gradle project-specific cache directory | ||
.gradle | ||
|
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> { | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure we want this change. The value here can be a string or a container type (we seem to be using maps as T), and if it is a container type we'll end up doing a deep traversal for Semantically, ids should be unique and generated based on a similar (and somewhat expensive) map lookup, so we should be fine with using the id only. @korniltsev can you confirm this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In 32f2f77 I reverted this change. I did this in the interest of keeping the changes in this PR non-controversial. I mostly wanted this PR to clean up a few untidy spots in the code base. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I confirm the id already represents the contents of the value which is uniquely stored in a mp Aleks pointed. It will not hurt to hash/equals the value, but it is also not needed. |
||
} | ||
|
||
@Override | ||
|
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()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note this uses
?
instead ofT
. I gather that?
is somewhat safer when casting generic classes.