View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.functor.core.algorithm;
18  
19  import java.io.Serializable;
20  import java.util.ListIterator;
21  
22  import org.apache.commons.functor.BinaryProcedure;
23  import org.apache.commons.functor.UnaryFunction;
24  
25  /**
26   * Implements an in-place transformation of a ListIterator's contents.
27   *
28   * @version $Revision: 1156337 $ $Date: 2011-08-10 21:44:54 +0200 (Wed, 10 Aug 2011) $
29   */
30  public final class InPlaceTransform<T>
31      implements BinaryProcedure<ListIterator<T>, UnaryFunction<? super T, ? extends T>>, Serializable {
32      /**
33       * serialVersionUID declaration.
34       */
35      private static final long serialVersionUID = 4365206078517376006L;
36      private static final InPlaceTransform<Object> INSTANCE = new InPlaceTransform<Object>();
37  
38      /**
39       * {@inheritDoc}
40       * @param left {@link ListIterator}
41       * @param right {@link UnaryFunction}
42       */
43      public void run(ListIterator<T> left, UnaryFunction<? super T, ? extends T> right) {
44          while (left.hasNext()) {
45              left.set(right.evaluate(left.next()));
46          }
47      }
48  
49      /**
50       * {@inheritDoc}
51       */
52      public boolean equals(Object obj) {
53          return obj == this || obj != null && obj.getClass().equals(getClass());
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      public int hashCode() {
60          return System.identityHashCode(INSTANCE);
61      }
62  
63      /**
64       * Get an {@link InPlaceTransform} instance.
65       * @return InPlaceTransform
66       */
67      public static InPlaceTransform<Object> instance() {
68          return INSTANCE;
69      }
70  
71  }