org.pitest.functional.FCollection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pitest Show documentation
Show all versions of pitest Show documentation
Mutation testing system for Java.
/*
* Copyright 2010 Henry Coles
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package org.pitest.functional;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Functional programming style operations for plain old Java iterables.
*/
public abstract class FCollection {
public static void forEach(final Iterable as,
final SideEffect1 e) {
for (final A a : as) {
e.apply(a);
}
}
public static void mapTo(final Iterable as,
final F f, final Collection bs) {
if (as != null) {
for (final A a : as) {
bs.add(f.apply(a));
}
}
}
public static FunctionalList map(final Iterable as,
final F f) {
final FunctionalList bs = emptyList();
mapTo(as, f, bs);
return bs;
}
public static void flatMapTo(final Iterable as,
final F> f, final Collection bs) {
if (as != null) {
for (final A a : as) {
for (final B each : f.apply(a)) {
bs.add(each);
}
}
}
}
public static FunctionalList flatMap(
final Iterable as, final F> f) {
final FunctionalList bs = emptyList();
flatMapTo(as, f, bs);
return bs;
}
private static FunctionalList emptyList() {
return new MutableList();
}
public static FunctionalList filter(final Iterable xs,
final F predicate) {
final FunctionalList dest = emptyList();
filter(xs, predicate, dest);
return dest;
}
public static void filter(final Iterable xs,
final F predicate, final Collection dest) {
for (final T x : xs) {
if (predicate.apply(x)) {
dest.add(x);
}
}
}
public static boolean contains(final Iterable xs,
final F predicate) {
for (final T x : xs) {
if (predicate.apply(x)) {
return true;
}
}
return false;
}
public static A fold(final F2 f, final A z,
final Iterable xs) {
A p = z;
for (final B x : xs) {
p = f.apply(p, x);
}
return p;
}
public static FunctionalCollection flatten(
final Iterable> ts) {
final MutableList list = new MutableList();
for (final Iterable it : ts) {
for (final T each : it) {
list.add(each);
}
}
return list;
}
public static FunctionalList> splitToLength(
final int targetLength, final Iterable ts) {
final FunctionalList> list = new MutableList>();
List temp = new ArrayList();
int i = 0;
for (final T each : ts) {
if (i == targetLength) {
list.add(temp);
temp = new ArrayList();
i = 0;
}
temp.add(each);
i++;
}
if (!temp.isEmpty()) {
list.add(temp);
}
return list;
}
public static Map> bucket(final Iterable bs,
final F f) {
final Map> bucketed = new HashMap>();
for (final B each : bs) {
final A key = f.apply(each);
Collection existing = bucketed.get(key);
if (existing == null) {
existing = new ArrayList();
bucketed.put(key, existing);
}
existing.add(each);
}
return bucketed;
}
}