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.composite;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.functor.BinaryProcedure;
22  import org.apache.commons.lang3.Validate;
23  
24  /**
25   * Transposes (swaps) the arguments to some other
26   * {@link BinaryProcedure procedure}.
27   * For example, given a procedure <i>p</i>
28   * and the ordered pair of arguments <i>a</i>,
29   * <i>b</i>.
30   * {@link #run runs}
31   * <code>p.run(b,a)</code>.
32   * <p>
33   * Note that although this class implements
34   * {@link Serializable}, a given instance will
35   * only be truly <code>Serializable</code> if the
36   * underlying functor is.  Attempts to serialize
37   * an instance whose delegate is not
38   * <code>Serializable</code> will result in an exception.
39   * </p>
40   * @param <L> the left argument type.
41   * @param <R> the right argument type.
42   * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
43   */
44  public class TransposedProcedure<L, R> implements BinaryProcedure<L, R>, Serializable {
45      /**
46       * serialVersionUID declaration.
47       */
48      private static final long serialVersionUID = 4472683678460290015L;
49      // attributes
50      // ------------------------------------------------------------------------
51      /**
52       * The adapted procedure.
53       */
54      private final BinaryProcedure<? super R, ? super L> procedure;
55  
56      // constructor
57      // ------------------------------------------------------------------------
58      /**
59       * Create a new TransposedProcedure.
60       * @param procedure BinaryProcedure to transpose
61       */
62      public TransposedProcedure(BinaryProcedure<? super R, ? super L> procedure) {
63          this.procedure = Validate.notNull(procedure, "BinaryProcedure argument was null");
64      }
65  
66      // functor interface
67      // ------------------------------------------------------------------------
68      /**
69       * {@inheritDoc}
70       */
71      public void run(L left, R right) {
72          procedure.run(right, left);
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public boolean equals(Object that) {
80          return that == this || (that instanceof TransposedProcedure<?, ?> && equals((TransposedProcedure<?, ?>) that));
81      }
82  
83      /**
84       * Learn whether another TransposedProcedure is equal to this.
85       * @param that TransposedPredicate to test
86       * @return boolean
87       */
88      public boolean equals(TransposedProcedure<?, ?> that) {
89          return null != that && procedure.equals(that.procedure);
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public int hashCode() {
97          int hash = "TransposedProcedure".hashCode();
98          hash ^= procedure.hashCode();
99          return hash;
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public String toString() {
107         return "TransposedProcedure<" + procedure + ">";
108     }
109 
110     // static
111     // ------------------------------------------------------------------------
112     /**
113      * Transpose a BinaryProcedure.
114      *
115      * @param <L> the left argument type.
116      * @param <R> the right argument type.
117      * @param p to transpose
118      * @return TransposedProcedure
119      */
120     public static <L, R> TransposedProcedure<R, L> transpose(BinaryProcedure<? super L, ? super R> p) {
121         return null == p ? null : new TransposedProcedure<R, L>(p);
122     }
123 
124 }