Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 930497FC0 for ; Wed, 14 Sep 2011 07:41:15 +0000 (UTC) Received: (qmail 20411 invoked by uid 500); 14 Sep 2011 07:41:15 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 20236 invoked by uid 500); 14 Sep 2011 07:41:11 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 20183 invoked by uid 99); 14 Sep 2011 07:41:10 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 14 Sep 2011 07:41:10 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 14 Sep 2011 07:41:08 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 8FE8D2388994 for ; Wed, 14 Sep 2011 07:40:48 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@commons.apache.org From: celestin@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110914074048.8FE8D2388994@eris.apache.org> 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 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(); + } + + /** + * 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 not 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