Skip to content

Commit 8acbafd

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents c05945b + d1ea5e8 commit 8acbafd

48 files changed

Lines changed: 1426 additions & 109 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.baeldung.java9.language.stream;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.stream.Collectors;
10+
import java.util.stream.Stream;
11+
12+
import static java.lang.Integer.*;
13+
import static org.junit.Assert.assertEquals;
14+
15+
public class StreamFeaturesTest {
16+
17+
public static class TakeAndDropWhileTest {
18+
19+
public Stream<String> getStreamAfterTakeWhileOperation() {
20+
return Stream
21+
.iterate("", s -> s + "s")
22+
.takeWhile(s -> s.length() < 10);
23+
}
24+
25+
public Stream<String> getStreamAfterDropWhileOperation() {
26+
return Stream
27+
.iterate("", s -> s + "s")
28+
.takeWhile(s -> s.length() < 10)
29+
.dropWhile(s -> !s.contains("sssss"));
30+
}
31+
32+
@Test
33+
public void testTakeWhileOperation() {
34+
List<String> list = getStreamAfterTakeWhileOperation().collect(Collectors.toList());
35+
36+
assertEquals(10, list.size());
37+
38+
assertEquals("", list.get(0));
39+
assertEquals("ss", list.get(2));
40+
assertEquals("sssssssss", list.get(list.size() - 1));
41+
}
42+
43+
@Test
44+
public void testDropWhileOperation() {
45+
List<String> list = getStreamAfterDropWhileOperation().collect(Collectors.toList());
46+
47+
assertEquals(5, list.size());
48+
49+
assertEquals("sssss", list.get(0));
50+
assertEquals("sssssss", list.get(2));
51+
assertEquals("sssssssss", list.get(list.size() - 1));
52+
}
53+
54+
}
55+
56+
public static class IterateTest {
57+
58+
private Stream<Integer> getStream() {
59+
return Stream.iterate(0, i -> i < 10, i -> i + 1);
60+
}
61+
62+
@Test
63+
public void testIterateOperation() {
64+
List<Integer> list = getStream().collect(Collectors.toList());
65+
66+
assertEquals(10, list.size());
67+
68+
assertEquals(valueOf(0), list.get(0));
69+
assertEquals(valueOf(5), list.get(5));
70+
assertEquals(valueOf(9), list.get(list.size() - 1));
71+
}
72+
73+
}
74+
75+
public static class OfNullableTest {
76+
77+
private List<String> collection = Arrays.asList("A", "B", "C");
78+
private Map<String, Integer> map = new HashMap<>() {{
79+
put("A", 10);
80+
put("C", 30);
81+
}};
82+
83+
private Stream<Integer> getStreamWithOfNullable() {
84+
return collection.stream()
85+
.flatMap(s -> Stream.ofNullable(map.get(s)));
86+
}
87+
88+
private Stream<Integer> getStream() {
89+
return collection.stream()
90+
.flatMap(s -> {
91+
Integer temp = map.get(s);
92+
return temp != null ? Stream.of(temp) : Stream.empty();
93+
});
94+
}
95+
96+
private List<Integer> testOfNullableFrom(Stream<Integer> stream) {
97+
List<Integer> list = stream.collect(Collectors.toList());
98+
99+
assertEquals(2, list.size());
100+
101+
assertEquals(valueOf(10), list.get(0));
102+
assertEquals(valueOf(30), list.get(list.size() - 1));
103+
104+
return list;
105+
}
106+
107+
@Test
108+
public void testOfNullable() {
109+
110+
assertEquals(
111+
testOfNullableFrom(getStream()),
112+
testOfNullableFrom(getStreamWithOfNullable())
113+
);
114+
115+
}
116+
117+
}
118+
119+
}

‎core-java/0.12457740242410742‎

Whitespace-only changes.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.java.conversion;
2+
3+
import java.text.ParseException;
4+
import java.text.SimpleDateFormat;
5+
import java.time.LocalDateTime;
6+
import java.util.Date;
7+
8+
import com.baeldung.datetime.UseLocalDateTime;
9+
10+
public class StringConversion {
11+
12+
public static int getInt(String str) {
13+
return Integer.parseInt(str);
14+
}
15+
16+
public static int getInteger(String str) {
17+
return Integer.valueOf(str);
18+
}
19+
20+
public static long getLongPrimitive(String str) {
21+
return Long.parseLong(str);
22+
}
23+
24+
public static Long getLong(String str) {
25+
return Long.valueOf(str);
26+
}
27+
28+
public static double getDouble(String str) {
29+
return Double.parseDouble(str);
30+
}
31+
32+
public static double getDoublePrimitive(String str) {
33+
return Double.valueOf(str);
34+
}
35+
36+
public static byte[] getByteArray(String str) {
37+
return str.getBytes();
38+
}
39+
40+
public static char[] getCharArray(String str) {
41+
return str.toCharArray();
42+
}
43+
44+
public static boolean getBooleanPrimitive(String str) {
45+
return Boolean.parseBoolean(str);
46+
}
47+
48+
public static boolean getBoolean(String str) {
49+
return Boolean.valueOf(str);
50+
}
51+
52+
public static Date getJava6Date(String str, String format) throws ParseException {
53+
SimpleDateFormat formatter = new SimpleDateFormat(format);
54+
return formatter.parse(str);
55+
}
56+
57+
public static LocalDateTime getJava8Date(String str) throws ParseException {
58+
return new UseLocalDateTime().getLocalDateTimeUsingParseMethod(str);
59+
}
60+
}

‎pdf/pom.xml‎

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,54 +24,41 @@
2424
<version>3.8.1</version>
2525
<scope>test</scope>
2626
</dependency>
27-
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
28-
<dependency>
29-
<groupId>org.apache.pdfbox</groupId>
30-
<artifactId>pdfbox</artifactId>
31-
<version>2.0.3</version>
32-
</dependency>
33-
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox-tools -->
3427
<dependency>
3528
<groupId>org.apache.pdfbox</groupId>
3629
<artifactId>pdfbox-tools</artifactId>
3730
<version>2.0.3</version>
3831
</dependency>
39-
<!-- https://mvnrepository.com/artifact/net.sf.cssbox/pdf2dom -->
4032
<dependency>
4133
<groupId>net.sf.cssbox</groupId>
4234
<artifactId>pdf2dom</artifactId>
4335
<version>1.6</version>
4436
</dependency>
45-
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
4637
<dependency>
4738
<groupId>com.itextpdf</groupId>
4839
<artifactId>itextpdf</artifactId>
4940
<version>5.5.10</version>
5041
</dependency>
51-
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
5242
<dependency>
5343
<groupId>org.apache.poi</groupId>
5444
<artifactId>poi</artifactId>
5545
<version>3.15</version>
5646
</dependency>
57-
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
58-
<dependency>
59-
<groupId>org.apache.poi</groupId>
60-
<artifactId>poi-ooxml</artifactId>
61-
<version>3.15</version>
62-
</dependency>
63-
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
6447
<dependency>
6548
<groupId>org.apache.poi</groupId>
6649
<artifactId>poi-scratchpad</artifactId>
6750
<version>3.15</version>
6851
</dependency>
69-
<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/batik-transcoder -->
7052
<dependency>
7153
<groupId>org.apache.xmlgraphics</groupId>
7254
<artifactId>batik-transcoder</artifactId>
7355
<version>1.8</version>
7456
</dependency>
57+
<dependency>
58+
<groupId>org.apache.poi</groupId>
59+
<artifactId>poi-ooxml</artifactId>
60+
<version>3.15</version>
61+
</dependency>
7562
</dependencies>
7663

7764
<build>

‎pdf/src/main/java/com/baeldung/pdf/PDF2HTMLExample.java‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,13 @@ public static void main(String[] args) {
2323
}
2424

2525
private static void generateHTMLFromPDF(String filename) throws ParserConfigurationException, IOException {
26-
try {
27-
PDDocument pdf = PDDocument.load(new File(filename));
28-
PDFDomTree parser = new PDFDomTree();
29-
Writer output = new PrintWriter("src/output/pdf.html", "utf-8");
30-
parser.writeText(pdf, output);
31-
output.close();
32-
if (pdf != null) {
33-
pdf.close();
34-
}
35-
} catch (IOException e) {
36-
e.printStackTrace();
26+
PDDocument pdf = PDDocument.load(new File(filename));
27+
PDFDomTree parser = new PDFDomTree();
28+
Writer output = new PrintWriter("src/output/pdf.html", "utf-8");
29+
parser.writeText(pdf, output);
30+
output.close();
31+
if (pdf != null) {
32+
pdf.close();
3733
}
3834
}
3935
}

‎pdf/src/main/java/com/baeldung/pdf/PDF2TextExample.java‎

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,26 @@ public static void main(String[] args) {
2323
}
2424

2525
private static void generateTxtFromPDF(String filename) throws IOException {
26-
try {
27-
File f = new File(filename);
28-
String parsedText;
29-
PDFParser parser = new PDFParser(new RandomAccessFile(f, "r"));
30-
parser.parse();
31-
32-
COSDocument cosDoc = parser.getDocument();
33-
34-
PDFTextStripper pdfStripper = new PDFTextStripper();
35-
PDDocument pdDoc = new PDDocument(cosDoc);
36-
37-
parsedText = pdfStripper.getText(pdDoc);
38-
39-
if (cosDoc != null)
40-
cosDoc.close();
41-
if (pdDoc != null)
42-
pdDoc.close();
43-
44-
PrintWriter pw = new PrintWriter("src/output/pdf.txt");
45-
pw.print(parsedText);
46-
pw.close();
47-
} catch (Exception e) {
48-
e.printStackTrace();
49-
}
26+
File f = new File(filename);
27+
String parsedText;
28+
PDFParser parser = new PDFParser(new RandomAccessFile(f, "r"));
29+
parser.parse();
30+
31+
COSDocument cosDoc = parser.getDocument();
32+
33+
PDFTextStripper pdfStripper = new PDFTextStripper();
34+
PDDocument pdDoc = new PDDocument(cosDoc);
35+
36+
parsedText = pdfStripper.getText(pdDoc);
37+
38+
if (cosDoc != null)
39+
cosDoc.close();
40+
if (pdDoc != null)
41+
pdDoc.close();
42+
43+
PrintWriter pw = new PrintWriter("src/output/pdf.txt");
44+
pw.print(parsedText);
45+
pw.close();
5046
}
5147

5248
}

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
<module>spring-security-rest-full</module>
133133
<module>spring-security-rest</module>
134134
<module>spring-security-x509</module>
135+
<module>spring-session</module>
135136
<module>spring-spel</module>
136137
<module>spring-thymeleaf</module>
137138
<module>spring-userservice</module>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.camel.file;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.util.Date;
5+
6+
import org.apache.camel.Exchange;
7+
import org.apache.camel.Processor;
8+
9+
public class FileProcessor implements Processor {
10+
11+
public void process(Exchange exchange) throws Exception {
12+
String originalFileName = (String) exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
13+
14+
Date date = new Date();
15+
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
16+
String changedFileName = dateFormat.format(date) + originalFileName;
17+
exchange.getIn().setHeader(Exchange.FILE_NAME, changedFileName);
18+
}
19+
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.camel.file;
2+
3+
import org.apache.camel.builder.RouteBuilder;
4+
5+
public class FileRouter extends RouteBuilder {
6+
7+
private static final String SOURCE_FOLDER = "src/test/source-folder";
8+
private static final String DESTINATION_FOLDER = "src/test/destination-folder";
9+
10+
@Override
11+
public void configure() throws Exception {
12+
from("file://" + SOURCE_FOLDER + "?delete=true").process(new FileProcessor()).to("file://" + DESTINATION_FOLDER);
13+
}
14+
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
5+
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
6+
7+
<bean id="fileRouter" class="com.baeldung.camel.file.FileRouter" />
8+
<bean id="fileProcessor" class="com.baeldung.camel.file.FileProcessor" />
9+
10+
<camelContext xmlns="http://camel.apache.org/schema/spring">
11+
<routeBuilder ref="fileRouter" />
12+
</camelContext>
13+
14+
</beans>

0 commit comments

Comments
 (0)