Skip to content

Commit 0e5c9f1

Browse files
authored
Merge branch 'eugenp:master' into master
2 parents 8b2c8eb + 029177d commit 0e5c9f1

File tree

301 files changed

+9170
-459
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

301 files changed

+9170
-459
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.example</groupId>
8+
<artifactId>stringbuilder</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>17</maven.compiler.source>
13+
<maven.compiler.target>17</maven.compiler.target>
14+
</properties>
15+
16+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.example;
2+
3+
import java.io.BufferedReader;
4+
import java.io.StringReader;
5+
import java.io.IOException;
6+
7+
public class BufferedReaderApproach {
8+
9+
public static void main(String[] args) {
10+
11+
StringBuilder sb = new StringBuilder(
12+
"StringBuilder\nBufferedReader Approach\r\nLine by Line Reading\rAnother line"
13+
);
14+
15+
try (BufferedReader reader = new BufferedReader(new StringReader(sb.toString()))) {
16+
17+
String line;
18+
while ((line = reader.readLine()) != null) {
19+
System.out.println(line);
20+
}
21+
22+
} catch (IOException e) {
23+
e.printStackTrace();
24+
}
25+
26+
}
27+
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.example;
2+
3+
public class LineSeparatorApproach {
4+
public static void main(String[] args) {
5+
StringBuilder sb = new StringBuilder(
6+
"StringBuilder\nLine Separator Approach\r\nLine by Line Reading\rAnother line"
7+
);
8+
9+
// \R matches any line break (\n, \r\n, \r)
10+
String[] lines = sb.toString().split("\\R");
11+
12+
for (String line : lines) {
13+
System.out.println(line);
14+
}
15+
}
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
6+
7+
8+
}
9+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.example;
2+
3+
public class ManualIterationApproach {
4+
5+
public static void main(String[] args) {
6+
7+
StringBuilder sb = new StringBuilder(
8+
"StringBuilder\nManual Iteration Approach\r\nLine by Line Reading\rAnother line"
9+
);
10+
11+
int start = 0;
12+
13+
for (int i = 0; i < sb.length(); i++) {
14+
15+
char c = sb.charAt(i);
16+
17+
if (c == '\n' || c == '\r') {
18+
19+
System.out.println(sb.substring(start, i));
20+
21+
if (c == '\r' && i + 1 < sb.length() && sb.charAt(i + 1) == '\n') {
22+
i++;
23+
}
24+
25+
start = i + 1;
26+
}
27+
}
28+
29+
if (start < sb.length()) {
30+
System.out.println(sb.substring(start));
31+
}
32+
}
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.example;
2+
3+
import java.util.Scanner;
4+
import java.io.StringReader;
5+
6+
public class ScannerApproach {
7+
8+
public static void main(String[] args) {
9+
10+
StringBuilder sb = new StringBuilder(
11+
"StringBuilder\nScanner Approach\r\nLine by Line Reading\rAnother line"
12+
);
13+
14+
Scanner scanner = new Scanner(new StringReader(sb.toString()));
15+
16+
while (scanner.hasNextLine()) {
17+
System.out.println(scanner.nextLine());
18+
}
19+
20+
scanner.close();
21+
}
22+
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.example;
2+
3+
public class StreamLinesApproach {
4+
5+
public static void main(String[] args) {
6+
7+
StringBuilder sb = new StringBuilder(
8+
"StringBuilder\nStream Approach\r\nLine by Line Reading\rAnother line"
9+
);
10+
11+
sb.toString()
12+
.lines()
13+
.forEach(System.out::println);
14+
15+
}
16+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.instantandlong;
2+
3+
import java.time.Instant;
4+
import java.time.temporal.ChronoUnit;
5+
6+
public class InstantAndLong {
7+
8+
public static void main(String[] args) {
9+
long nowLong = Instant.now().toEpochMilli();
10+
11+
long someDayLong = 1_753_610_399_076L;
12+
Instant someDay = Instant.ofEpochMilli(someDayLong);
13+
14+
long expirationPeriod = 2_592_000_000L; // 30 days in milliseconds
15+
Instant now = Instant.now();
16+
Instant expirationTime = now.plus(expirationPeriod, ChronoUnit.MILLIS);
17+
18+
expirationTime = Instant.now().plusMillis(2_592_000_000L);
19+
20+
Instant aDayAgo = now.minus(86_400_000L, ChronoUnit.MILLIS);
21+
22+
aDayAgo = now.plus(-86_400_000L, ChronoUnit.MILLIS);
23+
24+
expirationPeriod = 30 // number of days
25+
* 24 // hours in one day
26+
* 3600 // seconds in one hour
27+
* 1000L;// from seconds to milliseconds
28+
29+
nowLong = Instant.now().toEpochMilli();
30+
31+
long expirationTimeLong = nowLong + expirationPeriod;
32+
}
33+
34+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.baeldung.instantandlong;
2+
3+
4+
import org.junit.Test;
5+
import static org.junit.Assert.assertEquals;
6+
7+
import java.time.Instant;
8+
import java.time.LocalDateTime;
9+
import java.time.ZoneId;
10+
import java.time.ZonedDateTime;
11+
import java.time.format.DateTimeFormatter;
12+
import java.time.temporal.ChronoUnit;
13+
import java.util.Locale;
14+
15+
public class InstantAndLongUnitTest {
16+
17+
private long someDayLong = 1_753_610_399_076L;
18+
private long oneDayLong = 86_400_000L;
19+
private String stringDate = "2025-01-30T17:33:21";
20+
//2025.01.30 17:33:21 in milliseconds, Java epoch
21+
private long dayInMillis = ((((2025 - 1970) * 365 + 29 + 14) * 24 // days, including an additional day for each of the 14 leap years
22+
+ 17) * 3600 //to seconds
23+
+ 33*60 + 21) //add minutes and seconds
24+
* 1000L; //to milliseconds
25+
26+
@Test
27+
public void whenPlusMillis_thenNextDay() {
28+
Instant someDay = Instant.ofEpochMilli(someDayLong);
29+
Instant nextDay = someDay.plusMillis(oneDayLong);
30+
31+
assertEquals(nextDay.toEpochMilli(), someDayLong + oneDayLong);
32+
}
33+
34+
@Test
35+
public void whenPlus_thenNextDay() {
36+
Instant someDay = Instant.ofEpochMilli(someDayLong);
37+
Instant nextDay = someDay.plus(oneDayLong, ChronoUnit.MILLIS);
38+
39+
assertEquals(nextDay.toEpochMilli(), someDayLong + oneDayLong);
40+
}
41+
42+
@Test
43+
public void whenMinusMillis_thenPreviousDay() {
44+
Instant someDay = Instant.ofEpochMilli(someDayLong);
45+
Instant previousDay = someDay.minusMillis(oneDayLong);
46+
47+
assertEquals(previousDay.toEpochMilli(), someDayLong - oneDayLong);
48+
}
49+
50+
@Test
51+
public void whenMinus_thenPreviousDay() {
52+
Instant someDay = Instant.ofEpochMilli(someDayLong);
53+
Instant previousDay = someDay.minus(oneDayLong, ChronoUnit.MILLIS);
54+
55+
assertEquals(previousDay.toEpochMilli(), someDayLong - oneDayLong);
56+
}
57+
58+
@Test
59+
public void whenToEpochMilli_thenDaysInMillis() {
60+
LocalDateTime dateTime = LocalDateTime.parse(stringDate, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
61+
ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.of("UTC"));
62+
Instant instant = zonedDateTime.toInstant();
63+
64+
assertEquals(instant.toEpochMilli(), dayInMillis);
65+
}
66+
67+
@Test
68+
public void whenOfEpochMilli_thenDateTimeAsInstant() {
69+
LocalDateTime dateTime = LocalDateTime.parse(stringDate, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
70+
ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.of("UTC"));
71+
Instant instant = zonedDateTime.toInstant();
72+
73+
assertEquals(Instant.ofEpochMilli(dayInMillis), instant);
74+
}
75+
76+
}
77+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.listwithnullorempty;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
9+
10+
class ListNullOrEmptyUnitTest {
11+
12+
List<String> list = Arrays.asList("Madrid", null, " ", "Havana", "");
13+
14+
@Test
15+
void givenListWithNullOrEmpty_whenCheckForNullOrEmptyUsingForLoop_thenReturnTrue() {
16+
boolean hasNullOrEmpty = false;
17+
for (String s : list) {
18+
if (s == null || s.isEmpty()) {
19+
hasNullOrEmpty = true;
20+
break;
21+
}
22+
}
23+
24+
assertTrue(hasNullOrEmpty, "List should contain null or empty elements");
25+
}
26+
27+
@Test
28+
void givenListWithNullOrEmpty_whenCheckForNullOrEmptyUsingStreams_thenReturnTrue() {
29+
boolean hasNullOrEmpty = list.stream()
30+
.anyMatch(s -> s == null || s.isEmpty());
31+
32+
assertTrue(hasNullOrEmpty, "List should contain null or blank elements");
33+
}
34+
35+
@Test
36+
void givenListWithNullOrEmpty_whenCheckUsingParallelStream_thenReturnTrue() {
37+
boolean hasNullOrEmpty = list.parallelStream()
38+
.anyMatch(s -> s == null || s.isEmpty());
39+
40+
assertTrue(hasNullOrEmpty, "List should contain null or empty elements");
41+
}
42+
43+
}

0 commit comments

Comments
 (0)