commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From celes...@apache.org
Subject svn commit: r1170461 - in /commons/proper/math/trunk/src/main/java/org/apache/commons/math/util: IterationEvent.java IterationListener.java IterationManager.java
Date Wed, 14 Sep 2011 07:40:48 GMT
Author: celestin
Date: Wed Sep 14 07:40:48 2011
New Revision: 1170461

URL: http://svn.apache.org/viewvc?rev=1170461&view=rev
Log:
Genereal classes for managing iterations and handling events in iterative algorithms.

Added:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/IterationEvent.java
  (with props)
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/IterationListener.java
  (with props)
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/IterationManager.java
  (with props)

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/IterationEvent.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/IterationEvent.java?rev=1170461&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/IterationEvent.java
(added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/IterationEvent.java
Wed Sep 14 07:40:48 2011
@@ -0,0 +1,40 @@
+/*
+ * 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.math.util;
+
+import java.util.EventObject;
+
+/**
+ * The root class from which all events occurring while running an
+ * {@link IterationManager} should be derived.
+ *
+ * @version $Id$
+ */
+public class IterationEvent extends EventObject {
+    /** */
+    private static final long serialVersionUID = -1405936936084001482L;
+
+    /**
+     * Creates a new instance of this class.
+     *
+     * @param source The iterative algorithm on which the event initially
+     * occurred.
+     */
+    public IterationEvent(final Object source) {
+        super(source);
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/IterationEvent.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/IterationListener.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/IterationListener.java?rev=1170461&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/IterationListener.java
(added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/IterationListener.java
Wed Sep 14 07:40:48 2011
@@ -0,0 +1,58 @@
+/*
+ * 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.math.util;
+
+import java.util.EventListener;
+
+/**
+ * The listener interface for receiving events occurring in an iterative
+ * algorithm.
+ *
+ * @version $Id$
+ */
+public interface IterationListener extends EventListener {
+    /**
+     * Invoked after completion of the initial phase of the iterative algorithm
+     * (prior to the main iteration loop).
+     *
+     * @param e The {@link IterationEvent} object.
+     */
+    void initializationPerformed(IterationEvent e);
+
+    /**
+     * Invoked each time an iteration is completed (in the main iteration loop).
+     *
+     * @param e The {@link IterationEvent} object.
+     */
+    void iterationPerformed(IterationEvent e);
+
+    /**
+     * Invoked each time a new iteration is completed (in the main iteration
+     * loop).
+     *
+     * @param e The {@link IterationEvent} object.
+     */
+    void iterationStarted(IterationEvent e);
+
+    /**
+     * Invoked after completion of the operations which occur after breaking out
+     * of the main iteration loop.
+     *
+     * @param e The {@link IterationEvent} object.
+     */
+    void terminationPerformed(IterationEvent e);
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/IterationListener.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/IterationManager.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/IterationManager.java?rev=1170461&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/IterationManager.java
(added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/IterationManager.java
Wed Sep 14 07:40:48 2011
@@ -0,0 +1,155 @@
+/*
+ * 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.math.util;
+
+import java.util.Collection;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+/**
+ * This abstract class provides a general framework for managing iterative
+ * algorithms. The maximum number of iterations can be set, and methods are
+ * provided to monitor the current iteration count. A lightweight event
+ * framework is also provided.
+ *
+ * @version $Id$
+ */
+public class IterationManager {
+    /** Keeps a count of the number of iterations. */
+    private final Incrementor iterations;
+
+    /** The collection of all listeners attached to this iterative algorithm. */
+    private final Collection<IterationListener> listeners;
+
+    /**
+     * Creates a new instance of this class.
+     *
+     * @param maxIterations Maximum number of iterations.
+     */
+    public IterationManager(final int maxIterations) {
+        this.iterations = new Incrementor();
+        this.iterations.setMaximalCount(maxIterations);
+        this.listeners = new CopyOnWriteArrayList<IterationListener>();
+    }
+
+    /**
+     * Attaches a listener to this manager.
+     *
+     * @param listener A {@code IterationListener} object.
+     */
+    public void addIterationListener(final IterationListener listener) {
+        listeners.add(listener);
+    }
+
+    /**
+     * Informs all registered listeners that the initial phase (prior to the
+     * main iteration loop) has been completed.
+     *
+     * @param e The {@link IterationEvent} object.
+     */
+    public void fireInitializationEvent(final IterationEvent e) {
+        for (IterationListener l : listeners) {
+            l.initializationPerformed(e);
+        }
+    }
+
+    /**
+     * Informs all registered listeners that a new iteration (in the main
+     * iteration loop) has been performed.
+     *
+     * @param e The {@link IterationEvent} object.
+     */
+    public void fireIterationPerformedEvent(final IterationEvent e) {
+        for (IterationListener l : listeners) {
+            l.iterationPerformed(e);
+        }
+    }
+
+    /**
+     * Informs all registered listeners that a new iteration (in the main
+     * iteration loop) has been started.
+     *
+     * @param e The {@link IterationEvent} object.
+     */
+    public void fireIterationStartedEvent(final IterationEvent e) {
+        for (IterationListener l : listeners) {
+            l.iterationStarted(e);
+        }
+    }
+
+    /**
+     * Informs all registered listeners that the final phase (post-iterations)
+     * has been completed.
+     *
+     * @param e The {@link IterationEvent} object.
+     */
+    public void fireTerminationEvent(final IterationEvent e) {
+        for (IterationListener l : listeners) {
+            l.terminationPerformed(e);
+        }
+    }
+
+    /**
+     * Returns the number of iterations of this solver, 0 if no iterations has
+     * been performed yet.
+     *
+     * @return the number of iterations.
+     */
+    public int getIterations() {
+        return iterations.getCount();
+    }
+
+    /**
+     * Returns the maximum number of iterations.
+     *
+     * @return the maximum number of iterations.
+     */
+    public int getMaxIterations() {
+        return iterations.getMaximalCount();
+    }
+
+    /**
+     * Increments the iteration count by one, and throws an exception if the
+     * maximum number of iterations is reached. This method should be called at
+     * the beginning of a new iteration.
+     *
+     * @throws MaxCountExceededException if the maximum number of iterations
+     * is reached.
+     */
+    public void incrementIterationCount() {
+        iterations.incrementCount();
+    }
+
+    /**
+     * Removes the specified iteration listener from the list of listeners
+     * currently attached to {@code this} object. Attempting to remove a
+     * listener which was <em>not</em> previously registered does not cause any
+     * error.
+     *
+     * @param listener The {@link IterationListener} to be removed.
+     */
+    public void removeIterationListener(final IterationListener listener) {
+        listeners.remove(listener);
+    }
+
+    /**
+     * Sets the iteration count to 0. This method must be called during the
+     * initial phase.
+     */
+    public void resetIterationCount() {
+        iterations.resetCount();
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/IterationManager.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision



Mime
View raw message