You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mb...@apache.org on 2008/06/09 19:21:47 UTC

svn commit: r665790 - in /commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite: TransformedBinaryFunction.java TransformedBinaryProcedure.java TransformedFunction.java TransformedProcedure.java

Author: mbenson
Date: Mon Jun  9 10:21:46 2008
New Revision: 665790

URL: http://svn.apache.org/viewvc?rev=665790&view=rev
Log:
add new Transformed* composite functors

Added:
    commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedBinaryFunction.java   (with props)
    commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedBinaryProcedure.java   (with props)
    commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedFunction.java   (with props)
    commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedProcedure.java   (with props)

Added: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedBinaryFunction.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedBinaryFunction.java?rev=665790&view=auto
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedBinaryFunction.java (added)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedBinaryFunction.java Mon Jun  9 10:21:46 2008
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.core.composite;
+
+import java.io.Serializable;
+
+import org.apache.commons.functor.BinaryFunction;
+import org.apache.commons.functor.UnaryFunction;
+
+/**
+ * A BinaryFunction whose result is then run through a UnaryFunction.
+ * @version $Revision$ $Date$
+ * @author Matt Benson
+ */
+public class TransformedBinaryFunction<L, R, T> implements BinaryFunction<L, R, T>, Serializable {
+    /**
+     * Type-remembering helper
+     * @param <X>
+     */
+    private class Helper<X> implements BinaryFunction<L, R, T>, Serializable {
+        private BinaryFunction<? super L, ? super R, ? extends X> preceding;
+        private UnaryFunction<? super X, ? extends T> following;
+
+        /**
+         * Create a new Helper.
+         * @param preceding BinaryFunction
+         * @param following UnaryFunction
+         */
+        private Helper(BinaryFunction<? super L, ? super R, ? extends X> preceding, UnaryFunction<? super X, ? extends T> following) {
+            this.preceding = preceding;
+            this.following = following;
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public T evaluate(L left, R right) {
+            return following.evaluate(preceding.evaluate(left, right));
+        }
+    }
+
+    private Helper<?> helper;
+
+    /**
+     * Create a new TransformedBinaryFunction.
+     * @param <X>
+     * @param preceding BinaryFunction
+     * @param following UnaryFunction
+     */
+    public <X> TransformedBinaryFunction(BinaryFunction<? super L, ? super R, ? extends X> preceding,
+            UnaryFunction<? super X, ? extends T> following) {
+        this.helper = new Helper<X>(preceding, following);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public T evaluate(L left, R right) {
+        return helper.evaluate(left, right);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean equals(Object obj) {
+        return obj == this || obj instanceof TransformedBinaryFunction
+                && equals((TransformedBinaryFunction<?, ?, ?>) obj);
+    }
+
+    /**
+     * Learn whether another TransformedBinaryFunction is equal to <code>this</code>.
+     * @param that instance to test
+     * @return whether equal
+     */
+    public boolean equals(TransformedBinaryFunction<?, ?, ?> that) {
+        return that != null && that.helper.preceding.equals(this.helper.preceding)
+                && that.helper.following.equals(this.helper.following);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int hashCode() {
+        int result = "TransformedBinaryFunction".hashCode();
+        result <<= 2;
+        result |= helper.following.hashCode();
+        result <<= 2;
+        result |= helper.preceding.hashCode();
+        return result;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String toString() {
+        return "TransformedBinaryFunction<" + helper.preceding + "; " + helper.following + ">";
+    }
+}

Propchange: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedBinaryFunction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedBinaryProcedure.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedBinaryProcedure.java?rev=665790&view=auto
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedBinaryProcedure.java (added)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedBinaryProcedure.java Mon Jun  9 10:21:46 2008
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.core.composite;
+
+import java.io.Serializable;
+
+import org.apache.commons.functor.BinaryFunction;
+import org.apache.commons.functor.BinaryProcedure;
+import org.apache.commons.functor.UnaryProcedure;
+
+/**
+ * A BinaryProcedure composed of a BinaryFunction whose result is then run through a UnaryProcedure.
+ * @version $Revision$ $Date$
+ * @author Matt Benson
+ */
+public class TransformedBinaryProcedure<L, R> implements BinaryProcedure<L, R>, Serializable {
+    /**
+     * Type-remembering helper
+     * @param <X>
+     */
+    private class Helper<X> implements BinaryProcedure<L, R>, Serializable {
+        private BinaryFunction<? super L, ? super R, ? extends X> function;
+        private UnaryProcedure<? super X> procedure;
+
+        /**
+         * Create a new Helper.
+         * @param function BinaryFunction
+         * @param procedure UnaryFunction
+         */
+        private Helper(BinaryFunction<? super L, ? super R, ? extends X> function, UnaryProcedure<? super X> procedure) {
+            this.function = function;
+            this.procedure = procedure;
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public void run(L left, R right) {
+            procedure.run(function.evaluate(left, right));
+        }
+    }
+
+    private Helper<?> helper;
+
+    /**
+     * Create a new TransformedBinaryProcedure.
+     * @param <X>
+     * @param function BinaryFunction
+     * @param procedure UnaryProcedure
+     */
+    public <X> TransformedBinaryProcedure(BinaryFunction<? super L, ? super R, ? extends X> function,
+            UnaryProcedure<? super X> procedure) {
+        this.helper = new Helper<X>(function, procedure);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void run(L left, R right) {
+        helper.run(left, right);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean equals(Object obj) {
+        return obj == this || obj instanceof TransformedBinaryProcedure
+                && equals((TransformedBinaryProcedure<?, ?>) obj);
+    }
+
+    /**
+     * Learn whether another TransformedBinaryProcedure is equal to <code>this</code>.
+     * @param that instance to test
+     * @return whether equal
+     */
+    public boolean equals(TransformedBinaryProcedure<?, ?> that) {
+        return that != null && that.helper.function.equals(this.helper.function)
+                && that.helper.procedure.equals(this.helper.procedure);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int hashCode() {
+        int result = "TransformedBinaryProcedure".hashCode();
+        result <<= 2;
+        result |= helper.procedure.hashCode();
+        result <<= 2;
+        result |= helper.function.hashCode();
+        return result;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String toString() {
+        return "TransformedBinaryProcedure<" + helper.function + "; " + helper.procedure + ">";
+    }
+}

Propchange: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedBinaryProcedure.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedFunction.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedFunction.java?rev=665790&view=auto
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedFunction.java (added)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedFunction.java Mon Jun  9 10:21:46 2008
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.core.composite;
+
+import java.io.Serializable;
+
+import org.apache.commons.functor.Function;
+import org.apache.commons.functor.UnaryFunction;
+
+/**
+ * A Function whose result is then run through a UnaryFunction.
+ * @version $Revision$ $Date$
+ * @author Matt Benson
+ */
+public class TransformedFunction<T> implements Function<T>, Serializable {
+    /**
+     * Type-remembering helper
+     * @param <X>
+     */
+    private class Helper<X> implements Function<T>, Serializable {
+        private Function<? extends X> preceding;
+        private UnaryFunction<? super X, ? extends T> following;
+
+        /**
+         * Create a new Helper.
+         * @param preceding Function
+         * @param following UnaryFunction
+         */
+        private Helper(Function<? extends X> preceding, UnaryFunction<? super X, ? extends T> following) {
+            this.preceding = preceding;
+            this.following = following;
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public T evaluate() {
+            return following.evaluate(preceding.evaluate());
+        }
+    }
+
+    private Helper<?> helper;
+
+    /**
+     * Create a new TransformedFunction.
+     * @param <X>
+     * @param preceding Function
+     * @param following UnaryFunction
+     */
+    public <X> TransformedFunction(Function<? extends X> preceding,
+            UnaryFunction<? super X, ? extends T> following) {
+        this.helper = new Helper<X>(preceding, following);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public T evaluate() {
+        return helper.evaluate();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean equals(Object obj) {
+        return obj == this || obj instanceof TransformedFunction && equals((TransformedFunction<?>) obj);
+    }
+
+    /**
+     * Learn whether another TransformedFunction is equal to <code>this</code>.
+     * @param that instance to test
+     * @return whether equal
+     */
+    public boolean equals(TransformedFunction<?> that) {
+        return that != null && that.helper.preceding.equals(this.helper.preceding)
+                && that.helper.following.equals(this.helper.following);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int hashCode() {
+        int result = "TransformedFunction".hashCode();
+        result <<= 2;
+        result |= helper.following.hashCode();
+        result <<= 2;
+        result |= helper.preceding.hashCode();
+        return result;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String toString() {
+        return "TransformedFunction<" + helper.preceding + "; " + helper.following + ">";
+    }
+}

Propchange: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedFunction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedProcedure.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedProcedure.java?rev=665790&view=auto
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedProcedure.java (added)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedProcedure.java Mon Jun  9 10:21:46 2008
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.core.composite;
+
+import java.io.Serializable;
+
+import org.apache.commons.functor.Function;
+import org.apache.commons.functor.Procedure;
+import org.apache.commons.functor.UnaryProcedure;
+
+/**
+ * A Procedure composed of a Function whose result is then run through a UnaryProcedure.
+ * @version $Revision$ $Date$
+ * @author Matt Benson
+ */
+public class TransformedProcedure implements Procedure, Serializable {
+    /**
+     * Type-remembering helper
+     * @param <X>
+     */
+    private class Helper<X> implements Procedure, Serializable {
+        private Function<? extends X> function;
+        private UnaryProcedure<? super X> procedure;
+
+        /**
+         * Create a new Helper.
+         * @param function Function
+         * @param procedure UnaryFunction
+         */
+        private Helper(Function<? extends X> function, UnaryProcedure<? super X> procedure) {
+            this.function = function;
+            this.procedure = procedure;
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public void run() {
+            procedure.run(function.evaluate());
+        }
+    }
+
+    private Helper<?> helper;
+
+    /**
+     * Create a new TransformedProcedure.
+     * @param <X>
+     * @param function Function
+     * @param procedure UnaryProcedure
+     */
+    public <X> TransformedProcedure(Function<? extends X> function, UnaryProcedure<? super X> procedure) {
+        this.helper = new Helper<X>(function, procedure);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void run() {
+        helper.run();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean equals(Object obj) {
+        return obj == this || obj instanceof TransformedProcedure
+                && equals((TransformedProcedure) obj);
+    }
+
+    /**
+     * Learn whether another TransformedProcedure is equal to <code>this</code>.
+     * @param that instance to test
+     * @return whether equal
+     */
+    public boolean equals(TransformedProcedure that) {
+        return that != null && that.helper.function.equals(this.helper.function)
+                && that.helper.procedure.equals(this.helper.procedure);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int hashCode() {
+        int result = "TransformedProcedure".hashCode();
+        result <<= 2;
+        result |= helper.procedure.hashCode();
+        result <<= 2;
+        result |= helper.function.hashCode();
+        return result;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String toString() {
+        return "TransformedProcedure<" + helper.function + "; " + helper.procedure + ">";
+    }
+}

Propchange: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/composite/TransformedProcedure.java
------------------------------------------------------------------------------
    svn:eol-style = native