diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-11-21 10:58:48 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-11-21 11:08:41 +0100 |
commit | 4498173ba720d9ab3d10982894ad76d08c321802 (patch) | |
tree | 74e5e2e5048182b0a06eb6fbe4bf0809e22cf8f7 /sonar-plugin-api | |
parent | 50050c7ea204110ee4ac3bf59e8f42f510622f12 (diff) | |
download | sonarqube-4498173ba720d9ab3d10982894ad76d08c321802.tar.gz sonarqube-4498173ba720d9ab3d10982894ad76d08c321802.zip |
Centralize algorithm of UUID generation
Diffstat (limited to 'sonar-plugin-api')
5 files changed, 93 insertions, 9 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssue.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssue.java index ab7912cc8a8..df4779a4894 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssue.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssue.java @@ -28,12 +28,11 @@ import org.sonar.api.batch.sensor.SensorStorage; import org.sonar.api.batch.sensor.internal.DefaultStorable; import org.sonar.api.batch.sensor.issue.Issue; import org.sonar.api.rule.RuleKey; +import org.sonar.api.utils.internal.Uuids; import javax.annotation.CheckForNull; import javax.annotation.Nullable; -import java.util.UUID; - public class DefaultIssue extends DefaultStorable implements Issue { private static final String INPUT_DIR_SHOULD_BE_NON_NULL = "InputDir should be non null"; @@ -51,12 +50,12 @@ public class DefaultIssue extends DefaultStorable implements Issue { public DefaultIssue() { super(null); - this.key = UUID.randomUUID().toString(); + this.key = Uuids.create(); } public DefaultIssue(SensorStorage storage) { super(storage); - this.key = UUID.randomUUID().toString(); + this.key = Uuids.create(); } @Override diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/internal/DefaultIssueComment.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/internal/DefaultIssueComment.java index dbff36ff056..ad77a21f1c1 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/issue/internal/DefaultIssueComment.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/internal/DefaultIssueComment.java @@ -20,13 +20,12 @@ package org.sonar.api.issue.internal; import org.sonar.api.issue.IssueComment; +import org.sonar.api.utils.internal.Uuids; import javax.annotation.CheckForNull; import javax.annotation.Nullable; - import java.io.Serializable; import java.util.Date; -import java.util.UUID; /** * PLUGINS MUST NOT BE USED THIS CLASS, EXCEPT FOR UNIT TESTING. @@ -118,7 +117,7 @@ public class DefaultIssueComment implements Serializable, IssueComment { public static DefaultIssueComment create(String issueKey, @Nullable String login, String markdownText) { DefaultIssueComment comment = new DefaultIssueComment(); comment.setIssueKey(issueKey); - comment.setKey(UUID.randomUUID().toString()); + comment.setKey(Uuids.create()); Date now = new Date(); comment.setUserLogin(login); comment.setMarkdownText(markdownText); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/platform/ComponentKeys.java b/sonar-plugin-api/src/main/java/org/sonar/api/platform/ComponentKeys.java index 13da7d21776..3a603649e3b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/platform/ComponentKeys.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/platform/ComponentKeys.java @@ -22,10 +22,10 @@ package org.sonar.api.platform; import com.google.common.annotations.VisibleForTesting; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.sonar.api.utils.internal.Uuids; import java.util.HashSet; import java.util.Set; -import java.util.UUID; import java.util.regex.Pattern; /** @@ -50,7 +50,7 @@ class ComponentKeys { if (!objectsWithoutToString.add(component.getClass())) { log.warn(String.format("Bad component key: %s. Please implement toString() method on class %s", key, component.getClass().getName())); } - key += UUID.randomUUID().toString(); + key += Uuids.create(); } return new StringBuilder().append(component.getClass().getCanonicalName()).append("-").append(key).toString(); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/Uuids.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/Uuids.java new file mode 100644 index 00000000000..e72c123e42e --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/Uuids.java @@ -0,0 +1,39 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.utils.internal; + +import java.util.UUID; + +/** + * @since 5.0 + */ +public class Uuids { + + private Uuids() { + // only static fields + } + + /** + * Create a universally unique identifier. Underlying algorithm can change over SQ versions. + */ + public static String create() { + return UUID.randomUUID().toString(); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/UuidsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/UuidsTest.java new file mode 100644 index 00000000000..70f11710d19 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/UuidsTest.java @@ -0,0 +1,47 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.utils.internal; + +import com.google.common.collect.Sets; +import org.junit.Test; +import org.sonar.test.TestUtils; + +import java.util.Set; + +import static org.fest.assertions.Assertions.assertThat; + +public class UuidsTest { + + @Test + public void create_unique() throws Exception { + Set<String> all = Sets.newHashSet(); + for (int i = 0; i < 50; i++) { + String uuid = Uuids.create(); + assertThat(uuid).isNotEmpty(); + all.add(uuid); + } + assertThat(all).hasSize(50); + } + + @Test + public void constructor_is_private() throws Exception { + TestUtils.hasOnlyPrivateConstructors(Uuids.class); + } +} |