summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--compatibility-server/src/main/java/com/vaadin/v7/ui/Grid.java10
-rw-r--r--compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/grid/GridColumnsTest.java18
-rw-r--r--shared/src/main/java/com/vaadin/shared/util/SharedUtil.java41
-rw-r--r--shared/src/test/java/com/vaadin/shared/util/SharedUtilTest.java32
4 files changed, 101 insertions, 0 deletions
diff --git a/compatibility-server/src/main/java/com/vaadin/v7/ui/Grid.java b/compatibility-server/src/main/java/com/vaadin/v7/ui/Grid.java
index e47294d440..6c811d070e 100644
--- a/compatibility-server/src/main/java/com/vaadin/v7/ui/Grid.java
+++ b/compatibility-server/src/main/java/com/vaadin/v7/ui/Grid.java
@@ -5367,6 +5367,11 @@ public class Grid extends AbstractComponent
* properties in the desired column order
*/
public void setColumns(Object... propertyIds) {
+ if (SharedUtil.containsDuplicates(propertyIds)) {
+ throw new IllegalArgumentException(
+ "The propertyIds array contains duplicates: "
+ + SharedUtil.getDuplicates(propertyIds));
+ }
Set<?> removePids = new HashSet<>(columns.keySet());
removePids.removeAll(Arrays.asList(propertyIds));
for (Object removePid : removePids) {
@@ -5389,6 +5394,11 @@ public class Grid extends AbstractComponent
* properties in the order columns should be
*/
public void setColumnOrder(Object... propertyIds) {
+ if (SharedUtil.containsDuplicates(propertyIds)) {
+ throw new IllegalArgumentException(
+ "The propertyIds array contains duplicates: "
+ + SharedUtil.getDuplicates(propertyIds));
+ }
List<String> columnOrder = new ArrayList<>();
for (Object propertyId : propertyIds) {
if (columns.containsKey(propertyId)) {
diff --git a/compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/grid/GridColumnsTest.java b/compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/grid/GridColumnsTest.java
index a0b2dc54bd..48d6baeaf6 100644
--- a/compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/grid/GridColumnsTest.java
+++ b/compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/grid/GridColumnsTest.java
@@ -410,4 +410,22 @@ public class GridColumnsTest {
assertThat(firstColumn.getHeaderCaption(), is("Column0"));
}
+
+ @Test(expected = IllegalStateException.class)
+ public void addColumnManyTimes() {
+ grid.removeAllColumns();
+ grid.addColumn("column0");
+ grid.addColumn("column0");
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void setColumnDuplicates() {
+ grid.removeAllColumns();
+ grid.setColumns("column0", "column0");
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void setColumnOrderDuplicates() {
+ grid.setColumnOrder("column0", "column0");
+ }
}
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 }));
+ }
+
}