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

com.gs.collections.impl.block.factory.Predicates2 Maven / Gradle / Ivy

Go to download

GS Collections is a collections framework for Java. It has JDK-compatible List, Set and Map implementations with a rich API and set of utility classes that work with any JDK compatible Collections, Arrays, Maps or Strings. The iteration protocol was inspired by the Smalltalk collection framework.

There is a newer version: 7.0.3
Show newest version
/*
 * Copyright 2014 Goldman Sachs.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gs.collections.impl.block.factory;

import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.block.predicate.Predicate2;
import com.gs.collections.impl.block.predicate.checked.CheckedPredicate2;
import com.gs.collections.impl.block.predicate.checked.ThrowingPredicate2;
import com.gs.collections.impl.utility.Iterate;

/**
 * Predicates2 is a static version of Predicates.  All of its values are statically initialized, with the exception
 * of and, or, not and all of the attribute combination methods.  Predicates2 can only work with one parameter,
 * so it is limited to handling only conditions of a simple nature.
 */
public abstract class Predicates2
        implements Predicate2
{
    private static final long serialVersionUID = 1L;
    private static final Predicates2 NOT_EQUAL = new NotEqual();
    private static final Predicates2> IN = new In();
    private static final Predicates2 EQUAL = new Equal();
    private static final Predicates2> NOT_IN = new NotIn();
    private static final Predicates2 LESS_THAN = new LessThan();
    private static final Predicates2 LESS_THAN_OR_EQUAL = new LessThanOrEqual();
    private static final Predicates2 GREATER_THAN = new GreaterThan();
    private static final Predicates2 GREATER_THAN_OR_EQUAL = new GreaterThanOrEqual();
    private static final Predicates2> INSTANCE_OF = new IsInstanceOf();
    private static final Predicates2 IS_IDENTICAL = new IsIdentical();
    private static final Predicates2 NOT_IDENTITICAL = new NotIdentitical();
    private static final Predicates2> NOT_INSTANCE_OF = new NotInstanceOf();
    private static final Predicates2 ALWAYS_TRUE = new AlwaysTrue();
    private static final Predicates2 ALWAYS_FALSE = new AlwaysFalse();
    private static final Predicates2 IS_NULL = new IsNull();
    private static final Predicates2 NOT_NULL = new NotNull();

    public Predicates2 and(Predicate2 op)
    {
        return Predicates2.and(this, op);
    }

    public Predicates2 or(Predicate2 op)
    {
        return Predicates2.or(this, op);
    }

    public static  Predicate2 throwing(ThrowingPredicate2 throwingPredicate2)
    {
        return new ThrowingPredicate2Adapter(throwingPredicate2);
    }

    public static  Predicates2 not(Predicate2 predicate)
    {
        return new Not(predicate);
    }

    public static  Predicates2 or(
            Predicate2 left,
            Predicate2 right)
    {
        return new Or(left, right);
    }

    public static  Predicates2 and(
            Predicate2 left,
            Predicate2 right)
    {
        return new And(left, right);
    }

    public static  Predicates2> attributeIn(Function function)
    {
        return new AttributePredicates2(function, Predicates2.in());
    }

    public static  Predicates2> attributeNotIn(Function function)
    {
        return new AttributePredicates2(function, Predicates2.notIn());
    }

    public static Predicates2 alwaysTrue()
    {
        return ALWAYS_TRUE;
    }

    public static Predicates2 alwaysFalse()
    {
        return ALWAYS_FALSE;
    }

    public static Predicates2 isNull()
    {
        return IS_NULL;
    }

    public static Predicates2 notNull()
    {
        return NOT_NULL;
    }

    public static Predicates2 equal()
    {
        return EQUAL;
    }

    public static Predicates2 notEqual()
    {
        return NOT_EQUAL;
    }

    public static Predicates2> in()
    {
        return IN;
    }

    public static Predicates2> notIn()
    {
        return NOT_IN;
    }

    public static > Predicates2 lessThan()
    {
        return (Predicates2) LESS_THAN;
    }

    public static > Predicates2 lessThanOrEqualTo()
    {
        return (Predicates2) LESS_THAN_OR_EQUAL;
    }

    public static > Predicates2 greaterThan()
    {
        return (Predicates2) GREATER_THAN;
    }

    public static Predicates2 sameAs()
    {
        return IS_IDENTICAL;
    }

    public static Predicates2 notSameAs()
    {
        return NOT_IDENTITICAL;
    }

    public static Predicates2> instanceOf()
    {
        return INSTANCE_OF;
    }

    public static Predicates2> notInstanceOf()
    {
        return NOT_INSTANCE_OF;
    }

    public static > Predicates2 greaterThanOrEqualTo()
    {
        return (Predicates2) GREATER_THAN_OR_EQUAL;
    }

    public static  Predicates2 attributeNotEqual(Function function)
    {
        return new AttributePredicates2(function, Predicates2.notEqual());
    }

    public static > Predicates2 attributeLessThan(Function function)
    {
        return new AttributePredicates2(function, (Predicate2) LESS_THAN);
    }

    public static > Predicates2 attributeLessThanOrEqualTo(
            Function function)
    {
        return new AttributePredicates2(function, (Predicate2) LESS_THAN_OR_EQUAL);
    }

    public static > Predicates2 attributeGreaterThan(Function function)
    {
        return new AttributePredicates2(function, (Predicate2) GREATER_THAN);
    }

    public static > Predicates2 attributeGreaterThanOrEqualTo(
            Function function)
    {
        return new AttributePredicates2(function, (Predicate2) GREATER_THAN_OR_EQUAL);
    }

    public static  Predicates2 attributeEqual(Function function)
    {
        return new AttributePredicates2(function, Predicates2.equal());
    }

    private static final class Or
            extends Predicates2
    {
        private static final long serialVersionUID = 1L;

        private final Predicate2 left;
        private final Predicate2 right;

        private Or(Predicate2 one, Predicate2 two)
        {
            this.left = one;
            this.right = two;
        }

        public boolean accept(T each, P injectedValue)
        {
            return this.left.accept(each, injectedValue) || this.right.accept(each, injectedValue);
        }

        @Override
        public String toString()
        {
            return "(" + this.left + " or " + this.right + ')';
        }
    }

    private static final class And
            extends Predicates2
    {
        private static final long serialVersionUID = 1L;

        private final Predicate2 left;
        private final Predicate2 right;

        private And(Predicate2 one, Predicate2 two)
        {
            this.left = one;
            this.right = two;
        }

        public boolean accept(T each, P injectedValue)
        {
            return this.left.accept(each, injectedValue) && this.right.accept(each, injectedValue);
        }

        @Override
        public String toString()
        {
            return "(" + this.left + " and " + this.right + ')';
        }
    }

    private static final class AttributePredicates2
            extends Predicates2
    {
        private static final long serialVersionUID = 1L;

        private final Function function;
        private final Predicate2 predicate;

        private AttributePredicates2(
                Function function,
                Predicate2 predicate)
        {
            this.function = function;
            this.predicate = predicate;
        }

        public boolean accept(T each, P injectedValue)
        {
            return this.predicate.accept(this.function.valueOf(each), injectedValue);
        }

        @Override
        public String toString()
        {
            return this.function + " " + this.predicate;
        }
    }

    private static final class Not
            extends Predicates2
    {
        private static final long serialVersionUID = 1L;

        private final Predicate2 predicate;

        private Not(Predicate2 predicate)
        {
            this.predicate = predicate;
        }

        public boolean accept(T each, P injectedValue)
        {
            return !this.predicate.accept(each, injectedValue);
        }

        @Override
        public String toString()
        {
            return "not " + this.predicate;
        }
    }

    private static final class Equal
            extends Predicates2
    {
        private static final long serialVersionUID = 1L;

        public boolean accept(Object each, Object compareTo)
        {
            if (compareTo == null)
            {
                return each == null;
            }
            return compareTo.equals(each);
        }

        @Override
        public String toString()
        {
            return "= ";
        }
    }

    private static final class In
            extends Predicates2>
    {
        private static final long serialVersionUID = 1L;

        public boolean accept(Object each, Iterable injectedIterable)
        {
            return Iterate.contains(injectedIterable, each);
        }

        @Override
        public String toString()
        {
            return "in ";
        }
    }

    private static final class NotIn
            extends Predicates2>
    {
        private static final long serialVersionUID = 1L;

        public boolean accept(Object each, Iterable injectedIterable)
        {
            return !Iterate.contains(injectedIterable, each);
        }

        @Override
        public String toString()
        {
            return "not in ";
        }
    }

    private static class LessThan>
            extends Predicates2
    {
        private static final long serialVersionUID = 1L;

        public boolean accept(T each, T comparison)
        {
            return each.compareTo(comparison) < 0;
        }

        @Override
        public String toString()
        {
            return "< ";
        }
    }

    private static class LessThanOrEqual>
            extends Predicates2
    {
        private static final long serialVersionUID = 1L;

        public boolean accept(T each, T comparison)
        {
            return each.compareTo(comparison) <= 0;
        }

        @Override
        public String toString()
        {
            return "<= ";
        }
    }

    private static class GreaterThan>
            extends Predicates2
    {
        private static final long serialVersionUID = 1L;

        public boolean accept(T each, T comparison)
        {
            return each.compareTo(comparison) > 0;
        }

        @Override
        public String toString()
        {
            return "> ";
        }
    }

    private static class GreaterThanOrEqual>
            extends Predicates2
    {
        private static final long serialVersionUID = 1L;

        public boolean accept(T each, T comparison)
        {
            return each.compareTo(comparison) >= 0;
        }

        @Override
        public String toString()
        {
            return ">= ";
        }
    }

    private static final class NotEqual
            extends Predicates2
    {
        private static final long serialVersionUID = 1L;

        public boolean accept(Object each, Object compareTo)
        {
            if (compareTo == null)
            {
                return each != null;
            }
            return !compareTo.equals(each);
        }

        @Override
        public String toString()
        {
            return "not = ";
        }
    }

    private static final class IsInstanceOf
            extends Predicates2>
    {
        private static final long serialVersionUID = 1L;

        public boolean accept(Object each, Class injectedClass)
        {
            return injectedClass.isInstance(each);
        }

        @Override
        public String toString()
        {
            return "is a(n) ";
        }
    }

    private static final class NotInstanceOf
            extends Predicates2>
    {
        private static final long serialVersionUID = 1L;

        public boolean accept(Object each, Class injectedClass)
        {
            return !injectedClass.isInstance(each);
        }

        @Override
        public String toString()
        {
            return "is not a(n) ";
        }
    }

    private static final class IsIdentical
            extends Predicates2
    {
        private static final long serialVersionUID = 1L;

        public boolean accept(Object each, Object injectedValue)
        {
            return each == injectedValue;
        }

        @Override
        public String toString()
        {
            return "is identical to ";
        }
    }

    private static final class NotIdentitical
            extends Predicates2
    {
        private static final long serialVersionUID = 1L;

        public boolean accept(Object each, Object injectedValue)
        {
            return each != injectedValue;
        }

        @Override
        public String toString()
        {
            return "is not identical to ";
        }
    }

    private static final class IsNull
            extends Predicates2
    {
        private static final long serialVersionUID = 1L;

        public boolean accept(Object each, Object notImportant)
        {
            return each == null;
        }

        @Override
        public String toString()
        {
            return "is null";
        }
    }

    private static final class NotNull
            extends Predicates2
    {
        private static final long serialVersionUID = 1L;

        public boolean accept(Object each, Object notImportant)
        {
            return each != null;
        }

        @Override
        public String toString()
        {
            return "not null";
        }
    }

    private static final class AlwaysTrue
            extends Predicates2
    {
        private static final long serialVersionUID = 1L;

        public boolean accept(Object each, Object notImportant)
        {
            return true;
        }

        @Override
        public String toString()
        {
            return "always true";
        }
    }

    private static final class AlwaysFalse
            extends Predicates2
    {
        private static final long serialVersionUID = 1L;

        public boolean accept(Object each, Object notImportant)
        {
            return false;
        }

        @Override
        public String toString()
        {
            return "always false";
        }
    }

    private static final class ThrowingPredicate2Adapter extends CheckedPredicate2
    {
        private static final long serialVersionUID = 1L;
        private final ThrowingPredicate2 throwingPredicate2;

        private ThrowingPredicate2Adapter(ThrowingPredicate2 throwingPredicate2)
        {
            this.throwingPredicate2 = throwingPredicate2;
        }

        public boolean safeAccept(T object, P param) throws Exception
        {
            return this.throwingPredicate2.safeAccept(object, param);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy