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

org.neo4j.function.Suppliers Maven / Gradle / Ivy

There is a newer version: 2025.03.0
Show newest version
/*
 * Copyright (c) "Neo4j"
 * Neo4j Sweden AB [https://neo4j.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.neo4j.function;

import static java.lang.System.nanoTime;

import java.util.concurrent.TimeUnit;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * Constructors for basic {@link Supplier} types
 */
public final class Suppliers {
    private Suppliers() {}

    /**
     * Creates a {@link Supplier} that returns a single object
     *
     * @param instance The object to return
     * @param  The object type
     * @return A {@link Supplier} returning the specified object instance
     */
    public static  Supplier singleton(final T instance) {
        return () -> instance;
    }

    /**
     * Creates a lazy initialized {@link Supplier} of a single object
     *
     * @param supplier A supplier that will provide the object when required
     * @param  The object type
     * @return A {@link Supplier} returning the specified object instance
     */
    public static  Lazy lazySingleton(final Supplier supplier) {
        return new Lazy<>() {
            volatile T instance;

            @Override
            public T get() {
                if (isInitialised()) {
                    return instance;
                }

                synchronized (this) {
                    if (instance == null) {
                        instance = supplier.get();
                    }
                }
                return instance;
            }

            @Override
            public T getIfPresent() {
                return instance;
            }

            @Override
            public boolean isInitialised() {
                return instance != null;
            }

            @Override
            public String toString() {
                return isInitialised() ? get().toString() : "null";
            }
        };
    }

    /**
     * Creates a new {@link Supplier} that applies the specified function to the values obtained from a source supplier. The
     * function is only invoked once for every sequence of identical objects obtained from the source supplier (the previous result
     * is cached and returned again if the source object hasn't changed).
     *
     * @param supplier A supplier of source objects
     * @param adaptor A function mapping source objects to result objects
     * @param  The source object type
     * @param  The result object type
     * @return A {@link Supplier} of objects
     */
    public static  Supplier adapted(final Supplier supplier, final Function adaptor) {
        return new Supplier<>() {
            volatile V lastValue;
            T instance;

            @Override
            public T get() {
                V value = supplier.get();
                if (value == lastValue) {
                    return instance;
                }

                T adaptedValue = adaptor.apply(value);
                synchronized (this) {
                    if (value != lastValue) {
                        instance = adaptedValue;
                        lastValue = value;
                    }
                }
                return instance;
            }
        };
    }

    public static  ThrowingCapturingSupplier compose(
            final ThrowingSupplier input, final ThrowingPredicate predicate) {
        return new ThrowingCapturingSupplier<>(input, predicate);
    }

    public static BooleanSupplier untilTimeExpired(long duration, TimeUnit unit) {
        final long endTimeInNanos = nanoTime() + unit.toNanos(duration);
        return () -> nanoTime() <= endTimeInNanos;
    }

    static class ThrowingCapturingSupplier implements ThrowingSupplier {
        private final ThrowingSupplier input;
        private final ThrowingPredicate predicate;

        private T current;

        ThrowingCapturingSupplier(ThrowingSupplier input, ThrowingPredicate predicate) {
            this.input = input;
            this.predicate = predicate;
        }

        T lastInput() {
            return current;
        }

        @Override
        public Boolean get() throws E {
            current = input.get();
            return predicate.test(current);
        }

        @Override
        public String toString() {
            return String.format("%s on %s", predicate, input);
        }
    }

    public interface Lazy extends Supplier {
        T getIfPresent();

        boolean isInitialised();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy