My favorites
|
Sign in
hamcrest-collections
A library using hamcrest to manipulate collections
Project Home
Downloads
Wiki
Issues
Source
Checkout
|
Browse
|
Changes
|
‹r2
r7
Source path:
svn
/
trunk
/
hamcrest-collections
/
src
/
org
/
hamcrestcollections
/
Reduction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package org.hamcrestcollections;
import java.util.List;
public class Reduction {
public static <T> T reduce(Iterable<T> iterable, Reducer<T> reducer) {
List<T> items = ListUtils.asList(iterable);
T currentValue = null;
if (items.size() == 0) {
throw new InvalidReductionException("Cannot reduce zero items");
}
if (items.size() == 1) {
return items.get(0);
}
T first = items.get(0);
T second = items.get(1);
currentValue = reducer.apply(first, second);
for (int i = 2; i < items.size(); i++) {
currentValue = reducer.apply(currentValue, items.get(i));
}
return currentValue;
}
}
Show details
Hide details
Change log
r3
by sam.newman on Aug 09, 2007
Diff
Initial Checkin
Go to:
/trunk/hamcrest-collections
...k/hamcrest-collections/build.xml
...ections/hamcrest-collections.iml
...-collections/lib/junit-4.3.1.jar
...tcollections/FunctionMapper.java
...mcrestcollections/Functions.java
...mcrestcollections/ListUtils.java
...hamcrestcollections/Reducer.java
...mcrestcollections/Reduction.java
...stcollections/RejectMatcher.java
...stcollections/SelectMatcher.java
...amcrestcollections/Selector.java
.../hamcrestcollections/Zipper.java
...collections/FunctionMapTest.java
...lections/FunctionReduceTest.java
...llections/SelectMatcherTest.java
...crestcollections/ZipperTest.java
Project members,
sign in
to write a code review
Older revisions
r2
by sam.newman on Aug 07, 2007
Diff
Initial Checkin
All revisions of this file
File info
Size: 772 bytes, 29 lines
View raw file
Hosted by