| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| FullyBoundProcedure |
|
| 3.0;3 |
| 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.BinaryProcedure; | |
| 22 | import org.apache.commons.functor.Procedure; | |
| 23 | ||
| 24 | /** | |
| 25 | * Adapts a | |
| 26 | * {@link BinaryProcedure BinaryProcedure} | |
| 27 | * to the | |
| 28 | * {@link Procedure Procedure} interface | |
| 29 | * using a constant left-side argument. | |
| 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 objects are. Attempts to serialize | |
| 35 | * an instance whose delegates are not | |
| 36 | * <code>Serializable</code> will result in an exception. | |
| 37 | * | |
| 38 | * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $ | |
| 39 | * @author Matt Benson | |
| 40 | */ | |
| 41 | public final class FullyBoundProcedure implements Procedure, Serializable { | |
| 42 | /** | |
| 43 | * serialVersionUID declaration. | |
| 44 | */ | |
| 45 | private static final long serialVersionUID = -904891610081737737L; | |
| 46 | /** The {@link BinaryProcedure BinaryProcedure} I'm wrapping. */ | |
| 47 | private final BinaryProcedure<Object, Object> procedure; | |
| 48 | /** The left parameter to pass to {@code procedure}. */ | |
| 49 | private final Object left; | |
| 50 | /** The right parameter to pass to {@code procedure}. */ | |
| 51 | private final Object right; | |
| 52 | ||
| 53 | /** | |
| 54 | * Create a new FullyBoundProcedure instance. | |
| 55 | * @param <L> left type | |
| 56 | * @param <R> right type | |
| 57 | * @param procedure the procedure to adapt | |
| 58 | * @param left the left argument to use | |
| 59 | * @param right the right argument to use | |
| 60 | */ | |
| 61 | @SuppressWarnings("unchecked") | |
| 62 | 24 | public <L, R> FullyBoundProcedure(BinaryProcedure<? super L, ? super R> procedure, L left, R right) { |
| 63 | 24 | if (procedure == null) { |
| 64 | 0 | throw new IllegalArgumentException("BinaryProcedure argument was null"); |
| 65 | } | |
| 66 | 24 | this.procedure = (BinaryProcedure<Object, Object>) procedure; |
| 67 | 24 | this.left = left; |
| 68 | 24 | this.right = right; |
| 69 | 24 | } |
| 70 | ||
| 71 | /** | |
| 72 | * {@inheritDoc} | |
| 73 | */ | |
| 74 | public void run() { | |
| 75 | 2 | procedure.run(left, right); |
| 76 | 2 | } |
| 77 | ||
| 78 | /** | |
| 79 | * {@inheritDoc} | |
| 80 | */ | |
| 81 | public boolean equals(Object that) { | |
| 82 | 30 | return that == this || (that instanceof FullyBoundProcedure && equals((FullyBoundProcedure) that)); |
| 83 | } | |
| 84 | ||
| 85 | /** | |
| 86 | * Learn whether another FullyBoundProcedure is equal to this. | |
| 87 | * @param that FullyBoundProcedure to test | |
| 88 | * @return boolean | |
| 89 | */ | |
| 90 | public boolean equals(FullyBoundProcedure that) { | |
| 91 | 22 | return null != that && (null == procedure ? null == that.procedure : procedure.equals(that.procedure)) |
| 92 | && (null == left ? null == that.left : left.equals(that.left)) | |
| 93 | && (null == right ? null == that.right : right.equals(that.right)); | |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * {@inheritDoc} | |
| 98 | */ | |
| 99 | public int hashCode() { | |
| 100 | 32 | int hash = "FullyBoundProcedure".hashCode(); |
| 101 | 32 | if (null != procedure) { |
| 102 | 32 | hash <<= 2; |
| 103 | 32 | hash ^= procedure.hashCode(); |
| 104 | } | |
| 105 | 32 | hash <<= 2; |
| 106 | 32 | if (null != left) { |
| 107 | 30 | hash ^= left.hashCode(); |
| 108 | } | |
| 109 | 32 | hash <<= 2; |
| 110 | 32 | if (null != right) { |
| 111 | 0 | hash ^= right.hashCode(); |
| 112 | } | |
| 113 | 32 | return hash; |
| 114 | } | |
| 115 | ||
| 116 | /** | |
| 117 | * {@inheritDoc} | |
| 118 | */ | |
| 119 | public String toString() { | |
| 120 | 24 | return "FullyBoundProcedure<" + procedure + "(" + left + ", " + right + ")>"; |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * Adapt a BinaryProcedure to the Procedure interface. | |
| 125 | * @param <L> left type | |
| 126 | * @param <R> right type | |
| 127 | * @param procedure to adapt | |
| 128 | * @param left left side argument | |
| 129 | * @param right right side argument | |
| 130 | * @return FullyBoundProcedure | |
| 131 | */ | |
| 132 | public static <L, R> FullyBoundProcedure bind(BinaryProcedure<? super L, ? super R> procedure, L left, R right) { | |
| 133 | 6 | return null == procedure ? null : new FullyBoundProcedure(procedure, left, right); |
| 134 | } | |
| 135 | ||
| 136 | } |