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

com.jnape.palatable.lambda.functions.builtin.fn1.Cycle 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 com.jnape.palatable.lambda.iterators.CyclicIterator;

import static java.util.Arrays.asList;

/**
 * Given an Iterable, return an infinite Iterable that repeatedly cycles its elements, in
 * order.
 *
 * @param  The Iterable element type
 */
public final class Cycle implements Fn1, Iterable> {

    private static final Cycle INSTANCE = new Cycle();

    private Cycle() {
    }

    @Override
    public Iterable apply(Iterable as) {
        return () -> new CyclicIterator<>(as.iterator());
    }

    @SuppressWarnings("unchecked")
    public static  Cycle cycle() {
        return INSTANCE;
    }

    public static  Iterable cycle(Iterable as) {
        return Cycle.cycle().apply(as);
    }

    @SafeVarargs
    public static  Iterable cycle(A... as) {
        return cycle(asList(as));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy