You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2018/01/10 16:57:29 UTC
commons-collections git commit: [COLLECTIONS-671] Add
org.apache.commons.collections4.IterableUtils.first(Iterable).
Repository: commons-collections
Updated Branches:
refs/heads/master b5b45d326 -> 22daa5f1d
[COLLECTIONS-671] Add
org.apache.commons.collections4.IterableUtils.first(Iterable).
Project: http://git-wip-us.apache.org/repos/asf/commons-collections/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-collections/commit/22daa5f1
Tree: http://git-wip-us.apache.org/repos/asf/commons-collections/tree/22daa5f1
Diff: http://git-wip-us.apache.org/repos/asf/commons-collections/diff/22daa5f1
Branch: refs/heads/master
Commit: 22daa5f1dd4396e94224841b3bf0e9f226e86695
Parents: b5b45d3
Author: Gary Gregory <gg...@apache.org>
Authored: Wed Jan 10 09:57:27 2018 -0700
Committer: Gary Gregory <gg...@apache.org>
Committed: Wed Jan 10 09:57:27 2018 -0700
----------------------------------------------------------------------
src/changes/changes.xml | 3 +++
.../commons/collections4/IterableUtils.java | 16 ++++++++++++++
.../commons/collections4/IterableUtilsTest.java | 22 +++++++++++++++++++-
3 files changed, 40 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-collections/blob/22daa5f1/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index efdd030..d2ae55e 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -81,6 +81,9 @@
<action issue="COLLECTIONS-670" dev="ggregory" type="add" due-to="Gary Gregory">
Add org.apache.commons.collections4.IteratorUtils.first(Iterator).
</action>
+ <action issue="COLLECTIONS-671" dev="ggregory" type="add" due-to="Gary Gregory">
+ Add org.apache.commons.collections4.IterableUtils.first(Iterable).
+ </action>
</release>
<release version="4.1" date="2015-11-28" description="This is a security and minor release.">
<action issue="COLLECTIONS-508" dev="tn" type="add">
http://git-wip-us.apache.org/repos/asf/commons-collections/blob/22daa5f1/src/main/java/org/apache/commons/collections4/IterableUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/collections4/IterableUtils.java b/src/main/java/org/apache/commons/collections4/IterableUtils.java
index d87a5a4..d5cb444 100644
--- a/src/main/java/org/apache/commons/collections4/IterableUtils.java
+++ b/src/main/java/org/apache/commons/collections4/IterableUtils.java
@@ -775,6 +775,22 @@ public class IterableUtils {
}
/**
+ * Returns the <code>first</code> value in the <code>iterable</code>'s {@link Iterator}, throwing
+ * <code>IndexOutOfBoundsException</code> if there is no such element.
+ * <p>
+ * If the {@link Iterable} is a {@link List}, then it will use {@link List#get(int)}.
+ *
+ * @param <T> the type of object in the {@link Iterable}.
+ * @param iterable the {@link Iterable} to get a value from, may be null
+ * @return the first object
+ * @throws IndexOutOfBoundsException if the request is invalid
+ * @since 4.2
+ */
+ public static <T> T first(final Iterable<T> iterable) {
+ return get(iterable, 0);
+ }
+
+ /**
* Returns the number of elements contained in the given iterator.
* <p>
* A <code>null</code> or empty iterator returns {@code 0}.
http://git-wip-us.apache.org/repos/asf/commons-collections/blob/22daa5f1/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java b/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java
index 494592c..564193b 100644
--- a/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java
+++ b/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java
@@ -393,17 +393,37 @@ public class IterableUtilsTest {
assertTrue(IterableUtils.matchesAll(emptyIterable, lessThanFour));
}
- @Test(expected = IndexOutOfBoundsException.class)
public void getFromIterable() throws Exception {
// Collection, entry exists
final Bag<String> bag = new HashBag<>();
bag.add("element", 1);
assertEquals("element", IterableUtils.get(bag, 0));
+ }
+ @Test(expected = IndexOutOfBoundsException.class)
+ public void getFromIterableIndexOutOfBoundsException() throws Exception {
+ // Collection, entry exists
+ final Bag<String> bag = new HashBag<>();
+ bag.add("element", 1);
// Collection, non-existent entry
IterableUtils.get(bag, 1);
}
+ public void firstFromIterable() throws Exception {
+ // Collection, entry exists
+ final Bag<String> bag = new HashBag<>();
+ bag.add("element", 1);
+ assertEquals("element", IterableUtils.first(bag));
+ }
+
+ @Test(expected = IndexOutOfBoundsException.class)
+ public void firstFromIterableIndexOutOfBoundsException() throws Exception {
+ // Collection, entry exists
+ final Bag<String> bag = new HashBag<>();
+ // Collection, non-existent entry
+ IterableUtils.first(bag);
+ }
+
@SuppressWarnings("unchecked")
@Test
public void partition() {