Martin Peřina has uploaded a new change for review. Change subject: core: Add Collection and Map support to ToStringBuilder ......................................................................
core: Add Collection and Map support to ToStringBuilder Adds append(String, Collection) and append(String, Map) to allow adding collections and maps attributes to toString() output. Change-Id: Ie6ae7bced08092e4f1e635be394edc940d7083b4 Signed-off-by: Martin Perina <[email protected]> --- M backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ToStringBuilder.java M backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/ToStringBuilderTest.java 2 files changed, 61 insertions(+), 1 deletion(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/20/39720/1 diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ToStringBuilder.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ToStringBuilder.java index f0af401..0623f69 100644 --- a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ToStringBuilder.java +++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ToStringBuilder.java @@ -1,6 +1,8 @@ package org.ovirt.engine.core.common.utils; import java.util.Arrays; +import java.util.Collection; +import java.util.Map; /** * Eases implementation of {@code toString()} methods. @@ -304,6 +306,34 @@ } /** + * Appends {@code Collection} attribute with specified {@code name} and {@code value} + * + * @param name + * attribute name + * @param value + * attribute value + * @return this + */ + public ToStringBuilder append(final String name, final Collection value) { + appendAttribute(name, value == null ? null : Arrays.toString(value.toArray())); + return this; + } + + /** + * Appends {@code Map} attribute with specified {@code name} and {@code value} + * + * @param name + * attribute name + * @param value + * attribute value + * @return this + */ + public ToStringBuilder append(final String name, final Map value) { + append(name, value == null ? null : value.entrySet()); + return this; + } + + /** * Appends {@code String} attribute with specified {@code name} and its filtered {@code value}: * <ul> * <li>If value is not {@code null}, then {@code getFilteredContent()} is appended as value</li> diff --git a/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/ToStringBuilderTest.java b/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/ToStringBuilderTest.java index 848c80f..fbbd6cd 100644 --- a/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/ToStringBuilderTest.java +++ b/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/ToStringBuilderTest.java @@ -4,7 +4,11 @@ import java.math.BigDecimal; import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; import java.util.Date; +import java.util.HashMap; +import java.util.Map; import org.junit.Test; @@ -127,6 +131,20 @@ final String nullValue = null; final String nullArrayName = "nullArray"; final Object[] nullArrayValue = null; + final String nullCollectionName = "nullCollection"; + final Collection nullCollectionValue = null; + final String emptyCollectionName = "emptyCollection"; + final Collection emptyCollectionValue = Collections.emptyList(); + final String collectionName = "collection"; + final Collection collectionValue = Arrays.asList("item1", "item2"); + final String nullMapName = "nullMap"; + final Collection nullMapValue = null; + final String emptyMapName = "emptyMap"; + final Map emptyMapValue = Collections.emptyMap(); + final String mapName = "map"; + final Map<String, String> mapValue = new HashMap<>(); + mapValue.put("key1", "value1"); + mapValue.put("key2", "value2"); String expected = ToStringBuilder.ATTRIBUTES_LIST_PREFIX + createFirstAttributeOutput(booleanName, String.valueOf(booleanValue)) @@ -150,7 +168,13 @@ + createAttributeOutput(nullName, String.valueOf(nullValue)) + createAttributeOutput(nullArrayName, Arrays.toString(nullArrayValue)) + createAttributeOutput(passwordName, ToStringBuilder.FILTERED_CONTENT) - + createAttributeOutput(nullPasswordName, null) + + createAttributeOutput(nullPasswordName, nullPasswordValue) + + createAttributeOutput(nullCollectionName, null) + + createAttributeOutput(emptyCollectionName, Arrays.toString(emptyCollectionValue.toArray())) + + createAttributeOutput(collectionName, Arrays.toString(collectionValue.toArray())) + + createAttributeOutput(nullMapName, null) + + createAttributeOutput(emptyMapName, Arrays.toString(emptyMapValue.entrySet().toArray())) + + createAttributeOutput(mapName, Arrays.toString(mapValue.entrySet().toArray())) + ToStringBuilder.ATTRIBUTES_LIST_SUFFIX; System.out.println("ToString: " + expected); @@ -179,6 +203,12 @@ .append(nullArrayName, nullArrayValue) .appendFiltered(passwordName, passwordValue) .appendFiltered(nullPasswordName, nullPasswordValue) + .append(nullCollectionName, nullCollectionValue) + .append(emptyCollectionName, emptyCollectionValue) + .append(collectionName, collectionValue) + .append(nullMapName, nullMapValue) + .append(emptyMapName, emptyMapValue) + .append(mapName, mapValue) .build()); } -- To view, visit https://gerrit.ovirt.org/39720 To unsubscribe, visit https://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ie6ae7bced08092e4f1e635be394edc940d7083b4 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Martin Peřina <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
