Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CompositeUnaryProcedure |
|
| 1.625;1.625 |
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.composite; | |
18 | ||
19 | import java.io.Serializable; | |
20 | ||
21 | import org.apache.commons.functor.UnaryFunction; | |
22 | import org.apache.commons.functor.UnaryProcedure; | |
23 | import org.apache.commons.functor.adapter.UnaryProcedureUnaryFunction; | |
24 | ||
25 | /** | |
26 | * A {@link UnaryProcedure UnaryProcedure} | |
27 | * representing the composition of | |
28 | * {@link UnaryFunction UnaryFunctions}, | |
29 | * "chaining" the output of one to the input | |
30 | * of another. For example, | |
31 | * <pre>new CompositeUnaryProcedure(p).of(f)</pre> | |
32 | * {@link #run runs} to | |
33 | * <code>p.run(f.evaluate(obj))</code>, and | |
34 | * <pre>new CompositeUnaryProcedure(p).of(f).of(g)</pre> | |
35 | * {@link #run runs} | |
36 | * <code>p.run(f.evaluate(g.evaluate(obj)))</code>. | |
37 | * <p> | |
38 | * Note that although this class implements | |
39 | * {@link Serializable}, a given instance will | |
40 | * only be truly <code>Serializable</code> if all the | |
41 | * underlying functors are. Attempts to serialize | |
42 | * an instance whose delegates are not all | |
43 | * <code>Serializable</code> will result in an exception. | |
44 | * </p> | |
45 | * @version $Revision: 1166378 $ $Date: 2011-09-07 22:17:16 +0200 (Wed, 07 Sep 2011) $ | |
46 | * @author Rodney Waldhoff | |
47 | */ | |
48 | public final class CompositeUnaryProcedure<A> implements UnaryProcedure<A>, Serializable { | |
49 | /** | |
50 | * serialVersionUID declaration. | |
51 | */ | |
52 | private static final long serialVersionUID = -7496282561355676509L; | |
53 | // attributes | |
54 | // ------------------------------------------------------------------------ | |
55 | private final CompositeUnaryFunction<? super A, Object> function; | |
56 | ||
57 | // constructor | |
58 | // ------------------------------------------------------------------------ | |
59 | /** | |
60 | * Create a new CompositeUnaryProcedure. | |
61 | * @param procedure final UnaryProcedure to run | |
62 | */ | |
63 | 10 | public CompositeUnaryProcedure(UnaryProcedure<? super A> procedure) { |
64 | 10 | if (null == procedure) { |
65 | 0 | throw new IllegalArgumentException("procedure must not be null"); |
66 | } | |
67 | 10 | this.function = new CompositeUnaryFunction<A, Object>(new UnaryProcedureUnaryFunction<A, Object>(procedure)); |
68 | 10 | } |
69 | ||
70 | 10 | private CompositeUnaryProcedure(CompositeUnaryFunction<? super A, Object> function) { |
71 | 10 | this.function = function; |
72 | 10 | } |
73 | ||
74 | // modifiers | |
75 | // ------------------------------------------------------------------------ | |
76 | /** | |
77 | * Fluently obtain a CompositeUnaryProcedure that runs our procedure against the result of the preceding function. | |
78 | * @param preceding UnaryFunction | |
79 | * @return CompositeUnaryProcedure<P> | |
80 | */ | |
81 | public <T> CompositeUnaryProcedure<T> of(UnaryFunction<? super T, ? extends A> preceding) { | |
82 | 10 | return new CompositeUnaryProcedure<T>(function.of(preceding)); |
83 | } | |
84 | ||
85 | // predicate interface | |
86 | // ------------------------------------------------------------------------ | |
87 | /** | |
88 | * {@inheritDoc} | |
89 | */ | |
90 | public void run(A obj) { | |
91 | 0 | function.evaluate(obj); |
92 | 0 | } |
93 | ||
94 | /** | |
95 | * {@inheritDoc} | |
96 | */ | |
97 | public boolean equals(Object that) { | |
98 | 10 | return that == this || (that instanceof CompositeUnaryProcedure<?> |
99 | && equals((CompositeUnaryProcedure<?>) that)); | |
100 | } | |
101 | ||
102 | /** | |
103 | * Learn whether another CompositeUnaryProcedure is equal to this. | |
104 | * @param that CompositeUnaryProcedure to test | |
105 | * @return boolean | |
106 | */ | |
107 | public boolean equals(CompositeUnaryProcedure<?> that) { | |
108 | 6 | return null != that && function.equals(that.function); |
109 | } | |
110 | ||
111 | /** | |
112 | * {@inheritDoc} | |
113 | */ | |
114 | public int hashCode() { | |
115 | 14 | int hash = "CompositeUnaryProcedure".hashCode(); |
116 | 14 | hash <<= 2; |
117 | 14 | hash ^= function.hashCode(); |
118 | 14 | return hash; |
119 | } | |
120 | ||
121 | /** | |
122 | * {@inheritDoc} | |
123 | */ | |
124 | public String toString() { | |
125 | 6 | return "CompositeUnaryProcedure<" + function + ">"; |
126 | } | |
127 | ||
128 | } |