
com.github.dakusui.crest.functions.CrestFunctions Maven / Gradle / Ivy
package com.github.dakusui.crest.functions;
import com.github.dakusui.crest.core.Printable;
import com.github.dakusui.crest.core.InternalUtils;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
import static java.util.Arrays.asList;
import static java.util.Objects.requireNonNull;
public enum CrestFunctions {
;
public static Function identity() {
return Printable.function(
"identity",
Function.identity()
);
}
public static Function super E, String> stringify() {
return Printable.function(
"toString",
Object::toString
);
}
@SuppressWarnings("unchecked")
public static Function super I, ? extends E> invoke(String methodName, Object... args) {
return Printable.function(
String.format("@%s%s", methodName, asList(args)),
(I target) -> (E) InternalUtils.invokeMethod(target, methodName, args)
);
}
public static Function super String, Integer> length() {
return Printable.function(
"length",
String::length
);
}
public static Function, ? extends E> elementAt(int i) {
return Printable.function(
String.format("elementAt[%s]", i),
es -> (E) es.get(i)
);
}
public static Function super Collection, Integer> size() {
return Printable.function(
"size",
Collection::size
);
}
public static Function, Stream extends E>> stream() {
return Printable.function(
"stream",
Collection::stream
);
}
public static Function super Object, ? extends E> cast(Class type) {
return Printable.function(
String.format("castTo[%s]", requireNonNull(type).getSimpleName()),
type::cast
);
}
public static , E> Function> collectionToList() {
return Printable.function("collectionToList", (I c) -> new LinkedList() {
{
addAll(c);
}
});
}
public static Function> arrayToList() {
return Printable.function("arrayToList", Arrays::asList);
}
public static Function countLines() {
return Printable.function("countLines", (String s) -> s.split("\n").length);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy