private static String process(final Configuration configuration, final String embeddedTemplate, final Map<String, Object> embeddedData) throws IOException, TemplateException { final Map<String, Object> data = Maps.ofEntries( entry("timestamp", Date.from(DateUtil.now())), entry("robozonkyUrl", Defaults.ROBOZONKY_URL), entry("embed", embeddedTemplate), entry("data", embeddedData)); final Template template = configuration.getTemplate("core.ftl"); final StringWriter sw = new StringWriter(); template.process(data, sw); return sw.toString().trim(); }
@Test void ofEntries() { final Map.Entry<String, String> e1 = Maps.entry("b", "a"); final Map.Entry<String, String> e2 = Maps.entry("a", "b"); final SortedMap<String, String> map = Maps.ofEntriesSorted(e1, e2); assertThat(map).containsExactly(e2, e1); } }
@SafeVarargs @SuppressWarnings("varargs") public static <K, V> SortedMap<K, V> ofEntriesSorted(final Map.Entry<? extends K, ? extends V>... entries) { final SortedMap<K, V> sorted = new TreeMap<>(ofEntries(entries)); return unmodifiableSortedMap(sorted); } }
@Test void entry() { final Map.Entry<String, String> e = Maps.entry("a", "b"); SoftAssertions.assertSoftly(softly -> { softly.assertThat(e.getKey()).isEqualTo("a"); softly.assertThat(e.getValue()).isEqualTo("b"); }); }
@SafeVarargs @SuppressWarnings("varargs") public static <K, V> SortedMap<K, V> ofEntriesSorted(final Map.Entry<? extends K, ? extends V>... entries) { final SortedMap<K, V> sorted = new TreeMap<>(ofEntries(entries)); return unmodifiableSortedMap(sorted); } }
@Test void processCommandLine() { final CommandLinePart part = new CommandLinePart().setProperty("someNullProperty", null); // will be skipped // ends up in properties final CommandLinePart part2 = new CommandLinePart().setProperty("robozonky.something", "somethingElse"); // ends up on the command line final CommandLinePart part3 = new CommandLinePart().setProperty("something", "somethingCompletelyDifferent"); final CommandLinePart target = new CommandLinePart(); final Properties settings = new Properties(); Util.processCommandLine(target, settings, part, part2, part3); assertThat(settings).containsOnly(Maps.entry("robozonky.something", "somethingElse")); assertThat(target.getProperties()).containsOnly(Maps.entry("something", "somethingCompletelyDifferent")); } }
private static String process(final Configuration configuration, final String embeddedTemplate, final Map<String, Object> embeddedData) throws IOException, TemplateException { final Map<String, Object> data = Maps.ofEntries( entry("timestamp", Date.from(DateUtil.now())), entry("robozonkyUrl", Defaults.ROBOZONKY_URL), entry("embed", embeddedTemplate), entry("data", embeddedData)); final Template template = configuration.getTemplate("core.ftl"); final StringWriter sw = new StringWriter(); template.process(data, sw); return sw.toString().trim(); }
@Override public File apply(final File execScript) { try { final File root = execScript.getParentFile(); final String result = TemplateProcessor.INSTANCE.process(filename + ".ftl", Maps.ofEntries( entry("uid", ID), entry("gid", ID), entry("pwd", root.getAbsolutePath()), entry("script", execScript) )); final File target = new File(root, filename); Files.write(target.toPath(), result.getBytes(Defaults.CHARSET)); return target.getAbsoluteFile(); } catch (final IOException | TemplateException e) { throw new IllegalStateException("Failed creating service.", e); } } }
@Override public File apply(final File execScript) { try { final File root = execScript.getParentFile(); final String result = TemplateProcessor.INSTANCE.process(filename + ".ftl", Maps.ofEntries( entry("uid", ID), entry("gid", ID), entry("pwd", root.getAbsolutePath()), entry("script", execScript) )); final File target = new File(root, filename); Files.write(target.toPath(), result.getBytes(Defaults.CHARSET)); return target.getAbsoluteFile(); } catch (final IOException | TemplateException e) { throw new IllegalStateException("Failed creating service.", e); } } }
public static Map<String, Object> summarizePortfolioStructure(final PortfolioOverview portfolioOverview) { return Maps.ofEntries( entry("absoluteShare", perRating(portfolioOverview::getCzkInvested)), entry("relativeShare", perRating(portfolioOverview::getShareOnInvestment)), entry("absoluteRisk", perRating(portfolioOverview::getCzkAtRisk)), entry("relativeRisk", perRating(portfolioOverview::getAtRiskShareOnInvestment)), entry("total", portfolioOverview.getCzkInvested()), entry("totalRisk", portfolioOverview.getCzkAtRisk()), entry("totalShare", portfolioOverview.getShareAtRisk()), entry("balance", portfolioOverview.getCzkAvailable()), entry("timestamp", toDate(portfolioOverview.getTimestamp())) ); }
public static Map<String, Object> summarizePortfolioStructure(final PortfolioOverview portfolioOverview) { return Maps.ofEntries( entry("absoluteShare", perRating(portfolioOverview::getCzkInvested)), entry("relativeShare", perRating(portfolioOverview::getShareOnInvestment)), entry("absoluteRisk", perRating(portfolioOverview::getCzkAtRisk)), entry("relativeRisk", perRating(portfolioOverview::getAtRiskShareOnInvestment)), entry("total", portfolioOverview.getCzkInvested()), entry("totalRisk", portfolioOverview.getCzkAtRisk()), entry("totalShare", portfolioOverview.getShareAtRisk()), entry("balance", portfolioOverview.getCzkAvailable()), entry("timestamp", toDate(portfolioOverview.getTimestamp())) ); }
public static Map<String, Object> getLoanData(final MarketplaceLoan loan) { return Maps.ofEntries( entry("loanId", loan.getId()), entry("loanAmount", loan.getAmount()), entry("loanRating", loan.getRating().getCode()), entry("loanTerm", loan.getTermInMonths()), entry("loanUrl", loan.getUrl()), entry("loanRegion", loan.getRegion()), entry("loanMainIncomeType", loan.getMainIncomeType()), entry("loanPurpose", loan.getPurpose()), entry("loanName", loan.getName()), entry("insurance", loan.isInsuranceActive()) ); }
public static Map<String, Object> getLoanData(final MarketplaceLoan loan) { return Maps.ofEntries( entry("loanId", loan.getId()), entry("loanAmount", loan.getAmount()), entry("loanRating", loan.getRating().getCode()), entry("loanTerm", loan.getTermInMonths()), entry("loanUrl", loan.getUrl()), entry("loanRegion", loan.getRegion()), entry("loanMainIncomeType", loan.getMainIncomeType()), entry("loanPurpose", loan.getPurpose()), entry("loanName", loan.getName()), entry("insurance", loan.isInsuranceActive()) ); }
protected File process(final CommandLinePart commandLine, final String templateName, final Function<String, String> finisher) { try { final String result = TemplateProcessor.INSTANCE.process(templateName, Maps.ofEntries( entry("root", distributionDirectory.getAbsolutePath()), entry("options", configFile.getAbsolutePath()), entry("javaOpts", assembleJavaOpts(commandLine)), entry("envVars", commandLine.getEnvironmentVariables()) )); final File target = this.getRunScript(); Files.write(target.toPath(), finisher.apply(result).getBytes(Defaults.CHARSET)); LOGGER.info("Generated run script: {}.", target.getAbsolutePath()); return target; } catch (final IOException | TemplateException e) { throw new IllegalStateException("Failed creating run script.", e); } }
protected File process(final CommandLinePart commandLine, final String templateName, final Function<String, String> finisher) { try { final String result = TemplateProcessor.INSTANCE.process(templateName, Maps.ofEntries( entry("root", distributionDirectory.getAbsolutePath()), entry("options", configFile.getAbsolutePath()), entry("javaOpts", assembleJavaOpts(commandLine)), entry("envVars", commandLine.getEnvironmentVariables()) )); final File target = this.getRunScript(); Files.write(target.toPath(), finisher.apply(result).getBytes(Defaults.CHARSET)); LOGGER.info("Generated run script: {}.", target.getAbsolutePath()); return target; } catch (final IOException | TemplateException e) { throw new IllegalStateException("Failed creating run script.", e); } }
final Map<String, Object> getData(final T event, final SessionInfo sessionInfo) { final Map<String, Object> result = new HashMap<>(this.getData(event)); // ratings here need to have a stable iteration order, as they will be used to list them in notifications result.put("ratings", Stream.of(Rating.values()).collect(Collectors.toList())); result.put("session", Maps.ofEntries( entry("userName", Util.obfuscateEmailAddress(sessionInfo.getUsername())), entry("userAgent", Defaults.ROBOZONKY_USER_AGENT), entry("isDryRun", sessionInfo.isDryRun()) )); result.put("conception", Date.from(event.getConceivedOn().toInstant())); result.put("creation", Date.from(event.getCreatedOn().toInstant())); return Collections.unmodifiableMap(result); }
public static Map<String, Object> getDelinquentData(final Investment i, final Loan loan, final Collection<Development> collections, final LocalDate date) { final Map<String, Object> result = new HashMap<>(getLoanData(i, loan)); result.put("since", Util.toDate(date)); result.put("collections", collections.stream() .sorted(Comparator.comparing(Development::getDateFrom).reversed()) .limit(5) .map(action -> Maps.ofEntries( entry("code", action.getType().getCode()), entry("note", action.getPublicNote().orElse("Bez dalšího vysvětlení.")), entry("startDate", Util.toDate(action.getDateFrom())), entry("endDate", action.getDateTo().map(Util::toDate).orElse(null)) )).collect(Collectors.toList())); return result; }
public static Map<String, Object> getDelinquentData(final Investment i, final Loan loan, final Collection<Development> collections, final LocalDate date) { final Map<String, Object> result = new HashMap<>(getLoanData(i, loan)); result.put("since", Util.toDate(date)); result.put("collections", collections.stream() .sorted(Comparator.comparing(Development::getDateFrom).reversed()) .limit(5) .map(action -> Maps.ofEntries( entry("code", action.getType().getCode()), entry("note", action.getPublicNote().orElse("Bez dalšího vysvětlení.")), entry("startDate", Util.toDate(action.getDateFrom())), entry("endDate", action.getDateTo().map(Util::toDate).orElse(null)) )).collect(Collectors.toList())); return result; }
final Map<String, Object> getData(final T event, final SessionInfo sessionInfo) { final Map<String, Object> result = new HashMap<>(this.getData(event)); // ratings here need to have a stable iteration order, as they will be used to list them in notifications result.put("ratings", Stream.of(Rating.values()).collect(Collectors.toList())); result.put("session", Maps.ofEntries( entry("userName", Util.obfuscateEmailAddress(sessionInfo.getUsername())), entry("userAgent", Defaults.ROBOZONKY_USER_AGENT), entry("isDryRun", sessionInfo.isDryRun()) )); result.put("conception", Date.from(event.getConceivedOn().toInstant())); result.put("creation", Date.from(event.getCreatedOn().toInstant())); return Collections.unmodifiableMap(result); }