Skip to content

Commit a955425

Browse files
authored
Merge branch 'master' into master
2 parents 9d0b9db + d3597a8 commit a955425

202 files changed

Lines changed: 6080 additions & 1220 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.

‎.travis.yml‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
language: java
22

33
before_install:
4-
- export MAVEN_OPTS="-Xmx2048M -Xss128M -XX:+CMSClassUnloadingEnabled -XX:+UseG1GC -XX:-UseGCOverheadLimit"
54
- echo "MAVEN_OPTS='-Xmx2048M -Xss128M -XX:+CMSClassUnloadingEnabled -XX:+UseG1GC -XX:-UseGCOverheadLimit'" > ~/.mavenrc
65

76
install: travis_wait 60 mvn -q test -fae
87

98
sudo: required
109

11-
before_script:
12-
- echo "MAVEN_OPTS='-Xmx2048M -Xss128M -XX:+CMSClassUnloadingEnabled -XX:+UseG1GC -XX:-UseGCOverheadLimit'" > ~/.mavenrc
13-
1410
jdk:
1511
- oraclejdk8
1612

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.baeldung.java9;
2+
3+
public class Java9OptionalTest {
4+
@Test
5+
public void givenOptionalOfSome_whenToStream_thenShouldTreatItAsOneElementStream() {
6+
//given
7+
Optional<String> value = Optional.of("a");
8+
9+
//when
10+
List<String> collect = value.stream().map(String::toUpperCase).collect(Collectors.toList());
11+
12+
//then
13+
assertThat(collect).hasSameElementsAs(List.of("A"));
14+
}
15+
16+
@Test
17+
public void givenOptionalOfNone_whenToStream_thenShouldTreatItAsZeroElementStream() {
18+
//given
19+
Optional<String> value = Optional.empty();
20+
21+
//when
22+
List<String> collect = value.stream().map(String::toUpperCase).collect(Collectors.toList());
23+
24+
//then
25+
assertThat(collect).isEmpty();
26+
}
27+
28+
@Test
29+
public void givenOptional_whenPresent_thenShouldExecuteProperCallback() {
30+
//given
31+
Optional<String> value = Optional.of("properValue");
32+
AtomicInteger successCounter = new AtomicInteger(0);
33+
AtomicInteger onEmptyOptionalCounter = new AtomicInteger(0);
34+
35+
//when
36+
value.ifPresentOrElse((v) -> successCounter.incrementAndGet(), onEmptyOptionalCounter::incrementAndGet);
37+
38+
//then
39+
assertThat(successCounter.get()).isEqualTo(1);
40+
assertThat(onEmptyOptionalCounter.get()).isEqualTo(0);
41+
}
42+
43+
@Test
44+
public void givenOptional_whenNotPresent_thenShouldExecuteProperCallback() {
45+
//given
46+
Optional<String> value = Optional.empty();
47+
AtomicInteger successCounter = new AtomicInteger(0);
48+
AtomicInteger onEmptyOptionalCounter = new AtomicInteger(0);
49+
50+
//when
51+
value.ifPresentOrElse((v) -> successCounter.incrementAndGet(), onEmptyOptionalCounter::incrementAndGet);
52+
53+
//then
54+
assertThat(successCounter.get()).isEqualTo(0);
55+
assertThat(onEmptyOptionalCounter.get()).isEqualTo(1);
56+
}
57+
58+
@Test
59+
public void givenOptional_whenPresent_thenShouldTakeAValueFromIt() {
60+
//given
61+
String expected = "properValue";
62+
Optional<String> value = Optional.of(expected);
63+
Optional<String> defaultValue = Optional.of("default");
64+
65+
//when
66+
Optional<String> result = value.or(() -> defaultValue);
67+
68+
//then
69+
assertThat(result.get()).isEqualTo(expected);
70+
}
71+
72+
@Test
73+
public void givenOptional_whenEmpty_thenShouldTakeAValueFromOr() {
74+
//given
75+
String defaultString = "default";
76+
Optional<String> value = Optional.empty();
77+
Optional<String> defaultValue = Optional.of(defaultString);
78+
79+
//when
80+
Optional<String> result = value.or(() -> defaultValue);
81+
82+
//then
83+
assertThat(result.get()).isEqualTo(defaultString);
84+
}
85+
}

‎core-java/README.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,15 @@
102102
- [How to Perform a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request)
103103
- [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection)
104104
- [Guide to DelayQueue](http://www.baeldung.com/java-delay-queue)
105+
- [Guide to UUID in JAVA] (http://www.baeldung.com/guide-to-uuid-in-java)
106+
- [Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java](http://www.baeldung.com/java-path)
107+
- [How to Add a Single Element to a Stream](http://www.baeldung.com/java-stream-append-prepend)
108+
- [Iterating Over Enum Values in Java](http://www.baeldung.com/java-enum-iteration)
109+
- [A Guide to Java SynchronousQueue](http://www.baeldung.com/java-synchronous-queue)
110+
- [Guide to the Java TransferQueue](http://www.baeldung.com/java-transfer-queue)
111+
- [Kotlin Java Interoperability](http://www.baeldung.com/kotlin-java-interoperability)
112+
- [Guide to the ConcurrentSkipListMap](http://www.baeldung.com/java-concurrent-skip-list-map)
113+
- [Difference Between Wait and Sleep in Java](http://www.baeldung.com/java-wait-and-sleep)
114+
- [LongAdder and LongAccumulator in Java](http://www.baeldung.com/java-longadder-and-longaccumulator)
115+
- [Using Java MappedByteBuffer](http://www.baeldung.com/java-mapped-byte-buffer)
116+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.concurrent.diningphilosophers;
2+
3+
public class DiningPhilosophers {
4+
5+
public static void main(String[] args) throws Exception {
6+
7+
Philosopher[] philosophers = new Philosopher[5];
8+
Object[] forks = new Object[philosophers.length];
9+
10+
for (int i = 0; i < forks.length; i++) {
11+
forks[i] = new Object();
12+
}
13+
14+
for (int i = 0; i < philosophers.length; i++) {
15+
16+
Object leftFork = forks[i];
17+
Object rightFork = forks[(i + 1) % forks.length];
18+
19+
if (i == philosophers.length - 1) {
20+
philosophers[i] = new Philosopher(rightFork, leftFork); // The last philosopher picks up the right fork first
21+
} else {
22+
philosophers[i] = new Philosopher(leftFork, rightFork);
23+
}
24+
25+
Thread t = new Thread(philosophers[i], "Philosopher " + (i + 1));
26+
t.start();
27+
}
28+
}
29+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.concurrent.diningphilosophers;
2+
3+
public class Philosopher implements Runnable {
4+
5+
private final Object leftFork;
6+
private final Object rightFork;
7+
8+
public Philosopher(Object left, Object right) {
9+
this.leftFork = left;
10+
this.rightFork = right;
11+
}
12+
13+
private void doAction(String action) throws InterruptedException {
14+
System.out.println(Thread.currentThread().getName() + " " + action);
15+
Thread.sleep(((int) (Math.random() * 100)));
16+
}
17+
18+
@Override public void run() {
19+
try {
20+
while (true) {
21+
doAction(System.nanoTime() + ": Thinking"); // thinking
22+
synchronized (leftFork) {
23+
doAction(System.nanoTime() + ": Picked up left fork");
24+
synchronized (rightFork) {
25+
doAction(System.nanoTime() + ": Picked up right fork - eating"); // eating
26+
doAction(System.nanoTime() + ": Put down right fork");
27+
}
28+
doAction(System.nanoTime() + ": Put down left fork. Returning to thinking");
29+
}
30+
}
31+
} catch (InterruptedException e) {
32+
Thread.currentThread().interrupt();
33+
return;
34+
}
35+
}
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.java.enumiteration;
2+
3+
import java.util.stream.Stream;
4+
5+
public enum DaysOfWeekEnum {
6+
SUNDAY("off"),
7+
MONDAY("working"),
8+
TUESDAY("working"),
9+
WEDNESDAY("working"),
10+
THURSDAY("working"),
11+
FRIDAY("working"),
12+
SATURDAY("off");
13+
14+
private String typeOfDay;
15+
16+
DaysOfWeekEnum(String typeOfDay) {
17+
this.typeOfDay = typeOfDay;
18+
}
19+
20+
public String getTypeOfDay() {
21+
return typeOfDay;
22+
}
23+
24+
public void setTypeOfDay(String typeOfDay) {
25+
this.typeOfDay = typeOfDay;
26+
}
27+
28+
public static Stream<DaysOfWeekEnum> stream() {
29+
return Stream.of(DaysOfWeekEnum.values());
30+
}
31+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.java.enumiteration;
2+
3+
import java.util.EnumSet;
4+
5+
public class EnumIterationExamples {
6+
public static void main(String[] args) {
7+
System.out.println("Enum iteration using forEach:");
8+
EnumSet.allOf(DaysOfWeekEnum.class).forEach(day -> System.out.println(day));
9+
10+
System.out.println("Enum iteration using Stream:");
11+
DaysOfWeekEnum.stream().filter(d -> d.getTypeOfDay().equals("off")).forEach(System.out::println);
12+
13+
System.out.println("Enum iteration using for loop:");
14+
for (DaysOfWeekEnum day : DaysOfWeekEnum.values()) {
15+
System.out.println(day);
16+
}
17+
}
18+
}

‎core-java/src/main/java/com/baeldung/maths/Round.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static double round(double value, int places) {
3333

3434
public static double roundNotPrecise(double value, int places) {
3535
if (places < 0) throw new IllegalArgumentException();
36-
36+
3737
BigDecimal bd = new BigDecimal(value);
3838
bd = bd.setScale(places, RoundingMode.HALF_UP);
3939
return bd.doubleValue();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.noclassdeffounderror;
2+
3+
public class ClassWithInitErrors {
4+
static int data = 1 / 0;
5+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.noclassdeffounderror;
2+
3+
public class NoClassDefFoundErrorExample {
4+
public ClassWithInitErrors getClassWithInitErrors() {
5+
ClassWithInitErrors test;
6+
try {
7+
test = new ClassWithInitErrors();
8+
} catch (Throwable t) {
9+
System.out.println(t);
10+
}
11+
test = new ClassWithInitErrors();
12+
return test;
13+
}
14+
}

0 commit comments

Comments
 (0)