Skip to content

Commit ebaceb7

Browse files
author
Anuraag Agrawal
authored
Simplify ParentBased equals/hashcode and add coverage. (open-telemetry#2823)
1 parent 900c35e commit ebaceb7

4 files changed

Lines changed: 28 additions & 22 deletions

File tree

sdk/trace/src/main/java/io/opentelemetry/sdk/trace/samplers/ParentBasedSampler.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import io.opentelemetry.context.Context;
1313
import io.opentelemetry.sdk.trace.data.LinkData;
1414
import java.util.List;
15-
import java.util.Objects;
1615
import javax.annotation.Nullable;
1716
import javax.annotation.concurrent.Immutable;
1817

@@ -104,28 +103,20 @@ public boolean equals(Object o) {
104103

105104
ParentBasedSampler that = (ParentBasedSampler) o;
106105

107-
if (!Objects.equals(root, that.root)) {
108-
return false;
109-
}
110-
if (!Objects.equals(remoteParentSampled, that.remoteParentSampled)) {
111-
return false;
112-
}
113-
if (!Objects.equals(remoteParentNotSampled, that.remoteParentNotSampled)) {
114-
return false;
115-
}
116-
if (!Objects.equals(localParentSampled, that.localParentSampled)) {
117-
return false;
118-
}
119-
return Objects.equals(localParentNotSampled, that.localParentNotSampled);
106+
return root.equals(that.root)
107+
&& remoteParentSampled.equals(that.remoteParentSampled)
108+
&& remoteParentNotSampled.equals(that.remoteParentNotSampled)
109+
&& localParentSampled.equals(that.localParentSampled)
110+
&& localParentNotSampled.equals(that.localParentNotSampled);
120111
}
121112

122113
@Override
123114
public int hashCode() {
124-
int result = root != null ? root.hashCode() : 0;
125-
result = 31 * result + (remoteParentSampled != null ? remoteParentSampled.hashCode() : 0);
126-
result = 31 * result + (remoteParentNotSampled != null ? remoteParentNotSampled.hashCode() : 0);
127-
result = 31 * result + (localParentSampled != null ? localParentSampled.hashCode() : 0);
128-
result = 31 * result + (localParentNotSampled != null ? localParentNotSampled.hashCode() : 0);
115+
int result = root.hashCode();
116+
result = 31 * result + remoteParentSampled.hashCode();
117+
result = 31 * result + remoteParentNotSampled.hashCode();
118+
result = 31 * result + localParentSampled.hashCode();
119+
result = 31 * result + localParentNotSampled.hashCode();
129120
return result;
130121
}
131122
}

sdk/trace/src/test/java/io/opentelemetry/sdk/trace/samplers/AlwaysOffSamplerTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,9 @@ void parentSampled() {
6969
void getDescription() {
7070
assertThat(Sampler.alwaysOff().getDescription()).isEqualTo("AlwaysOffSampler");
7171
}
72+
73+
@Test
74+
void string() {
75+
assertThat(Sampler.alwaysOff().toString()).isEqualTo("AlwaysOffSampler");
76+
}
7277
}

sdk/trace/src/test/java/io/opentelemetry/sdk/trace/samplers/AlwaysOnSamplerTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,9 @@ void parentNotSampled() {
6969
void getDescription() {
7070
assertThat(Sampler.alwaysOn().getDescription()).isEqualTo("AlwaysOnSampler");
7171
}
72+
73+
@Test
74+
void string() {
75+
assertThat(Sampler.alwaysOn().toString()).isEqualTo("AlwaysOnSampler");
76+
}
7277
}

sdk/trace/src/test/java/io/opentelemetry/sdk/trace/ParentBasedSamplerTest.java renamed to sdk/trace/src/test/java/io/opentelemetry/sdk/trace/samplers/ParentBasedSamplerTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.sdk.trace;
6+
package io.opentelemetry.sdk.trace.samplers;
77

88
import static org.assertj.core.api.Assertions.assertThat;
99

@@ -14,9 +14,9 @@
1414
import io.opentelemetry.api.trace.TraceFlags;
1515
import io.opentelemetry.api.trace.TraceState;
1616
import io.opentelemetry.context.Context;
17-
import io.opentelemetry.sdk.trace.samplers.Sampler;
18-
import io.opentelemetry.sdk.trace.samplers.SamplingDecision;
17+
import io.opentelemetry.sdk.trace.IdGenerator;
1918
import java.util.Collections;
19+
import nl.jqno.equalsverifier.EqualsVerifier;
2020
import org.junit.jupiter.api.Test;
2121

2222
class ParentBasedSamplerTest {
@@ -407,4 +407,9 @@ void getDescription() {
407407
+ "remoteParentNotSampled:AlwaysOffSampler,localParentSampled:AlwaysOnSampler,"
408408
+ "localParentNotSampled:AlwaysOffSampler}");
409409
}
410+
411+
@Test
412+
void equals() {
413+
EqualsVerifier.forClass(ParentBasedSampler.class).verify();
414+
}
410415
}

0 commit comments

Comments
 (0)