Skip to content

Commit 6e2feb7

Browse files
authored
Merge branch 'master' into BAEL-1689
2 parents 9fd6025 + ff9d4d4 commit 6e2feb7

285 files changed

Lines changed: 4669 additions & 726 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.

‎README.md‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,3 @@ This tutorials project is being built **[>> HERE](https://rest-security.ci.cloud
4040
- [Apache Maven Standard Directory Layout](http://www.baeldung.com/maven-directory-structure)
4141
- [Apache Maven Tutorial](http://www.baeldung.com/maven)
4242
- [Designing a User Friendly Java Library](http://www.baeldung.com/design-a-user-friendly-java-library)
43-
- [Java Service Provider Interface](http://www.baeldung.com/java-spi)
44-
- [Java Streams vs Vavr Streams](http://www.baeldung.com/vavr-java-streams)

‎algorithms/src/main/java/com/baeldung/algorithms/mcts/tictactoe/Board.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public void printStatus() {
148148
System.out.println("Game Draw");
149149
break;
150150
case IN_PROGRESS:
151-
System.out.println("Game In rogress");
151+
System.out.println("Game In Progress");
152152
break;
153153
}
154154
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package com.baeldung.algorithms.analysis;
2+
3+
import org.junit.Test;
4+
5+
public class AnalysisRunnerLiveTest {
6+
7+
int n = 10;
8+
int total = 0;
9+
10+
@Test
11+
public void whenConstantComplexity_thenConstantRuntime() {
12+
13+
System.out.println("**** n = " + n + " ****");
14+
System.out.println();
15+
16+
// Constant Time
17+
System.out.println("**** Constant time ****");
18+
19+
System.out.println("Hey - your input is: " + n);
20+
System.out.println("Running time not dependent on input size!");
21+
System.out.println();
22+
}
23+
24+
@Test
25+
public void whenLogarithmicComplexity_thenLogarithmicRuntime() {
26+
// Logarithmic Time
27+
System.out.println("**** Logarithmic Time ****");
28+
for (int i = 1; i < n; i = i * 2) {
29+
// System.out.println("Hey - I'm busy looking at: " + i);
30+
total++;
31+
}
32+
System.out.println("Total amount of times run: " + total);
33+
System.out.println();
34+
}
35+
36+
@Test
37+
public void whenLinearComplexity_thenLinearRuntime() {
38+
// Linear Time
39+
System.out.println("**** Linear Time ****");
40+
for (int i = 0; i < n; i++) {
41+
// System.out.println("Hey - I'm busy looking at: " + i);
42+
total++;
43+
}
44+
System.out.println("Total amount of times run: " + total);
45+
System.out.println();
46+
47+
}
48+
49+
@Test
50+
public void whenNLogNComplexity_thenNLogNRuntime() {
51+
// N Log N Time
52+
System.out.println("**** nlogn Time ****");
53+
total = 0;
54+
for (
55+
56+
int i = 1; i <= n; i++) {
57+
for (int j = 1; j < n; j = j * 2) {
58+
// System.out.println("Hey - I'm busy looking at: " + i + " and " + j);
59+
total++;
60+
}
61+
}
62+
System.out.println("Total amount of times run: " + total);
63+
System.out.println();
64+
}
65+
66+
@Test
67+
public void whenQuadraticComplexity_thenQuadraticRuntime() {
68+
// Quadratic Time
69+
System.out.println("**** Quadratic Time ****");
70+
total = 0;
71+
for (
72+
73+
int i = 1; i <= n; i++) {
74+
for (int j = 1; j <= n; j++) {
75+
// System.out.println("Hey - I'm busy looking at: " + i + " and " + j);
76+
total++;
77+
}
78+
}
79+
System.out.println("Total amount of times run: " + total);
80+
System.out.println();
81+
}
82+
83+
@Test
84+
public void whenCubicComplexity_thenCubicRuntime() {
85+
// Cubic Time
86+
System.out.println("**** Cubic Time ****");
87+
total = 0;
88+
for (
89+
90+
int i = 1; i <= n; i++) {
91+
for (int j = 1; j <= n; j++) {
92+
for (int k = 1; k <= n; k++) {
93+
// System.out.println("Hey - I'm busy looking at: " + i + " and " + j + " and " + k);
94+
total++;
95+
}
96+
}
97+
}
98+
System.out.println("Total amount of times run: " + total);
99+
System.out.println();
100+
}
101+
102+
@Test
103+
public void whenExponentialComplexity_thenExponentialRuntime() {
104+
// Exponential Time
105+
System.out.println("**** Exponential Time ****");
106+
total = 0;
107+
for (
108+
109+
int i = 1; i <= Math.pow(2, n); i++) {
110+
// System.out.println("Hey - I'm busy looking at: " + i);
111+
total++;
112+
}
113+
System.out.println("Total amount of times run: " + total);
114+
System.out.println();
115+
}
116+
117+
@Test
118+
public void whenFactorialComplexity_thenFactorialRuntime() {
119+
// Factorial Time
120+
System.out.println("**** Factorial Time ****");
121+
total = 0;
122+
for (
123+
124+
int i = 1; i <=
125+
126+
factorial(n); i++) {
127+
// System.out.println("Hey - I'm busy looking at: " + i);
128+
total++;
129+
}
130+
System.out.println("Total amount of times run: " + total);
131+
}
132+
133+
static int factorial(int n) {
134+
if (n == 0 || n == 1)
135+
return 1;
136+
else
137+
return n * factorial(n - 1);
138+
}
139+
}

‎animal-sniffer-mvn-plugin/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<artifactId>animal-sniffer-mvn-plugin</artifactId>
66
<packaging>jar</packaging>
77
<version>1.0-SNAPSHOT</version>
8-
<name>example-animal-sniffer-mvn-plugin</name>
8+
<name>animal-sniffer-mvn-plugin</name>
99
<url>http://maven.apache.org</url>
1010

1111
<parent>

‎apache-poi/src/test/java/com/baeldung/poi/powerpoint/PowerPointIntegrationTest.java‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
package com.baeldung.poi.powerpoint;
22

3+
import java.io.File;
4+
import java.util.List;
5+
36
import org.apache.poi.xslf.usermodel.XMLSlideShow;
47
import org.apache.poi.xslf.usermodel.XSLFShape;
58
import org.apache.poi.xslf.usermodel.XSLFSlide;
69
import org.junit.After;
710
import org.junit.Assert;
811
import org.junit.Before;
12+
import org.junit.Rule;
913
import org.junit.Test;
10-
11-
import java.io.File;
12-
import java.util.List;
14+
import org.junit.rules.TemporaryFolder;
1315

1416
public class PowerPointIntegrationTest {
1517

1618
private PowerPointHelper pph;
1719
private String fileLocation;
1820
private static final String FILE_NAME = "presentation.pptx";
21+
22+
@Rule
23+
public TemporaryFolder tempFolder = new TemporaryFolder();
1924

2025
@Before
2126
public void setUp() throws Exception {
22-
File currDir = new File(".");
27+
File currDir = tempFolder.newFolder();
2328
String path = currDir.getAbsolutePath();
2429
fileLocation = path.substring(0, path.length() - 1) + FILE_NAME;
2530

‎camel-api/pom.xml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<groupId>com.example</groupId>
66
<artifactId>spring-boot-camel</artifactId>
77
<version>0.0.1-SNAPSHOT</version>
8-
<name>Spring-Boot - Camel API</name>
98

109
<parent>
1110
<groupId>com.baeldung</groupId>

‎core-java-8/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@
5252
- [Measure Elapsed Time in Java](http://www.baeldung.com/java-measure-elapsed-time)
5353
- [Java Optional – orElse() vs orElseGet()](http://www.baeldung.com/java-optional-or-else-vs-or-else-get)
5454
- [An Introduction to Java.util.Hashtable Class](http://www.baeldung.com/java-hash-table)
55-
55+
- [Method Parameter Reflection in Java](http://www.baeldung.com/java-parameter-reflection)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.java8;
2+
3+
import org.junit.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
import static org.assertj.core.api.Assertions.catchThrowable;
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class UnsignedArithmeticUnitTest {
10+
@Test
11+
public void whenDoublingALargeByteNumber_thenOverflow() {
12+
byte b1 = 100;
13+
byte b2 = (byte) (b1 << 1);
14+
15+
assertEquals(-56, b2);
16+
}
17+
18+
@Test
19+
public void whenComparingNumbers_thenNegativeIsInterpretedAsUnsigned() {
20+
int positive = Integer.MAX_VALUE;
21+
int negative = Integer.MIN_VALUE;
22+
23+
int signedComparison = Integer.compare(positive, negative);
24+
assertEquals(1, signedComparison);
25+
26+
int unsignedComparison = Integer.compareUnsigned(positive, negative);
27+
assertEquals(-1, unsignedComparison);
28+
29+
assertEquals(negative, positive + 1);
30+
}
31+
32+
@Test
33+
public void whenDividingNumbers_thenNegativeIsInterpretedAsUnsigned() {
34+
int positive = Integer.MAX_VALUE;
35+
int negative = Integer.MIN_VALUE;
36+
37+
assertEquals(-1, negative / positive);
38+
assertEquals(1, Integer.divideUnsigned(negative, positive));
39+
40+
assertEquals(-1, negative % positive);
41+
assertEquals(1, Integer.divideUnsigned(negative, positive));
42+
}
43+
44+
@Test
45+
public void whenParsingNumbers_thenNegativeIsInterpretedAsUnsigned() {
46+
Throwable thrown = catchThrowable(() -> Integer.parseInt("2147483648"));
47+
assertThat(thrown).isInstanceOf(NumberFormatException.class);
48+
49+
assertEquals(Integer.MAX_VALUE + 1, Integer.parseUnsignedInt("2147483648"));
50+
}
51+
52+
@Test
53+
public void whenFormattingNumbers_thenNegativeIsInterpretedAsUnsigned() {
54+
String signedString = Integer.toString(Integer.MIN_VALUE);
55+
assertEquals("-2147483648", signedString);
56+
57+
String unsignedString = Integer.toUnsignedString(Integer.MIN_VALUE);
58+
assertEquals("2147483648", unsignedString);
59+
}
60+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.baeldung.typeinference;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.Collections;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
import org.junit.Test;
15+
16+
public class TypeInferenceUnitTest {
17+
18+
@Test
19+
public void givenNoTypeInference_whenInvokingGenericMethodsWithTypeParameters_ObjectsAreCreated() {
20+
// Without type inference. code is verbose.
21+
Map<String, Map<String, String>> mapOfMaps = new HashMap<String, Map<String, String>>();
22+
List<String> strList = Collections.<String> emptyList();
23+
List<Integer> intList = Collections.<Integer> emptyList();
24+
25+
assertTrue(mapOfMaps.isEmpty());
26+
assertTrue(strList.isEmpty());
27+
assertTrue(intList.isEmpty());
28+
}
29+
30+
@Test
31+
public void givenTypeInference_whenInvokingGenericMethodsWithoutTypeParameters_ObjectsAreCreated() {
32+
// With type inference. code is concise.
33+
List<String> strListInferred = Collections.emptyList();
34+
List<Integer> intListInferred = Collections.emptyList();
35+
36+
assertTrue(strListInferred.isEmpty());
37+
assertTrue(intListInferred.isEmpty());
38+
}
39+
40+
@Test
41+
public void givenJava7_whenInvokingCostructorWithoutTypeParameters_ObjectsAreCreated() {
42+
// Type Inference for constructor using diamond operator.
43+
Map<String, Map<String, String>> mapOfMapsInferred = new HashMap<>();
44+
45+
assertTrue(mapOfMapsInferred.isEmpty());
46+
assertEquals("public class java.util.HashMap<K,V>", mapOfMapsInferred.getClass()
47+
.toGenericString());
48+
}
49+
50+
static <T> List<T> add(List<T> list, T a, T b) {
51+
list.add(a);
52+
list.add(b);
53+
return list;
54+
}
55+
56+
@Test
57+
public void givenGenericMethod_WhenInvokedWithoutExplicitTypes_TypesAreInferred() {
58+
// Generalized target-type inference
59+
List<String> strListGeneralized = add(new ArrayList<>(), "abc", "def");
60+
List<Integer> intListGeneralized = add(new ArrayList<>(), 1, 2);
61+
List<Number> numListGeneralized = add(new ArrayList<>(), 1, 2.0);
62+
63+
assertEquals("public class java.util.ArrayList<E>", strListGeneralized.getClass()
64+
.toGenericString());
65+
assertFalse(intListGeneralized.isEmpty());
66+
assertEquals(2, numListGeneralized.size());
67+
}
68+
69+
@Test
70+
public void givenLambdaExpressions_whenParameterTypesNotSpecified_ParameterTypesAreInferred() {
71+
// Type Inference and Lambda Expressions.
72+
List<Integer> intList = Arrays.asList(5, 3, 4, 2, 1);
73+
Collections.sort(intList, (a, b) -> {
74+
assertEquals("java.lang.Integer", a.getClass().getName());
75+
return a.compareTo(b);
76+
});
77+
assertEquals("[1, 2, 3, 4, 5]", Arrays.toString(intList.toArray()));
78+
79+
List<String> strList = Arrays.asList("Red", "Blue", "Green");
80+
Collections.sort(strList, (a, b) -> {
81+
assertEquals("java.lang.String", a.getClass().getName());
82+
return a.compareTo(b);
83+
});
84+
assertEquals("[Blue, Green, Red]", Arrays.toString(strList.toArray()));
85+
}
86+
87+
}

‎core-java-9/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@
2525
- [Introduction to Chronicle Queue](http://www.baeldung.com/java-chronicle-queue)
2626
- [A Guide to Java 9 Modularity](http://www.baeldung.com/java-9-modularity)
2727
- [Optional orElse Optional](http://www.baeldung.com/java-optional-or-else-optional)
28+
- [Java 9 java.lang.Module API](http://www.baeldung.com/java-9-module-api)

0 commit comments

Comments
 (0)