blob: f89c2cba9845dad0c7b0dfb941ee0851854833a7 [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.core.composite;
import java.io.Serializable;
import org.apache.commons.functor.Function;
import org.apache.commons.functor.Procedure;
import org.apache.commons.functor.adapter.ProcedureFunction;
import org.apache.commons.lang3.Validate;
/**
* A {@link Procedure Procedure}
* representing the composition of
* {@link Function Functions},
* "chaining" the output of one to the input
* of another. For example,
* <pre>new CompositeProcedure(p).of(f)</pre>
* {@link #run runs} to
* <code>p.run(f.evaluate(obj))</code>, and
* <pre>new CompositeProcedure(p).of(f).of(g)</pre>
* {@link #run runs}
* <code>p.run(f.evaluate(g.evaluate(obj)))</code>.
* <p>
* Note that although this class implements
* {@link Serializable}, a given instance will
* only be truly <code>Serializable</code> if all the
* underlying functors are. Attempts to serialize
* an instance whose delegates are not all
* <code>Serializable</code> will result in an exception.
* </p>
* @param <A> the procedure argument type.
* @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $
*/
public final class CompositeProcedure<A> implements Procedure<A>, Serializable {
/**
* serialVersionUID declaration.
*/
private static final long serialVersionUID = -7496282561355676509L;
// attributes
// ------------------------------------------------------------------------
/**
* The adapted composite procedure.
*/
private final CompositeFunction<? super A, Object> function;
// constructor
// ------------------------------------------------------------------------
/**
* Create a new CompositeProcedure.
* @param procedure final Procedure to run
*/
public CompositeProcedure(Procedure<? super A> procedure) {
this.function =
new CompositeFunction<A, Object>(
new ProcedureFunction<A, Object>(Validate.notNull(
procedure, "procedure must not be null")));
}
/**
* Create a new CompositeProcedure.
* @param function final CompositeFunction to run
*/
private CompositeProcedure(CompositeFunction<? super A, Object> function) {
this.function = function;
}
// modifiers
// ------------------------------------------------------------------------
/**
* Fluently obtain a CompositeProcedure that runs our procedure against the result of the preceding function.
* @param <T> the input function left argument and output procedure argument type
* @param preceding Function
* @return CompositeProcedure<P>
*/
public <T> CompositeProcedure<T> of(Function<? super T, ? extends A> preceding) {
return new CompositeProcedure<T>(function.of(preceding));
}
// predicate interface
// ------------------------------------------------------------------------
/**
* {@inheritDoc}
*/
public void run(A obj) {
function.evaluate(obj);
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object that) {
return that == this || (that instanceof CompositeProcedure<?>
&& equals((CompositeProcedure<?>) that));
}
/**
* Learn whether another CompositeProcedure is equal to this.
* @param that CompositeProcedure to test
* @return boolean
*/
public boolean equals(CompositeProcedure<?> that) {
return null != that && function.equals(that.function);
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
int hash = "CompositeProcedure".hashCode();
hash <<= 2;
hash ^= function.hashCode();
return hash;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "CompositeProcedure<" + function + ">";
}
}