Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jan 29, 2018
2 parents 5d12743 + 3dd28a3 commit befb49b
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/main/java/org/cactoos/scalar/Folded.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017-2018 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.scalar;

import java.util.Iterator;
import java.util.NoSuchElementException;
import org.cactoos.BiFunc;
import org.cactoos.Scalar;

/**
* Folds iterable via BiFunc
*
* <p>There is no thread-safety guarantee.
*
* <p>This class implements {@link Scalar}, which throws a checked
* {@link Exception}. This may not be convenient in many cases. To make
* it more convenient and get rid of the checked exception you can
* use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.</p>
*
* @author Alexander Dyadyushenko (gookven@gmail.com)
* @author Eduard Balovnev (bedward70@mail.ru)
* @version $Id$
* @param <T> Scalar type
* @since 0.9
*/
public final class Folded<T> implements Scalar<T> {

/**
* Items.
*/
private final Iterable<Scalar<T>> items;

/**
* Folding function.
*/
private final BiFunc<T, T, T> function;

/**
* Ctor.
* @param fold Folding function
* @param scalars The scalars
*/
public Folded(
final BiFunc<T, T, T> fold,
final Iterable<Scalar<T>> scalars
) {
this.items = scalars;
this.function = fold;
}

@Override
public T value() throws Exception {
final Iterator<Scalar<T>> iter = this.items.iterator();
if (!iter.hasNext()) {
throw new NoSuchElementException(
"Can't find first element in an empty iterable"
);
}
T acc = iter.next().value();
while (iter.hasNext()) {
final T next = iter.next().value();
acc = this.function.apply(acc, next);
}
return acc;
}
}
103 changes: 103 additions & 0 deletions src/test/java/org/cactoos/scalar/FoldedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017-2018 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.scalar;

import java.util.Collections;
import java.util.NoSuchElementException;
import org.cactoos.Scalar;
import org.cactoos.iterable.IterableOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Test case for {@link Folded}.
*
* @author Eduard Balovnev (bedward70@mail.ru)
* @version $Id$
* @since 1.0
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class FoldedTest {

@Test(expected = NoSuchElementException.class)
public void failsForEmptyIterable() throws Exception {
new Folded<>(
(first, last) -> first,
Collections.emptyList()
)
.value();
}

@Test
public void singleAtSingleIterable() throws Exception {
final Integer single = 10;
MatcherAssert.assertThat(
"Can't find the single",
new Folded<>(
(first, last) -> first,
new IterableOf<Scalar<Integer>>(() -> single)
).value(),
Matchers.equalTo(single)
);
}

@Test
public void firstAtIterable() throws Exception {
final String one = "Apple";
final String two = "Banana";
final String three = "Orange";
MatcherAssert.assertThat(
"Can't find the first",
new Folded<>(
(first, last) -> first,
new IterableOf<Scalar<String>>(
() -> one,
() -> two,
() -> three
)
).value(),
Matchers.equalTo(one)
);
}

@Test
public void lastAtIterable() throws Exception {
final Character one = 'A';
final Character two = 'B';
final Character three = 'O';
MatcherAssert.assertThat(
"Can't find the last",
new Folded<>(
(first, last) -> last,
new IterableOf<Scalar<Character>>(
() -> one,
() -> two,
() -> three
)
).value(),
Matchers.equalTo(three)
);
}
}

0 comments on commit befb49b

Please sign in to comment.