9 package javolution.util.function;
13 import java.util.Collection;
14 import java.util.Comparator;
15 import java.util.Iterator;
16 import java.util.concurrent.atomic.AtomicInteger;
17 import java.util.concurrent.atomic.AtomicReference;
42 public static <E>
Reducer<E>
any(Class<? extends E> type) {
47 private final Class<? extends E>
type;
55 public void accept(Collection<E> param) {
56 Iterator<E> it = param.iterator();
57 while (it.hasNext() && (
found ==
null)) {
59 if (
type.isInstance(e)) {
78 @
Parallelizable(mutexFree =
true, comment =
"Internal use of AtomicReference")
80 public static <E>
Reducer<E>
max(Comparator<? super E> comparator) {
85 private final Comparator<? super E>
cmp;
86 private final AtomicReference<E>
max =
new AtomicReference<E>(
null);
93 public void accept(Collection<E> param) {
94 Iterator<E> it = param.iterator();
95 while (it.hasNext()) {
98 while ((read ==
null) || (
cmp.compare(e, read) > 0)) {
99 if (
max.compareAndSet(read, e))
break;
115 @
Parallelizable(mutexFree =
true, comment =
"Internal use of AtomicReference")
117 public static <E>
Reducer<E>
min(Comparator<? super E> comparator) {
122 private final Comparator<? super E>
cmp;
123 private final AtomicReference<E>
min =
new AtomicReference<E>(
null);
130 public void accept(Collection<E> param) {
131 Iterator<E> it = param.iterator();
132 while (it.hasNext()) {
135 while ((read ==
null) || (
cmp.compare(e, read) < 0)) {
136 if (
min.compareAndSet(read, e))
break;
163 public void accept(Collection<Boolean> param) {
164 Iterator<Boolean> it = param.iterator();
165 while (
result && it.hasNext()) {
166 if (!it.next())
result =
false;
171 public Boolean
get() {
191 public void accept(Collection<Boolean> param) {
192 Iterator<Boolean> it = param.iterator();
193 while (!
result && it.hasNext()) {
194 if (!it.next())
result =
true;
199 public Boolean
get() {
215 private final AtomicInteger
sum =
new AtomicInteger(0);
218 public void accept(Collection<Integer> param) {
219 Iterator<Integer> it = param.iterator();
220 while (it.hasNext()) {
221 sum.getAndAdd(it.next().intValue());
226 public Integer
get() {