commons-commits mailing list archives

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

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

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

Added: commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/generator/FilteredGenerator.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/generator/FilteredGenerator.java?rev=656293&view=auto
==============================================================================
--- commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/generator/FilteredGenerator.java
(added)
+++ commons/sandbox/functor/branches/mjbRadicalExperimentation/src/main/java/org/apache/commons/functor/generator/FilteredGenerator.java
Wed May 14 07:45:43 2008
@@ -0,0 +1,81 @@
+/*
+ * 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;
+import org.apache.commons.functor.core.composite.ConditionalUnaryProcedure;
+
+/**
+ * Generator that filters another Generator by only passing through those elements
+ * that are matched by a specified UnaryPredicate.
+ *
+ * @version $Revision$ $Date$
+ */
+public class FilteredGenerator extends BaseGenerator {
+    private UnaryPredicate pred;
+
+    /**
+     * Create a new FilteredGenerator.
+     * @param wrapped Generator to wrap
+     * @param pred filtering UnaryPredicate
+     */
+    public FilteredGenerator(Generator wrapped, UnaryPredicate pred) {
+        super(wrapped);
+        if (wrapped == null) {
+            throw new IllegalArgumentException("Generator argument was null");
+        }
+        if (pred == null) {
+            throw new IllegalArgumentException("UnaryPredicate argument was null");
+        }
+        this.pred = pred;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void run(UnaryProcedure proc) {
+        getWrappedGenerator().run(new ConditionalUnaryProcedure(pred, proc));
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean equals(Object obj) {
+        if (obj == this) {
+            return true;
+        }
+        if (obj instanceof FilteredGenerator == false) {
+            return false;
+        }
+        FilteredGenerator other = (FilteredGenerator) obj;
+        return other.getWrappedGenerator().equals(getWrappedGenerator()) && other.pred.equals(pred);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public int hashCode() {
+        int result = "FilteredGenerator".hashCode();
+        result <<= 2;
+        Generator gen = getWrappedGenerator();
+        result ^= gen == null ? 0 : gen.hashCode();
+        result <<= 2;
+        result ^= pred == null ? 0 : pred.hashCode();
+        return result;
+    }
+}

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



Mime
View raw message