Skip to content
Snippets Groups Projects

Iterables to stream

Merged Powers, Peter M. requested to merge ghsc/users/pmpowers/nshmp-lib:iterables-to-stream into main
13 files
+ 172
327
Compare changes
  • Side-by-side
  • Inline
Files
13
package gov.usgs.earthquake.nshmp.calc;
package gov.usgs.earthquake.nshmp.calc;
 
import static gov.usgs.earthquake.nshmp.Text.Delimiter.COMMA;
import static java.nio.file.StandardOpenOption.APPEND;
import static java.nio.file.StandardOpenOption.APPEND;
import java.io.IOException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Path;
 
import java.util.Arrays;
import java.util.List;
import java.util.List;
import java.util.function.Function;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Lists;
import com.google.common.primitives.Doubles;
import gov.usgs.earthquake.nshmp.Text;
import gov.usgs.earthquake.nshmp.Text;
import gov.usgs.earthquake.nshmp.Text.Delimiter;
import gov.usgs.earthquake.nshmp.data.XySequence;
import gov.usgs.earthquake.nshmp.data.XySequence;
import gov.usgs.earthquake.nshmp.geo.Location;
import gov.usgs.earthquake.nshmp.geo.Location;
import gov.usgs.earthquake.nshmp.model.HazardModel;
import gov.usgs.earthquake.nshmp.model.HazardModel;
@@ -87,7 +85,7 @@ public final class EqRateExport {
@@ -87,7 +85,7 @@ public final class EqRateExport {
*/
*/
public void write(EqRate rate) throws IOException {
public void write(EqRate rate) throws IOException {
Iterable<Double> emptyValues = Doubles.asList(new double[rate.totalMfd.size()]);
double[] emptyValues = new double[rate.totalMfd.size()];
Function<Double, String> formatter = Text.formatDoubleFunction(valueFormat);
Function<Double, String> formatter = Text.formatDoubleFunction(valueFormat);
/* Can't init output until we have a demo rate record for mfd x-values */
/* Can't init output until we have a demo rate record for mfd x-values */
@@ -97,16 +95,15 @@ public final class EqRateExport {
@@ -97,16 +95,15 @@ public final class EqRateExport {
}
}
String name = namedSites ? rate.site.name() : null;
String name = namedSites ? rate.site.name() : null;
Location location = rate.site.location();
Location loc = rate.site.location();
List<String> locData = Lists.newArrayList(
String locStr = name + "," +
name,
String.format("%.5f", loc.longitude) + "," +
String.format("%.5f", location.longitude),
String.format("%.5f", loc.latitude);
String.format("%.5f", location.latitude));
String totalLine = toLine(locData, rate.totalMfd.yValues(), formatter);
double[] values = rate.totalMfd.yValues().toArray();
String totalLine = toLine(locStr, values, formatter);
String emptyLine = toLine(locData, emptyValues, formatter);
String emptyLine = toLine(locStr, emptyValues, formatter);
/* write/append */
/* write/append */
Path totalFile = out.resolve(file);
Path totalFile = out.resolve(file);
@@ -118,7 +115,7 @@ public final class EqRateExport {
@@ -118,7 +115,7 @@ public final class EqRateExport {
String typeLine = emptyLine;
String typeLine = emptyLine;
if (rate.typeMfds.containsKey(type)) {
if (rate.typeMfds.containsKey(type)) {
XySequence typeRate = rate.typeMfds.get(type);
XySequence typeRate = rate.typeMfds.get(type);
typeLine = toLine(locData, typeRate.yValues(), formatter);
typeLine = toLine(locStr, typeRate.yValues().toArray(), formatter);
}
}
HazardExport.writeLine(typeFile, typeLine, APPEND);
HazardExport.writeLine(typeFile, typeLine, APPEND);
}
}
@@ -129,7 +126,7 @@ public final class EqRateExport {
@@ -129,7 +126,7 @@ public final class EqRateExport {
Iterable<?> headerElements = Iterables.concat(
Iterable<?> headerElements = Iterables.concat(
Lists.newArrayList(namedSites ? "name" : null, "lon", "lat"),
Lists.newArrayList(namedSites ? "name" : null, "lon", "lat"),
demo.totalMfd.xValues().boxed().collect(Collectors.toList()));
demo.totalMfd.xValues().boxed().collect(Collectors.toList()));
String header = Text.join(headerElements, Delimiter.COMMA);
String header = COMMA.joiner().join(headerElements);
Path totalFile = out.resolve(file);
Path totalFile = out.resolve(file);
HazardExport.writeLine(totalFile, header);
HazardExport.writeLine(totalFile, header);
@@ -146,24 +143,12 @@ public final class EqRateExport {
@@ -146,24 +143,12 @@ public final class EqRateExport {
}
}
private static String toLine(
private static String toLine(
Iterable<String> location,
String locStr,
Iterable<Double> values,
double[] values,
Function<Double, String> formatter) {
Function<Double, String> formatter) {
return Text.join(
StringBuilder sb = new StringBuilder(locStr);
FluentIterable.from(location)
Arrays.stream(values).forEach(v -> sb.append("," + v));
.append(Iterables.transform(
return sb.toString();
values,
formatter::apply)),
Delimiter.COMMA);
}
private static String toLine(
Iterable<String> location,
DoubleStream stream,
Function<Double, String> formatter) {
List<Double> values = stream.boxed().collect(Collectors.toList());
return toLine(location, values, formatter);
}
}
}
}
Loading