My favorites | Sign in
Project Logo
                
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: 
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
Hosted by Google Code