org.neo4j.function.Suppliers Maven / Gradle / Ivy
/*
* Copyright (c) 2018-2020 "Graph Foundation,"
* Graph Foundation, Inc. [https://graphfoundation.org]
*
* This file is part of ONgDB.
*
* ONgDB 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 .
*/
/*
* Copyright (c) 2002-2020 "Neo4j,"
* Neo4j Sweden AB [http://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 java.util.concurrent.TimeUnit;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
import java.util.function.Supplier;
import static java.lang.System.currentTimeMillis;
/**
* 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 ( instance != null )
{
return instance;
}
synchronized ( this )
{
if ( instance == null )
{
instance = supplier.get();
}
}
return instance;
}
};
}
/**
* 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 endTimeInMilliseconds = currentTimeMillis() + unit.toMillis( duration );
return () -> currentTimeMillis() <= endTimeInMilliseconds;
}
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
{
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy