Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
IsInstance |
|
| 1.0;1 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | package org.apache.commons.functor.core; | |
18 | ||
19 | import java.io.Serializable; | |
20 | ||
21 | import org.apache.commons.functor.BinaryPredicate; | |
22 | import org.apache.commons.functor.UnaryPredicate; | |
23 | import org.apache.commons.functor.adapter.RightBoundPredicate; | |
24 | ||
25 | /** | |
26 | * {@link #test Tests} | |
27 | * <code>true</code> iff its argument | |
28 | * {@link Class#isInstance is an instance} | |
29 | * of some specified {@link Class Class}. | |
30 | * | |
31 | * @param <T> the object instance has to be tested against the input class. | |
32 | * @version $Revision: 1160394 $ $Date: 2011-08-22 21:40:35 +0200 (Mon, 22 Aug 2011) $ | |
33 | * @author Rodney Waldhoff | |
34 | */ | |
35 | 172 | public final class IsInstance<T> implements BinaryPredicate<T, Class<?>>, Serializable { |
36 | /** | |
37 | * Basic IsInstanceOf instance. | |
38 | */ | |
39 | 2 | public static final IsInstance<Object> INSTANCE = IsInstance.<Object>instance(); |
40 | /** | |
41 | * serialVersionUID declaration. | |
42 | */ | |
43 | private static final long serialVersionUID = 9104265415387129627L; | |
44 | /** | |
45 | * The non zero number used to shift the initial hashcode. | |
46 | */ | |
47 | private static final int NONZERO_SHIFT_NUMBER = 4; | |
48 | /** | |
49 | * The non zero number used to bitwise or the hashcode. | |
50 | */ | |
51 | private static final int NONZERO_BITWISE_NUMBER = 37; | |
52 | ||
53 | // predicate interface | |
54 | // ------------------------------------------------------------------------ | |
55 | ||
56 | /** | |
57 | * {@inheritDoc} | |
58 | */ | |
59 | public boolean test(T left, Class<?> right) { | |
60 | 106 | return right.isInstance(left); |
61 | } | |
62 | ||
63 | /** | |
64 | * {@inheritDoc} | |
65 | */ | |
66 | public boolean equals(Object that) { | |
67 | 26 | return that instanceof IsInstance<?>; |
68 | } | |
69 | ||
70 | /** | |
71 | * {@inheritDoc} | |
72 | */ | |
73 | public int hashCode() { | |
74 | 34 | return ("IsInstance".hashCode() << NONZERO_SHIFT_NUMBER) | NONZERO_BITWISE_NUMBER; |
75 | } | |
76 | ||
77 | /** | |
78 | * {@inheritDoc} | |
79 | */ | |
80 | public String toString() { | |
81 | 26 | return "IsInstance"; |
82 | } | |
83 | ||
84 | /** | |
85 | * Get an IsInstance instance. | |
86 | * @param <T> the object instance has to be tested against the input class. | |
87 | * @return IsInstance<T> | |
88 | */ | |
89 | public static <T> IsInstance<T> instance() { | |
90 | 4 | return new IsInstance<T>(); |
91 | } | |
92 | ||
93 | /** | |
94 | * Get an IsInstanceOf UnaryPredicate. | |
95 | * @param <T> the object instance has to be tested against the input class. | |
96 | * @param clazz bound right-side argument | |
97 | * @return UnaryPredicate<T> | |
98 | */ | |
99 | public static <T> UnaryPredicate<T> of(Class<?> clazz) { | |
100 | 60 | return RightBoundPredicate.bind(new IsInstance<T>(), clazz); |
101 | } | |
102 | } |