All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jnape.palatable.lambda.functions.builtin.fn1.Head Maven / Gradle / Ivy

There is a newer version: 5.5.0
Show newest version
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