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.core.algorithm;
018
019import java.util.ListIterator;
020
021import org.apache.commons.functor.BinaryProcedure;
022import org.apache.commons.functor.Function;
023
024/**
025 * Implements an in-place transformation of a ListIterator's contents.
026 *
027 * @param <T> the arguments type
028 * @version $Revision: 1537906 $ $Date: 2013-11-01 12:47:33 +0100 (Fr, 01 Nov 2013) $
029 */
030public final class InPlaceTransform<T>
031    implements BinaryProcedure<ListIterator<T>, Function<? super T, ? extends T>> {
032    /**
033     * A static {@code InPlaceTransform} instance reference.
034     */
035    private static final InPlaceTransform<Object> INSTANCE = new InPlaceTransform<Object>();
036
037    /**
038     * {@inheritDoc}
039     * @param left {@link ListIterator}
040     * @param right {@link Function}
041     */
042    public void run(ListIterator<T> left, Function<? super T, ? extends T> right) {
043        while (left.hasNext()) {
044            left.set(right.evaluate(left.next()));
045        }
046    }
047
048    /**
049     * {@inheritDoc}
050     */
051    @Override
052    public boolean equals(Object obj) {
053        return obj == this || obj != null && obj.getClass().equals(getClass());
054    }
055
056    /**
057     * {@inheritDoc}
058     */
059    @Override
060    public int hashCode() {
061        return System.identityHashCode(INSTANCE);
062    }
063
064    /**
065     * {@inheritDoc}
066     */
067    @Override
068    public String toString() {
069        return "InPlaceTransform";
070    }
071
072    /**
073     * Get an {@link InPlaceTransform} instance.
074     * @return InPlaceTransform
075     */
076    public static InPlaceTransform<Object> instance() {
077        return INSTANCE;
078    }
079
080}