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 */ 017 package org.apache.commons.functor.adapter; 018 019 import org.apache.commons.functor.BinaryProcedure; 020 import org.apache.commons.functor.UnaryProcedure; 021 022 /** 023 * Adapts a BinaryProcedure as a UnaryProcedure by sending the same argument to both sides of the BinaryProcedure. 024 * @param <A> the argument type. 025 * @version $Revision: 1160375 $ $Date: 2011-08-22 21:17:24 +0200 (Mon, 22 Aug 2011) $ 026 * @author Matt Benson 027 */ 028 public final class BinaryProcedureUnaryProcedure<A> implements UnaryProcedure<A> { 029 /** 030 * The adapted procedure. 031 */ 032 private final BinaryProcedure<? super A, ? super A> procedure; 033 034 /** 035 * Create a new BinaryProcedureUnaryProcedure. 036 * @param procedure to adapt 037 */ 038 public BinaryProcedureUnaryProcedure(BinaryProcedure<? super A, ? super A> procedure) { 039 if (null == procedure) { 040 throw new IllegalArgumentException("BinaryProcedure argument was null"); 041 } 042 this.procedure = procedure; 043 } 044 045 /** 046 * {@inheritDoc} 047 */ 048 public void run(A obj) { 049 procedure.run(obj, obj); 050 }; 051 052 /** 053 * {@inheritDoc} 054 */ 055 @Override 056 public boolean equals(Object obj) { 057 return obj == this || obj instanceof BinaryProcedureUnaryProcedure<?> 058 && equals((BinaryProcedureUnaryProcedure<?>) obj); 059 } 060 061 /** 062 * Learn whether another BinaryProcedureUnaryProcedure is equal to 063 * <code>this</code>. 064 * 065 * @param that BinaryProcedureUnaryProcedure to check 066 * 067 * @return whether equal 068 */ 069 public boolean equals(BinaryProcedureUnaryProcedure<?> that) { 070 return that != null && that.procedure.equals(this.procedure); 071 } 072 073 /** 074 * {@inheritDoc} 075 */ 076 @Override 077 public int hashCode() { 078 return ("BinaryProcedureUnaryProcedure".hashCode() << 2) | procedure.hashCode(); 079 } 080 081 /** 082 * {@inheritDoc} 083 */ 084 @Override 085 public String toString() { 086 return "BinaryProcedureUnaryProcedure<" + procedure + ">"; 087 } 088 089 /** 090 * Adapt a BinaryProcedure as a UnaryProcedure. 091 * @param <A> argument type 092 * @param procedure BinaryProcedure to adapt 093 * @return UnaryProcedure<A> 094 */ 095 public static <A> UnaryProcedure<A> adapt(BinaryProcedure<? super A, ? super A> procedure) { 096 return null == procedure ? null : new BinaryProcedureUnaryProcedure<A>(procedure); 097 } 098 099 }