Java tutorial
/* * Copyright 2015 by Yields. * * Licensed 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 io.yields.math.framework.kpi; import org.apache.commons.lang.StringUtils; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import java.util.Map; import java.util.function.BinaryOperator; import java.util.function.Function; import java.util.stream.IntStream; import io.yields.math.framework.Explorer; import io.yields.math.framework.Stats; import io.yields.math.framework.executor.PropertyVerifications; import static java.lang.String.format; import static java.lang.String.valueOf; import static java.util.Comparator.comparing; import static java.util.stream.Collectors.toList; import static org.apache.commons.lang.StringUtils.isNotBlank; public class ExplorerCsvExporter implements ExplorerExporter { private static final String PROPERTY_PREFIX = "p_"; @Override public void export(Explorer<?> explorer, File destinationFile) { try { // variables names String variablesHeader = explorer .all().findFirst().map(propertyVerification -> propertyVerification.getVariables().entrySet() .stream().map(entry -> escape(entry.getKey())).sorted()) .get().reduce("", commaSeparated()); // properties result String propertiesHeader = explorer.all().findFirst() .map(propertyVerifications -> propertyVerifications.getResults().stream() .map(propertyVerification -> PROPERTY_PREFIX + escape(propertyVerification.getName())) .reduce("", commaSeparated())) .orElse(""); // stats String statsHeader = explorer.getStats().stream().findFirst().map( stats -> stats.getStatsNames().stream().map(name -> escape(name)).reduce("", commaSeparated())) .orElse(""); // descriptors String descriptorsHeader = explorer.getStats().stream().findFirst().map(stats -> stats .getDescriptorNames().stream().map(name -> escape(name)).reduce("", commaSeparated())) .orElse(""); // variables content List<String> variablesContent = explorer.all().map(variablesToCsv()).collect(toList()); // properties content List<String> propertiesContent = explorer.all().map(propertiesToCsv()).collect(toList()); // stats content List<String> statsContent = explorer.getStats().stream().map(statsToCsv()).collect(toList()); // descriptors content List<String> descriptorsContent = explorer.getStats().stream().map(descriptorsToCsv()) .collect(toList()); String header = ""; header += variablesHeader; if (isNotBlank(propertiesHeader)) { header += "," + propertiesHeader; } if (isNotBlank(statsHeader)) { header += "," + statsHeader; } if (isNotBlank(descriptorsHeader)) { header += "," + descriptorsHeader; } try (PrintWriter fileWriter = new PrintWriter(new FileWriter(destinationFile))) { fileWriter.print(header); IntStream.range(0, variablesContent.size()).mapToObj(index -> { String line = variablesContent.get(index); if (!propertiesContent.isEmpty()) { line += "," + propertiesContent.get(index); } if (!statsContent.isEmpty()) { if (isNotBlank(statsContent.get(index))) { line += "," + statsContent.get(index); } } if (!descriptorsContent.isEmpty()) { if (isNotBlank(descriptorsContent.get(index))) { line += "," + descriptorsContent.get(index); } } return line; }).forEach(line -> { fileWriter.println(); fileWriter.print(line); }); } } catch (IOException e) { throw new IllegalStateException( format("Failed to write data export to %s", destinationFile.getAbsolutePath()), e); } } private static Function<PropertyVerifications<?>, String> variablesToCsv() { return propertyVerifications -> propertyVerifications.getVariables().entrySet().stream() .sorted(comparing(Map.Entry::getKey)).map(entry -> escape(valueOf(entry.getValue()))) .reduce("", commaSeparated()); } private static Function<PropertyVerifications<?>, String> propertiesToCsv() { return propertyVerifications -> propertyVerifications.getResults().stream() .map(propertyVerification -> String.valueOf(propertyVerification.getResult().getCode())) .reduce("", commaSeparated()); } private static Function<Stats, String> statsToCsv() { return stats -> stats.getStatsNames().stream().map(name -> escape(valueOf(stats.getStats(name)))).reduce("", commaSeparated()); } private static Function<Stats, String> descriptorsToCsv() { return stats -> stats.getDescriptorNames().stream() .map(name -> escape(valueOf(stats.getDescriptorAt(name)))).reduce("", commaSeparated()); } private static BinaryOperator<String> commaSeparated() { return joinWithSeparator(","); } private static String escape(String value) { return String.format("\"%s\"", value); } private static BinaryOperator<String> newlineSeparated() { return joinWithSeparator("\n"); } private static BinaryOperator<String> joinWithSeparator(String separator) { return (previous, current) -> { if (StringUtils.isBlank(previous)) { return current; } else { return previous + separator + current; } }; } }