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

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

The newest version!
/*
 * Copyright (c) 2002-2016 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.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.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.BooleanSupplier;
import java.util.function.IntPredicate;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * Constructors for basic {@link Predicate} types
 */
public class Predicates
{
    private static final Predicate TRUE = item -> true;

    private static final Predicate FALSE = item -> false;

    private static final Predicate NOT_NULL = item -> item != null;

    @SuppressWarnings( "unchecked" )
    public static  Predicate alwaysTrue()
    {
        return TRUE;
    }

    @SuppressWarnings( "unchecked" )
    public static  Predicate alwaysFalse()
    {
        return FALSE;
    }

    @SuppressWarnings( "unchecked" )
    public static  Predicate notNull()
    {
        return NOT_NULL;
    }

    @SafeVarargs
    public static  Predicate all( final Predicate... predicates )
    {
        return all( Arrays.asList( predicates ) );
    }

    public static  Predicate all( final Iterable> predicates )
    {
        return item -> {
            for ( Predicate predicate : predicates )
            {
                if ( !predicate.test( item ) )
                {
                    return false;
                }
            }
            return true;
        };
    }

    @SafeVarargs
    public static  Predicate any( final Predicate... predicates )
    {
        return any( Arrays.asList( predicates ) );
    }

    public static  Predicate any( final Iterable> predicates )
    {
        return item -> {
            for ( Predicate predicate : predicates )
            {
                if ( predicate.test( item ) )
                {
                    return true;
                }
            }
            return false;
        };
    }

    public static  Predicate instanceOf( final Class clazz )
    {
        return item -> item != null && clazz.isInstance( item );
    }

    public static  Predicate instanceOfAny( final Class... classes )
    {
        return item -> {
            if ( item != null )
            {
                for ( Class clazz : classes )
                {
                    if ( clazz.isInstance( item ) )
                    {
                        return true;
                    }
                }
            }
            return false;
        };
    }

    public static  Predicate noDuplicates()
    {
        return new Predicate()
        {
            private final Set visitedItems = new HashSet<>();

            @Override
            public boolean test( T item )
            {
                return visitedItems.add( item );
            }
        };
    }

    public static  void await( Supplier supplier, Predicate predicate, long timeout, TimeUnit unit )
            throws TimeoutException, InterruptedException
    {
        await( Suppliers.compose( supplier, predicate ), timeout, unit );
    }

    public static void await( Supplier condition, long timeout, TimeUnit unit )
            throws TimeoutException, InterruptedException
    {
        long sleep = Math.max( unit.toMillis( timeout ) / 100, 1 );
        long deadline = System.currentTimeMillis() + unit.toMillis( timeout );
        do
        {
            if ( condition.get() )
            {
                return;
            }
            Thread.sleep( sleep );
        }
        while ( System.currentTimeMillis() < deadline );
        throw new TimeoutException( "Waited for " + timeout + " " + unit + ", but " + condition + " was not accepted." );
    }


    public static void awaitForever( BooleanSupplier condition, long checkInterval, TimeUnit unit ) throws InterruptedException
    {
        long sleep = unit.toMillis( checkInterval );
        do
        {
            if ( condition.getAsBoolean() )
            {
                return;
            }
            Thread.sleep( sleep );
        }
        while ( true );
    }

    public static  Predicate in( final T... allowed )
    {
        return in( Arrays.asList( allowed ) );
    }

    public static  Predicate in( final Iterable allowed )
    {
        return item -> {
            for ( T allow : allowed )
            {
                if ( allow.equals( item ) )
                {
                    return true;
                }
            }
            return false;
        };
    }

    public static IntPredicate ALWAYS_TRUE_INT = (value) -> true;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy