From c81019e390436e8656a694a4fed08083b89dee80 Mon Sep 17 00:00:00 2001
From: Peter Powers <pmpowers@usgs.gov>
Date: Fri, 14 Jun 2024 12:02:32 -0600
Subject: [PATCH] config empty array fixes for IMTs and output

---
 .../usgs/earthquake/nshmp/calc/CalcConfig.java  | 17 +++++++----------
 .../earthquake/nshmp/calc/CalcConfigTests.java  | 11 +++++++++++
 .../calc/calc-config-extends-empty-arrays.json  |  8 ++++++++
 3 files changed, 26 insertions(+), 10 deletions(-)
 create mode 100644 src/test/resources/calc/calc-config-extends-empty-arrays.json

diff --git a/src/main/java/gov/usgs/earthquake/nshmp/calc/CalcConfig.java b/src/main/java/gov/usgs/earthquake/nshmp/calc/CalcConfig.java
index 9d1ca70a..ecbd29e7 100644
--- a/src/main/java/gov/usgs/earthquake/nshmp/calc/CalcConfig.java
+++ b/src/main/java/gov/usgs/earthquake/nshmp/calc/CalcConfig.java
@@ -24,7 +24,6 @@ import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.EnumSet;
 import java.util.List;
 import java.util.Map;
 import java.util.NavigableMap;
@@ -341,7 +340,7 @@ public final class CalcConfig {
         if (that.truncationLevel != null) {
           this.truncationLevel = that.truncationLevel;
         }
-        if (that.imts != null) {
+        if (that.imts != null && !that.imts.isEmpty()) {
           this.imts = that.imts;
         }
         if (that.tectonicSettings != null) {
@@ -797,10 +796,7 @@ public final class CalcConfig {
 
     private Output(Builder b) {
       this.directory = b.directory;
-      Set<DataType> dataTypes = EnumSet.copyOf(b.dataTypes);
-      dataTypes.add(DataType.TOTAL);
-      dataTypes.add(DataType.MAP);
-      this.dataTypes = Collections.unmodifiableSet(dataTypes);
+      this.dataTypes = Collections.unmodifiableSet(b.dataTypes);
       this.returnPeriods = List.copyOf(b.returnPeriods);
     }
 
@@ -819,7 +815,7 @@ public final class CalcConfig {
 
       void copy(Output that) {
         this.directory = that.directory;
-        this.dataTypes = EnumSet.copyOf(that.dataTypes);
+        this.dataTypes = that.dataTypes;
         this.returnPeriods = that.returnPeriods;
       }
 
@@ -827,8 +823,8 @@ public final class CalcConfig {
         if (that.directory != null) {
           this.directory = that.directory;
         }
-        if (that.dataTypes != null) {
-          this.dataTypes = EnumSet.copyOf(that.dataTypes);
+        if (that.dataTypes != null && !that.dataTypes.isEmpty()) {
+          this.dataTypes.addAll(that.dataTypes);
         }
         if (that.returnPeriods != null) {
           this.returnPeriods = that.returnPeriods;
@@ -984,7 +980,8 @@ public final class CalcConfig {
 
     /**
      * Extend {@code this} builder to match {@code that} builder. Fields in
-     * {@code that} builder take precedence unless they are not set.
+     * {@code that} builder take precedence unless they are not set. Array based
+     * settings are replaced.
      */
     public Builder extend(Builder that) {
       checkNotNull(that);
diff --git a/src/test/java/gov/usgs/earthquake/nshmp/calc/CalcConfigTests.java b/src/test/java/gov/usgs/earthquake/nshmp/calc/CalcConfigTests.java
index a8933b79..53246033 100644
--- a/src/test/java/gov/usgs/earthquake/nshmp/calc/CalcConfigTests.java
+++ b/src/test/java/gov/usgs/earthquake/nshmp/calc/CalcConfigTests.java
@@ -63,6 +63,8 @@ class CalcConfigTests {
   static final CalcConfig DEFAULTS;
   static final CalcConfig EXTENDS;
   static final CalcConfig EXTENDS_EMPTY; // same as DEFAULTS
+  // used to test that arrays are both non-null and not empty
+  static final CalcConfig EXTENDS_EMPTY_ARRAYS; // imts, types
 
   static {
     try {
@@ -73,6 +75,9 @@ class CalcConfigTests {
       EXTENDS_EMPTY = CalcConfig.defaults()
           .extend(CalcConfig.from(RESOURCES.resolve("calc-config-extends-empty.json")))
           .build();
+      EXTENDS_EMPTY_ARRAYS = CalcConfig.defaults()
+          .extend(CalcConfig.from(RESOURCES.resolve("calc-config-extends-empty-arrays.json")))
+          .build();
 
     } catch (IOException ioe) {
       throw new RuntimeException();
@@ -278,6 +283,9 @@ class CalcConfigTests {
     assertEquals(ValueFormat.ANNUAL_RATE, def.valueFormat);
     assertEquals(true, def.distanceFilterUpdate);
     assertEquals(true, def.gridFocalMechUpdate);
+
+    def = EXTENDS_EMPTY_ARRAYS.hazard;
+    assertEquals(IMTS, def.imts);
   }
 
   @Test
@@ -379,6 +387,9 @@ class CalcConfigTests {
     assertEquals(Path.of("hazout"), def.directory);
     assertEquals(Set.of(DataType.TOTAL, DataType.MAP), def.dataTypes);
     assertEquals(defaultReturnPeroiods, def.returnPeriods);
+
+    def = EXTENDS_EMPTY_ARRAYS.output;
+    assertEquals(Set.of(DataType.TOTAL, DataType.MAP), def.dataTypes);
   }
 
   @Test
diff --git a/src/test/resources/calc/calc-config-extends-empty-arrays.json b/src/test/resources/calc/calc-config-extends-empty-arrays.json
new file mode 100644
index 00000000..1f82401f
--- /dev/null
+++ b/src/test/resources/calc/calc-config-extends-empty-arrays.json
@@ -0,0 +1,8 @@
+{
+  "hazard": {
+    "imts": []
+  },
+  "output": {
+  	"dataTypes": []
+  }
+}
-- 
GitLab