Skip to content

Commit 7b84d6c

Browse files
authored
Merge branch 'master' into master
2 parents 2e99f89 + ca93498 commit 7b84d6c

File tree

186 files changed

+2812
-229
lines changed

Some content is hidden

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

186 files changed

+2812
-229
lines changed

Diff for: .gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
.idea/
2020
*.iml
2121
*.iws
22+
out/
2223

2324
# Mac
2425
.DS_Store
@@ -27,6 +28,9 @@
2728
log/
2829
target/
2930

31+
# Gradle
32+
.gradle/
33+
3034
spring-openid/src/main/resources/application.properties
3135
.recommenders/
3236
/spring-hibernate4/nbproject/

Diff for: azure/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
### Relevant Articles:
22

3-
- [Deploy Spring Boot App to Azure](http://www.baeldung.com/spring-boot-azure)
3+
- [Deploy a Spring Boot App to Azure](http://www.baeldung.com/spring-boot-azure)
44

Diff for: blade/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
### Relevant Articles:
22

3-
- [Blade - A Complete GuideBook](http://www.baeldung.com/blade)
3+
- [Blade A Complete Guidebook](http://www.baeldung.com/blade)
44

5-
Run Integration Tests with `mvn integration-test`
5+
Run Integration Tests with `mvn integration-test`

Diff for: core-groovy-2/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
## Relevant articles:
44

5+
- [String Matching in Groovy](http://www.baeldung.com/)
56
- [Groovy def Keyword]
7+

Diff for: core-groovy-2/pom.xml

+9
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@
9696
</configuration>
9797
</execution>
9898
</executions>
99+
<artifactId>maven-surefire-plugin</artifactId>
100+
<version>2.20.1</version>
101+
<configuration>
102+
<useFile>false</useFile>
103+
<includes>
104+
<include>**/*Test.java</include>
105+
<include>**/*Spec.java</include>
106+
</includes>
107+
</configuration>
99108
</plugin>
100109
</plugins>
101110
</build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.strings
2+
3+
import spock.lang.Specification
4+
5+
import java.util.regex.Pattern
6+
7+
class StringMatchingSpec extends Specification {
8+
9+
def "pattern operator example"() {
10+
given: "a pattern"
11+
def p = ~'foo'
12+
13+
expect:
14+
p instanceof Pattern
15+
16+
and: "you can use slash strings to avoid escaping of blackslash"
17+
def digitPattern = ~/\d*/
18+
digitPattern.matcher('4711').matches()
19+
}
20+
21+
def "match operator example"() {
22+
expect:
23+
'foobar' ==~ /.*oba.*/
24+
25+
and: "matching is strict"
26+
!('foobar' ==~ /foo/)
27+
}
28+
29+
def "find operator example"() {
30+
when: "using the find operator"
31+
def matcher = 'foo and bar, baz and buz' =~ /(\w+) and (\w+)/
32+
33+
then: "will find groups"
34+
matcher.size() == 2
35+
36+
and: "can access groups using array"
37+
matcher[0][0] == 'foo and bar'
38+
matcher[1][2] == 'buz'
39+
40+
and: "you can use it as a predicate"
41+
'foobarbaz' =~ /bar/
42+
}
43+
44+
}

Diff for: core-groovy/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
- [Types of Strings in Groovy](https://www.baeldung.com/groovy-strings)
99
- [A Quick Guide to Iterating a Map in Groovy](https://www.baeldung.com/groovy-map-iterating)
1010
- [An Introduction to Traits in Groovy](https://www.baeldung.com/groovy-traits)
11+
- [Closures in Groovy](https://www.baeldung.com/groovy-closures)
12+
- [Finding Elements in Collections in Groovy](https://www.baeldung.com/groovy-collections-find-elements)
1113
- [Lists in Groovy](https://www.baeldung.com/groovy-lists)
1214
- [Converting a String to a Date in Groovy](https://www.baeldung.com/groovy-string-to-date)
13-
- [Guide to I/O in Groovy](https://www.baeldung.com/groovy-io)
15+
- [Guide to I/O in Groovy](https://www.baeldung.com/groovy-io)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.strings
2+
3+
import spock.lang.Specification
4+
5+
import java.util.regex.Pattern
6+
7+
class StringMatchingSpec extends Specification {
8+
9+
def "pattern operator example"() {
10+
given: "a pattern"
11+
def p = ~'foo'
12+
13+
expect:
14+
p instanceof Pattern
15+
16+
and: "you can use slash strings to avoid escaping of blackslash"
17+
def digitPattern = ~/\d*/
18+
digitPattern.matcher('4711').matches()
19+
}
20+
21+
def "match operator example"() {
22+
expect:
23+
'foobar' ==~ /.*oba.*/
24+
25+
and: "matching is strict"
26+
!('foobar' ==~ /foo/)
27+
}
28+
29+
def "find operator example"() {
30+
when: "using the find operator"
31+
def matcher = 'foo and bar, baz and buz' =~ /(\w+) and (\w+)/
32+
33+
then: "will find groups"
34+
matcher.size() == 2
35+
36+
and: "can access groups using array"
37+
matcher[0][0] == 'foo and bar'
38+
matcher[1][2] == 'buz'
39+
40+
and: "you can use it as a predicate"
41+
'foobarbaz' =~ /bar/
42+
}
43+
44+
}

Diff for: core-java-11/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
- [Java 11 Nest Based Access Control](https://www.baeldung.com/java-nest-based-access-control)
77
- [Exploring the New HTTP Client in Java 9 and 11](https://www.baeldung.com/java-9-http-client)
88
- [An Introduction to Epsilon GC: A No-Op Experimental Garbage Collector](https://www.baeldung.com/jvm-epsilon-gc-garbage-collector)
9+
- [Guide to jlink](https://www.baeldung.com/jlink)

Diff for: core-java-8/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Core Java 8 Cookbooks and Examples
44

55
### Relevant Articles:
6-
- [Java 8 Collectors](http://www.baeldung.com/java-8-collectors)
6+
- [Guide to Java 8’s Collectors](http://www.baeldung.com/java-8-collectors)
77
- [Functional Interfaces in Java 8](http://www.baeldung.com/java-8-functional-interfaces)
88
- [Java 8 – Powerful Comparison with Lambdas](http://www.baeldung.com/java-8-sort-lambda)
99
- [New Features in Java 8](http://www.baeldung.com/java-8-new-features)

Diff for: core-java-9/README.md

+6-11
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,21 @@
55
[Java 9 New Features](http://www.baeldung.com/new-java-9)
66

77
### Relevant Articles:
8-
- [Java 9 Stream API Improvements](http://www.baeldung.com/java-9-stream-api)
9-
- [Java 9 Convenience Factory Methods for Collections](http://www.baeldung.com/java-9-collections-factory-methods)
8+
9+
- [Java 9 New Features](https://www.baeldung.com/new-java-9)
1010
- [New Stream Collectors in Java 9](http://www.baeldung.com/java9-stream-collectors)
11-
- [Java 9 CompletableFuture API Improvements](http://www.baeldung.com/java-9-completablefuture)
12-
- [Introduction to Java 9 StackWalking API](http://www.baeldung.com/java-9-stackwalking-api)
1311
- [Introduction to Project Jigsaw](http://www.baeldung.com/project-jigsaw-java-modularity)
14-
- [Java 9 Optional API Additions](http://www.baeldung.com/java-9-optional)
15-
- [Java 9 Reactive Streams](http://www.baeldung.com/java-9-reactive-streams)
16-
- [Java 9 java.util.Objects Additions](http://www.baeldung.com/java-9-objects-new)
17-
- [Java 9 Variable Handles Demistyfied](http://www.baeldung.com/java-variable-handles)
12+
- [Java 9 Variable Handles Demystified](http://www.baeldung.com/java-variable-handles)
1813
- [Exploring the New HTTP Client in Java 9 and 11](http://www.baeldung.com/java-9-http-client)
1914
- [Method Handles in Java](http://www.baeldung.com/java-method-handles)
2015
- [Introduction to Chronicle Queue](http://www.baeldung.com/java-chronicle-queue)
21-
- [A Guide to Java 9 Modularity](http://www.baeldung.com/java-9-modularity)
2216
- [Optional orElse Optional](http://www.baeldung.com/java-optional-or-else-optional)
23-
- [Java 9 java.lang.Module API](http://www.baeldung.com/java-9-module-api)
2417
- [Iterate Through a Range of Dates in Java](https://www.baeldung.com/java-iterate-date-range)
2518
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
26-
- [Java 9 Platform Logging API](https://www.baeldung.com/java-9-logging-api)
2719
- [Immutable Set in Java](https://www.baeldung.com/java-immutable-set)
2820
- [Multi-Release Jar Files](https://www.baeldung.com/java-multi-release-jar)
2921
- [Ahead of Time Compilation (AoT)](https://www.baeldung.com/ahead-of-time-compilation)
22+
- [Java 9 Process API Improvements](https://www.baeldung.com/java-9-process-api)
23+
- [Guide to java.lang.Process API](https://www.baeldung.com/java-process-api)
24+
3025

Diff for: core-java-collections/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Core Java Collections Cookbooks and Examples
44

55
### Relevant Articles:
6-
- [Java - Combine Multiple Collections](http://www.baeldung.com/java-combine-multiple-collections)
6+
- [Java Combine Multiple Collections](http://www.baeldung.com/java-combine-multiple-collections)
77
- [HashSet and TreeSet Comparison](http://www.baeldung.com/java-hashset-vs-treeset)
88
- [Collect a Java Stream to an Immutable Collection](http://www.baeldung.com/java-stream-immutable-collection)
99
- [Introduction to the Java ArrayDeque](http://www.baeldung.com/java-array-deque)

Diff for: core-java-concurrency-advanced/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
- [Guide to the Java Phaser](http://www.baeldung.com/java-phaser)
1313
- [An Introduction to Atomic Variables in Java](http://www.baeldung.com/java-atomic-variables)
1414
- [CyclicBarrier in Java](http://www.baeldung.com/java-cyclic-barrier)
15-
- [Guide to Volatile Keyword in Java](http://www.baeldung.com/java-volatile)
15+
- [Guide to the Volatile Keyword in Java](http://www.baeldung.com/java-volatile)
1616
- [Semaphores in Java](http://www.baeldung.com/java-semaphore)
1717
- [Daemon Threads in Java](http://www.baeldung.com/java-daemon-thread)
1818
- [Priority-based Job Scheduling in Java](http://www.baeldung.com/java-priority-job-schedule)
1919
- [Brief Introduction to Java Thread.yield()](https://www.baeldung.com/java-thread-yield)
2020
- [Print Even and Odd Numbers Using 2 Threads](https://www.baeldung.com/java-even-odd-numbers-with-2-threads)
2121
- [Java CyclicBarrier vs CountDownLatch](https://www.baeldung.com/java-cyclicbarrier-countdownlatch)
2222
- [Guide to the Fork/Join Framework in Java](http://www.baeldung.com/java-fork-join)
23-
- [A Guide to ThreadLocalRandom in Java](http://www.baeldung.com/java-thread-local-random)
23+
- [Guide to ThreadLocalRandom in Java](http://www.baeldung.com/java-thread-local-random)
2424
- [The Thread.join() Method in Java](http://www.baeldung.com/java-thread-join)
2525
- [Passing Parameters to Java Threads](https://www.baeldung.com/java-thread-parameters)

Diff for: core-java-concurrency-basic/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
- [A Guide to the Java ExecutorService](http://www.baeldung.com/java-executor-service-tutorial)
88
- [Guide to java.util.concurrent.Future](http://www.baeldung.com/java-future)
99
- [Difference Between Wait and Sleep in Java](http://www.baeldung.com/java-wait-and-sleep)
10-
- [Guide to Synchronized Keyword in Java](http://www.baeldung.com/java-synchronized)
10+
- [Guide to the Synchronized Keyword in Java](http://www.baeldung.com/java-synchronized)
1111
- [Overview of the java.util.concurrent](http://www.baeldung.com/java-util-concurrent)
1212
- [Implementing a Runnable vs Extending a Thread](http://www.baeldung.com/java-runnable-vs-extending-thread)
1313
- [How to Kill a Java Thread](http://www.baeldung.com/java-thread-stop)
14-
- [ExecutorService - Waiting for Threads to Finish](http://www.baeldung.com/java-executor-wait-for-threads)
14+
- [ExecutorService Waiting for Threads to Finish](http://www.baeldung.com/java-executor-wait-for-threads)
1515
- [wait and notify() Methods in Java](http://www.baeldung.com/java-wait-notify)
1616
- [Life Cycle of a Thread in Java](http://www.baeldung.com/java-thread-lifecycle)
1717
- [Runnable vs. Callable in Java](http://www.baeldung.com/java-runnable-callable)
18-
- [What is Thread-Safety and How to Achieve it](https://www.baeldung.com/java-thread-safety)
18+
- [What is Thread-Safety and How to Achieve it?](https://www.baeldung.com/java-thread-safety)
1919
- [How to Start a Thread in Java](https://www.baeldung.com/java-start-thread)

Diff for: core-java-io/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
- [How to Read a Large File Efficiently with Java](http://www.baeldung.com/java-read-lines-large-file)
77
- [Java InputStream to String](http://www.baeldung.com/convert-input-stream-to-string)
88
- [Java – Write to File](http://www.baeldung.com/java-write-to-file)
9-
- [Java - Convert File to InputStream](http://www.baeldung.com/convert-file-to-input-stream)
9+
- [Java Convert File to InputStream](http://www.baeldung.com/convert-file-to-input-stream)
1010
- [Java Scanner](http://www.baeldung.com/java-scanner)
1111
- [Java – Byte Array to Writer](http://www.baeldung.com/java-convert-byte-array-to-writer)
1212
- [Java – Directory Size](http://www.baeldung.com/java-folder-size)
1313
- [Differences Between the Java WatchService API and the Apache Commons IO Monitor Library](http://www.baeldung.com/java-watchservice-vs-apache-commons-io-monitor-library)
1414
- [File Size in Java](http://www.baeldung.com/java-file-size)
1515
- [Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java](http://www.baeldung.com/java-path)
1616
- [Using Java MappedByteBuffer](http://www.baeldung.com/java-mapped-byte-buffer)
17-
- [Copy a File with Java](http://www.baeldung.com/java-copy-file)
17+
- [How to Copy a File with Java](http://www.baeldung.com/java-copy-file)
1818
- [Java – Append Data to a File](http://www.baeldung.com/java-append-to-file)
1919
- [FileNotFoundException in Java](http://www.baeldung.com/java-filenotfound-exception)
2020
- [How to Read a File in Java](http://www.baeldung.com/reading-file-in-java)

Diff for: core-java-jvm/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
## Core Java JVM Cookbooks and Examples
44

55
### Relevant Articles:
6-
- [Method Inlining in the JVM](http://www.baeldung.com/method-inlining-in-the-jvm/)
6+
- [Method Inlining in the JVM](https://www.baeldung.com/jvm-method-inlining)

Diff for: core-java-lambdas/pom.xml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
<artifactId>core-java-lambdas</artifactId>
7+
<version>0.1.0-SNAPSHOT</version>
8+
<name>core-java</name>
9+
<packaging>jar</packaging>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>parent-java</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
<relativePath>../parent-java</relativePath>
16+
</parent>
17+
18+
19+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.baeldung.lambdas;
2+
3+
import java.util.Random;
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Executors;
6+
import java.util.function.Supplier;
7+
import java.util.stream.IntStream;
8+
9+
/**
10+
* Class with examples about working with capturing lambdas.
11+
*/
12+
public class LambdaVariables {
13+
14+
private volatile boolean run = true;
15+
private int start = 0;
16+
17+
private ExecutorService executor = Executors.newFixedThreadPool(3);
18+
19+
public static void main(String[] args) {
20+
new LambdaVariables().localVariableMultithreading();
21+
}
22+
23+
Supplier<Integer> incrementer(int start) {
24+
return () -> start; // can't modify start parameter inside the lambda
25+
}
26+
27+
Supplier<Integer> incrementer() {
28+
return () -> start++;
29+
}
30+
31+
public void localVariableMultithreading() {
32+
boolean run = true;
33+
executor.execute(() -> {
34+
while (run) {
35+
// do operation
36+
}
37+
});
38+
// commented because it doesn't compile, it's just an example of non-final local variables in lambdas
39+
// run = false;
40+
}
41+
42+
public void instanceVariableMultithreading() {
43+
executor.execute(() -> {
44+
while (run) {
45+
// do operation
46+
}
47+
});
48+
49+
run = false;
50+
}
51+
52+
/**
53+
* WARNING: always avoid this workaround!!
54+
*/
55+
public void workaroundSingleThread() {
56+
int[] holder = new int[] { 2 };
57+
IntStream sums = IntStream
58+
.of(1, 2, 3)
59+
.map(val -> val + holder[0]);
60+
61+
holder[0] = 0;
62+
63+
System.out.println(sums.sum());
64+
}
65+
66+
/**
67+
* WARNING: always avoid this workaround!!
68+
*/
69+
public void workaroundMultithreading() {
70+
int[] holder = new int[] { 2 };
71+
Runnable runnable = () -> System.out.println(IntStream
72+
.of(1, 2, 3)
73+
.map(val -> val + holder[0])
74+
.sum());
75+
76+
new Thread(runnable).start();
77+
78+
// simulating some processing
79+
try {
80+
Thread.sleep(new Random().nextInt(3) * 1000L);
81+
} catch (InterruptedException e) {
82+
throw new RuntimeException(e);
83+
}
84+
85+
holder[0] = 0;
86+
}
87+
88+
}

0 commit comments

Comments
 (0)