Author: mbenson
Date: Wed May 14 07:45:59 2008
New Revision: 656297
URL: http://svn.apache.org/viewvc?rev=656297&view=rev
Log:
add functor implementations of Algorithms algorithms
Added:
commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/
commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/DoUntil.java (with props)
commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/DoWhile.java (with props)
commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/FindWithinGenerator.java (with props)
commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/FoldLeft.java (with props)
commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/FoldRight.java (with props)
commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/GeneratorContains.java (with props)
commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/InPlaceTransform.java (with props)
commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/IndexOfInGenerator.java (with props)
commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/PredicatedLoop.java (with props)
commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/RecursiveEvaluation.java (with props)
commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/RemoveMatching.java (with props)
commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/RetainMatching.java (with props)
commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/UntilDo.java (with props)
commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/WhileDo.java (with props)
commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/package.html (with props)
Added: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/DoUntil.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/DoUntil.java?rev=656297&view=auto
==============================================================================
--- commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/DoUntil.java (added)
+++ commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/DoUntil.java Wed May 14 07:45:59 2008
@@ -0,0 +1,46 @@
+/*
+ * 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.algorithm;
+
+import org.apache.commons.functor.Predicate;
+import org.apache.commons.functor.Procedure;
+
+/**
+ * Do-until algorithm (test after).
+ *
+ * @version $Revision$ $Date$
+ */
+public class DoUntil extends PredicatedLoop {
+
+ /**
+ * Create a new DoUntil.
+ * @param body to execute
+ * @param test whether to keep going
+ */
+ public DoUntil(Procedure body, Predicate test) {
+ super(body, test);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void run() {
+ do {
+ getBody().run();
+ } while (!getTest().test());
+ }
+}
Propchange: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/DoUntil.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/DoWhile.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/DoWhile.java?rev=656297&view=auto
==============================================================================
--- commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/DoWhile.java (added)
+++ commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/DoWhile.java Wed May 14 07:45:59 2008
@@ -0,0 +1,46 @@
+/*
+ * 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.algorithm;
+
+import org.apache.commons.functor.Predicate;
+import org.apache.commons.functor.Procedure;
+
+/**
+ * Do-while algorithm (test after).
+ *
+ * @version $Revision$ $Date$
+ */
+public class DoWhile extends PredicatedLoop {
+
+ /**
+ * Create a new DoWhile.
+ * @param body to execute
+ * @param test whether to keep going
+ */
+ public DoWhile(Procedure body, Predicate test) {
+ super(body, test);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void run() {
+ do {
+ getBody().run();
+ } while (getTest().test());
+ }
+}
Propchange: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/DoWhile.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/FindWithinGenerator.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/FindWithinGenerator.java?rev=656297&view=auto
==============================================================================
--- commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/FindWithinGenerator.java (added)
+++ commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/FindWithinGenerator.java Wed May 14 07:45:59 2008
@@ -0,0 +1,134 @@
+/*
+ * 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.algorithm;
+
+import java.io.Serializable;
+import java.util.NoSuchElementException;
+
+import org.apache.commons.functor.BinaryFunction;
+import org.apache.commons.functor.UnaryPredicate;
+import org.apache.commons.functor.UnaryProcedure;
+import org.apache.commons.functor.generator.Generator;
+
+/**
+ * Return the first Object in a {@link Generator} matching a {@link UnaryPredicate}.
+ *
+ * @version $Revision$ $Date$
+ */
+public final class FindWithinGenerator implements BinaryFunction, Serializable {
+ private static final FindWithinGenerator INSTANCE = new FindWithinGenerator();
+
+ /**
+ * Helper procedure.
+ */
+ private class FindProcedure implements UnaryProcedure {
+ private Object found;
+ private boolean wasFound;
+ private UnaryPredicate pred;
+
+ /**
+ * Create a new FindProcedure.
+ * @pred test
+ */
+ public FindProcedure(UnaryPredicate pred) {
+ this.pred = pred;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void run(Object obj) {
+ if (!wasFound && pred.test(obj)) {
+ wasFound = true;
+ found = obj;
+ }
+ }
+ }
+
+ private boolean useIfNone;
+ private Object ifNone;
+
+ /**
+ * Create a new FindWithinGenerator.
+ */
+ public FindWithinGenerator() {
+ super();
+ }
+
+ /**
+ * Create a new FindWithinGenerator.
+ * @param ifNone object to return if the Generator contains no matches.
+ */
+ public FindWithinGenerator(Object ifNone) {
+ this();
+ this.ifNone = ifNone;
+ useIfNone = true;
+ }
+
+ /**
+ * {@inheritDoc}
+ * @param left Generator
+ * @param right UnaryPredicate
+ */
+ public Object evaluate(Object left, Object right) {
+ UnaryPredicate pred = (UnaryPredicate) right;
+ FindProcedure findProcedure = new FindProcedure(pred);
+ ((Generator) left).run(findProcedure);
+ if (!findProcedure.wasFound) {
+ if (useIfNone) {
+ return ifNone;
+ }
+ throw new NoSuchElementException("No element matching " + pred + " was found.");
+ }
+ return findProcedure.found;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (obj instanceof FindWithinGenerator == false) {
+ return false;
+ }
+ FindWithinGenerator other = (FindWithinGenerator) obj;
+ return other.useIfNone == useIfNone && !useIfNone
+ || (other.ifNone == this.ifNone || other.ifNone != null && other.ifNone.equals(this.ifNone));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int hashCode() {
+ if (!this.useIfNone) {
+ return System.identityHashCode(INSTANCE);
+ }
+ int result = "FindWithinGenerator".hashCode();
+ result ^= this.ifNone == null ? 0 : this.ifNone.hashCode();
+ return result;
+ }
+
+ /**
+ * Get a static {@link FindWithinGenerator} instance.
+ * @return {@link FindWithinGenerator}
+ */
+ public static FindWithinGenerator instance() {
+ return INSTANCE;
+ }
+}
Propchange: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/FindWithinGenerator.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/FoldLeft.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/FoldLeft.java?rev=656297&view=auto
==============================================================================
--- commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/FoldLeft.java (added)
+++ commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/FoldLeft.java Wed May 14 07:45:59 2008
@@ -0,0 +1,135 @@
+/*
+ * 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.algorithm;
+
+import java.io.Serializable;
+
+import org.apache.commons.functor.BinaryFunction;
+import org.apache.commons.functor.UnaryFunction;
+import org.apache.commons.functor.UnaryProcedure;
+import org.apache.commons.functor.generator.Generator;
+
+/**
+ * Functional left-fold algorithm against the elements of a {@link Generator}.
+ * Uses the seed object (if supplied) as the initial left-side argument to the {@link BinaryFunction},
+ * then uses the result of that evaluation as the next left-side argument, until the {@link Generator}'s
+ * elements have been expended.
+ *
+ * @version $Revision$ $Date$
+ */
+public class FoldLeft implements UnaryFunction, BinaryFunction, Serializable {
+
+ private BinaryFunction func;
+
+ /**
+ * Create a new FoldLeft.
+ * @param func {@link BinaryFunction} to apply to each (seed, next)
+ */
+ public FoldLeft(BinaryFunction func) {
+ this.func = func;
+ }
+
+ /**
+ * {@inheritDoc}
+ * @param obj {@link Generator} to transform
+ */
+ public Object evaluate(Object obj) {
+ FoldLeftHelper helper = new FoldLeftHelper(func);
+ ((Generator) obj).run(helper);
+ return helper.getResult();
+ }
+
+ /**
+ * {@inheritDoc}
+ * @param left {@link Generator} to transform
+ * @param right seed object
+ */
+ public Object evaluate(Object left, Object right) {
+ FoldLeftHelper helper = new FoldLeftHelper(func, right);
+ ((Generator) left).run(helper);
+ return helper.getResult();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (obj instanceof FoldLeft == false) {
+ return false;
+ }
+ return ((FoldLeft) obj).func.equals(func);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int hashCode() {
+ return "FoldLeft".hashCode() << 2 ^ func.hashCode();
+ }
+
+ /**
+ * Helper procedure
+ */
+ private static class FoldLeftHelper implements UnaryProcedure {
+ private BinaryFunction function;
+ private Object seed;
+ private boolean started;
+
+ /**
+ * Create a seedless FoldLeftHelper.
+ * @param function to apply
+ */
+ public FoldLeftHelper(BinaryFunction function) {
+ this.function = function;
+ }
+
+ /**
+ * Create a new FoldLeftHelper.
+ * @param function to apply
+ * @param seed initial left argument
+ */
+ FoldLeftHelper(BinaryFunction function, Object seed) {
+ this(function);
+ this.seed = seed;
+ started = true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void run(Object obj) {
+ if (!started) {
+ seed = obj;
+ started = true;
+ } else {
+ seed = function.evaluate(seed, obj);
+ }
+ }
+
+ /**
+ * Get current result.
+ * @return Object
+ */
+ Object getResult() {
+ return started ? seed : null;
+ }
+
+ }
+}
Propchange: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/FoldLeft.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/FoldRight.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/FoldRight.java?rev=656297&view=auto
==============================================================================
--- commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/FoldRight.java (added)
+++ commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/FoldRight.java Wed May 14 07:45:59 2008
@@ -0,0 +1,142 @@
+/*
+ * 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.algorithm;
+
+import java.io.Serializable;
+import java.util.Stack;
+
+import org.apache.commons.functor.BinaryFunction;
+import org.apache.commons.functor.UnaryFunction;
+import org.apache.commons.functor.UnaryProcedure;
+import org.apache.commons.functor.generator.Generator;
+
+/**
+ * Functional right-fold algorithm against the elements of a {@link Generator}.
+ * Uses the seed object (if supplied) as the initial right-side argument to the {@link BinaryFunction},
+ * then uses the result of that evaluation as the next right-side argument, until the {@link Generator}'s
+ * elements have been expended.
+ * @version $Revision$ $Date$
+ */
+public class FoldRight implements UnaryFunction, BinaryFunction, Serializable {
+
+ private BinaryFunction func;
+
+ /**
+ * Create a new FoldLeft.
+ * @param func {@link BinaryFunction} to apply to each (seed, next)
+ */
+ public FoldRight(BinaryFunction func) {
+ this.func = func;
+ }
+
+ /**
+ * {@inheritDoc}
+ * @param obj {@link Generator} to transform
+ */
+ public Object evaluate(Object obj) {
+ FoldRightHelper helper = new FoldRightHelper(func);
+ ((Generator) obj).run(helper);
+ return helper.getResult();
+ }
+
+ /**
+ * {@inheritDoc}
+ * @param left {@link Generator} to transform
+ * @param right seed object
+ */
+ public Object evaluate(Object left, Object right) {
+ FoldRightHelper helper = new FoldRightHelper(func, right);
+ ((Generator) left).run(helper);
+ return helper.getResult();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (obj instanceof FoldRight == false) {
+ return false;
+ }
+ return ((FoldRight) obj).func.equals(func);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int hashCode() {
+ return "FoldRight".hashCode() << 2 ^ func.hashCode();
+ }
+
+ /**
+ * Helper class
+ */
+ private static class FoldRightHelper implements UnaryProcedure {
+ private BinaryFunction function;
+ private Object seed;
+ private Stack stk = new Stack();
+ private boolean hasSeed;
+
+ /**
+ * Create a seedless FoldRightHelper.
+ * @param function to apply
+ */
+ public FoldRightHelper(BinaryFunction function) {
+ this.function = function;
+ }
+
+ /**
+ * Create a new FoldRightHelper.
+ * @param function to apply
+ * @param seed initial left argument
+ */
+ FoldRightHelper(BinaryFunction function, Object seed) {
+ this(function);
+ this.seed = seed;
+ hasSeed = true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void run(Object obj) {
+ stk.push(obj);
+ }
+
+ /**
+ * Get result after processing.
+ * Get current result.
+ * @return Object
+ */
+ Object getResult() {
+ Object right = seed;
+ if (!hasSeed) {
+ if (stk.isEmpty()) {
+ return null;
+ }
+ right = stk.pop();
+ }
+ while (!stk.isEmpty()) {
+ right = function.evaluate(stk.pop(), right);
+ }
+ return right;
+ }
+
+ }
+}
Propchange: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/FoldRight.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/GeneratorContains.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/GeneratorContains.java?rev=656297&view=auto
==============================================================================
--- commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/GeneratorContains.java (added)
+++ commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/GeneratorContains.java Wed May 14 07:45:59 2008
@@ -0,0 +1,89 @@
+/*
+ * 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.algorithm;
+
+import java.io.Serializable;
+
+import org.apache.commons.functor.BinaryPredicate;
+import org.apache.commons.functor.UnaryPredicate;
+import org.apache.commons.functor.UnaryProcedure;
+import org.apache.commons.functor.generator.Generator;
+
+/**
+ * Tests whether a {@link Generator} contains an element that matches a {@link UnaryPredicate}.
+ *
+ * @version $Revision$ $Date$
+ */
+public final class GeneratorContains implements BinaryPredicate, Serializable {
+ private static final GeneratorContains INSTANCE = new GeneratorContains();
+
+ /**
+ * Helper procedure.
+ */
+ private class ContainsProcedure implements UnaryProcedure {
+ private boolean found;
+ private UnaryPredicate pred;
+
+ /**
+ * Create a new ContainsProcedure.
+ * @pred test
+ */
+ public ContainsProcedure(UnaryPredicate pred) {
+ this.pred = pred;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void run(Object obj) {
+ found |= pred.test(obj);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ * @param left Generator
+ * @param right UnaryPredicate
+ */
+ public boolean test(Object left, Object right) {
+ ContainsProcedure findProcedure = new ContainsProcedure((UnaryPredicate) right);
+ ((Generator) left).run(findProcedure);
+ return findProcedure.found;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean equals(Object obj) {
+ return obj == this || obj != null && obj.getClass().equals(getClass());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int hashCode() {
+ return System.identityHashCode(INSTANCE);
+ }
+
+ /**
+ * Get a static {@link GeneratorContains} instance.
+ * @return {@link GeneratorContains}
+ */
+ public static GeneratorContains instance() {
+ return INSTANCE;
+ }
+}
Propchange: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/GeneratorContains.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/InPlaceTransform.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/InPlaceTransform.java?rev=656297&view=auto
==============================================================================
--- commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/InPlaceTransform.java (added)
+++ commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/InPlaceTransform.java Wed May 14 07:45:59 2008
@@ -0,0 +1,68 @@
+/*
+ * 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.algorithm;
+
+import java.io.Serializable;
+import java.util.ListIterator;
+
+import org.apache.commons.functor.BinaryProcedure;
+import org.apache.commons.functor.UnaryFunction;
+
+/**
+ * Implements an in-place transformation of a ListIterator's contents.
+ *
+ * @version $Revision$ $Date$
+ */
+public final class InPlaceTransform implements BinaryProcedure, Serializable {
+ private static final InPlaceTransform INSTANCE = new InPlaceTransform();
+
+ /**
+ * {@inheritDoc}
+ * @param left {@link ListIterator}
+ * @param right {@link UnaryFunction}
+ */
+ public void run(Object left, Object right) {
+ ListIterator li = (ListIterator) left;
+ UnaryFunction func = (UnaryFunction) right;
+ while (li.hasNext()) {
+ li.set(func.evaluate(li.next()));
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean equals(Object obj) {
+ return obj == this || obj != null && obj.getClass().equals(getClass());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int hashCode() {
+ return System.identityHashCode(INSTANCE);
+ }
+
+ /**
+ * Get an {@link InPlaceTransform} instance.
+ * @return InPlaceTransform
+ */
+ public static InPlaceTransform instance() {
+ return INSTANCE;
+ }
+
+}
Propchange: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/InPlaceTransform.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/IndexOfInGenerator.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/IndexOfInGenerator.java?rev=656297&view=auto
==============================================================================
--- commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/IndexOfInGenerator.java (added)
+++ commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/IndexOfInGenerator.java Wed May 14 07:45:59 2008
@@ -0,0 +1,93 @@
+/*
+ * 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.algorithm;
+
+import java.io.Serializable;
+
+import org.apache.commons.functor.BinaryFunction;
+import org.apache.commons.functor.UnaryPredicate;
+import org.apache.commons.functor.UnaryProcedure;
+import org.apache.commons.functor.generator.Generator;
+
+/**
+ * Return the index of the first Object in a {@link Generator} matching a {@link UnaryPredicate}, or -1 if not found.
+ *
+ * @version $Revision$ $Date$
+ */
+public final class IndexOfInGenerator implements BinaryFunction, Serializable {
+ private static final IndexOfInGenerator INSTANCE = new IndexOfInGenerator();
+
+ /**
+ * Helper procedure.
+ */
+ private class IndexProcedure implements UnaryProcedure {
+ private int index = -1;
+ private int current = 0;
+ private UnaryPredicate pred;
+
+ /**
+ * Create a new IndexProcedure.
+ * @pred test
+ */
+ public IndexProcedure(UnaryPredicate pred) {
+ this.pred = pred;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void run(Object obj) {
+ if (index < 0 && pred.test(obj)) {
+ index = current;
+ }
+ current++;
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ * @param left Generator
+ * @param right UnaryPredicate
+ */
+ public Object evaluate(Object left, Object right) {
+ IndexProcedure findProcedure = new IndexProcedure((UnaryPredicate) right);
+ ((Generator) left).run(findProcedure);
+ return new Integer(findProcedure.index);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean equals(Object obj) {
+ return obj == this || obj != null && obj.getClass().equals(getClass());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int hashCode() {
+ return System.identityHashCode(INSTANCE);
+ }
+
+ /**
+ * Get a static {@link IndexOfInGenerator} instance.
+ * @return {@link IndexOfInGenerator}
+ */
+ public static IndexOfInGenerator instance() {
+ return INSTANCE;
+ }
+}
Propchange: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/IndexOfInGenerator.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/PredicatedLoop.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/PredicatedLoop.java?rev=656297&view=auto
==============================================================================
--- commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/PredicatedLoop.java (added)
+++ commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/PredicatedLoop.java Wed May 14 07:45:59 2008
@@ -0,0 +1,86 @@
+/*
+ * 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.algorithm;
+
+import java.io.Serializable;
+
+import org.apache.commons.functor.Predicate;
+import org.apache.commons.functor.Procedure;
+
+/**
+ * Base class for predicated procedure algorithms.
+ *
+ * @version $Revision$ $Date$
+ */
+abstract class PredicatedLoop implements Procedure, Serializable {
+ private Procedure body;
+ private Predicate test;
+
+ /**
+ * Create a new DoWithTest.
+ * @param body to execute
+ * @param test whether to keep going
+ */
+ protected PredicatedLoop(Procedure body, Predicate test) {
+ this.body = body;
+ this.test = test;
+ }
+
+ /**
+ * Get the body of this loop.
+ * @return Procedure
+ */
+ protected Procedure getBody() {
+ return body;
+ }
+
+ /**
+ * Get the test for this loop.
+ * @return Predicate
+ */
+ protected Predicate getTest() {
+ return test;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (obj == null || !obj.getClass().equals(getClass())) {
+ return false;
+ }
+ PredicatedLoop other = (PredicatedLoop) obj;
+ return other.body.equals(body) && other.test.equals(test);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int hashCode() {
+ String classname = getClass().getName();
+ int dot = classname.lastIndexOf('.');
+ int result = classname.substring(dot + 1).hashCode();
+ result <<= 2;
+ result ^= body.hashCode();
+ result <<= 2;
+ result ^= test.hashCode();
+ return result;
+ }
+}
Propchange: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/PredicatedLoop.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/RecursiveEvaluation.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/RecursiveEvaluation.java?rev=656297&view=auto
==============================================================================
--- commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/RecursiveEvaluation.java (added)
+++ commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/RecursiveEvaluation.java Wed May 14 07:45:59 2008
@@ -0,0 +1,105 @@
+/*
+ * 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.algorithm;
+
+import java.io.Serializable;
+
+import org.apache.commons.functor.Function;
+
+/**
+ * Tail recursion for {@link Function functions}. If the {@link Function}
+ * returns another function of type <code>functionType</code>, that function
+ * is executed. Functions are executed until a non function value or a
+ * function of a type other than that expected is returned.
+ */
+public class RecursiveEvaluation implements Function, Serializable {
+ private Function function;
+ private Class functionType;
+
+ /**
+ * Create a new RecursiveEvaluation. Recursion will continue while the
+ * returned value is of the same runtime class as <code>function</code>.
+ * @param function initial, potentially recursive Function
+ */
+ public RecursiveEvaluation(Function function) {
+ this(function, getClass(function));
+ }
+
+ /**
+ * Create a new RecursiveEvaluation.
+ * @param function initial, potentially recursive Function
+ * @param functionType as long as result is an instance, keep processing.
+ */
+ public RecursiveEvaluation(Function function, Class functionType) {
+ if (function == null) {
+ throw new IllegalArgumentException("Function argument was null");
+ }
+ if (functionType == null || !Function.class.isAssignableFrom(functionType)) {
+ throw new IllegalArgumentException(Function.class + " is not assignable from " + functionType);
+ }
+ this.function = function;
+ this.functionType = functionType;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Object evaluate() {
+ Object result = null;
+ // if the function returns another function, execute it. stop executing
+ // when the result is not of the expected type.
+ while (true) {
+ result = function.evaluate();
+ if (functionType.isInstance(result)) {
+ function = (Function) result;
+ continue;
+ } else {
+ break;
+ }
+ }
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (obj instanceof RecursiveEvaluation == false) {
+ return false;
+ }
+ return ((RecursiveEvaluation) obj).function.equals(function);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int hashCode() {
+ return "RecursiveEvaluation".hashCode() << 2 ^ function.hashCode();
+ }
+
+ /**
+ * Get the class of the specified object, or <code>null</code> if <code>o</code> is <code>null</code>.
+ * @param o Object to check
+ * @return Class found
+ */
+ private static Class getClass(Object o) {
+ return o == null ? null : o.getClass();
+ }
+}
Propchange: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/RecursiveEvaluation.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/RemoveMatching.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/RemoveMatching.java?rev=656297&view=auto
==============================================================================
--- commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/RemoveMatching.java (added)
+++ commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/RemoveMatching.java Wed May 14 07:45:59 2008
@@ -0,0 +1,69 @@
+/*
+ * 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.algorithm;
+
+import java.io.Serializable;
+import java.util.Iterator;
+
+import org.apache.commons.functor.BinaryProcedure;
+import org.apache.commons.functor.UnaryPredicate;
+
+/**
+ * Remove elements from left Iterator that match right UnaryPredicate.
+ *
+ * @version $Revision$ $Date$
+ */
+public final class RemoveMatching implements BinaryProcedure, Serializable {
+ private static final RemoveMatching INSTANCE = new RemoveMatching();
+
+ /**
+ * {@inheritDoc}
+ * @param left {@link Iterator}
+ * @param right {@link UnaryPredicate}
+ */
+ public void run(Object left, Object right) {
+ UnaryPredicate test = (UnaryPredicate) right;
+ for (Iterator iter = (Iterator) left; iter.hasNext();) {
+ if (test.test(iter.next())) {
+ iter.remove();
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean equals(Object obj) {
+ return obj == this || obj != null && obj.getClass().equals(getClass());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int hashCode() {
+ return System.identityHashCode(INSTANCE);
+ }
+
+ /**
+ * Get a static {@link RemoveMatching} instance.
+ * @return {@link RemoveMatching}
+ */
+ public static final RemoveMatching instance() {
+ return INSTANCE;
+ }
+
+}
Propchange: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/RemoveMatching.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/RetainMatching.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/RetainMatching.java?rev=656297&view=auto
==============================================================================
--- commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/RetainMatching.java (added)
+++ commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/RetainMatching.java Wed May 14 07:45:59 2008
@@ -0,0 +1,64 @@
+/*
+ * 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.algorithm;
+
+import java.io.Serializable;
+import java.util.Iterator;
+
+import org.apache.commons.functor.BinaryProcedure;
+import org.apache.commons.functor.UnaryPredicate;
+import org.apache.commons.functor.core.composite.UnaryNot;
+
+/**
+ * Retain elements in left Iterator that match right UnaryPredicate.
+ *
+ * @version $Revision$ $Date$
+ */
+public final class RetainMatching implements BinaryProcedure, Serializable {
+ private static final RetainMatching INSTANCE = new RetainMatching();
+
+ /**
+ * {@inheritDoc}
+ * @param left {@link Iterator}
+ * @param right {@link UnaryPredicate}
+ */
+ public void run(Object left, Object right) {
+ RemoveMatching.instance().run(left, new UnaryNot((UnaryPredicate) right));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean equals(Object obj) {
+ return obj == this || obj != null && obj.getClass().equals(getClass());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int hashCode() {
+ return System.identityHashCode(INSTANCE);
+ }
+
+ /**
+ * Get a static {@link RetainMatching} instance.
+ * @return {@link RetainMatching}
+ */
+ public static final RetainMatching instance() {
+ return INSTANCE;
+ }
+}
Propchange: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/RetainMatching.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/UntilDo.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/UntilDo.java?rev=656297&view=auto
==============================================================================
--- commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/UntilDo.java (added)
+++ commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/UntilDo.java Wed May 14 07:45:59 2008
@@ -0,0 +1,46 @@
+/*
+ * 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.algorithm;
+
+import org.apache.commons.functor.Predicate;
+import org.apache.commons.functor.Procedure;
+
+/**
+ * Until-do algorithm (test before).
+ *
+ * @version $Revision$ $Date$
+ */
+public class UntilDo extends PredicatedLoop{
+
+ /**
+ * Create a new UntilDo.
+ * @param test whether to keep going
+ * @param body to execute
+ */
+ public UntilDo(Predicate test, Procedure body) {
+ super(body, test);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void run() {
+ while (!getTest().test()) {
+ getBody().run();
+ }
+ }
+}
Propchange: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/UntilDo.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/WhileDo.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/WhileDo.java?rev=656297&view=auto
==============================================================================
--- commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/WhileDo.java (added)
+++ commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/WhileDo.java Wed May 14 07:45:59 2008
@@ -0,0 +1,46 @@
+/*
+ * 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.algorithm;
+
+import org.apache.commons.functor.Predicate;
+import org.apache.commons.functor.Procedure;
+
+/**
+ * While-do algorithm (test before).
+ *
+ * @version $Revision$ $Date$
+ */
+public class WhileDo extends PredicatedLoop {
+
+ /**
+ * Create a new WhileDo.
+ * @param test whether to keep going
+ * @param body to execute
+ */
+ public WhileDo(Predicate test, Procedure body) {
+ super(body, test);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void run() {
+ while (getTest().test()) {
+ getBody().run();
+ }
+ }
+}
Propchange: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/WhileDo.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/package.html
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/package.html?rev=656297&view=auto
==============================================================================
--- commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/package.html (added)
+++ commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/package.html Wed May 14 07:45:59 2008
@@ -0,0 +1,21 @@
+<!--
+ 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.
+-->
+<html><body>
+<p>
+Various algorithm-esque functors.
+</p>
+</body></html>
\ No newline at end of file
Propchange: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/core/algorithm/package.html
------------------------------------------------------------------------------
svn:eol-style = native
|