Author: mbenson
Date: Wed May 14 07:45:36 2008
New Revision: 656291
URL: http://svn.apache.org/viewvc?rev=656291&view=rev
Log:
add functor implementations of Algorithms algorithms
Added:
commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/generator/GenerateWhile.java
(with props)
Added:
commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/generator/GenerateWhile.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/generator/GenerateWhile.java?rev=656291&view=auto
==============================================================================
---
commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/generator/GenerateWhile.java
(added)
+++
commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/generator/GenerateWhile.java
Wed May 14 07:45:36 2008
@@ -0,0 +1,87 @@
+/*
+ * 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 [EMAIL PROTECTED] Generator} such that [EMAIL PROTECTED]
#run(UnaryProcedure)} continues
+ * as long as a condition is true (test after).
+ *
+ * @version $Revision$ $Date$
+ */
+public class GenerateWhile extends BaseGenerator {
+ private UnaryPredicate test;
+
+ /**
+ * Create a new GenerateWhile.
+ * @param wrapped [EMAIL PROTECTED] Generator}
+ * @param test [EMAIL PROTECTED] UnaryPredicate}
+ */
+ public GenerateWhile(Generator wrapped, UnaryPredicate test) {
+ 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;
+ }
+
+ /**
+ * [EMAIL PROTECTED]
+ */
+ public void run(final UnaryProcedure proc) {
+ getWrappedGenerator().run(new UnaryProcedure() {
+ public void run(Object obj) {
+ proc.run(obj);
+ if (!test.test(obj)) {
+ getWrappedGenerator().stop();
+ }
+ }
+ });
+ }
+
+ /**
+ * [EMAIL PROTECTED]
+ */
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (obj instanceof GenerateWhile == false) {
+ return false;
+ }
+ GenerateWhile other = (GenerateWhile) obj;
+ return other.getWrappedGenerator().equals(getWrappedGenerator()) &&
other.test.equals(test);
+ }
+
+ /**
+ * [EMAIL PROTECTED]
+ */
+ public int hashCode() {
+ int result = "GenerateWhile".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/GenerateWhile.java
------------------------------------------------------------------------------
svn:eol-style = native