commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r656292 - /commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/generator/WhileGenerate.java
Date Wed, 14 May 2008 14:45:40 GMT
Author: mbenson
Date: Wed May 14 07:45:39 2008
New Revision: 656292

URL: http://svn.apache.org/viewvc?rev=656292&view=rev
Log:
add functor implementations of Algorithms algorithms

Added:
    commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/generator/WhileGenerate.java
  (with props)

Added: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/generator/WhileGenerate.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/generator/WhileGenerate.java?rev=656292&view=auto
==============================================================================
--- commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/generator/WhileGenerate.java
(added)
+++ commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/generator/WhileGenerate.java
Wed May 14 07:45:39 2008
@@ -0,0 +1,88 @@
+/*
+ * 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.generator;
+
+import org.apache.commons.functor.UnaryPredicate;
+import org.apache.commons.functor.UnaryProcedure;
+
+/**
+ * Wrap another {@link Generator} such that {@link #run(UnaryProcedure)} continues
+ * as long as a condition is true (test before).
+ *
+ * @version $Revision$ $Date$
+ */
+public class WhileGenerate extends BaseGenerator {
+    private UnaryPredicate test;
+
+    /**
+     * Create a new WhileGenerate.
+     * @param test {@link UnaryPredicate}
+     * @param wrapped {@link Generator}
+     */
+    public WhileGenerate(UnaryPredicate test, Generator wrapped) {
+        super(wrapped);
+        if (wrapped == null) {
+            throw new IllegalArgumentException("Generator argument was null");
+        }
+        if (test == null) {
+            throw new IllegalArgumentException("UnaryPredicate argument was null");
+        }
+        this.test = test;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void run(final UnaryProcedure proc) {
+        getWrappedGenerator().run(new UnaryProcedure() {
+            public void run(Object obj) {
+                if (!test.test(obj)) {
+                    getWrappedGenerator().stop();
+                } else {
+                    proc.run(obj);
+                }
+            }
+        });
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean equals(Object obj) {
+        if (obj == this) {
+            return true;
+        }
+        if (obj instanceof WhileGenerate == false) {
+            return false;
+        }
+        WhileGenerate other = (WhileGenerate) obj;
+        return other.getWrappedGenerator().equals(getWrappedGenerator()) && other.test.equals(test);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public int hashCode() {
+        int result = "WhileGenerate".hashCode();
+        result <<= 2;
+        Generator gen = getWrappedGenerator();
+        result ^= gen == null ? 0 : gen.hashCode();
+        result <<= 2;
+        result ^= test == null ? 0 : test.hashCode();
+        return result;
+    }
+}

Propchange: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/generator/WhileGenerate.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message