Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
UnaryProcedureUnaryFunction |
|
| 2.142857142857143;2.143 |
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.adapter; | |
18 | ||
19 | import java.io.Serializable; | |
20 | ||
21 | import org.apache.commons.functor.UnaryFunction; | |
22 | import org.apache.commons.functor.UnaryProcedure; | |
23 | ||
24 | /** | |
25 | * Adapts a | |
26 | * {@link UnaryProcedure UnaryProcedure} | |
27 | * to the | |
28 | * {@link UnaryFunction UnaryFunction} interface | |
29 | * by always returning <code>null</code>. | |
30 | * <p/> | |
31 | * Note that although this class implements | |
32 | * {@link Serializable}, a given instance will | |
33 | * only be truly <code>Serializable</code> if the | |
34 | * underlying procedure is. Attempts to serialize | |
35 | * an instance whose delegate is not | |
36 | * <code>Serializable</code> will result in an exception. | |
37 | * | |
38 | * @param <A> the argument type. | |
39 | * @param <T> the returned value type. | |
40 | * @version $Revision: 1157657 $ $Date: 2011-08-14 22:28:19 +0200 (Sun, 14 Aug 2011) $ | |
41 | * @author Rodney Waldhoff | |
42 | */ | |
43 | public final class UnaryProcedureUnaryFunction<A, T> implements UnaryFunction<A, T>, Serializable { | |
44 | /** | |
45 | * serialVersionUID declaration. | |
46 | */ | |
47 | private static final long serialVersionUID = 6153848695167906659L; | |
48 | /** The {@link UnaryProcedure UnaryProcedure} I'm wrapping. */ | |
49 | private final UnaryProcedure<? super A> procedure; | |
50 | ||
51 | /** | |
52 | * Create a new UnaryProcedureUnaryFunction. | |
53 | * @param procedure to adapt | |
54 | */ | |
55 | 28 | public UnaryProcedureUnaryFunction(UnaryProcedure<? super A> procedure) { |
56 | 28 | if (procedure == null) { |
57 | 0 | throw new IllegalArgumentException("UnaryProcedure argument was null"); |
58 | } | |
59 | 28 | this.procedure = procedure; |
60 | 28 | } |
61 | ||
62 | /** | |
63 | * {@inheritDoc} | |
64 | */ | |
65 | public T evaluate(A obj) { | |
66 | 2 | procedure.run(obj); |
67 | 2 | return null; |
68 | } | |
69 | ||
70 | /** | |
71 | * {@inheritDoc} | |
72 | */ | |
73 | public boolean equals(Object that) { | |
74 | 30 | return that == this || (that instanceof UnaryProcedureUnaryFunction<?, ?> |
75 | && equals((UnaryProcedureUnaryFunction<?, ?>) that)); | |
76 | } | |
77 | ||
78 | /** | |
79 | * Learn whether a given UnaryProcedureUnaryFunction is equal to this. | |
80 | * @param that UnaryProcedureUnaryFunction to test | |
81 | * @return boolean | |
82 | */ | |
83 | public boolean equals(UnaryProcedureUnaryFunction<?, ?> that) { | |
84 | 20 | return null != that && (null == procedure ? null == that.procedure : procedure.equals(that.procedure)); |
85 | } | |
86 | ||
87 | /** | |
88 | * {@inheritDoc} | |
89 | */ | |
90 | public int hashCode() { | |
91 | 40 | int hash = "UnaryProcedureUnaryFunction".hashCode(); |
92 | 40 | if (null != procedure) { |
93 | 40 | hash ^= procedure.hashCode(); |
94 | } | |
95 | 40 | return hash; |
96 | } | |
97 | ||
98 | /** | |
99 | * {@inheritDoc} | |
100 | */ | |
101 | public String toString() { | |
102 | 24 | return "UnaryProcedureUnaryFunction<" + procedure + ">"; |
103 | } | |
104 | ||
105 | /** | |
106 | * Adapt a UnaryProcedure to the UnaryFunction interface. | |
107 | * @param <A> the argument type. | |
108 | * @param <T> the returned value type. | |
109 | * @param procedure to adapt | |
110 | * @return UnaryProcedureUnaryFunction | |
111 | */ | |
112 | public static <A, T> UnaryProcedureUnaryFunction<A, T> adapt(UnaryProcedure<? super A> procedure) { | |
113 | 2 | return null == procedure ? null : new UnaryProcedureUnaryFunction<A, T>(procedure); |
114 | } | |
115 | ||
116 | } |