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

org.glassfish.jersey.internal.guava.Predicates Maven / Gradle / Ivy

Go to download

A bundle project producing JAX-RS RI bundles. The primary artifact is an "all-in-one" OSGi-fied JAX-RS RI bundle (jaxrs-ri.jar). Attached to that are two compressed JAX-RS RI archives. The first archive (jaxrs-ri.zip) consists of binary RI bits and contains the API jar (under "api" directory), RI libraries (under "lib" directory) as well as all external RI dependencies (under "ext" directory). The secondary archive (jaxrs-ri-src.zip) contains buildable JAX-RS RI source bundle and contains the API jar (under "api" directory), RI sources (under "src" directory) as well as all external RI dependencies (under "ext" directory). The second archive also contains "build.xml" ANT script that builds the RI sources. To build the JAX-RS RI simply unzip the archive, cd to the created jaxrs-ri directory and invoke "ant" from the command line.

There is a newer version: 3.1.8
Show newest version
/*
 * Copyright (C) 2007 The Guava Authors
 *
 * 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 org.glassfish.jersey.internal.guava;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

import static org.glassfish.jersey.internal.guava.Preconditions.checkNotNull;

/**
 * Static utility methods pertaining to {@code Predicate} instances.
 * 

*

All methods returns serializable predicates as long as they're given * serializable parameters. *

*

See the Guava User Guide article on the * use of {@code Predicate}. * * @author Kevin Bourrillion * @since 2.0 (imported from Google Collections Library) */ public final class Predicates { private static final Joiner COMMA_JOINER = Joiner.on(); // TODO(kevinb): considering having these implement a VisitablePredicate // interface which specifies an accept(PredicateVisitor) method. private Predicates() { } /** * Returns a predicate that always evaluates to {@code true}. */ public static Predicate alwaysTrue() { return ObjectPredicate.ALWAYS_TRUE.withNarrowedType(); } /** * Returns a predicate that evaluates to {@code true} if the object reference * being tested is null. */ private static Predicate isNull() { return ObjectPredicate.IS_NULL.withNarrowedType(); } /** * Returns a predicate that evaluates to {@code true} if the given predicate * evaluates to {@code false}. */ public static Predicate not(Predicate predicate) { return new NotPredicate(predicate); } /** * Returns a predicate that evaluates to {@code true} if the object being * tested {@code equals()} the given target or both are null. */ public static Predicate equalTo(T target) { return (target == null) ? Predicates.isNull() : new IsEqualToPredicate(target); } /** * Returns a predicate that evaluates to {@code true} if the object reference * being tested is a member of the given collection. It does not defensively * copy the collection passed in, so future changes to it will alter the * behavior of the predicate. *

*

This method can technically accept any {@code Collection}, but using * a typed collection helps prevent bugs. This approach doesn't block any * potential users since it is always possible to use {@code * Predicates.in()}. * * @param target the collection that may contain the function input */ public static Predicate in(Collection target) { return new InPredicate(target); } // End public API, begin private implementation classes. /** * Returns the composition of a function and a predicate. For every {@code x}, * the generated predicate returns {@code predicate(function(x))}. * * @return the composition of the provided function and predicate */ public static Predicate compose( Predicate predicate, Function function) { return new CompositionPredicate(predicate, function); } // Package private for GWT serialization. enum ObjectPredicate implements Predicate { /** * @see Predicates#alwaysTrue() */ ALWAYS_TRUE { @Override public boolean test(Object o) { return true; } @Override public String toString() { return "Predicates.alwaysTrue()"; } }, /** * @see Predicates#isNull() */ IS_NULL { @Override public boolean test(Object o) { return o == null; } @Override public String toString() { return "Predicates.isNull()"; } }; @SuppressWarnings("unchecked") // safe contravariant cast Predicate withNarrowedType() { return (Predicate) this; } } /** * @see Predicates#not(Predicate) */ private static class NotPredicate implements Predicate, Serializable { private static final long serialVersionUID = 0; final Predicate predicate; NotPredicate(Predicate predicate) { this.predicate = Preconditions.checkNotNull(predicate); } @Override public boolean test(T t) { return !predicate.test(t); } @Override public int hashCode() { return ~predicate.hashCode(); } @Override public boolean equals(Object obj) { if (obj instanceof NotPredicate) { NotPredicate that = (NotPredicate) obj; return predicate.equals(that.predicate); } return false; } @Override public String toString() { return "Predicates.not(" + predicate.toString() + ")"; } } /** * @see Predicates#equalTo(Object) */ private static class IsEqualToPredicate implements Predicate, Serializable { private static final long serialVersionUID = 0; private final T target; private IsEqualToPredicate(T target) { this.target = target; } @Override public boolean test(T t) { return target.equals(t); } @Override public int hashCode() { return target.hashCode(); } @Override public boolean equals(Object obj) { if (obj instanceof IsEqualToPredicate) { IsEqualToPredicate that = (IsEqualToPredicate) obj; return target.equals(that.target); } return false; } @Override public String toString() { return "Predicates.equalTo(" + target + ")"; } } /** * @see Predicates#in(Collection) */ private static class InPredicate implements Predicate, Serializable { private static final long serialVersionUID = 0; private final Collection target; private InPredicate(Collection target) { this.target = Preconditions.checkNotNull(target); } @Override public boolean test(T t) { try { return target.contains(t); } catch (NullPointerException e) { return false; } catch (ClassCastException e) { return false; } } @Override public boolean equals(Object obj) { if (obj instanceof InPredicate) { InPredicate that = (InPredicate) obj; return target.equals(that.target); } return false; } @Override public int hashCode() { return target.hashCode(); } @Override public String toString() { return "Predicates.in(" + target + ")"; } } /** * @see Predicates#compose(Predicate, Function) */ private static class CompositionPredicate implements Predicate, Serializable { private static final long serialVersionUID = 0; final Predicate p; final Function f; private CompositionPredicate(Predicate p, Function f) { this.p = Preconditions.checkNotNull(p); this.f = Preconditions.checkNotNull(f); } @Override public boolean test(A a) { return p.test(f.apply(a)); } @Override public boolean equals(Object obj) { if (obj instanceof CompositionPredicate) { CompositionPredicate that = (CompositionPredicate) obj; return f.equals(that.f) && p.equals(that.p); } return false; } @Override public int hashCode() { return f.hashCode() ^ p.hashCode(); } @Override public String toString() { return p.toString() + "(" + f.toString() + ")"; } } }