diff options
author | Artur Signell <artur@vaadin.com> | 2016-10-12 21:59:08 +0300 |
---|---|---|
committer | Pekka Hyvönen <pekka@vaadin.com> | 2016-12-09 09:39:00 +0200 |
commit | 76447b941ac64d5f1ad7bbf12555c3b73db23285 (patch) | |
tree | 555168174da9ce25fd05a1968bc35eb48452b987 /shared | |
parent | c94312a0f8a2c4afc019da16092eabad92ab7f2b (diff) | |
download | vaadin-framework-76447b941ac64d5f1ad7bbf12555c3b73db23285.tar.gz vaadin-framework-76447b941ac64d5f1ad7bbf12555c3b73db23285.zip |
Check for duplicate property ids when setting Grid columns or column order (#20386)
Change-Id: I76be83642f0e56e55b0c0e502ac6769de1ee8af0
Diffstat (limited to 'shared')
-rw-r--r-- | shared/src/main/java/com/vaadin/shared/util/SharedUtil.java | 41 | ||||
-rw-r--r-- | shared/src/test/java/com/vaadin/shared/util/SharedUtilTest.java | 32 |
2 files changed, 73 insertions, 0 deletions
diff --git a/shared/src/main/java/com/vaadin/shared/util/SharedUtil.java b/shared/src/main/java/com/vaadin/shared/util/SharedUtil.java index 311b32c7bf..3ee30bd4b5 100644 --- a/shared/src/main/java/com/vaadin/shared/util/SharedUtil.java +++ b/shared/src/main/java/com/vaadin/shared/util/SharedUtil.java @@ -16,6 +16,9 @@ package com.vaadin.shared.util; import java.io.Serializable; +import java.util.Arrays; +import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.Locale; /** @@ -145,6 +148,9 @@ public class SharedUtil implements Serializable { * @return The constructed string of words and separators */ public static String join(String[] parts, String separator) { + if (parts.length == 0) { + return ""; + } StringBuilder sb = new StringBuilder(); for (int i = 0; i < parts.length; i++) { sb.append(parts[i]); @@ -266,4 +272,39 @@ public class SharedUtil implements Serializable { return join(parts, ""); } + /** + * Checks if the given array contains duplicates (according to + * {@link Object#equals(Object)}. + * + * @param values + * the array to check for duplicates + * @return <code>true</code> if the array contains duplicates, + * <code>false</code> otherwise + */ + public static boolean containsDuplicates(Object[] values) { + int uniqueCount = new HashSet<Object>(Arrays.asList(values)).size(); + return uniqueCount != values.length; + } + + /** + * Return duplicate values in the given array in the format + * "duplicateValue1, duplicateValue2". + * + * @param values + * the values to check for duplicates + * @return a comma separated string of duplicates or an empty string if no + * duplicates were found + */ + public static String getDuplicates(Object[] values) { + HashSet<Object> set = new HashSet<Object>(); + LinkedHashSet<String> duplicates = new LinkedHashSet<String>(); + for (Object o : values) { + if (!set.add(o)) { + duplicates.add(String.valueOf(o)); + } + + } + return join(duplicates.toArray(new String[duplicates.size()]), ", "); + } + } diff --git a/shared/src/test/java/com/vaadin/shared/util/SharedUtilTest.java b/shared/src/test/java/com/vaadin/shared/util/SharedUtilTest.java index 081f594a97..6d8cab4fea 100644 --- a/shared/src/test/java/com/vaadin/shared/util/SharedUtilTest.java +++ b/shared/src/test/java/com/vaadin/shared/util/SharedUtilTest.java @@ -118,4 +118,36 @@ public class SharedUtilTest { } } + @Test + public void duplicatesInArray() { + Assert.assertTrue( + SharedUtil.containsDuplicates(new Object[] { "a", "a" })); + Assert.assertTrue( + SharedUtil.containsDuplicates(new Object[] { "a", "b", "a" })); + Assert.assertTrue(SharedUtil + .containsDuplicates(new Object[] { "a", "b", "a", "b" })); + Assert.assertTrue( + SharedUtil.containsDuplicates(new Object[] { 1, "b", 1 })); + + Assert.assertFalse(SharedUtil.containsDuplicates(new Object[] {})); + Assert.assertFalse(SharedUtil.containsDuplicates(new Object[] { "a" })); + Assert.assertFalse( + SharedUtil.containsDuplicates(new Object[] { "a", "b" })); + Assert.assertFalse( + SharedUtil.containsDuplicates(new Object[] { "1", 1 })); + } + + @Test + public void getDuplicates() { + Assert.assertEquals("", SharedUtil.getDuplicates(new Object[] { "a" })); + Assert.assertEquals("a", + SharedUtil.getDuplicates(new Object[] { "a", "a" })); + Assert.assertEquals("a, b", + SharedUtil.getDuplicates(new Object[] { "a", "b", "a", "b" })); + Assert.assertEquals("a, b, c", SharedUtil + .getDuplicates(new Object[] { "c", "a", "b", "a", "b", "c" })); + Assert.assertEquals("1.2", + SharedUtil.getDuplicates(new Object[] { 1.2, "a", 1.2 })); + } + } |