com.jnape.palatable.lambda.functions.builtin.fn1.Head Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lambda Show documentation
Show all versions of lambda Show documentation
Functional patterns for Java
package com.jnape.palatable.lambda.functions.builtin.fn1;
import com.jnape.palatable.lambda.functions.Fn1;
import java.util.Iterator;
import java.util.Optional;
/**
* Retrieve the head element of an Iterable
, wrapped in an Optional
. If the
* Iterable
is empty, the result is Optional.empty()
.
*
* @param the Iterable element type
*/
public final class Head implements Fn1, Optional> {
private Head() {
}
@Override
public Optional apply(Iterable as) {
Iterator iterator = as.iterator();
return iterator.hasNext()
? Optional.of(iterator.next())
: Optional.empty();
}
public static Head head() {
return new Head<>();
}
public static Optional head(Iterable as) {
return Head.head().apply(as);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy