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

leap.lang.Predicates Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2013 the original author or 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 leap.lang;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.util.Map.Entry;
import java.util.function.Predicate;

import leap.lang.annotation.Nullable;

//some codes copy from google guava library 15.0-SNAPSHOT,under Apache License 2.0
public class Predicates {

	/**
	 * Returns a predicate that evaluates to {@code true} if the object reference being tested is null.
	 */
	public static  Predicate isNull() {
		return ObjectPredicate.IS_NULL.withNarrowedType();
	}
	
	public static  Predicate nameEquals(final String name){
		return new Predicate() {
			public boolean test(T object) {
				return Strings.equals(object.getName(), name);
			}
		};
	}

	public static  Predicate nameEqualsIgnoreCase(final String name){
		return new Predicate() {
			public boolean test(T object) {
				return Strings.equalsIgnoreCase(object.getName(),name);
			}
		};
	}
	
	public static  Predicate> entryKeyEquals(final K key){
		return new Predicate>() {
			public boolean test(Entry entry) {
	            return entry.getKey().equals(key);
            }
		};
	}
	
	public static  Predicate> entryKeyEqualsIgnoreCase(final String key){
		return new Predicate>() {
			public boolean test(Entry entry) {
	            return entry.getKey().equalsIgnoreCase(key);
            }
		};
	}	
	
	/**
	 * Returns a predicate that evaluates to {@code true} if the object being tested is an instance of the given class.
	 * If the object being tested is {@code null} this predicate evaluates to {@code false}.
	 * 
	 * 

* If you want to filter an {@code Iterable} to narrow its type, consider using * {@link com.google.common.collect.Iterables#filter(Iterable, Class)} in preference. * *

* Warning: contrary to the typical assumptions about predicates (as documented at {@link Predicate#apply}), * the returned predicate may not be consistent with equals. For example, {@code instanceOf(ArrayList.class)} * will yield different results for the two equal instances {@code Lists.newArrayList(1)} and * {@code Arrays.asList(1)}. */ public static Predicate instanceOf(Class clazz) { return new InstanceOfPredicate(clazz); } @SuppressWarnings("unchecked") public static Predicate> annotatedWith(final Class... annotationClasses){ return new Predicate>() { @Override public boolean test(Class input) { for(int i=0;i Predicate equalTo(@Nullable T target) { return (target == null) ? Predicates. isNull() : new IsEqualToPredicate(target); } enum ObjectPredicate implements Predicate { ALWAYS_TRUE { @Override public boolean test(@Nullable Object o) { return true; } }, ALWAYS_FALSE { @Override public boolean test(@Nullable Object o) { return false; } }, IS_NULL { @Override public boolean test(@Nullable Object o) { return o == null; } }, NOT_NULL { @Override public boolean test(@Nullable Object o) { return o != null; } }; @SuppressWarnings("unchecked") // these Object predicates work for any T Predicate withNarrowedType() { return (Predicate) this; } } private static class InstanceOfPredicate implements Predicate, Serializable { private final Class clazz; private InstanceOfPredicate(Class clazz) { this.clazz = Args.notNull(clazz); } @Override public boolean test(@Nullable Object o) { return clazz.isInstance(o); } @Override public int hashCode() { return clazz.hashCode(); } @Override public boolean equals(@Nullable Object obj) { if (obj instanceof InstanceOfPredicate) { InstanceOfPredicate that = (InstanceOfPredicate) obj; return clazz == that.clazz; } return false; } @Override public String toString() { return "IsInstanceOf(" + clazz.getName() + ")"; } private static final long serialVersionUID = 0; } /** @see Predicates#equalTo(Object) */ private static class IsEqualToPredicate implements Predicate, Serializable { 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(@Nullable Object obj) { if (obj instanceof IsEqualToPredicate) { IsEqualToPredicate that = (IsEqualToPredicate) obj; return target.equals(that.target); } return false; } @Override public String toString() { return "IsEqualTo(" + target + ")"; } private static final long serialVersionUID = 0; } protected Predicates(){ } }