| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| TransformedProcedure |
|
| 1.5;1.5 | ||||
| TransformedProcedure$1 |
|
| 1.5;1.5 | ||||
| TransformedProcedure$Helper |
|
| 1.5;1.5 |
| 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.Function; | |
| 22 | import org.apache.commons.functor.Procedure; | |
| 23 | import org.apache.commons.functor.UnaryProcedure; | |
| 24 | ||
| 25 | /** | |
| 26 | * A Procedure composed of a Function whose result is then run through a UnaryProcedure. | |
| 27 | * @version $Revision: 1171154 $ $Date: 2011-09-15 17:58:38 +0200 (Thu, 15 Sep 2011) $ | |
| 28 | * @author Matt Benson | |
| 29 | */ | |
| 30 | public class TransformedProcedure implements Procedure, Serializable { | |
| 31 | /** | |
| 32 | * serialVersionUID declaration. | |
| 33 | */ | |
| 34 | private static final long serialVersionUID = -4111958123789033410L; | |
| 35 | ||
| 36 | /** Base hash integer used to shift hash */ | |
| 37 | private static final int HASH_SHIFT = 2; | |
| 38 | ||
| 39 | /** | |
| 40 | * Type-remembering helper | |
| 41 | * @param <X> | |
| 42 | */ | |
| 43 | 0 | private static final class Helper<X> implements Procedure, Serializable { |
| 44 | /** | |
| 45 | * serialVersionUID declaration. | |
| 46 | */ | |
| 47 | private static final long serialVersionUID = -4093503167446891318L; | |
| 48 | private Function<? extends X> function; | |
| 49 | private UnaryProcedure<? super X> procedure; | |
| 50 | ||
| 51 | /** | |
| 52 | * Create a new Helper. | |
| 53 | * @param function Function | |
| 54 | * @param procedure UnaryFunction | |
| 55 | */ | |
| 56 | 0 | private Helper(Function<? extends X> function, UnaryProcedure<? super X> procedure) { |
| 57 | 0 | this.function = function; |
| 58 | 0 | this.procedure = procedure; |
| 59 | 0 | } |
| 60 | ||
| 61 | /** | |
| 62 | * {@inheritDoc} | |
| 63 | */ | |
| 64 | public void run() { | |
| 65 | 0 | procedure.run(function.evaluate()); |
| 66 | 0 | } |
| 67 | } | |
| 68 | ||
| 69 | private final Helper<?> helper; | |
| 70 | ||
| 71 | /** | |
| 72 | * Create a new TransformedProcedure. | |
| 73 | * @param <X> | |
| 74 | * @param function Function | |
| 75 | * @param procedure UnaryProcedure | |
| 76 | */ | |
| 77 | 0 | public <X> TransformedProcedure(Function<? extends X> function, UnaryProcedure<? super X> procedure) { |
| 78 | 0 | this.helper = new Helper<X>(function, procedure); |
| 79 | 0 | } |
| 80 | ||
| 81 | /** | |
| 82 | * {@inheritDoc} | |
| 83 | */ | |
| 84 | public final void run() { | |
| 85 | 0 | helper.run(); |
| 86 | 0 | } |
| 87 | ||
| 88 | /** | |
| 89 | * {@inheritDoc} | |
| 90 | */ | |
| 91 | @Override | |
| 92 | public final boolean equals(Object obj) { | |
| 93 | 0 | return obj == this || obj instanceof TransformedProcedure |
| 94 | && equals((TransformedProcedure) obj); | |
| 95 | } | |
| 96 | ||
| 97 | /** | |
| 98 | * Learn whether another TransformedProcedure is equal to <code>this</code>. | |
| 99 | * @param that instance to test | |
| 100 | * @return whether equal | |
| 101 | */ | |
| 102 | public final boolean equals(TransformedProcedure that) { | |
| 103 | 0 | return that != null && that.helper.function.equals(this.helper.function) |
| 104 | && that.helper.procedure.equals(this.helper.procedure); | |
| 105 | } | |
| 106 | ||
| 107 | /** | |
| 108 | * {@inheritDoc} | |
| 109 | */ | |
| 110 | @Override | |
| 111 | public int hashCode() { | |
| 112 | 0 | int result = "TransformedProcedure".hashCode(); |
| 113 | 0 | result <<= HASH_SHIFT; |
| 114 | 0 | result |= helper.procedure.hashCode(); |
| 115 | 0 | result <<= HASH_SHIFT; |
| 116 | 0 | result |= helper.function.hashCode(); |
| 117 | 0 | return result; |
| 118 | } | |
| 119 | ||
| 120 | /** | |
| 121 | * {@inheritDoc} | |
| 122 | */ | |
| 123 | @Override | |
| 124 | public String toString() { | |
| 125 | 0 | return "TransformedProcedure<" + helper.function + "; " + helper.procedure + ">"; |
| 126 | } | |
| 127 | } |