From e56313c708502cf6af836fed195836c7b43eaa3c Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Fri, 6 Nov 2015 15:42:50 +0100 Subject: [PATCH] SONAR-6990 add ComponentImpl.equals and hashcode based on uuid --- .../computation/component/ComponentImpl.java | 24 ++++++++++++++----- .../component/ComponentImplTest.java | 16 +++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/component/ComponentImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/component/ComponentImpl.java index e2a65f0c142..ad6a5c8e9ec 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/component/ComponentImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/component/ComponentImpl.java @@ -24,6 +24,7 @@ import com.google.common.collect.ImmutableList; import java.util.ArrayList; import java.util.List; import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; import org.sonar.batch.protocol.Constants; import org.sonar.batch.protocol.output.BatchReport; @@ -66,17 +67,11 @@ public class ComponentImpl implements Component { @Override public String getUuid() { - if (uuid == null) { - throw new UnsupportedOperationException(String.format("Component uuid of ref '%s' has not be fed yet", this.reportAttributes.getRef())); - } return uuid; } @Override public String getKey() { - if (key == null) { - throw new UnsupportedOperationException(String.format("Component key of ref '%s' has not be fed yet", this.reportAttributes.getRef())); - } return key; } @@ -207,4 +202,21 @@ public class ComponentImpl implements Component { ", reportAttributes=" + reportAttributes + '}'; } + + @Override + public boolean equals(@Nullable Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ComponentImpl component = (ComponentImpl) o; + return uuid.equals(component.uuid); + } + + @Override + public int hashCode() { + return uuid.hashCode(); + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/component/ComponentImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/component/ComponentImplTest.java index fde6c340d66..a3d3823df35 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/component/ComponentImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/component/ComponentImplTest.java @@ -159,4 +159,20 @@ public class ComponentImplTest { return builder(reportComponent).setKey(KEY).setUuid(UUID).build(); } + @Test + public void equals_compares_on_uuid_only() { + ComponentImpl.Builder builder = builder(BatchReport.Component.newBuilder().build()).setUuid(UUID); + + assertThat(builder.setKey("1").build()).isEqualTo(builder.setKey("1").build()); + assertThat(builder.setKey("1").build()).isEqualTo(builder.setKey("2").build()); + } + + @Test + public void hashCode_is_hashcode_of_uuid() { + ComponentImpl.Builder builder = builder(BatchReport.Component.newBuilder().build()).setUuid(UUID); + + assertThat(builder.setKey("1").build().hashCode()).isEqualTo(builder.setKey("1").build().hashCode()); + assertThat(builder.setKey("1").build().hashCode()).isEqualTo(builder.setKey("2").build().hashCode()); + assertThat(builder.setKey("1").build().hashCode()).isEqualTo(UUID.hashCode()); + } } -- 2.39.5