You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2015/02/03 00:10:32 UTC
svn commit: r1656600 - in /commons/proper/collections/trunk: pom.xml
src/changes/changes.xml
src/main/java/org/apache/commons/collections4/IterableUtils.java
src/test/java/org/apache/commons/collections4/IterableUtilsTest.java
Author: tn
Date: Mon Feb 2 23:10:32 2015
New Revision: 1656600
URL: http://svn.apache.org/r1656600
Log:
[COLLECTIONS-550] Added IterableUtils#toString(...) to newly created IterableUtils class. Thanks to Goncalo Marques. This closes #7.
Added:
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IterableUtils.java (with props)
commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java (with props)
Modified:
commons/proper/collections/trunk/pom.xml
commons/proper/collections/trunk/src/changes/changes.xml
Modified: commons/proper/collections/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/pom.xml?rev=1656600&r1=1656599&r2=1656600&view=diff
==============================================================================
--- commons/proper/collections/trunk/pom.xml (original)
+++ commons/proper/collections/trunk/pom.xml Mon Feb 2 23:10:32 2015
@@ -429,6 +429,9 @@
<contributor>
<name>Geoff Schoeman</name>
</contributor>
+ <contributor>
+ <name>Goncalo Marques</name>
+ </contributor>
</contributors>
<dependencies>
Modified: commons/proper/collections/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/changes/changes.xml?rev=1656600&r1=1656599&r2=1656600&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/changes/changes.xml (original)
+++ commons/proper/collections/trunk/src/changes/changes.xml Mon Feb 2 23:10:32 2015
@@ -22,6 +22,10 @@
<body>
<release version="4.1" date="TBD" description="">
+ <action issue="COLLECTIONS-427" dev="tn" type="add" due-to="Gonçalo Marques">
+ Added "toString(...)" methods to newly created "IteratorUtils" class to get a
+ string representation of an Iterable instance similar to "Arrays#toString(...)".
+ </action>
<action issue="COLLECTIONS-427" dev="tn" type="fix">
Reverted performance improvement for "SetUniqueList#retainAll(Collection)"
introduced in 4.0. Added clarifying javadoc wrt runtime complexity instead.
Added: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IterableUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IterableUtils.java?rev=1656600&view=auto
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IterableUtils.java (added)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IterableUtils.java Mon Feb 2 23:10:32 2015
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.collections4;
+
+/**
+ * Provides utility methods and decorators for {@link Iterable} instances.
+ *
+ * @since 4.1
+ * @version $Id$
+ */
+public class IterableUtils {
+
+ /**
+ * Default prefix used while converting an Iterable to its String representation.
+ */
+ private static final String DEFAULT_TOSTRING_PREFIX = "[";
+
+ /**
+ * Default suffix used while converting an Iterable to its String representation.
+ */
+ private static final String DEFAULT_TOSTRING_SUFFIX = "]";
+
+ /**
+ * Default delimiter used to delimit elements while converting an Iterable
+ * to its String representation.
+ */
+ private static final String DEFAULT_TOSTRING_DELIMITER = ", ";
+
+ /**
+ * Returns a string representation of the elements of the specified iterable.
+ * The string representation consists of a list of the iterable's elements,
+ * enclosed in square brackets ({@code "[]"}). Adjacent elements are separated
+ * by the characters {@code ", "} (a comma followed by a space). Elements are
+ * converted to strings as by {@code String.valueOf(Object)}.
+ *
+ * @param <C> the element type
+ * @param iterable the iterable to convert to a string
+ * @return a string representation of {@code iterable}
+ * @throws IllegalArgumentException if {@code iterable} is null
+ */
+ public static <C> String toString(Iterable<C> iterable) {
+ return toString(iterable, new Transformer<C, String>() {
+ public String transform(C input) {
+ return String.valueOf(input);
+ }
+ }, DEFAULT_TOSTRING_DELIMITER, DEFAULT_TOSTRING_PREFIX, DEFAULT_TOSTRING_SUFFIX);
+ }
+
+ /**
+ * Returns a string representation of the elements of the specified iterable.
+ * The string representation consists of a list of the iterable's elements,
+ * enclosed in square brackets ({@code "[]"}). Adjacent elements are separated
+ * by the characters {@code ", "} (a comma followed by a space). Elements are
+ * converted to strings as by using the provided {@code transformer}.
+ *
+ * @param <C> the element type
+ * @param iterable the iterable to convert to a string
+ * @param transformer the transformer used to get a string representation of an element
+ * @return a string representation of {@code iterable}
+ * @throws IllegalArgumentException if {@code iterable} or {@code transformer} is null
+ */
+ public static <C> String toString(Iterable<C> iterable, Transformer<? super C, String> transformer) {
+ return toString(iterable, transformer, DEFAULT_TOSTRING_DELIMITER,
+ DEFAULT_TOSTRING_PREFIX, DEFAULT_TOSTRING_SUFFIX);
+ }
+
+ /**
+ * Returns a string representation of the elements of the specified iterable.
+ * The string representation consists of a list of the iterable's elements,
+ * enclosed by the provided {@code prefix} and {@code suffix}. Adjacent elements
+ * are separated by the provided {@code delimiter}. Elements are converted to
+ * strings as by using the provided {@code transformer}.
+ *
+ * @param <C> the element type
+ * @param iterable the iterable to convert to a string
+ * @param transformer the transformer used to get a string representation of an element
+ * @param delimiter the string to delimit elements
+ * @param prefix the prefix, prepended to the string representation
+ * @param suffix the suffix, appended to the string representation
+ * @return a string representation of {@code iterable}
+ * @throws IllegalArgumentException if any argument is null
+ */
+ public static <C> String toString(Iterable<C> iterable,
+ Transformer<? super C, String> transformer,
+ String delimiter,
+ String prefix,
+ String suffix) {
+ if (iterable == null) {
+ throw new IllegalArgumentException("iterable may not be null");
+ }
+ if (transformer == null) {
+ throw new IllegalArgumentException("transformer may not be null");
+ }
+ if (delimiter == null) {
+ throw new IllegalArgumentException("delimiter may not be null");
+ }
+ if (prefix == null) {
+ throw new IllegalArgumentException("prefix may not be null");
+ }
+ if (suffix == null) {
+ throw new IllegalArgumentException("suffix may not be null");
+ }
+ final StringBuilder stringBuilder = new StringBuilder(prefix);
+ for(final C element : iterable) {
+ stringBuilder.append(transformer.transform(element));
+ stringBuilder.append(delimiter);
+ }
+ if(stringBuilder.length() > prefix.length()) {
+ stringBuilder.setLength(stringBuilder.length() - delimiter.length());
+ }
+ stringBuilder.append(suffix);
+ return stringBuilder.toString();
+ }
+}
Propchange: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IterableUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IterableUtils.java
------------------------------------------------------------------------------
svn:keywords = Id Revision HeadURL
Propchange: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IterableUtils.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java?rev=1656600&view=auto
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java (added)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java Mon Feb 2 23:10:32 2015
@@ -0,0 +1,195 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.collections4;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Test;
+
+/**
+ * Tests for IterableUtils.
+ *
+ * @since 4.1
+ * @version $Id$
+ */
+public class IterableUtilsTest extends BulkTest {
+
+ /**
+ * Iterable of {@link Integer}s
+ */
+ private Iterable<Integer> iterableA = null;
+
+ public IterableUtilsTest(final String name) {
+ super(name);
+ }
+
+ public static Test suite() {
+ return BulkTest.makeSuite(IterableUtilsTest.class);
+ }
+
+ @Override
+ public void setUp() {
+ List<Integer> listA = new ArrayList<Integer>();
+ listA.add(1);
+ listA.add(2);
+ listA.add(2);
+ listA.add(3);
+ listA.add(3);
+ listA.add(3);
+ listA.add(4);
+ listA.add(4);
+ listA.add(4);
+ listA.add(4);
+ iterableA = listA;
+ }
+
+ public void testToString() {
+ String result = IterableUtils.toString(iterableA);
+ assertEquals("[1, 2, 2, 3, 3, 3, 4, 4, 4, 4]", result);
+
+ result = IterableUtils.toString(new ArrayList<Integer>());
+ assertEquals("[]", result);
+
+ try {
+ IterableUtils.toString(null);
+ fail("expecting IllegalArgumentException");
+ } catch (final IllegalArgumentException iae) {
+ // expected
+ }
+
+ result = IterableUtils.toString(iterableA, new Transformer<Integer, String>() {
+ public String transform(Integer input) {
+ return new Integer(input * 2).toString();
+ }
+ });
+ assertEquals("[2, 4, 4, 6, 6, 6, 8, 8, 8, 8]", result);
+
+ result = IterableUtils.toString(new ArrayList<Integer>(), new Transformer<Integer, String>() {
+ public String transform(Integer input) {
+ fail("not supposed to reach here");
+ return "";
+ }
+ });
+ assertEquals("[]", result);
+
+ try {
+ IterableUtils.toString(null, new Transformer<Integer, String>() {
+ public String transform(Integer input) {
+ fail("not supposed to reach here");
+ return "";
+ }
+ });
+ fail("expecting IllegalArgumentException");
+ } catch (final IllegalArgumentException iae) {
+ // expected
+ }
+ }
+
+ public void testToStringDelimiter() {
+
+ Transformer<Integer, String> transformer = new Transformer<Integer, String>() {
+ public String transform(Integer input) {
+ return new Integer(input * 2).toString();
+ }
+ };
+
+ String result = IterableUtils.toString(iterableA, transformer, "", "", "");
+ assertEquals("2446668888", result);
+
+ result = IterableUtils.toString(iterableA, transformer, ",", "", "");
+ assertEquals("2,4,4,6,6,6,8,8,8,8", result);
+
+ result = IterableUtils.toString(iterableA, transformer, "", "[", "]");
+ assertEquals("[2446668888]", result);
+
+ result = IterableUtils.toString(iterableA, transformer, ",", "[", "]");
+ assertEquals("[2,4,4,6,6,6,8,8,8,8]", result);
+
+ result = IterableUtils.toString(iterableA, transformer, ",", "[[", "]]");
+ assertEquals("[[2,4,4,6,6,6,8,8,8,8]]", result);
+
+ result = IterableUtils.toString(iterableA, transformer, ",,", "[", "]");
+ assertEquals("[2,,4,,4,,6,,6,,6,,8,,8,,8,,8]", result);
+
+ result = IterableUtils.toString(iterableA, transformer, ",,", "((", "))");
+ assertEquals("((2,,4,,4,,6,,6,,6,,8,,8,,8,,8))", result);
+
+ result = IterableUtils.toString(new ArrayList<Integer>(), transformer, "", "(", ")");
+ assertEquals("()", result);
+
+ result = IterableUtils.toString(new ArrayList<Integer>(), transformer, "", "", "");
+ assertEquals("", result);
+ }
+
+ public void testToStringWithNullArguments() {
+ try {
+ IterableUtils.toString(null, new Transformer<Integer, String>() {
+ public String transform(Integer input) {
+ fail("not supposed to reach here");
+ return "";
+ }
+ }, "", "(", ")");
+ fail("expecting IllegalArgumentException");
+ } catch (final IllegalArgumentException iae) {
+ // expected
+ }
+
+ try {
+ IterableUtils.toString(new ArrayList<Integer>(), null, "", "(", ")");
+ fail("expecting IllegalArgumentException");
+ } catch (final IllegalArgumentException iae) {
+ // expected
+ }
+
+ try {
+ IterableUtils.toString(new ArrayList<Integer>(), new Transformer<Integer, String>() {
+ public String transform(Integer input) {
+ fail("not supposed to reach here");
+ return "";
+ }
+ }, null, "(", ")");
+ fail("expecting IllegalArgumentException");
+ } catch (final IllegalArgumentException iae) {
+ // expected
+ }
+
+ try {
+ IterableUtils.toString(new ArrayList<Integer>(), new Transformer<Integer, String>() {
+ public String transform(Integer input) {
+ fail("not supposed to reach here");
+ return "";
+ }
+ }, "", null, ")");
+ fail("expecting IllegalArgumentException");
+ } catch (final IllegalArgumentException iae) {
+ // expected
+ }
+
+ try {
+ IterableUtils.toString(new ArrayList<Integer>(), new Transformer<Integer, String>() {
+ public String transform(Integer input) {
+ fail("not supposed to reach here");
+ return "";
+ }
+ }, "", "(", null);
+ fail("expecting IllegalArgumentException");
+ } catch (final IllegalArgumentException iae) {
+ // expected
+ }
+ }
+}
Propchange: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java
------------------------------------------------------------------------------
svn:keywords = Id Revision HeadURL
Propchange: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain