[JAVA-22519] Created new module libraries-stream and added 5 articles (#15403)
This commit is contained in:
@@ -1,40 +0,0 @@
|
||||
package com.baeldung.jasperreports;
|
||||
|
||||
import com.baeldung.jasperreports.config.JasperRerportsSimpleConfig;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(JasperRerportsSimpleConfig.class);
|
||||
ctx.refresh();
|
||||
|
||||
SimpleReportFiller simpleReportFiller = ctx.getBean(SimpleReportFiller.class);
|
||||
simpleReportFiller.setReportFileName("employeeEmailReport.jrxml");
|
||||
simpleReportFiller.compileReport();
|
||||
|
||||
simpleReportFiller.setReportFileName("employeeReport.jrxml");
|
||||
simpleReportFiller.compileReport();
|
||||
|
||||
Map<String, Object> parameters = new HashMap<>();
|
||||
parameters.put("title", "Employee Report Example");
|
||||
parameters.put("minSalary", 15000.0);
|
||||
parameters.put("condition", " LAST_NAME ='Smith' ORDER BY FIRST_NAME");
|
||||
|
||||
simpleReportFiller.setParameters(parameters);
|
||||
simpleReportFiller.fillReport();
|
||||
|
||||
SimpleReportExporter simpleExporter = ctx.getBean(SimpleReportExporter.class);
|
||||
simpleExporter.setJasperPrint(simpleReportFiller.getJasperPrint());
|
||||
|
||||
simpleExporter.exportToPdf("employeeReport.pdf", "baeldung");
|
||||
simpleExporter.exportToXlsx("employeeReport.xlsx", "Employee Data");
|
||||
simpleExporter.exportToCsv("employeeReport.csv");
|
||||
simpleExporter.exportToHtml("employeeReport.html");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
package com.baeldung.jasperreports;
|
||||
|
||||
import net.sf.jasperreports.engine.JRException;
|
||||
import net.sf.jasperreports.engine.JasperPrint;
|
||||
import net.sf.jasperreports.engine.export.HtmlExporter;
|
||||
import net.sf.jasperreports.engine.export.JRCsvExporter;
|
||||
import net.sf.jasperreports.engine.export.JRPdfExporter;
|
||||
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
|
||||
import net.sf.jasperreports.export.*;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@Component
|
||||
public class SimpleReportExporter {
|
||||
|
||||
private JasperPrint jasperPrint;
|
||||
|
||||
public SimpleReportExporter() {
|
||||
}
|
||||
|
||||
public SimpleReportExporter(JasperPrint jasperPrint) {
|
||||
this.jasperPrint = jasperPrint;
|
||||
}
|
||||
|
||||
public JasperPrint getJasperPrint() {
|
||||
return jasperPrint;
|
||||
}
|
||||
|
||||
public void setJasperPrint(JasperPrint jasperPrint) {
|
||||
this.jasperPrint = jasperPrint;
|
||||
}
|
||||
|
||||
public void exportToPdf(String fileName, String author) {
|
||||
|
||||
// print report to file
|
||||
JRPdfExporter exporter = new JRPdfExporter();
|
||||
|
||||
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
|
||||
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(fileName));
|
||||
|
||||
SimplePdfReportConfiguration reportConfig = new SimplePdfReportConfiguration();
|
||||
reportConfig.setSizePageToContent(true);
|
||||
reportConfig.setForceLineBreakPolicy(false);
|
||||
|
||||
SimplePdfExporterConfiguration exportConfig = new SimplePdfExporterConfiguration();
|
||||
exportConfig.setMetadataAuthor(author);
|
||||
exportConfig.setEncrypted(true);
|
||||
exportConfig.setAllowedPermissionsHint("PRINTING");
|
||||
|
||||
exporter.setConfiguration(reportConfig);
|
||||
exporter.setConfiguration(exportConfig);
|
||||
try {
|
||||
exporter.exportReport();
|
||||
} catch (JRException ex) {
|
||||
Logger.getLogger(SimpleReportFiller.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void exportToXlsx(String fileName, String sheetName) {
|
||||
JRXlsxExporter exporter = new JRXlsxExporter();
|
||||
|
||||
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
|
||||
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(fileName));
|
||||
|
||||
SimpleXlsxReportConfiguration reportConfig = new SimpleXlsxReportConfiguration();
|
||||
reportConfig.setSheetNames(new String[] { sheetName });
|
||||
|
||||
exporter.setConfiguration(reportConfig);
|
||||
|
||||
try {
|
||||
exporter.exportReport();
|
||||
} catch (JRException ex) {
|
||||
Logger.getLogger(SimpleReportFiller.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void exportToCsv(String fileName) {
|
||||
JRCsvExporter exporter = new JRCsvExporter();
|
||||
|
||||
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
|
||||
exporter.setExporterOutput(new SimpleWriterExporterOutput(fileName));
|
||||
|
||||
try {
|
||||
exporter.exportReport();
|
||||
} catch (JRException ex) {
|
||||
Logger.getLogger(SimpleReportFiller.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void exportToHtml(String fileName) {
|
||||
HtmlExporter exporter = new HtmlExporter();
|
||||
|
||||
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
|
||||
exporter.setExporterOutput(new SimpleHtmlExporterOutput(fileName));
|
||||
|
||||
try {
|
||||
exporter.exportReport();
|
||||
} catch (JRException ex) {
|
||||
Logger.getLogger(SimpleReportFiller.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
package com.baeldung.jasperreports;
|
||||
|
||||
import net.sf.jasperreports.engine.*;
|
||||
import net.sf.jasperreports.engine.util.JRSaver;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.InputStream;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@Component
|
||||
public class SimpleReportFiller {
|
||||
|
||||
private String reportFileName;
|
||||
|
||||
private JasperReport jasperReport;
|
||||
|
||||
private JasperPrint jasperPrint;
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
private Map<String, Object> parameters;
|
||||
|
||||
public SimpleReportFiller() {
|
||||
parameters = new HashMap<>();
|
||||
}
|
||||
|
||||
public void prepareReport() {
|
||||
compileReport();
|
||||
fillReport();
|
||||
}
|
||||
|
||||
public void compileReport() {
|
||||
try {
|
||||
InputStream reportStream = getClass().getResourceAsStream("/".concat(reportFileName));
|
||||
jasperReport = JasperCompileManager.compileReport(reportStream);
|
||||
JRSaver.saveObject(jasperReport, reportFileName.replace(".jrxml", ".jasper"));
|
||||
} catch (JRException ex) {
|
||||
Logger.getLogger(SimpleReportFiller.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void fillReport() {
|
||||
try {
|
||||
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection());
|
||||
} catch (JRException | SQLException ex) {
|
||||
Logger.getLogger(SimpleReportFiller.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public DataSource getDataSource() {
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
public Map<String, Object> getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public void setParameters(Map<String, Object> parameters) {
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public String getReportFileName() {
|
||||
return reportFileName;
|
||||
}
|
||||
|
||||
public void setReportFileName(String reportFileName) {
|
||||
this.reportFileName = reportFileName;
|
||||
}
|
||||
|
||||
public JasperPrint getJasperPrint() {
|
||||
return jasperPrint;
|
||||
}
|
||||
|
||||
}
|
||||
-30
@@ -1,30 +0,0 @@
|
||||
package com.baeldung.jasperreports.config;
|
||||
|
||||
import com.baeldung.jasperreports.SimpleReportExporter;
|
||||
import com.baeldung.jasperreports.SimpleReportFiller;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
public class JasperRerportsSimpleConfig {
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL).addScript("classpath:employee-schema.sql").build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleReportFiller reportFiller() {
|
||||
return new SimpleReportFiller();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleReportExporter reportExporter() {
|
||||
return new SimpleReportExporter();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package com.baeldung.picocli.git;
|
||||
|
||||
import com.baeldung.picocli.git.commands.programmative.GitCommand;
|
||||
import com.baeldung.picocli.git.commands.subcommands.GitAddCommand;
|
||||
import com.baeldung.picocli.git.commands.subcommands.GitCommitCommand;
|
||||
import com.baeldung.picocli.git.commands.subcommands.GitConfigCommand;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import picocli.CommandLine;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application implements CommandLineRunner {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
private GitCommand gitCommand;
|
||||
private GitAddCommand addCommand;
|
||||
private GitCommitCommand commitCommand;
|
||||
private GitConfigCommand configCommand;
|
||||
|
||||
@Autowired
|
||||
public Application(GitCommand gitCommand, GitAddCommand addCommand, GitCommitCommand commitCommand, GitConfigCommand configCommand) {
|
||||
this.gitCommand = gitCommand;
|
||||
this.addCommand = addCommand;
|
||||
this.commitCommand = commitCommand;
|
||||
this.configCommand = configCommand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
CommandLine commandLine = new CommandLine(gitCommand);
|
||||
commandLine.addSubcommand("add", addCommand);
|
||||
commandLine.addSubcommand("commit", commitCommand);
|
||||
commandLine.addSubcommand("config", configCommand);
|
||||
|
||||
commandLine.parseWithHandler(new CommandLine.RunLast(), args);
|
||||
}
|
||||
}
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
package com.baeldung.picocli.git.commands.declarative;
|
||||
|
||||
import com.baeldung.picocli.git.model.ConfigElement;
|
||||
import com.baeldung.picocli.git.commands.subcommands.GitAddCommand;
|
||||
import com.baeldung.picocli.git.commands.subcommands.GitCommitCommand;
|
||||
import com.baeldung.picocli.git.commands.subcommands.GitConfigCommand;
|
||||
import picocli.CommandLine;
|
||||
|
||||
import static picocli.CommandLine.*;
|
||||
import static picocli.CommandLine.Command;
|
||||
|
||||
@Command(
|
||||
name = "git",
|
||||
subcommands = {
|
||||
GitAddCommand.class,
|
||||
GitCommitCommand.class,
|
||||
GitConfigCommand.class
|
||||
}
|
||||
)
|
||||
public class GitCommand implements Runnable {
|
||||
public static void main(String[] args) {
|
||||
CommandLine commandLine = new CommandLine(new GitCommand());
|
||||
commandLine.registerConverter(ConfigElement.class, ConfigElement::from);
|
||||
|
||||
commandLine.parseWithHandler(new RunLast(), args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("The popular git command");
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package com.baeldung.picocli.git.commands.methods;
|
||||
|
||||
import picocli.CommandLine;
|
||||
|
||||
import static picocli.CommandLine.Command;
|
||||
|
||||
@Command(name = "git")
|
||||
public class GitCommand implements Runnable {
|
||||
public static void main(String[] args) {
|
||||
CommandLine.run(new GitCommand(), args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("The popular git command");
|
||||
}
|
||||
|
||||
@Command(name = "add")
|
||||
public void addCommand() {
|
||||
System.out.println("Adding some files to the staging area");
|
||||
}
|
||||
|
||||
@Command(name = "commit")
|
||||
public void commitCommand() {
|
||||
System.out.println("Committing files in the staging area, how wonderful?");
|
||||
}
|
||||
}
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
package com.baeldung.picocli.git.commands.programmative;
|
||||
|
||||
import com.baeldung.picocli.git.commands.subcommands.GitAddCommand;
|
||||
import com.baeldung.picocli.git.commands.subcommands.GitCommitCommand;
|
||||
import org.springframework.stereotype.Component;
|
||||
import picocli.CommandLine;
|
||||
|
||||
import static picocli.CommandLine.Command;
|
||||
import static picocli.CommandLine.RunLast;
|
||||
|
||||
@Command(name = "git")
|
||||
@Component
|
||||
public class GitCommand implements Runnable {
|
||||
public static void main(String[] args) {
|
||||
CommandLine commandLine = new CommandLine(new GitCommand());
|
||||
commandLine.addSubcommand("add", new GitAddCommand());
|
||||
commandLine.addSubcommand("commit", new GitCommitCommand());
|
||||
|
||||
commandLine.parseWithHandler(new RunLast(), args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("The popular git command");
|
||||
}
|
||||
}
|
||||
-31
@@ -1,31 +0,0 @@
|
||||
package com.baeldung.picocli.git.commands.subcommands;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static picocli.CommandLine.*;
|
||||
|
||||
@Command(
|
||||
name = "add"
|
||||
)
|
||||
@Component
|
||||
public class GitAddCommand implements Runnable {
|
||||
@Option(names = "-A")
|
||||
private boolean allFiles;
|
||||
|
||||
@Parameters(index = "0..*")
|
||||
private List<Path> files;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (allFiles) {
|
||||
System.out.println("Adding all files to the staging area");
|
||||
}
|
||||
|
||||
if (files != null) {
|
||||
files.forEach(path -> System.out.println("Adding " + path + " to the staging area"));
|
||||
}
|
||||
}
|
||||
}
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
package com.baeldung.picocli.git.commands.subcommands;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static picocli.CommandLine.Command;
|
||||
import static picocli.CommandLine.Option;
|
||||
|
||||
@Command(
|
||||
name = "commit"
|
||||
)
|
||||
@Component
|
||||
public class GitCommitCommand implements Runnable {
|
||||
@Option(names = {"-m", "--message"}, required = true)
|
||||
private String[] messages;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Committing files in the staging area, how wonderful?");
|
||||
if (messages != null) {
|
||||
System.out.println("The commit message is");
|
||||
for (String message : messages) {
|
||||
System.out.println(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
-24
@@ -1,24 +0,0 @@
|
||||
package com.baeldung.picocli.git.commands.subcommands;
|
||||
|
||||
import com.baeldung.picocli.git.model.ConfigElement;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static picocli.CommandLine.Command;
|
||||
import static picocli.CommandLine.Parameters;
|
||||
|
||||
@Command(
|
||||
name = "config"
|
||||
)
|
||||
@Component
|
||||
public class GitConfigCommand implements Runnable {
|
||||
@Parameters(index = "0")
|
||||
private ConfigElement element;
|
||||
|
||||
@Parameters(index = "1")
|
||||
private String value;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Setting " + element.value() + " to " + value);
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.picocli.git.model;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public enum ConfigElement {
|
||||
USERNAME("user.name"),
|
||||
EMAIL("user.email");
|
||||
|
||||
private final String value;
|
||||
|
||||
ConfigElement(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String value() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public static ConfigElement from(String value) {
|
||||
return Arrays.stream(values())
|
||||
.filter(element -> element.value.equals(value))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new IllegalArgumentException("The argument " + value + " doesn't match any ConfigElement"));
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.baeldung.picocli.helloworld;
|
||||
|
||||
import picocli.CommandLine;
|
||||
|
||||
import static picocli.CommandLine.Command;
|
||||
|
||||
@Command(
|
||||
name = "hello",
|
||||
description = "Says hello"
|
||||
)
|
||||
public class HelloWorldCommand implements Runnable {
|
||||
public static void main(String[] args) {
|
||||
CommandLine.run(new HelloWorldCommand(), args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
CREATE TABLE EMPLOYEE(
|
||||
ID INT NOT NULL PRIMARY KEY,
|
||||
FIRST_NAME VARCHAR(255),
|
||||
LAST_NAME VARCHAR(255),
|
||||
SALARY DOUBLE,
|
||||
);
|
||||
|
||||
CREATE TABLE EMAIL(
|
||||
ID INT NOT NULL PRIMARY KEY,
|
||||
ID_EMPLOYEE VARCHAR(255),
|
||||
ADDRESS VARCHAR(255)
|
||||
);
|
||||
|
||||
INSERT INTO EMPLOYEE VALUES (1, 'John', 'Doe', 10000.10);
|
||||
INSERT INTO EMPLOYEE VALUES (2, 'Kevin', 'Smith', 20000.20);
|
||||
INSERT INTO EMPLOYEE VALUES (3, 'Kim', 'Smith', 30000.30);
|
||||
INSERT INTO EMPLOYEE VALUES (4, 'Stephen', 'Torvalds', 40000.40);
|
||||
INSERT INTO EMPLOYEE VALUES (5, 'Christian', 'Reynolds', 50000.50);
|
||||
|
||||
INSERT INTO EMAIL VALUES (1, 1, 'john@baeldung.com');
|
||||
INSERT INTO EMAIL VALUES (2, 1, 'john@gmail.com');
|
||||
INSERT INTO EMAIL VALUES (3, 2, 'kevin@baeldung.com');
|
||||
INSERT INTO EMAIL VALUES (4, 3, 'kim@baeldung.com');
|
||||
INSERT INTO EMAIL VALUES (5, 3, 'kim@gmail.com');
|
||||
INSERT INTO EMAIL VALUES (6, 3, 'kim@outlook.com');
|
||||
INSERT INTO EMAIL VALUES (7, 4, 'stephen@baeldung.com');
|
||||
INSERT INTO EMAIL VALUES (8, 5, 'christian@gmail.com');
|
||||
@@ -1,21 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
|
||||
name="employeeReport" pageWidth="612" pageHeight="792" columnWidth="555" leftMargin="20" rightMargin="20"
|
||||
topMargin="20" bottomMargin="20">
|
||||
<parameter name="idEmployee" class="java.lang.Integer" isForPrompting="false"/>
|
||||
<queryString>
|
||||
<![CDATA[SELECT * FROM EMAIL WHERE ID_EMPLOYEE = $P{idEmployee}]]>
|
||||
</queryString>
|
||||
<field name="ADDRESS" class="java.lang.String"/>
|
||||
<detail>
|
||||
<band height="20" splitType="Stretch">
|
||||
<textField>
|
||||
<reportElement x="0" y="0" width="156" height="20"/>
|
||||
<textElement/>
|
||||
<textFieldExpression class="java.lang.String"><![CDATA[$F{ADDRESS}]]></textFieldExpression>
|
||||
</textField>
|
||||
</band>
|
||||
</detail>
|
||||
</jasperReport>
|
||||
@@ -1,56 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
|
||||
name="employeeReport" pageWidth="612" pageHeight="792" columnWidth="555" leftMargin="20" rightMargin="20"
|
||||
topMargin="20" bottomMargin="20">
|
||||
<parameter name="title" class="java.lang.String" isForPrompting="false"/>
|
||||
<parameter name="condition" class="java.lang.String" isForPrompting="false">
|
||||
<defaultValueExpression><![CDATA[" 1 = 1"]]></defaultValueExpression>
|
||||
</parameter>
|
||||
<parameter name="minSalary" class="java.lang.Double" isForPrompting="false"/>
|
||||
<queryString>
|
||||
<![CDATA[SELECT * FROM EMPLOYEE WHERE SALARY >= $P{minSalary} AND $P!{condition}]]>
|
||||
</queryString>
|
||||
<field name="FIRST_NAME" class="java.lang.String"/>
|
||||
<field name="LAST_NAME" class="java.lang.String"/>
|
||||
<field name="SALARY" class="java.lang.Double"/>
|
||||
<field name="ID" class="java.lang.Integer"/>
|
||||
<title>
|
||||
<band height="20" splitType="Stretch">
|
||||
<textField>
|
||||
<reportElement x="238" y="0" width="100" height="20"/>
|
||||
<textElement/>
|
||||
<textFieldExpression class="java.lang.String"><![CDATA[$P{title}]]></textFieldExpression>
|
||||
</textField>
|
||||
</band>
|
||||
</title>
|
||||
<detail>
|
||||
<band height="47" splitType="Stretch">
|
||||
<textField>
|
||||
<reportElement x="0" y="0" width="100" height="20"/>
|
||||
<textElement/>
|
||||
<textFieldExpression class="java.lang.String"><![CDATA[$F{FIRST_NAME}]]></textFieldExpression>
|
||||
</textField>
|
||||
<textField>
|
||||
<reportElement x="100" y="0" width="100" height="20"/>
|
||||
<textElement/>
|
||||
<textFieldExpression class="java.lang.String"><![CDATA[$F{LAST_NAME}]]></textFieldExpression>
|
||||
</textField>
|
||||
<textField>
|
||||
<reportElement x="200" y="0" width="100" height="20"/>
|
||||
<textElement/>
|
||||
<textFieldExpression class="java.lang.String"><![CDATA[$F{SALARY}]]></textFieldExpression>
|
||||
</textField>
|
||||
<subreport>
|
||||
<reportElement x="0" y="20" width="300" height="27"/>
|
||||
<subreportParameter name="idEmployee">
|
||||
<subreportParameterExpression><![CDATA[$F{ID}]]></subreportParameterExpression>
|
||||
</subreportParameter>
|
||||
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
|
||||
<subreportExpression class="java.lang.String">
|
||||
<![CDATA["employeeEmailReport.jasper"]]></subreportExpression>
|
||||
</subreport>
|
||||
</band>
|
||||
</detail>
|
||||
</jasperReport>
|
||||
-168
@@ -1,168 +0,0 @@
|
||||
package com.baeldung.parallel_collectors;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.pivovarit.collectors.ParallelCollectors.parallel;
|
||||
import static com.pivovarit.collectors.ParallelCollectors.parallelOrdered;
|
||||
import static com.pivovarit.collectors.ParallelCollectors.parallelToCollection;
|
||||
import static com.pivovarit.collectors.ParallelCollectors.parallelToList;
|
||||
import static com.pivovarit.collectors.ParallelCollectors.parallelToMap;
|
||||
import static com.pivovarit.collectors.ParallelCollectors.parallelToStream;
|
||||
|
||||
public class ParallelCollectorsUnitTest {
|
||||
|
||||
@Test
|
||||
public void shouldProcessInParallelWithStreams() {
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
List<String> results = ids.parallelStream()
|
||||
.map(i -> fetchById(i))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
System.out.println(results);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldProcessInParallelWithParallelCollectors() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
CompletableFuture<List<String>> results = ids.stream()
|
||||
.collect(parallelToList(ParallelCollectorsUnitTest::fetchById, executor, 4));
|
||||
|
||||
System.out.println(results.join());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCollectToList() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
List<String> results = ids.stream()
|
||||
.collect(parallelToList(ParallelCollectorsUnitTest::fetchById, executor, 4))
|
||||
.join();
|
||||
|
||||
System.out.println(results); // [user-1, user-2, user-3]
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCollectToCollection() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
List<String> results = ids.stream()
|
||||
.collect(parallelToCollection(i -> fetchById(i), LinkedList::new, executor, 4))
|
||||
.join();
|
||||
|
||||
System.out.println(results); // [user-1, user-2, user-3]
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCollectToStream() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
Map<Integer, List<String>> results = ids.stream()
|
||||
.collect(parallelToStream(i -> fetchById(i), executor, 4))
|
||||
.thenApply(stream -> stream.collect(Collectors.groupingBy(i -> i.length())))
|
||||
.join();
|
||||
|
||||
System.out.println(results); // [user-1, user-2, user-3]
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldStreamInCompletionOrder() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
ids.stream()
|
||||
.collect(parallel(ParallelCollectorsUnitTest::fetchByIdWithRandomDelay, executor, 4))
|
||||
.forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldStreamInOriginalOrder() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
ids.stream()
|
||||
.collect(parallelOrdered(ParallelCollectorsUnitTest::fetchByIdWithRandomDelay, executor, 4))
|
||||
.forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCollectToMap() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
Map<Integer, String> results = ids.stream()
|
||||
.collect(parallelToMap(i -> i, ParallelCollectorsUnitTest::fetchById, executor, 4))
|
||||
.join();
|
||||
|
||||
System.out.println(results); // {1=user-1, 2=user-2, 3=user-3}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCollectToTreeMap() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
Map<Integer, String> results = ids.stream()
|
||||
.collect(parallelToMap(i -> i, ParallelCollectorsUnitTest::fetchById, TreeMap::new, executor, 4))
|
||||
.join();
|
||||
|
||||
System.out.println(results); // {1=user-1, 2=user-2, 3=user-3}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCollectToTreeMapAndResolveClashes() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
|
||||
List<Integer> ids = Arrays.asList(1, 2, 3);
|
||||
|
||||
Map<Integer, String> results = ids.stream()
|
||||
.collect(parallelToMap(i -> i, ParallelCollectorsUnitTest::fetchById, TreeMap::new, (s1, s2) -> s1, executor, 4))
|
||||
.join();
|
||||
|
||||
System.out.println(results); // {1=user-1, 2=user-2, 3=user-3}
|
||||
}
|
||||
|
||||
private static String fetchById(int id) {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
// ignore shamelessly
|
||||
}
|
||||
|
||||
return "user-" + id;
|
||||
}
|
||||
|
||||
private static String fetchByIdWithRandomDelay(int id) {
|
||||
try {
|
||||
Thread.sleep(ThreadLocalRandom.current().nextInt(1000));
|
||||
} catch (InterruptedException e) {
|
||||
// ignore shamelessly
|
||||
}
|
||||
|
||||
return "user-" + id;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user