Mike Kolesnik has uploaded a new change for review. Change subject: core: Added collectionToString to utils (#851991) ......................................................................
core: Added collectionToString to utils (#851991) https://bugzilla.redhat.com/851991 Added method capable of creating formatted string out of a collection of element. Change-Id: I8a3c45ae9d875733b90c9c9e327a20202c404caf Signed-off-by: Mike Kolesnik <[email protected]> --- M backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/Entities.java M backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/businessentities/EntitiesTest.java 2 files changed, 69 insertions(+), 0 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/74/7874/1 diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/Entities.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/Entities.java index 4e56ab3..09bd920 100644 --- a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/Entities.java +++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/Entities.java @@ -1,9 +1,11 @@ package org.ovirt.engine.core.common.businessentities; import java.io.Serializable; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -79,4 +81,40 @@ } } + /** + * Convert the given collections to a {@link String}. Empty or <code>null</code> collections will be converted to + * "[]", otherwise first element will appear after the opening bracket, and the consecutive elements (if any) will + * appear on a new line, prefixed by the given prefix. + * + * @param objects + * The collection to convert (can be <code>null</code>). + * @param prefix + * The prefix to print on new line of a collection element (not on 1st element). + * @return A {@link String} representation of the given collection. + */ + public static String collectionToString(Collection<?> objects, String prefix) { + StringBuilder sb = new StringBuilder("["); + if (objects != null) { + boolean first = true; + for (Iterator<?> iterator = objects.iterator(); iterator.hasNext();) { + Object object = iterator.next(); + + if (first) { + first = false; + } else { + sb.append(prefix); + } + + sb.append(object.toString()); + + if (iterator.hasNext()) { + sb.append(",\n"); + } + } + } + + sb.append("]"); + return sb.toString(); + } + } diff --git a/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/businessentities/EntitiesTest.java b/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/businessentities/EntitiesTest.java index 44a7730..489a3b6 100644 --- a/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/businessentities/EntitiesTest.java +++ b/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/businessentities/EntitiesTest.java @@ -1,6 +1,9 @@ package org.ovirt.engine.core.common.businessentities; +import static org.junit.Assert.assertEquals; + import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; @@ -60,4 +63,32 @@ Assert.assertTrue(Entities.objectNames(new ArrayList<Network>()).equals(Collections.emptySet())); } + @Test + public void collectiontoStringNull() { + assertEquals("[]", Entities.collectionToString(null, "")); + } + + @Test + public void collectiontoStringEmpty() { + assertEquals("[]", Entities.collectionToString(Collections.emptyList(), "")); + } + + @Test + public void collectiontoStringOneElement() { + String s = "abc"; + assertEquals("[" + s + "]", Entities.collectionToString(Arrays.asList(s), "")); + } + + @Test + public void collectiontoStringMultipleElements() { + String s = "abc"; + assertEquals("[" + s + ",\n" + s + "]", Entities.collectionToString(Arrays.asList(s, s), "")); + } + + @Test + public void collectiontoStringMultipleElementsWithPrefix() { + String s = "abc"; + String p = " "; + assertEquals("[" + s + ",\n" + p + s + "]", Entities.collectionToString(Arrays.asList(s, s), p)); + } } -- To view, visit http://gerrit.ovirt.org/7874 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I8a3c45ae9d875733b90c9c9e327a20202c404caf Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Mike Kolesnik <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
