[prev in list] [next in list] [prev in thread] [next in thread]
List: forgerock-commons
Subject: [Commons] [2587] forgerock-util/trunk/src: COMMONS-6 (CMN-36) Remove Iterables homebrew in favor of
From: noreply () forgerock ! org
Date: 2014-10-30 15:37:18
Message-ID: 20141030153718.489743F86E () sources ! internal ! forgerock ! com
[Download RAW message or body]
[Attachment #2 (text/html)]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[2587] forgerock-util/trunk/src: COMMONS-6 (CMN-36) Remove Iterables homebrew \
in favor of guava implementation</title> </head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: \
verdana,arial,helvetica,sans-serif; font-size: 10pt; } #msg dl a { font-weight: \
bold} #msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: \
bold; } #msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: \
6px; } #logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em \
0; } #logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg \
h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; } \
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; \
} #logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: \
-1.5em; padding-left: 1.5em; } #logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em \
1em 0 1em; background: white;} #logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid \
#fa0; border-bottom: 1px solid #fa0; background: #fff; } #logmsg table th { \
text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted \
#fa0; } #logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: \
0.2em 0.5em; } #logmsg table thead th { text-align: center; border-bottom: 1px solid \
#fa0; } #logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: \
6px; } #patch { width: 100%; }
#patch h4 {font-family: \
verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, \
#patch .copfile {border:1px solid #ccc;margin:10px 0;} #patch ins \
{background:#dfd;text-decoration:none;display:block;padding:0 10px;} #patch del \
{background:#fdd;text-decoration:none;display:block;padding:0 10px;} #patch .lines, \
.info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a \
href="http://sources.forgerock.org/changelog/commons/?cs=2587">2587</a></dd> \
<dt>Author</dt> <dd>brmiller</dd> <dt>Date</dt> <dd>2014-10-30 15:37:18 +0000 (Thu, \
30 Oct 2014)</dd> </dl>
<h3>Log Message</h3>
<pre>COMMONS-6 (CMN-36) Remove Iterables homebrew in favor of guava \
implementation</pre>
<h3>Removed Paths</h3>
<ul>
<li><a href="#forgerockutiltrunksrcmainjavaorgforgerockutilIterablesjava">forgerock-util/trunk/src/main/java/org/forgerock/util/Iterables.java</a></li>
<li><a href="#forgerockutiltrunksrctestjavaorgforgerockutilIterablesTestjava">forgerock-util/trunk/src/test/java/org/forgerock/util/IterablesTest.java</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="forgerockutiltrunksrcmainjavaorgforgerockutilIterablesjava"></a>
<div class="delfile"><h4>Deleted: \
forgerock-util/trunk/src/main/java/org/forgerock/util/Iterables.java (2586 => \
2587)</h4> <pre class="diff"><span>
<span class="info">--- \
forgerock-util/trunk/src/main/java/org/forgerock/util/Iterables.java 2014-10-30 \
15:33:13 UTC (rev 2586)
+++ forgerock-util/trunk/src/main/java/org/forgerock/util/Iterables.java 2014-10-30 \
15:37:18 UTC (rev 2587) </span><span class="lines">@@ -1,355 +0,0 @@
</span><del>-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright (c) 2014 ForgeRock AS. All Rights Reserved
- *
- * The contents of this file are subject to the terms
- * of the Common Development and Distribution License
- * (the License). You may not use this file except in
- * compliance with the License.
- *
- * You can obtain a copy of the License at
- * http://forgerock.org/license/CDDLv1.0.html
- * See the License for the specific language governing
- * permission and limitations under the License.
- *
- * When distributing Covered Code, include this CDDL
- * Header Notice in each file and include the License file
- * at http://forgerock.org/license/CDDLv1.0.html
- * If applicable, add the following below the CDDL Header,
- * with the fields enclosed by brackets [] replaced by
- * your own identifying information:
- * "Portions Copyrighted [year] [name of copyright owner]"
- */
-
-package org.forgerock.util;
-
-import java.util.Iterator;
-import java.util.NoSuchElementException;
-
-
-/**
- * Iterable utilties.
- */
-public class Iterables {
- /**
- * An {@link Iterable} that will filter the provided {@link Iterable}
- * using a {@link Predicate}.
- */
- private static class FilteredIterable<T> implements Iterable<T> {
-
- /** the Iterable to filter */
- private final Iterable<T> iterable;
-
- /** the Predicate to determine inclusion or exclusion */
- private final Predicate<T> predicate;
-
- /**
- * Construct the FilteredIterable.
- *
- * @param iterable the Iterable to filter
- * @param predicate the Predicate to determine inclusion
- */
- private FilteredIterable(Iterable<T> iterable, Predicate<T> \
predicate) {
- this.iterable = iterable;
- this.predicate = predicate;
- }
-
- /**
- * Return an {@link Iterator} that filters objects according to the \
Predicate.
- *
- * @return the filtered iterator.
- */
- @Override
- public Iterator<T> iterator() {
- return new Iterator<T>() {
- final Iterator<T> oIterator = iterable.iterator();
- T nextElement = null;
-
- @Override
- public boolean hasNext() {
- if (nextElement != null) {
- // fail-fast for next() or repeated hasNext() calls
- return true;
- }
-
- while (oIterator.hasNext()) {
- nextElement = oIterator.next();
- if (predicate.apply(nextElement)) {
- return true;
- }
- nextElement = null;
- }
- return false;
- }
-
- @Override
- public T next() {
- if (!hasNext()) { // ensure hasNext() has "advanced" \
the next "filtered-in" element
- throw new NoSuchElementException();
- }
-
- // return next element and reset for next iteration
- T retValue = nextElement;
- nextElement = null;
- return retValue;
- }
-
- @Override
- public void remove() {
- oIterator.remove();
- }
- };
- }
- }
-
- /**
- * An {@link Iterable} that will apply a {@link Function} on the elements of
- * the provided {@link Iterable}.
- */
- private static class MappedIterable<T, R, E extends RuntimeException> \
implements Iterable<R> {
-
- /** the Iterable to map */
- private final Iterable<T> iterable;
-
- /** the function to map across the elements */
- private final Function<T, R, E> mapper;
-
- /**
- * Construct the MappedIterable.
- *
- * @param iterable the Iterable to map
- * @param mapper the mapper function to apply
- */
- private MappedIterable(Iterable<T> iterable, Function<T, R, E> \
mapper) {
- this.iterable = iterable;
- this.mapper = mapper;
- }
-
- /**
- * Return an {@link Iterator} that maps objects according to the mapper \
function.
- *
- * @return the mapped iterator.
- */
- @Override
- public Iterator<R> iterator() {
- return new Iterator<R>() {
- final Iterator<T> oIterator = iterable.iterator();
-
- @Override
- public boolean hasNext() {
- return oIterator.hasNext();
- }
-
- @Override
- public R next() {
- return mapper.apply(oIterator.next());
- }
-
- @Override
- public void remove() {
- oIterator.remove();
- }
- };
- }
- }
-
- /**
- * An {@link Iterable} providing fluent expressions of
- * <ul>
- * <li>{@link Iterables#filter}</li>
- * <li>{@link Iterables#map}</li>
- * </ul>
- *
- * @param <T> The parameterized element type of the {@link Iterable}.
- */
- public static class FluentIterable<T> implements Iterable<T> {
- /** The Iterable to wrap */
- private final Iterable<T> iterable;
-
- /**
- * Construct the wrapped Iterable from <em>iterable</em>.
- *
- * @param iterable the Iterable to wrap
- */
- private FluentIterable(Iterable<T> iterable) {
- this.iterable = iterable;
- }
-
- /**
- * Return the wrapped Iterable's {@link Iterator}.
- *
- * @return the iterator.
- */
- @Override
- public Iterator<T> iterator() {
- return iterable.iterator();
- }
-
- /**
- * Filter the wrapped Iterable according the given {@link Predicate}.
- *
- * @param predicate the {@linkplain Predicate} used to include elements in \
the filtered result
- * @return the filtered {@linkplain Iterable}
- */
- public FluentIterable<T> filter(final Predicate<T> predicate) {
- return new FluentIterable<T>(new \
FilteredIterable<T>(iterable, predicate));
- }
-
- /**
- * Map the wrapped Iterable according the given {@link Function}.
- *
- * @param <R> The parameterized element type of the transform \
function's output,
- * also the returned element type of the new Iterable
- * @param <E> An optional exception that the transform function may \
throw.
- * @param mapper the {@linkplain Function} used to map elements from source \
type to return type
- * @return the mapped {@linkplain Iterable}
- */
- public <R, E extends RuntimeException> FluentIterable<R> \
map(final Function<T, R, E> mapper) {
- return new FluentIterable<R>(new MappedIterable<T, R, \
E>(iterable, mapper));
- }
- }
-
- private Iterables() {
- // prevent construction
- }
-
- /**
- * Filter an {@link Iterable} according to a {@link Predicate}. The
- * {@link Predicate} determines which elements are
- * <em>filtered <strong>in</strong></em>.
- *
- * <pre>
- * List&lt;String&gt; fruit = new ArrayList&lt;String&gt;();
- * fruit.add(&quot;apple&quot;);
- * fruit.add(&quot;pineapple&quot;);
- * fruit.add(&quot;banana&quot;);
- * fruit.add(&quot;orange&quot;);
- * fruit.add(&quot;pear&quot;);
- * return filter(fruit, new Predicate&lt;String&gt;() {
- * public boolean apply(String fruit) {
- * return fruit.startsWith(&quot;p&quot;);
- * }
- * });
- * </pre>
- *
- * would return
- *
- * <pre>
- * [ "pineapple, "pear" ]
- * </pre>
- *
- * @param <T>
- * the element type
- * @param iterable
- * the {@linkplain Iterable} to filter
- * @param predicate
- * the {@linkplain Predicate} used to include elements in the
- * filtered result
- * @return the filtered {@linkplain Iterable}
- */
- public static <T> Iterable<T> filter(Iterable<T> iterable, \
Predicate<T> predicate) {
- return new FilteredIterable<T>(iterable, predicate);
- }
-
- /**
- * Create an {@link Iterable} according to a source {@link Iterable} and a
- * mapper {@link Function}. The {@link Function} transforms the source
- * elements in the source Iterable.
- *
- * <pre>
- * List&lt;String&gt; fruit = new ArrayList&lt;String&gt;();
- * fruit.add(&quot;apple&quot;);
- * fruit.add(&quot;pineapple&quot;);
- * fruit.add(&quot;banana&quot;);
- * fruit.add(&quot;orange&quot;);
- * fruit.add(&quot;pear&quot;);
- * return map(fruit, new Function&lt;String, Integer, \
NeverThrowsException&gt;() {
- * public Integer apply(String fruit) {
- * return fruit.length();
- * }
- * });
- * </pre>
- *
- * would return an iterable whose elements are
- *
- * <pre>
- * [ 5, 9, 6, 6, 4 ]
- * </pre>
- *
- * @param <T>
- * the source element type
- * @param <R>
- * the returned iterable element type
- * @param <E>
- * a RuntimeException thrown by the map function
- * @param iterable
- * the source {@linkplain Iterable} to map
- * @param mapper
- * the {@linkplain Function} used to map elements from source
- * type to return type
- * @return the mapped {@linkplain Iterable}
- */
- public static <T, R, E extends RuntimeException> Iterable<R> \
map(Iterable<T> iterable, Function<T, R, E> mapper) {
- return new MappedIterable<T, R, E>(iterable, mapper);
- }
-
- /**
- * Create a Iterable providing fluent expressions of
- * <ul>
- * <li>{@link Iterables#filter}</li>
- * <li>{@link Iterables#map}</li>
- * </ul>
- * <p>
- * Given
- *
- * <pre>
- * List&lt;String&gt; fruit = new ArrayList&lt;String&gt;();
- * fruit.add(&quot;apple&quot;);
- * fruit.add(&quot;pineapple&quot;);
- * fruit.add(&quot;banana&quot;);
- * fruit.add(&quot;orange&quot;);
- * fruit.add(&quot;pear&quot;);
- *
- * final Predicate&lt;String&gt; pFruits = new \
Predicate&lt;String&gt;() {
- * public boolean apply(String fruit) {
- * return fruit.startsWith(&quot;p&quot;);
- * }
- * };
- *
- * final Function&lt;String, Integer, NeverThrowsException&gt; \
getLengths =
- * new Function&lt;String, Integer, NeverThrowsException&gt;() {
- * public Integer apply(String fruit) {
- * return fruit.length();
- * }
- * };
- * </pre>
- *
- * the invocation
- *
- * <pre>
- * from(fruit).filter(pFruits).map(getLengths);
- * </pre>
- *
- * is equivalent to
- *
- * <pre>
- * map(filter(fruit, pFruits), getLengths);
- * </pre>
- *
- * and returns an {@linkplain Iterable} whose elements are
- *
- * <pre>
- * [ 9, 4 ]
- * </pre>
- *
- * @param <T>
- * The parameterized element type of the Iterable.
- * @param iterable
- * the Iterable to wrap
- * @return a wrapped {@linkplain Iterable}
- */
- public static <T> FluentIterable<T> from(Iterable<T> iterable) \
{
- return new FluentIterable<T>(iterable);
- }
-}
</del></span></pre></div>
<a id="forgerockutiltrunksrctestjavaorgforgerockutilIterablesTestjava"></a>
<div class="delfile"><h4>Deleted: \
forgerock-util/trunk/src/test/java/org/forgerock/util/IterablesTest.java (2586 => \
2587)</h4> <pre class="diff"><span>
<span class="info">--- \
forgerock-util/trunk/src/test/java/org/forgerock/util/IterablesTest.java 2014-10-30 \
15:33:13 UTC (rev 2586)
+++ forgerock-util/trunk/src/test/java/org/forgerock/util/IterablesTest.java 2014-10-30 \
15:37:18 UTC (rev 2587) </span><span class="lines">@@ -1,208 +0,0 @@
</span><del>-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright (c) 2014 ForgeRock AS. All Rights Reserved
- *
- * The contents of this file are subject to the terms
- * of the Common Development and Distribution License
- * (the License). You may not use this file except in
- * compliance with the License.
- *
- * You can obtain a copy of the License at
- * http://forgerock.org/license/CDDLv1.0.html
- * See the License for the specific language governing
- * permission and limitations under the License.
- *
- * When distributing Covered Code, include this CDDL
- * Header Notice in each file and include the License file
- * at http://forgerock.org/license/CDDLv1.0.html
- * If applicable, add the following below the CDDL Header,
- * with the fields enclosed by brackets [] replaced by
- * your own identifying information:
- * "Portions Copyrighted [year] [name of copyright owner]"
- */
-package org.forgerock.util;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Iterator;
-import java.util.List;
-import java.util.NoSuchElementException;
-
-import org.forgerock.util.promise.NeverThrowsException;
-import org.testng.annotations.DataProvider;
-import org.testng.annotations.Test;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.forgerock.util.Iterables.filter;
-import static org.forgerock.util.Iterables.from;
-import static org.forgerock.util.Iterables.map;
-
-
-/**
- * Test the Iterables methods.
- */
-@SuppressWarnings("javadoc")
-public class IterablesTest {
-
- private static final Predicate<String> IGNORE_G_WORDS =
- new Predicate<String>() {
- @Override
- public boolean apply(String s) {
- return !s.toLowerCase().startsWith("g");
- }
- };
-
- private static final Predicate<String> ALWAYS_TRUE =
- new Predicate<String>() {
- @Override
- public boolean apply(String s) {
- return true;
- }
- };
-
- private static final Predicate<String> ALWAYS_FALSE =
- new Predicate<String>() {
- @Override
- public boolean apply(String s) {
- return false;
- }
- };
-
- @DataProvider
- public Object[][] filterCollections() {
- return new Object[][] {
- // @formatter:off
- { new String[] { "goo", "gah", "boo", \
"bah", "zoo" }, "bbz" },
- { new String[] { "boo", "baz", "goo", \
"gah", "zoo" }, "bbz" },
- { new String[] { "boo", "goo", "bah", \
"zoo", "gah" }, "bbz" }
-
- // @formatter:on
- };
- }
-
- @Test(dataProvider = "filterCollections")
- public void testFilter(final String[] value, final String firstLetters) {
- StringBuilder sb = new StringBuilder();
- for (String element : filter(Arrays.asList(value), IGNORE_G_WORDS)) {
- sb.append(element.charAt(0));
- }
- assertThat(sb.toString()).isEqualTo(firstLetters);
- }
-
- @Test
- public void testNoElementsHasNext()
- {
- assertThat(filter(new ArrayList<String>(), \
ALWAYS_TRUE).iterator().hasNext()).isFalse();
- }
-
- @Test(expectedExceptions = NoSuchElementException.class)
- public void testNoElementsNext()
- {
- filter(new ArrayList<String>(), ALWAYS_TRUE).iterator().next();
- }
-
- @Test
- public void testNextWithoutHasNextTruePredicate()
- {
- assertThat(filter(Arrays.asList(new String[] { "a" }), \
ALWAYS_TRUE).iterator().next()).isEqualTo("a");
- }
-
- @Test(expectedExceptions = NoSuchElementException.class)
- public void testNextWithoutHasNextFalsePredicate()
- {
- filter(Arrays.asList(new String[] { "a" }), \
ALWAYS_FALSE).iterator().next();
- }
-
- @Test
- public void testRemoveTruePredicate()
- {
- List<String> list = new ArrayList<String>();
- list.add("a");
- Iterator<String> iterator = filter(list, ALWAYS_TRUE).iterator();
- assertThat(iterator.next()).isEqualTo("a");
- iterator.remove();
- assertThat(list.contains("a")).isFalse();
- }
-
- @Test(expectedExceptions = IllegalStateException.class)
- public void testRemoveFalsePredicate()
- {
- List<String> list = new ArrayList<String>();
- list.add("a");
- filter(list, ALWAYS_FALSE).iterator().remove();
- }
-
- @Test(expectedExceptions = IllegalStateException.class)
- public void testRemoveEmpty()
- {
- filter(new ArrayList<String>(), ALWAYS_TRUE).iterator().remove();
- }
-
- private static final Function<String, Character, NeverThrowsException> \
FIRST_LETTERS =
- new Function<String, Character, NeverThrowsException>() {
- @Override
- public Character apply(String value) throws NeverThrowsException {
- return value.charAt(0);
- }
- };
-
- @DataProvider
- public Object[][] mapCollections() {
- return new Object[][] {
- // @formatter:off
- { new String[] { "goo", "gah", "boo", \
"bah", "zoo" }, "ggbbz" },
- { new String[] { "boo", "baz", "goo", \
"gah", "zoo" }, "bbggz" },
- { new String[] { "boo", "goo", "bah", \
"zoo", "gah" }, "bgbzg" }
- // @formatter:on
- };
- }
-
- @Test(dataProvider = "mapCollections")
- public void testMap(final String[] value, final String firstLetters) {
- StringBuilder sb = new StringBuilder();
- for (Character element : map(Arrays.asList(value), FIRST_LETTERS)) {
- sb.append(element);
- }
- assertThat(sb.toString()).isEqualTo(firstLetters);
- }
-
- @Test(dataProvider = "filterCollections")
- public void test_Filter(final String[] value, final String firstLetters) {
- StringBuilder sb = new StringBuilder();
- for (String element : from(Arrays.asList(value)).filter(IGNORE_G_WORDS)) {
- sb.append(element.charAt(0));
- }
- assertThat(sb.toString()).isEqualTo(firstLetters);
- }
-
- @Test(dataProvider = "mapCollections")
- public void test_Map(final String[] value, final String firstLetters) {
- StringBuilder sb = new StringBuilder();
- for (Character element : from(Arrays.asList(value)).map(FIRST_LETTERS)) {
- sb.append(element);
- }
- assertThat(sb.toString()).isEqualTo(firstLetters);
- }
-
- @DataProvider
- public Object[][] filterAndMapCollections() {
- return new Object[][] {
- // @formatter:off
- { new String[] { "goo", "gah", "boo", \
"bah", "zoo" }, "bbz" },
- { new String[] { "boo", "baz", "goo", \
"gah", "zoo" }, "bbz" },
- { new String[] { "boo", "goo", "bah", \
"zoo", "gah" }, "bbz" }
- // @formatter:on
- };
- }
-
- @Test(dataProvider = "filterAndMapCollections")
- public void test_FilterAndMap(final String[] value, final String firstLetters) {
- StringBuilder sb = new StringBuilder();
- for (Character element : \
from(Arrays.asList(value)).filter(IGNORE_G_WORDS).map(FIRST_LETTERS)) \
{
- sb.append(element);
- }
- assertThat(sb.toString()).isEqualTo(firstLetters);
- }
-
-}
</del></span></pre>
</div>
</div>
<div id="footer">Copyright (c) by ForgeRock. All rights reserved.</div>
</body>
</html>
_______________________________________________
Commons mailing list
Commons@forgerock.org
https://lists.forgerock.org/mailman/listinfo/commons
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic