Skip to content

Commit

Permalink
Merge pull request gem#17 from larsbutler/campbell-2003-gmpe-event-li…
Browse files Browse the repository at this point in the history
…stener-bug

Parameter listener bug in Campbell 2003 GMPE

[r=angri] [f=931453]
  • Loading branch information
larsbutler committed Feb 22, 2012
2 parents f925c3c + e591cac commit b767fb8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
11 changes: 8 additions & 3 deletions java/org/opensha/commons/param/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.opensha.commons.param;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import org.dom4j.Element;
Expand All @@ -27,6 +28,7 @@
import org.opensha.commons.exceptions.ParameterException;
import org.opensha.commons.metadata.XMLSaveable;
import org.opensha.commons.param.event.ParameterChangeEvent;
import org.opensha.commons.param.event.ParameterChangeListener;

/**
* <b>Title: </b> Parameter
Expand Down Expand Up @@ -109,7 +111,7 @@ public abstract class Parameter<E> implements ParameterAPI<E>,
* ArrayList of all the objects who want to listen on change of this
* paramter
*/
private transient ArrayList changeListeners;
private transient ArrayList<ParameterChangeListener> changeListeners;

/**
* ArrayList of all the objects who want to listen if the value for this
Expand All @@ -121,6 +123,10 @@ public abstract class Parameter<E> implements ParameterAPI<E>,
public Parameter() {
}

public List<ParameterChangeListener> getChangeListeners() {
return changeListeners;
}

/**
* If the editable boolean is set to true, the parameter value can be
* edited, else an EditableException is thrown.
Expand Down Expand Up @@ -374,8 +380,7 @@ public void firePropertyChangeFailed(
*
*/

public synchronized void addParameterChangeListener(
org.opensha.commons.param.event.ParameterChangeListener listener) {
public synchronized void addParameterChangeListener(ParameterChangeListener listener) {
if (changeListeners == null)
changeListeners = new ArrayList();
if (!changeListeners.contains(listener))
Expand Down
5 changes: 5 additions & 0 deletions java/org/opensha/commons/param/ParameterAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@

package org.opensha.commons.param;

import java.util.List;

import org.dom4j.Element;
import org.opensha.commons.data.NamedObjectAPI;
import org.opensha.commons.exceptions.ConstraintException;
import org.opensha.commons.exceptions.ParameterException;
import org.opensha.commons.metadata.XMLSaveable;
import org.opensha.commons.param.editor.ParameterEditor;
import org.opensha.commons.param.event.ParameterChangeEvent;
import org.opensha.commons.param.event.ParameterChangeListener;

/**
* <b>Title:</b> ParameterAPI Interface
Expand Down Expand Up @@ -182,6 +185,8 @@ public void setValue(E value) throws ConstraintException,
*/
public void unableToSetValue(E value) throws ConstraintException;

public List<ParameterChangeListener> getChangeListeners();

/**
* Adds a feature to the ParameterChangeFailListener attribute
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ public void parameterChange(ParameterChangeEvent e) {
* Allows to reset the change listeners on the parameters
*/
public void resetParameterEventListeners() {
saPeriodParam.removeParameterChangeListener(this);
magParam.removeParameterChangeListener(this);
distanceRupParam.removeParameterChangeListener(this);
stdDevTypeParam.removeParameterChangeListener(this);
Expand All @@ -460,6 +461,7 @@ public void resetParameterEventListeners() {
* the parameter is changed.
*/
protected void initParameterEventListeners() {
saPeriodParam.addParameterChangeListener(this);
magParam.addParameterChangeListener(this);
distanceRupParam.addParameterChangeListener(this);
stdDevTypeParam.addParameterChangeListener(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
import static org.junit.Assert.*;

import java.io.File;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.opensha.commons.param.ParameterAPI;
import org.opensha.commons.param.event.ParameterChangeWarningEvent;
import org.opensha.commons.param.event.ParameterChangeWarningListener;
import org.opensha.sha.imr.attenRelImpl.Campbell_2003_AttenRel;
import org.opensha.sha.imr.param.IntensityMeasureParams.PeriodParam;
import org.opensha.sha.imr.param.OtherParams.StdDevTypeParam;
import org.opensha.commons.param.event.ParameterChangeListener;

/**
* Class providing methods for testing {@link Campbell_2003_AttenRel}. Tables
Expand Down Expand Up @@ -144,4 +148,18 @@ private void validateStdDev(String stdDevType, double[][] table) {
@Override
public void parameterChangeWarning(ParameterChangeWarningEvent event) {
}

/**
* See https://bugs.launchpad.net/openquake/+bug/931453.
*
* Tests that the SA Period Parameter's change listener is set properly.
*/
@Test
public void testResetSaPeriodParamEventListener() {
ca03AttenRel.resetParameterEventListeners();
ParameterAPI param = ca03AttenRel.getParameter(PeriodParam.NAME);
List<ParameterChangeListener> listeners = param.getChangeListeners();
assertEquals(1, listeners.size());
assertEquals(ca03AttenRel, listeners.get(0));
}
}

0 comments on commit b767fb8

Please sign in to comment.