001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.functor.core; 018 019 import java.io.Serializable; 020 021 import org.apache.commons.functor.BinaryPredicate; 022 import org.apache.commons.functor.UnaryPredicate; 023 import org.apache.commons.functor.adapter.RightBoundPredicate; 024 025 /** 026 * {@link #test Tests} 027 * <code>true</code> iff its argument 028 * {@link Class#isInstance is an instance} 029 * of some specified {@link Class Class}. 030 * 031 * @param <T> the object instance has to be tested against the input class. 032 * @version $Revision: 1160394 $ $Date: 2011-08-22 21:40:35 +0200 (Mon, 22 Aug 2011) $ 033 * @author Rodney Waldhoff 034 */ 035 public final class IsInstance<T> implements BinaryPredicate<T, Class<?>>, Serializable { 036 /** 037 * Basic IsInstanceOf instance. 038 */ 039 public static final IsInstance<Object> INSTANCE = IsInstance.<Object>instance(); 040 /** 041 * serialVersionUID declaration. 042 */ 043 private static final long serialVersionUID = 9104265415387129627L; 044 /** 045 * The non zero number used to shift the initial hashcode. 046 */ 047 private static final int NONZERO_SHIFT_NUMBER = 4; 048 /** 049 * The non zero number used to bitwise or the hashcode. 050 */ 051 private static final int NONZERO_BITWISE_NUMBER = 37; 052 053 // predicate interface 054 // ------------------------------------------------------------------------ 055 056 /** 057 * {@inheritDoc} 058 */ 059 public boolean test(T left, Class<?> right) { 060 return right.isInstance(left); 061 } 062 063 /** 064 * {@inheritDoc} 065 */ 066 public boolean equals(Object that) { 067 return that instanceof IsInstance<?>; 068 } 069 070 /** 071 * {@inheritDoc} 072 */ 073 public int hashCode() { 074 return ("IsInstance".hashCode() << NONZERO_SHIFT_NUMBER) | NONZERO_BITWISE_NUMBER; 075 } 076 077 /** 078 * {@inheritDoc} 079 */ 080 public String toString() { 081 return "IsInstance"; 082 } 083 084 /** 085 * Get an IsInstance instance. 086 * @param <T> the object instance has to be tested against the input class. 087 * @return IsInstance<T> 088 */ 089 public static <T> IsInstance<T> instance() { 090 return new IsInstance<T>(); 091 } 092 093 /** 094 * Get an IsInstanceOf UnaryPredicate. 095 * @param <T> the object instance has to be tested against the input class. 096 * @param clazz bound right-side argument 097 * @return UnaryPredicate<T> 098 */ 099 public static <T> UnaryPredicate<T> of(Class<?> clazz) { 100 return RightBoundPredicate.bind(new IsInstance<T>(), clazz); 101 } 102 }