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 org.apache.commons.functor.NullaryFunction; 020import org.apache.commons.functor.Function; 021import org.apache.commons.lang3.Validate; 022 023/** 024 * Adapts a 025 * {@link Function Function} 026 * to the 027 * {@link NullaryFunction NullaryFunction} interface 028 * using a constant unary argument. 029 * 030 * @param <T> the returned value type. 031 * @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $ 032 */ 033public final class BoundNullaryFunction<T> implements NullaryFunction<T> { 034 /** The {@link Function Function} I'm wrapping. */ 035 private final Function<Object, ? extends T> function; 036 /** The argument to pass to {@code function}. */ 037 private final Object arg; 038 039 /** 040 * Create a new BoundNullaryFunction instance. 041 * @param <A> the argument value type 042 * @param function the function to adapt 043 * @param arg the constant argument to use 044 */ 045 @SuppressWarnings("unchecked") 046 public <A> BoundNullaryFunction(Function<? super A, ? extends T> function, A arg) { 047 this.function = 048 (Function<Object, ? extends T>) Validate.notNull(function, 049 "Function argument was null"); 050 this.arg = arg; 051 } 052 053 /** 054 * {@inheritDoc} 055 */ 056 public T evaluate() { 057 return function.evaluate(arg); 058 } 059 060 /** 061 * {@inheritDoc} 062 */ 063 @Override 064 public boolean equals(Object obj) { 065 if (obj == this) { 066 return true; 067 } 068 if (!(obj instanceof BoundNullaryFunction<?>)) { 069 return false; 070 } 071 BoundNullaryFunction<?> that = (BoundNullaryFunction<?>) obj; 072 if (!(that.function.equals(this.function))) { 073 return false; 074 } 075 return that.arg == this.arg || that.arg != null && that.arg.equals(this.arg); 076 } 077 078 /** 079 * {@inheritDoc} 080 */ 081 @Override 082 public int hashCode() { 083 int result = "BoundNullaryFunction".hashCode(); 084 result <<= 2; 085 result |= function.hashCode(); 086 result <<= 2; 087 return arg == null ? result : result | arg.hashCode(); 088 } 089 090 /** 091 * {@inheritDoc} 092 */ 093 @Override 094 public String toString() { 095 return "BoundNullaryFunction<" + function.toString() + "(" + arg + ")>"; 096 } 097 098 /** 099 * Adapt the given, possibly-<code>null</code>, 100 * {@link Function Function} to the 101 * {@link NullaryFunction NullaryFunction} interface by binding 102 * the specified <code>Object</code> as a constant 103 * argument. 104 * When the given <code>Function</code> is <code>null</code>, 105 * returns <code>null</code>. 106 * @param <A> input type 107 * @param <T> result type 108 * @param function the possibly-<code>null</code> 109 * {@link Function Function} to adapt 110 * @param arg the object to bind as a constant argument 111 * @return a <code>BoundNullaryFunction</code> wrapping the given 112 * {@link Function Function}, or <code>null</code> 113 * if the given <code>Function</code> is <code>null</code> 114 */ 115 public static <A, T> BoundNullaryFunction<T> bind(Function<? super A, ? extends T> function, A arg) { 116 return null == function ? null : new BoundNullaryFunction<T>(function, arg); 117 } 118 119}