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