001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.functor.adapter; 018 019import java.io.Serializable; 020 021import org.apache.commons.functor.BinaryFunction; 022import org.apache.commons.functor.UnaryFunction; 023import org.apache.commons.lang3.Validate; 024 025/** 026 * Adapts a 027 * {@link UnaryFunction UnaryFunction} 028 * to the 029 * {@link BinaryFunction BinaryFunction} interface 030 * by ignoring the first binary argument. 031 * <p/> 032 * Note that although this class implements 033 * {@link Serializable}, a given instance will 034 * only be truly <code>Serializable</code> if the 035 * underlying functor is. Attempts to serialize 036 * an instance whose delegate is not 037 * <code>Serializable</code> will result in an exception. 038 * 039 * @param <L> the left argument type. 040 * @param <R> the right argument type. 041 * @param <T> the returned value type. 042 * @version $Revision: 1365377 $ $Date: 2012-07-24 20:59:23 -0400 (Tue, 24 Jul 2012) $ 043 */ 044public final class IgnoreLeftFunction<L, R, T> implements BinaryFunction<L, R, T>, Serializable { 045 /** 046 * serialVersionUID declaration. 047 */ 048 private static final long serialVersionUID = 4677703245851183542L; 049 /** The {@link UnaryFunction UnaryFunction} I'm wrapping. */ 050 private final UnaryFunction<? super R, ? extends T> function; 051 052 /** 053 * Create a new IgnoreLeftFunction. 054 * @param function UnaryFunction for right argument 055 */ 056 public IgnoreLeftFunction(UnaryFunction<? super R, ? extends T> function) { 057 this.function = Validate.notNull(function, "UnaryFunction argument was null"); 058 } 059 060 /** 061 * {@inheritDoc} 062 */ 063 public T evaluate(L left, R right) { 064 return function.evaluate(right); 065 } 066 067 /** 068 * {@inheritDoc} 069 */ 070 @Override 071 public boolean equals(Object that) { 072 return that == this || (that instanceof IgnoreLeftFunction<?, ?, ?> 073 && equals((IgnoreLeftFunction<?, ?, ?>) that)); 074 } 075 076 /** 077 * Learn whether another IgnoreLeftFunction is equal to this. 078 * @param that IgnoreLeftFunction to test 079 * @return boolean 080 */ 081 public boolean equals(IgnoreLeftFunction<?, ?, ?> that) { 082 return null != that && function.equals(that.function); 083 } 084 085 /** 086 * {@inheritDoc} 087 */ 088 @Override 089 public int hashCode() { 090 int hash = "IgnoreLeftFunction".hashCode(); 091 hash ^= function.hashCode(); 092 return hash; 093 } 094 095 /** 096 * {@inheritDoc} 097 */ 098 @Override 099 public String toString() { 100 return "IgnoreLeftFunction<" + function + ">"; 101 } 102 103 /** 104 * Adapt a UnaryFunction to the BinaryFunction interface. 105 * @param <L> left type 106 * @param <R> right type 107 * @param <T> result type 108 * @param function to adapt 109 * @return IgnoreLeftFunction 110 */ 111 public static <L, R, T> IgnoreLeftFunction<L, R, T> adapt(UnaryFunction<? super R, ? extends T> function) { 112 return null == function ? null : new IgnoreLeftFunction<L, R, T>(function); 113 } 114 115}