Skip to content

Commit a2e4dac

Browse files
author
Muharem Hrnjadovic
committed
Merge pull request gem#19 from larsbutler/cy-2008-gmpe-sa-param-npe
[r=al-maisan][f=984838] Fix for CY_2008 GMPE SA param NullPointerException
2 parents 3502eef + 985fdbc commit a2e4dac

File tree

7 files changed

+194
-78
lines changed

7 files changed

+194
-78
lines changed

java/org/opensha/commons/param/DoubleConstraint.java

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ public class DoubleConstraint extends ParameterConstraint<Double> {
4949
protected final static boolean D = false;
5050

5151
/** The minimum value allowed in this constraint, inclusive */
52-
protected Double min = null;
52+
protected double min;
5353
/** The maximum value allowed in this constraint, inclusive */
54-
protected Double max = null;
54+
protected double max;
5555

5656
/** No-Arg Constructor, constraints are null so all values allowed */
5757
public DoubleConstraint() {
@@ -61,29 +61,14 @@ public DoubleConstraint() {
6161
/**
6262
* Constructor for the DoubleConstraint object. Sets the min and max values
6363
* allowed in this constraint. No checks are performed that min and max are
64-
* consistant with each other.
64+
* consistent with each other.
6565
*
6666
* @param min
6767
* The min value allowed
6868
* @param max
6969
* The max value allowed
7070
*/
7171
public DoubleConstraint(double min, double max) {
72-
this.min = new Double(min);
73-
this.max = new Double(max);
74-
}
75-
76-
/**
77-
* Constructor for the DoubleConstraint object. Sets the min and max values
78-
* allowed in this constraint. No checks are performed that min and max are
79-
* consistant with each other.
80-
*
81-
* @param min
82-
* The min value allowed
83-
* @param max
84-
* The max value allowed
85-
*/
86-
public DoubleConstraint(Double min, Double max) {
8772
this.min = min;
8873
this.max = max;
8974
}
@@ -103,25 +88,6 @@ public DoubleConstraint(Double min, Double max) {
10388
public void setMinMax(double min, double max) throws EditableException {
10489
String S = C + ": setMinMax(double, double): ";
10590
checkEditable(S);
106-
this.min = new Double(min);
107-
this.max = new Double(max);
108-
}
109-
110-
/**
111-
* Sets the min and max values allowed in this constraint. No checks are
112-
* performed that min and max are consistant with each other.
113-
*
114-
* @param min
115-
* The new min value
116-
* @param max
117-
* The new max value
118-
* @throws EditableException
119-
* Thrown when the constraint or parameter containing this
120-
* constraint has been made non-editable.
121-
*/
122-
public void setMinMax(Double min, Double max) throws EditableException {
123-
String S = C + ": setMinMax(Double, Double): ";
124-
checkEditable(S);
12591
this.min = min;
12692
this.max = max;
12793
}
@@ -150,12 +116,13 @@ public Double getMax() {
150116
*/
151117
public boolean isAllowed(Double d) {
152118
if (d == null)
119+
{
153120
return nullAllowed;
154-
if ((min == null) || (max == null))
155-
return true;
156-
else if ((d.compareTo(min) >= 0) && (d.compareTo(max) <= 0))
157-
return true;
158-
return false;
121+
}
122+
else
123+
{
124+
return isAllowed((double)d);
125+
}
159126
}
160127

161128
/**
@@ -170,7 +137,7 @@ else if ((d.compareTo(min) >= 0) && (d.compareTo(max) <= 0))
170137
* @return True if this is one of the allowed values.
171138
*/
172139
public boolean isAllowed(double d) {
173-
return isAllowed(new Double(d));
140+
return d >= min && d <= max;
174141
}
175142

176143
/**
@@ -182,10 +149,8 @@ public String toString() {
182149
StringBuffer b = new StringBuffer();
183150
if (name != null)
184151
b.append(TAB + "Name = " + name + '\n');
185-
if (min != null)
186-
b.append(TAB + "Min = " + min.toString() + '\n');
187-
if (max != null)
188-
b.append(TAB + "Max = " + max.toString() + '\n');
152+
b.append(TAB + "Min = " + String.valueOf(min) + '\n');
153+
b.append(TAB + "Max = " + String.valueOf(max) + '\n');
189154
b.append(TAB + "Null Allowed = " + this.nullAllowed + '\n');
190155
return b.toString();
191156
}

java/org/opensha/commons/param/WarningDoubleParameter.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.opensha.commons.param;
2020

2121
import java.util.ArrayList;
22+
import java.util.List;
2223
import java.util.ListIterator;
2324

2425
import org.dom4j.Element;
@@ -87,7 +88,7 @@ public class WarningDoubleParameter extends DoubleParameter implements
8788
* A list of listeners to receive warning events. Only created if needed,
8889
* else kept null. This is known as "lazy instantiation".
8990
*/
90-
protected transient ArrayList warningListeners = null;
91+
protected transient List<ParameterChangeWarningListener> warningListeners = null;
9192

9293
/**
9394
* Set to true to turn off warnings, i.e. bypass the warning constraint.
@@ -382,6 +383,10 @@ public WarningDoubleParameter(String name, DoubleConstraint constraint,
382383
// constraint.setName( name );
383384
}
384385

386+
public List<ParameterChangeWarningListener> getWarningListeners() {
387+
return warningListeners;
388+
}
389+
385390
/**
386391
* Adds a ParameterChangeFailListener to the list of listeners. This is the
387392
* interface all listeners must implement in order to fit into this
@@ -395,11 +400,16 @@ public WarningDoubleParameter(String name, DoubleConstraint constraint,
395400
public synchronized void addParameterChangeWarningListener(
396401
ParameterChangeWarningListener listener) throws EditableException {
397402

403+
if (listener == null) {
404+
// If the input listener is null, there's no need
405+
// to do anything.
406+
return;
407+
}
398408
String S = C + ": addParameterChangeWarningListener(): ";
399409
// checkEditable(S);
400410

401411
if (warningListeners == null)
402-
warningListeners = new ArrayList();
412+
warningListeners = new ArrayList<ParameterChangeWarningListener>();
403413
if (!warningListeners.contains(listener)) {
404414
if (D)
405415
System.out.println(S + "Adding listener: "
@@ -595,30 +605,30 @@ public boolean isRecommended(Double obj) {
595605
* The event encapsulating the attempted values passed to each
596606
* listener.
597607
*/
598-
public void fireParameterChangeWarning(ParameterChangeWarningEvent event) {
599-
608+
public void fireParameterChangeWarning(ParameterChangeWarningEvent event)
609+
{
600610
String S = C + ": firePropertyChange(): ";
601611
if (D)
612+
{
602613
System.out.println(S + "Starting: " + this.getName());
603-
604-
ArrayList vector;
605-
synchronized (this) {
606-
if (warningListeners == null)
607-
return;
608-
vector = (ArrayList) warningListeners.clone();
609614
}
610-
for (int i = 0; i < vector.size(); i++) {
611-
ParameterChangeWarningListener listener =
612-
(ParameterChangeWarningListener) vector.get(i);
613-
if (D)
614-
System.out.println(S + "Firing warning to (" + i + ") "
615-
+ listener.getClass().getName());
616-
listener.parameterChangeWarning(event);
615+
616+
if (this.warningListeners != null)
617+
{
618+
for (ParameterChangeWarningListener pcwl : warningListeners)
619+
{
620+
if (D)
621+
{
622+
System.out.println(S + "Firing warning to " + pcwl.getClass().getName());
623+
}
624+
pcwl.parameterChangeWarning(event);
625+
}
617626
}
618627

619628
if (D)
629+
{
620630
System.out.println(S + "Ending: " + this.getName());
621-
631+
}
622632
}
623633

624634
/**
@@ -746,10 +756,7 @@ public Object clone() {
746756
// so should be interested in the clone
747757

748758
if (this.warningListeners != null) {
749-
it = this.warningListeners.listIterator();
750-
while (it.hasNext()) {
751-
ParameterChangeWarningListener listener =
752-
(ParameterChangeWarningListener) it.next();
759+
for (ParameterChangeWarningListener listener : this.warningListeners) {
753760
param.addParameterChangeWarningListener(listener);
754761
}
755762
}

java/org/opensha/sha/imr/attenRelImpl/CY_2008_AttenRel.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ public class CY_2008_AttenRel extends AttenuationRelationship implements
269269
depthTop;
270270
private double distRupMinusDistX_OverRup, aftershock, f_meas, f_hw;
271271
private String stdDevType;
272-
private boolean parameterChange;
273272
private double depthTo1pt0kmPerSec; // defined this way to support null
274273
// values
275274
private double lnYref;
@@ -460,7 +459,6 @@ else if (im.getName().equalsIgnoreCase(PGA_Param.NAME))
460459
else
461460
iper = 23; // PGV
462461

463-
parameterChange = true;
464462
intensityMeasureChanged = false;
465463

466464
}
@@ -963,11 +961,10 @@ public void parameterChange(ParameterChangeEvent e) {
963961

964962
String pName = e.getParameterName();
965963
Object val = e.getNewValue();
966-
parameterChange = true;
967964
lnYref_is_not_fresh = true; // this could be placed below, only where
968965
// really needed.
969966

970-
if (pName.equals(magParam.NAME)) {
967+
if (pName.equals(MagParam.NAME)) {
971968
mag = ((Double) val).doubleValue();
972969
} else if (pName.equals(FaultTypeParam.NAME)) {
973970
String fltType = (String) fltTypeParam.getValue();
@@ -1041,6 +1038,7 @@ public void resetParameterEventListeners() {
10411038
hangingWallFlagParam.removeParameterChangeListener(this);
10421039
stdDevTypeParam.removeParameterChangeListener(this);
10431040
saPeriodParam.removeParameterChangeListener(this);
1041+
saParam.removeParameterChangeListener(this);
10441042
this.initParameterEventListeners();
10451043
}
10461044

@@ -1064,6 +1062,7 @@ protected void initParameterEventListeners() {
10641062
hangingWallFlagParam.addParameterChangeListener(this);
10651063
stdDevTypeParam.addParameterChangeListener(this);
10661064
saPeriodParam.addParameterChangeListener(this);
1065+
saParam.addParameterChangeListener(this);
10671066
}
10681067

10691068
/**

java/org/opensha/sha/imr/param/IntensityMeasureParams/SA_Param.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@ public class SA_Param extends WarningDoubleParameter {
3636
public final static String NAME = "SA";
3737
public final static String UNITS = "g";
3838
public final static String INFO = "Response Spectral Acceleration";
39-
protected final static Double MIN = new Double(Math.log(Double.MIN_VALUE));
40-
protected final static Double MAX = new Double(Double.MAX_VALUE);
41-
protected final static Double DEFAULT_WARN_MIN = new Double(
42-
Math.log(Double.MIN_VALUE));
43-
protected final static Double DEFAULT_WARN_MAX = new Double(Math.log(3.0));
39+
protected final static Double MIN = Math.log(Double.MIN_VALUE);
40+
protected final static Double MAX = Double.MAX_VALUE;
41+
protected final static Double DEFAULT_WARN_MIN = Math.log(Double.MIN_VALUE);
42+
protected final static Double DEFAULT_WARN_MAX = Math.log(Double.MAX_VALUE);
4443

4544
/**
4645
* This uses the DEFAULT_WARN_MIN and DEFAULT_WARN_MAX fields to set the
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.opensha.commons.param;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.opensha.commons.param.event.ParameterChangeWarningEvent;
8+
import org.opensha.commons.param.event.ParameterChangeWarningListener;
9+
10+
public class WarningDoubleParameterTest
11+
{
12+
13+
private WarningDoubleParameter wdp;
14+
private ParameterChangeWarningListener listener;
15+
16+
@Before
17+
public void setUp()
18+
{
19+
wdp = new WarningDoubleParameter("Test");
20+
listener = new ParameterChangeWarningListener()
21+
{
22+
public void parameterChangeWarning(ParameterChangeWarningEvent event){}
23+
24+
};
25+
}
26+
/**
27+
* Test addParameterChangeWarningListener with null input.
28+
*
29+
* If the input is null, it should simply be ignored.
30+
*/
31+
@Test
32+
public void testAddNullPCWL()
33+
{
34+
assertNull(wdp.getWarningListeners());
35+
36+
wdp.addParameterChangeWarningListener(null);
37+
// It should still be null.
38+
assertNull(wdp.getWarningListeners());
39+
40+
// Add a real listener.
41+
wdp.addParameterChangeWarningListener(listener);
42+
assertEquals(1, wdp.getWarningListeners().size());
43+
assertEquals(listener, wdp.getWarningListeners().get(0));
44+
45+
// Now try to add a null; the current list should be unaffected.
46+
wdp.addParameterChangeWarningListener(null);
47+
assertEquals(1, wdp.getWarningListeners().size());
48+
assertEquals(listener, wdp.getWarningListeners().get(0));
49+
}
50+
51+
/**
52+
* Test addParameterChangeWarningListener with normal input.
53+
*/
54+
@Test
55+
public void testAddPCWL()
56+
{
57+
assertNull(wdp.getWarningListeners());
58+
59+
wdp.addParameterChangeWarningListener(listener);
60+
assertEquals(1, wdp.getWarningListeners().size());
61+
assertEquals(listener, wdp.getWarningListeners().get(0));
62+
}
63+
}

java_tests/org/opensha/sha/imr/attenRelImpl/test/CY_2008_test.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,24 @@
1818

1919
package org.opensha.sha.imr.attenRelImpl.test;
2020

21+
import static org.junit.Assert.assertEquals;
22+
2123
import java.io.File;
2224
import java.io.FileNotFoundException;
2325
import java.io.IOException;
2426
import java.util.ArrayList;
27+
import java.util.List;
2528
import java.util.StringTokenizer;
2629

2730
import org.junit.Before;
31+
import org.junit.Test;
2832
import org.opensha.commons.exceptions.ConstraintException;
2933
import org.opensha.commons.exceptions.ParameterException;
3034
import org.opensha.commons.param.BooleanParameter;
3135
import org.opensha.commons.param.DoubleParameter;
36+
import org.opensha.commons.param.ParameterAPI;
3237
import org.opensha.commons.param.WarningDoubleParameter;
38+
import org.opensha.commons.param.event.ParameterChangeListener;
3339
import org.opensha.commons.util.DataUtils;
3440
import org.opensha.commons.util.FileUtils;
3541
import org.opensha.sha.imr.AttenuationRelationship;
@@ -538,4 +544,17 @@ public String getLastFailMetadata() {
538544
return failMetadata;
539545
}
540546

547+
/**
548+
* See https://bugs.launchpad.net/openquake/+bug/984838.
549+
*
550+
* Tests that the SA Parameter's change listener is set properly.
551+
*/
552+
@Test
553+
public void testResetSaParamEventListener() {
554+
cy_08.resetParameterEventListeners();
555+
ParameterAPI param = cy_08.getParameter(SA_Param.NAME);
556+
List<ParameterChangeListener> listeners = param.getChangeListeners();
557+
assertEquals(1, listeners.size());
558+
assertEquals(cy_08, listeners.get(0));
559+
}
541560
}

0 commit comments

Comments
 (0)