blob: d3b91842494de0d62f5c6654723e1e5cd0654ec3 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.functor.adapter;
import java.io.Serializable;
import org.apache.commons.functor.BinaryProcedure;
import org.apache.commons.functor.Procedure;
import org.apache.commons.lang3.Validate;
/**
* Adapts a
* {@link BinaryProcedure BinaryProcedure}
* to the
* {@link Procedure Procedure} interface
* using a constant left-side argument.
* <p/>
* Note that although this class implements
* {@link Serializable}, a given instance will
* only be truly <code>Serializable</code> if the
* underlying objects are. Attempts to serialize
* an instance whose delegates are not
* <code>Serializable</code> will result in an exception.
*
* @param <A> the argument type.
* @version $Revision$ $Date$
*/
public final class LeftBoundProcedure<A> implements Procedure<A>, Serializable {
/**
* serialVersionUID declaration.
*/
private static final long serialVersionUID = 7650992440581629785L;
/** The {@link BinaryProcedure BinaryProcedure} I'm wrapping. */
private final BinaryProcedure<Object, ? super A> procedure;
/** The parameter to pass to {@code procedure}. */
private final Object param;
/**
* Create a new LeftBoundProcedure.
* @param <L> the left argument type.
* @param procedure the procedure to adapt
* @param arg the constant argument to use
*/
@SuppressWarnings("unchecked")
public <L> LeftBoundProcedure(BinaryProcedure<? super L, ? super A> procedure, L arg) {
this.procedure =
(BinaryProcedure<Object, ? super A>) Validate.notNull(procedure,
"BinaryProcedure argument was null");
this.param = arg;
}
/**
* {@inheritDoc}
*/
public void run(A obj) {
procedure.run(param, obj);
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object that) {
return that == this || (that instanceof LeftBoundProcedure<?> && equals((LeftBoundProcedure<?>) that));
}
/**
* Learn whether another LeftBoundProcedure is equal to this.
* @param that LeftBoundProcedure to test
* @return boolean
*/
public boolean equals(LeftBoundProcedure<?> that) {
return null != that
&& procedure.equals(that.procedure)
&& (null == param ? null == that.param : param.equals(that.param));
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
int hash = "LeftBoundProcedure".hashCode();
hash <<= 2;
hash ^= procedure.hashCode();
if (null != param) {
hash <<= 2;
hash ^= param.hashCode();
}
return hash;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "LeftBoundProcedure<" + procedure + "(" + param + ",?)>";
}
/**
* Get a Procedure from <code>procedure</code>.
* @param <L> left type
* @param <R> right type
* @param procedure to adapt
* @param arg left side argument
* @return LeftBoundProcedure<R>
*/
public static <L, R> LeftBoundProcedure<R> bind(BinaryProcedure<? super L, ? super R> procedure, L arg) {
return null == procedure ? null : new LeftBoundProcedure<R>(procedure, arg);
}
}