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