Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.github.dakusui.valid8j_pcond.forms;
import com.github.dakusui.valid8j_pcond.experimentals.currying.CurriedFunction;
import com.github.dakusui.valid8j_pcond.experimentals.currying.CurryingUtils;
import com.github.dakusui.valid8j_pcond.experimentals.currying.multi.MultiFunction;
import com.github.dakusui.valid8j_pcond.experimentals.currying.multi.MultiFunctionUtils;
import com.github.dakusui.valid8j_pcond.core.printable.PrintableFunctionFactory;
import com.github.dakusui.valid8j_pcond.core.refl.MethodQuery;
import com.github.dakusui.valid8j_pcond.core.refl.Parameter;
import com.github.dakusui.valid8j_pcond.validator.Validator;
import java.util.Collection;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static com.github.dakusui.valid8j_pcond.core.refl.ReflUtils.invokeMethod;
import static com.github.dakusui.valid8j_pcond.forms.Predicates.allOf;
import static com.github.dakusui.valid8j_pcond.forms.Predicates.isInstanceOf;
import static com.github.dakusui.valid8j_pcond.internals.InternalUtils.formatObject;
import static java.lang.String.format;
import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;
/**
* An entry point for acquiring function objects.
* Functions retrieved by methods in this class are all "printable".
*/
public class Functions {
private Functions() {
}
/**
* Returns a printable function that returns a given object itself.
*
* @param The type of the object.
* @return The function.
*/
public static Function identity() {
return PrintableFunctionFactory.Simple.IDENTITY.instance();
}
/**
* Returns a function that gives a string representation of a object given to it.
* Internally, the returned function calls `toString` method on a given object.
*
* @param The type of the object
* @return The function.
*/
public static Function stringify() {
return PrintableFunctionFactory.Simple.STRINGIFY.instance();
}
/**
* Returns a function that gives a length of a string passed as an argument.
*
* @return The function.
*/
public static Function super String, Integer> length() {
return PrintableFunctionFactory.Simple.LENGTH.instance();
}
@SuppressWarnings({ "unchecked", "RedundantClassCall" })
public static Function, E> elementAt(int i) {
return Function.class.cast(PrintableFunctionFactory.Parameterized.ELEMENT_AT.create(singletonList(i)));
}
/**
* Returns a function that that returns a size of a given list.
*
* @return The function.
*/
public static Function, Integer> size() {
return PrintableFunctionFactory.Simple.SIZE.instance();
}
/**
* Returns a function that returns a stream for a given collection.
*
* @param Type of elements in the given collection.
* @return The function.
*/
public static Function, Stream> stream() {
return PrintableFunctionFactory.Simple.STREAM.instance();
}
/**
* Returns a function that returns a stream for a given collection.
*
* @param elementClass A parameter to let compiler know the type of the element in a collection.
* @param A type of elements in a collection.
* @return The function.
*/
public static Function, Stream> stream(@SuppressWarnings("unused") Class elementClass) {
return stream();
}
/**
* Returns a function that returns a stream for a given object.
* This method corresponds to {@link Stream#of(Object)} method.
*
* @param Type of object.
* @return The function.
*/
public static Function> streamOf() {
return PrintableFunctionFactory.Simple.STREAM_OF.instance();
}
/**
* Returns a function that casts an object into a given class.
*
* @param type The type to which the given object is cast
* @param The type to which the object is case.
* @return The function.
*/
public static Function super Object, E> cast(Class type) {
return PrintableFunctionFactory.Parameterized.CAST.create(singletonList(type));
}
/**
* Returns a function that casts an object into a given class.
* ```java
* assertThat(
* asList(lastName, fullName),
* allOf(
* transform(elementAt(0).andThen(cast(String.class))).check(allOf(isNotNull(), not(isEmptyString()))),
* transform(elementAt(1).andThen(castTo((List)value()))).check(Predicates.contains(lastName))));
* ```
*
* @param value A type place-holder.
* Always use a value returned from {@link Functions#value()} method.
* @param The type to which the object is case.
* @return The function.
*/
public static Function super Object, E> castTo(@SuppressWarnings("unused") E value) {
return PrintableFunctionFactory.Simple.CAST_TO.instance();
}
/**
* Returns a function that creates and returns a list that contains all the elements in the given list.
*
* @param The type of the input collection.
* @param Type of the elements in the collection
* @return The function.
*/
public static , E> Function> collectionToList() {
return PrintableFunctionFactory.Simple.COLLECTION_TO_LIST.instance();
}
/**
* Returns a function that converts a given array into a list.
*
* @param Type of elements in a given array.
* @return The function.
*/
public static Function> arrayToList() {
return PrintableFunctionFactory.Simple.ARRAY_TO_LIST.instance();
}
/**
* Returns a function the counts lines in a given string.
*
* @return The function.
*/
public static Function countLines() {
return PrintableFunctionFactory.Simple.COUNT_LINES.instance();
}
/**
* //@formatter:off
* The returned function tries to find a {@code substring} after a given string.
* If found, it returns the result of the following statement.
*
* [source,java]
* ----
* s.substring(s.indexOf(substring) + substring.length())
* ----
*
* If not found, a {@link StringIndexOutOfBoundsException} will be thrown.
* //@formatter:on
*
* @param substring A substring to find in a given string.
* @return The string after the {@code substring}.
*/
public static Function findString(String substring) {
requireNonNull(substring);
return PrintableFunctionFactory.function(
() -> format("findString[%s]", substring),
s -> {
int index = s.indexOf(substring);
if (index >= 0)
return s.substring(s.indexOf(substring) + substring.length());
throw new NoSuchElementException(format("'%s' was not found in '%s'", substring, s));
});
}
/**
* https://en.wikipedia.org/wiki/Currying[Curries] a static method specified by the given arguments.
*
* @param aClass A class to which the method to be curried belongs to.
* @param methodName A name of the method to be curried.
* @param parameterTypes Parameters types of the method.
* @return A printable and curried function of the target method.
*/
@SuppressWarnings("JavadocLinkAsPlainText")
public static CurriedFunction