Interface PartialFunction<T,R>
-
- Type Parameters:
T- the type of the function input (the domain)R- the type of the function output (the codomain)
- All Superinterfaces:
java.util.function.Function<T,R>,Function1<T,R>,java.io.Serializable
- All Known Subinterfaces:
API.Match.Case<T,R>,API.Match.Pattern<T,R>,IndexedSeq<T>,LinearSeq<T>,List<T>,Map<K,V>,Multimap<K,V>,Seq<T>,SortedMap<K,V>,SortedMultimap<K,V>,Stream<T>
- All Known Implementing Classes:
AbstractMultimap,API.Match.Case0,API.Match.Case1,API.Match.Case2,API.Match.Case3,API.Match.Case4,API.Match.Case5,API.Match.Case6,API.Match.Case7,API.Match.Case8,API.Match.Pattern0,API.Match.Pattern1,API.Match.Pattern2,API.Match.Pattern3,API.Match.Pattern4,API.Match.Pattern5,API.Match.Pattern6,API.Match.Pattern7,API.Match.Pattern8,Array,CharSeq,HashMap,HashMultimap,LinkedHashMap,LinkedHashMultimap,List.Cons,List.Nil,Queue,Stream.Cons,Stream.Empty,StreamModule.AppendElements,StreamModule.ConsImpl,TreeMap,TreeMultimap,Vector
public interface PartialFunction<T,R> extends Function1<T,R>
Represents a partial functionT -> Rthat may not be defined for all input values of typeT. The caller is responsible for checkingisDefinedAt(Object)before applying this function.If the function is not defined for a given value,
apply(Object)may produce an arbitrary result. There is no guarantee that an exception will be thrown in this case.If the function is defined for a given value,
apply(Object)may still throw an exception during execution.
-
-
Field Summary
Fields Modifier and Type Field Description static longserialVersionUIDThe serial version UID for serialization.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description Rapply(T t)Applies this function to the given argument and returns the result.static <T,V extends Value<T>>
PartialFunction<V,T>getIfDefined()Creates a partial function that maps a givenValueto its underlying value.booleanisDefinedAt(T value)Tests whether a value is contained in the function's domain.default Function1<T,Option<R>>lift()Lifts this partial function into a total function that returns anOptionresult.static <T,R>
PartialFunction<T,R>unlift(@NonNull java.util.function.Function<? super T,? extends Option<? extends R>> totalFunction)Converts (or "unlifts") atotalFunctionthat returns anOptioninto a partial function.
-
-
-
Field Detail
-
serialVersionUID
static final long serialVersionUID
The serial version UID for serialization.- See Also:
- Constant Field Values
-
-
Method Detail
-
unlift
static <T,R> PartialFunction<T,R> unlift(@NonNull java.util.function.Function<? super T,? extends Option<? extends R>> totalFunction)
Converts (or "unlifts") atotalFunctionthat returns anOptioninto a partial function.The provided
totalFunctionshould be side-effect-free, because it may be invoked twice: once when checking if the resulting partial function is defined at a value, and once when applying the partial function to that value.- Type Parameters:
T- the type of the function input (the domain)R- the type of the function output (the codomain)- Parameters:
totalFunction- the function returning anOptionresult- Returns:
- a partial function that is defined only for inputs for which the
totalFunctionreturns a definedOption
-
getIfDefined
static <T,V extends Value<T>> PartialFunction<V,T> getIfDefined()
Creates a partial function that maps a givenValueto its underlying value.The resulting partial function is defined for an input
Valueif and only if theValueis not empty. For defined inputs, the partial function returns the underlying value contained in theValue.- Type Parameters:
T- the type of the underlying valueV- the type of the inputValue(the domain of the function)- Returns:
- a partial function that maps a non-empty
Valueto its underlying value
-
isDefinedAt
boolean isDefinedAt(T value)
Tests whether a value is contained in the function's domain.- Parameters:
value- a potential input to the function- Returns:
trueif the given value is contained in the function's domain,falseotherwise
-
lift
default Function1<T,Option<R>> lift()
Lifts this partial function into a total function that returns anOptionresult.The resulting function applies an argument to this partial function and returns
Some(result)if the function is defined for that argument, orNoneif it is not defined.- Returns:
- a total function that returns an
Optioncontaining the result if defined, orNoneotherwise
-
-