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 java.io.Serializable;
020
021 import org.apache.commons.functor.UnaryFunction;
022 import org.apache.commons.functor.UnaryProcedure;
023
024 /**
025 * Adapts a
026 * {@link UnaryProcedure UnaryProcedure}
027 * to the
028 * {@link UnaryFunction UnaryFunction} interface
029 * by always returning <code>null</code>.
030 * <p/>
031 * Note that although this class implements
032 * {@link Serializable}, a given instance will
033 * only be truly <code>Serializable</code> if the
034 * underlying procedure is. Attempts to serialize
035 * an instance whose delegate is not
036 * <code>Serializable</code> will result in an exception.
037 *
038 * @param <A> the argument type.
039 * @param <T> the returned value type.
040 * @version $Revision: 1157657 $ $Date: 2011-08-14 22:28:19 +0200 (Sun, 14 Aug 2011) $
041 * @author Rodney Waldhoff
042 */
043 public final class UnaryProcedureUnaryFunction<A, T> implements UnaryFunction<A, T>, Serializable {
044 /**
045 * serialVersionUID declaration.
046 */
047 private static final long serialVersionUID = 6153848695167906659L;
048 /** The {@link UnaryProcedure UnaryProcedure} I'm wrapping. */
049 private final UnaryProcedure<? super A> procedure;
050
051 /**
052 * Create a new UnaryProcedureUnaryFunction.
053 * @param procedure to adapt
054 */
055 public UnaryProcedureUnaryFunction(UnaryProcedure<? super A> procedure) {
056 if (procedure == null) {
057 throw new IllegalArgumentException("UnaryProcedure argument was null");
058 }
059 this.procedure = procedure;
060 }
061
062 /**
063 * {@inheritDoc}
064 */
065 public T evaluate(A obj) {
066 procedure.run(obj);
067 return null;
068 }
069
070 /**
071 * {@inheritDoc}
072 */
073 public boolean equals(Object that) {
074 return that == this || (that instanceof UnaryProcedureUnaryFunction<?, ?>
075 && equals((UnaryProcedureUnaryFunction<?, ?>) that));
076 }
077
078 /**
079 * Learn whether a given UnaryProcedureUnaryFunction is equal to this.
080 * @param that UnaryProcedureUnaryFunction to test
081 * @return boolean
082 */
083 public boolean equals(UnaryProcedureUnaryFunction<?, ?> that) {
084 return null != that && (null == procedure ? null == that.procedure : procedure.equals(that.procedure));
085 }
086
087 /**
088 * {@inheritDoc}
089 */
090 public int hashCode() {
091 int hash = "UnaryProcedureUnaryFunction".hashCode();
092 if (null != procedure) {
093 hash ^= procedure.hashCode();
094 }
095 return hash;
096 }
097
098 /**
099 * {@inheritDoc}
100 */
101 public String toString() {
102 return "UnaryProcedureUnaryFunction<" + procedure + ">";
103 }
104
105 /**
106 * Adapt a UnaryProcedure to the UnaryFunction interface.
107 * @param <A> the argument type.
108 * @param <T> the returned value type.
109 * @param procedure to adapt
110 * @return UnaryProcedureUnaryFunction
111 */
112 public static <A, T> UnaryProcedureUnaryFunction<A, T> adapt(UnaryProcedure<? super A> procedure) {
113 return null == procedure ? null : new UnaryProcedureUnaryFunction<A, T>(procedure);
114 }
115
116 }