commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t.@apache.org
Subject svn commit: r1683646 - in /commons/proper/collections/trunk/src: main/java/org/apache/commons/collections4/FluentIterable.java main/java/org/apache/commons/collections4/IterableUtils.java test/java/org/apache/commons/collections4/FluentIterableTest.java
Date Thu, 04 Jun 2015 21:12:09 GMT
Author: tn
Date: Thu Jun  4 21:12:09 2015
New Revision: 1683646

URL: http://svn.apache.org/r1683646
Log:
Minor changes to FluentIterable, added toList to IterableUtils, added first FluentIterable
tests.

Added:
    commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/FluentIterableTest.java
  (with props)
Modified:
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/FluentIterable.java
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IterableUtils.java

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/FluentIterable.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/FluentIterable.java?rev=1683646&r1=1683645&r2=1683646&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/FluentIterable.java
(original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/FluentIterable.java
Thu Jun  4 21:12:09 2015
@@ -172,7 +172,7 @@ public class FluentIterable<E> implement
      * @see {@link org.apache.commons.collections4.iterators.CollatingIterator CollatingIterator}
      */
     public FluentIterable<E> collate(final Iterable<? extends E> other) {
-        return of(IterableUtils.collatedIterable(iterable, other, null));
+        return of(IterableUtils.collatedIterable(null, iterable, other));
     }
 
     /**
@@ -191,15 +191,15 @@ public class FluentIterable<E> implement
      * <p>
      * A <code>null</code> iterable will be treated as an empty iterable.
      *
-     * @param other  the other iterable to collate, may be null
      * @param comparator  the comparator to define an ordering, may be null,
      *   in which case natural ordering will be used
+     * @param other  the other iterable to collate, may be null
      * @return a new iterable, collating this iterable with the other in natural order
      * @see {@link org.apache.commons.collections4.iterators.CollatingIterator CollatingIterator}
      */
     public FluentIterable<E> collate(final Iterable<? extends E> other,
                                      final Comparator<? super E> comparator) {
-        return of(IterableUtils.collatedIterable(iterable, other, comparator));
+        return of(IterableUtils.collatedIterable(comparator, iterable, other));
     }
 
     /**
@@ -465,7 +465,7 @@ public class FluentIterable<E> implement
      * @return a list of the iterable contents
      */
     public List<E> toList() {
-        return IteratorUtils.toList(iterator());
+        return IterableUtils.toList(iterable);
     }
 
     /** {@inheritDoc} */

Modified: 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=1683646&r1=1683645&r2=1683646&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IterableUtils.java
(original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IterableUtils.java
Thu Jun  4 21:12:09 2015
@@ -163,8 +163,35 @@ public class IterableUtils {
      * @return a filtered view on the specified iterable
      */
     public static <E> Iterable<E> collatedIterable(final Iterable<? extends
E> a,
-                                                   final Iterable<? extends E> b,
-                                                   final Comparator<? super E> comparator)
{
+                                                   final Iterable<? extends E> b) {
+        return new FluentIterable<E>() {
+            @Override
+            public Iterator<E> iterator() {
+                return IteratorUtils.collatedIterator(null,
+                                                      emptyIteratorIfNull(a),
+                                                      emptyIteratorIfNull(b));
+            }
+        };
+    }
+
+    /**
+     * Combines the two provided iterables into an ordered iterable using the
+     * provided comparator. If the comparator is null, natural ordering will be
+     * used.
+     * <p>
+     * The returned iterable's iterator supports {@code remove()} when the corresponding
+     * input iterator supports it.
+     *
+     * @param <E>  the element type
+     * @param comparator  the comparator defining an ordering over the elements,
+     *   may be null, in which case natural ordering will be used
+     * @param a  the first iterable, may be null
+     * @param b  the second iterable, may be null
+     * @return a filtered view on the specified iterable
+     */
+    public static <E> Iterable<E> collatedIterable(final Comparator<? super
E> comparator,
+                                                   final Iterable<? extends E> a,
+                                                   final Iterable<? extends E> b) {
         return new FluentIterable<E>() {
             @Override
             public Iterator<E> iterator() {
@@ -630,6 +657,17 @@ public class IterableUtils {
     }
 
     /**
+     * Gets a new list with the contents of the provided iterable.
+     *
+     * @param <E>  the element type
+     * @param iterator  the iterable to use, may be null
+     * @return a list of the iterator contents
+     */
+    public static <E> List<E> toList(final Iterable<E> iterable) {
+        return IteratorUtils.toList(emptyIteratorIfNull(iterable));
+    }
+
+    /**
      * Returns a string representation of the elements of the specified iterable.
      * <p>
      * The string representation consists of a list of the iterable's elements,

Added: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/FluentIterableTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/FluentIterableTest.java?rev=1683646&view=auto
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/FluentIterableTest.java
(added)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/FluentIterableTest.java
Thu Jun  4 21:12:09 2015
@@ -0,0 +1,179 @@
+/*
+ * 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 static org.junit.Assert.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests for FluentIterable.
+ *
+ * @since 4.1
+ * @version $Id$
+ */
+public class FluentIterableTest {
+
+    /**
+     * Iterable of {@link Integer}s
+     */
+    private Iterable<Integer> iterableA = null;
+
+    /**
+     * Iterable of {@link Long}s
+     */
+    private Iterable<Long> iterableB = null;
+
+    /**
+     * Collection of even {@link Integer}s
+     */
+    private Iterable<Integer> iterableEven = null;
+
+    /**
+     * Collection of odd {@link Integer}s
+     */
+    private Iterable<Integer> iterableOdd = null;
+
+    /**
+     * An empty Iterable.
+     */
+    private Iterable<Integer> emptyIterable = null;
+
+    @Before
+    public void setUp() {
+        Collection<Integer> collectionA = new ArrayList<Integer>();
+        collectionA.add(1);
+        collectionA.add(2);
+        collectionA.add(2);
+        collectionA.add(3);
+        collectionA.add(3);
+        collectionA.add(3);
+        collectionA.add(4);
+        collectionA.add(4);
+        collectionA.add(4);
+        collectionA.add(4);
+        iterableA = collectionA;
+
+        Collection<Long> collectionB = new LinkedList<Long>();
+        collectionB.add(5L);
+        collectionB.add(4L);
+        collectionB.add(4L);
+        collectionB.add(3L);
+        collectionB.add(3L);
+        collectionB.add(3L);
+        collectionB.add(2L);
+        collectionB.add(2L);
+        collectionB.add(2L);
+        collectionB.add(2L);
+        iterableB = collectionB;
+
+        iterableEven = Arrays.asList(2, 4, 6, 8, 10, 12);
+        iterableOdd = Arrays.asList(1, 3, 5, 7, 9, 11);
+
+        emptyIterable = Collections.emptyList();
+    }
+
+    private static Predicate<Number> EVEN = new Predicate<Number>() {
+        public boolean evaluate(final Number input) {
+            return input.intValue() % 2 == 0;
+        }
+    };
+
+    // -----------------------------------------------------------------------
+    @Test
+    public void allMatch() {
+        assertTrue(FluentIterable.of(iterableEven).allMatch(EVEN));
+        assertFalse(FluentIterable.of(iterableOdd).allMatch(EVEN));
+        assertFalse(FluentIterable.of(iterableA).allMatch(EVEN));
+
+        try {
+            FluentIterable.of(iterableEven).allMatch(null);
+            fail("expecting NullPointerException");
+        } catch (NullPointerException npe) {
+            // expected
+        }
+    }
+
+    @Test
+    public void anyMatch() {
+        assertTrue(FluentIterable.of(iterableEven).anyMatch(EVEN));
+        assertFalse(FluentIterable.of(iterableOdd).anyMatch(EVEN));
+        assertTrue(FluentIterable.of(iterableA).anyMatch(EVEN));
+
+        try {
+            FluentIterable.of(iterableEven).anyMatch(null);
+            fail("expecting NullPointerException");
+        } catch (NullPointerException npe) {
+            // expected
+        }
+    }
+
+    @Test
+    public void collate() {
+        List<Integer> result = FluentIterable.of(iterableOdd).collate(iterableEven).toList();
+        List<Integer> combinedList = new ArrayList<Integer>();
+        CollectionUtils.addAll(combinedList, iterableOdd);
+        CollectionUtils.addAll(combinedList, iterableEven);
+        Collections.sort(combinedList);
+        assertEquals(combinedList, result);
+
+        result = FluentIterable.of(iterableOdd).collate(null).toList();
+        List<Integer> expected = IterableUtils.toList(iterableOdd);
+        assertEquals(expected, result);
+    }
+
+    @Test
+    public void isEmpty() {
+        assertTrue(FluentIterable.of(emptyIterable).isEmpty());
+        assertFalse(FluentIterable.of(iterableOdd).isEmpty());
+    }
+
+    @Test
+    public void contains() {
+        assertTrue(FluentIterable.of(iterableEven).contains(2));
+        assertFalse(FluentIterable.of(iterableEven).contains(1));
+        assertFalse(FluentIterable.of(iterableEven).contains(null));
+        assertTrue(FluentIterable.of(iterableEven).append((Integer) null).contains(null));
+    }
+
+    @Test
+    public void get() {
+        assertEquals(2, FluentIterable.of(iterableEven).get(0).intValue());
+
+        try {
+            FluentIterable.of(iterableEven).get(-1);
+            fail("expecting IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException ioe) {
+            // expected
+        }
+
+        try {
+            FluentIterable.of(iterableEven).get(IterableUtils.size(iterableEven));
+            fail("expecting IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException ioe) {
+            // expected
+        }
+    }
+}

Propchange: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/FluentIterableTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/FluentIterableTest.java
------------------------------------------------------------------------------
    svn:keywords = Id Revision HeadURL

Propchange: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/FluentIterableTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message