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.core.algorithm;
018    
019    import java.io.Serializable;
020    import java.util.ListIterator;
021    
022    import org.apache.commons.functor.BinaryProcedure;
023    import org.apache.commons.functor.UnaryFunction;
024    
025    /**
026     * Implements an in-place transformation of a ListIterator's contents.
027     *
028     * @version $Revision: 1156337 $ $Date: 2011-08-10 21:44:54 +0200 (Wed, 10 Aug 2011) $
029     */
030    public final class InPlaceTransform<T>
031        implements BinaryProcedure<ListIterator<T>, UnaryFunction<? super T, ? extends T>>, Serializable {
032        /**
033         * serialVersionUID declaration.
034         */
035        private static final long serialVersionUID = 4365206078517376006L;
036        private static final InPlaceTransform<Object> INSTANCE = new InPlaceTransform<Object>();
037    
038        /**
039         * {@inheritDoc}
040         * @param left {@link ListIterator}
041         * @param right {@link UnaryFunction}
042         */
043        public void run(ListIterator<T> left, UnaryFunction<? super T, ? extends T> right) {
044            while (left.hasNext()) {
045                left.set(right.evaluate(left.next()));
046            }
047        }
048    
049        /**
050         * {@inheritDoc}
051         */
052        public boolean equals(Object obj) {
053            return obj == this || obj != null && obj.getClass().equals(getClass());
054        }
055    
056        /**
057         * {@inheritDoc}
058         */
059        public int hashCode() {
060            return System.identityHashCode(INSTANCE);
061        }
062    
063        /**
064         * Get an {@link InPlaceTransform} instance.
065         * @return InPlaceTransform
066         */
067        public static InPlaceTransform<Object> instance() {
068            return INSTANCE;
069        }
070    
071    }