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