
org.neo4j.helpers.Predicates Maven / Gradle / Ivy
/**
* Copyright (c) 2002-2013 "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.helpers;
import java.util.Arrays;
import org.neo4j.helpers.collection.Iterables;
/**
* Common predicates
*/
public class Predicates
{
public static Predicate TRUE()
{
return new Predicate()
{
public boolean accept( T instance )
{
return true;
}
};
}
public static Predicate not( final Predicate specification )
{
return new Predicate()
{
public boolean accept( T instance )
{
return !specification.accept( instance );
}
};
}
public static AndPredicate and( final Predicate... predicates )
{
return and( Arrays.asList( predicates ) );
}
public static AndPredicate and( final Iterable> predicates )
{
return new AndPredicate( predicates );
}
public static OrPredicate or( final Predicate... predicates )
{
return or( Arrays.asList( predicates ) );
}
public static OrPredicate or( final Iterable> predicates )
{
return new OrPredicate( predicates );
}
public static Predicate equalTo( final T allowed )
{
return new Predicate()
{
@Override
public boolean accept( T item )
{
return allowed == null ? item == null : allowed.equals( item );
}
};
}
public static Predicate in( final T... allowed )
{
return in( Arrays.asList( allowed ) );
}
public static Predicate in( final Iterable allowed )
{
return new Predicate()
{
public boolean accept( T item )
{
for ( T allow : allowed )
{
if ( allow.equals( item ) )
{
return true;
}
}
return false;
}
};
}
public static Predicate notNull()
{
return new Predicate()
{
@Override
public boolean accept( T item )
{
return item != null;
}
};
}
public static Predicate translate( final Function function,
final Predicate super TO> specification )
{
return new Predicate()
{
@Override
public boolean accept( FROM item )
{
return specification.accept( function.apply( item ) );
}
};
}
public static class AndPredicate implements Predicate
{
private final Iterable> predicates;
private AndPredicate( Iterable> predicates )
{
this.predicates = predicates;
}
public boolean accept( T instance )
{
for ( Predicate specification : predicates )
{
if ( !specification.accept( instance ) )
{
return false;
}
}
return true;
}
public AndPredicate and( Predicate... predicates )
{
Iterable> iterable = Iterables.iterable( predicates );
Iterable> flatten = Iterables.flatten( this.predicates, iterable );
return Predicates.and( flatten );
}
public OrPredicate or( Predicate... predicates )
{
return Predicates.or( Iterables.prepend( this, Arrays.asList( predicates ) ) );
}
}
public static class OrPredicate implements Predicate
{
private final Iterable> predicates;
private OrPredicate( Iterable> predicates )
{
this.predicates = predicates;
}
public boolean accept( T instance )
{
for ( Predicate specification : predicates )
{
if ( specification.accept( instance ) )
{
return true;
}
}
return false;
}
public AndPredicate and( Predicate... predicates )
{
return Predicates.and( Iterables.prepend( this, Arrays.asList( predicates ) ) );
}
public OrPredicate or( Predicate... predicates )
{
Iterable> iterable = Iterables.iterable( predicates );
Iterable> flatten = Iterables.flatten( this.predicates, iterable );
return Predicates.or( flatten );
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy