Skip to content

Commit 5a44cfa

Browse files
Merge branch 'master' into javabeanconstraints
2 parents 128f4a5 + 7d03f03 commit 5a44cfa

20 files changed

Lines changed: 316 additions & 107 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.algorithms.insertionsort;
2+
3+
public class InsertionSort {
4+
5+
public static void insertionSortImperative(int[] input) {
6+
for (int i = 1; i < input.length; i++) {
7+
int key = input[i];
8+
int j = i - 1;
9+
while (j >= 0 && input[j] > key) {
10+
input[j + 1] = input[j];
11+
j = j - 1;
12+
}
13+
input[j + 1] = key;
14+
}
15+
}
16+
17+
public static void insertionSortRecursive(int[] input) {
18+
insertionSortRecursive(input, input.length);
19+
}
20+
21+
private static void insertionSortRecursive(int[] input, int i) {
22+
// base case
23+
if (i <= 1) {
24+
return;
25+
}
26+
27+
// sort the first i - 1 elements of the array
28+
insertionSortRecursive(input, i - 1);
29+
30+
// then find the correct position of the element at position i
31+
int key = input[i - 1];
32+
int j = i - 2;
33+
// shifting the elements from their position by 1
34+
while (j >= 0 && input[j] > key) {
35+
input[j + 1] = input[j];
36+
j = j - 1;
37+
}
38+
// inserting the key at the appropriate position
39+
input[j + 1] = key;
40+
}
41+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.algorithms.insertionsort;
2+
3+
import com.baeldung.algorithms.insertionsort.InsertionSort;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertArrayEquals;
7+
8+
public class InsertionSortUnitTest {
9+
10+
@Test
11+
public void givenUnsortedArray_whenInsertionSortImperative_thenSortedAsc() {
12+
int[] input = {6, 2, 3, 4, 5, 1};
13+
InsertionSort.insertionSortImperative(input);
14+
int[] expected = {1, 2, 3, 4, 5, 6};
15+
assertArrayEquals("the two arrays are not equal", expected, input);
16+
}
17+
18+
@Test
19+
public void givenUnsortedArray_whenInsertionSortRecursive_thenSortedAsc() {
20+
int[] input = {6, 4, 5, 2, 3, 1};
21+
InsertionSort.insertionSortRecursive(input);
22+
int[] expected = {1, 2, 3, 4, 5, 6};
23+
assertArrayEquals("the two arrays are not equal", expected, input);
24+
}
25+
}

‎core-java-collections/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
- [Thread Safe LIFO Data Structure Implementations](https://www.baeldung.com/java-lifo-thread-safe)
4949
- [Collections.emptyList() vs. New List Instance](https://www.baeldung.com/java-collections-emptylist-new-list)
5050
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
51+
- [Differences Between Collection.clear() and Collection.removeAll()](https://www.baeldung.com/java-collection-clear-vs-removeall)
5152
- [Performance of contains() in a HashSet vs ArrayList](https://www.baeldung.com/java-hashset-arraylist-contains-performance)
5253
- [Get the Key for a Value from a Java Map](https://www.baeldung.com/java-map-key-from-value)
5354
- [Time Complexity of Java Collections](https://www.baeldung.com/java-collections-complexity)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.stream;
2+
3+
import java.io.BufferedOutputStream;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.OutputStream;
7+
import java.io.OutputStreamWriter;
8+
import java.io.Writer;
9+
10+
public class OutputStreamExamples {
11+
12+
public void fileOutputStreamByteSequence(String file, String data) throws IOException {
13+
byte[] bytes = data.getBytes();
14+
try (OutputStream out = new FileOutputStream(file)) {
15+
out.write(bytes);
16+
}
17+
}
18+
19+
public void fileOutputStreamByteSubSequence(String file, String data) throws IOException {
20+
byte[] bytes = data.getBytes();
21+
try (OutputStream out = new FileOutputStream(file)) {
22+
out.write(bytes, 6, 5);
23+
}
24+
}
25+
26+
public void fileOutputStreamByteSingle(String file, String data) throws IOException {
27+
byte[] bytes = data.getBytes();
28+
try (OutputStream out = new FileOutputStream(file)) {
29+
out.write(bytes[6]);
30+
}
31+
}
32+
33+
public void bufferedOutputStream(String file, String... data) throws IOException {
34+
try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
35+
for (String s : data) {
36+
out.write(s.getBytes());
37+
out.write(" ".getBytes());
38+
}
39+
}
40+
}
41+
42+
public void outputStreamWriter(String file, String data) throws IOException {
43+
try (OutputStream out = new FileOutputStream(file); Writer writer = new OutputStreamWriter(out, "UTF-8")) {
44+
writer.write(data);
45+
}
46+
}
47+
48+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.baeldung.stream;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
public class OutputStreamExamplesTest {
12+
13+
StringBuilder filePath = new StringBuilder();
14+
15+
@Before
16+
public void init() {
17+
filePath.append("src");
18+
filePath.append(File.separator);
19+
filePath.append("test");
20+
filePath.append(File.separator);
21+
filePath.append("resources");
22+
filePath.append(File.separator);
23+
filePath.append("output_file.txt");
24+
}
25+
26+
@Test
27+
public void givenOutputStream_whenWriteSingleByteCalled_thenOutputCreated() throws IOException {
28+
29+
final File file = new File(filePath.toString());
30+
OutputStreamExamples examples = new OutputStreamExamples();
31+
examples.fileOutputStreamByteSingle(filePath.toString(), "Hello World!");
32+
assertTrue(file.exists());
33+
file.delete();
34+
}
35+
36+
@Test
37+
public void givenOutputStream_whenWriteByteSequenceCalled_thenOutputCreated() throws IOException {
38+
39+
final File file = new File(filePath.toString());
40+
OutputStreamExamples examples = new OutputStreamExamples();
41+
examples.fileOutputStreamByteSequence(filePath.toString(), "Hello World!");
42+
assertTrue(file.exists());
43+
file.delete();
44+
}
45+
46+
@Test
47+
public void givenOutputStream_whenWriteByteSubSequenceCalled_thenOutputCreated() throws IOException {
48+
49+
final File file = new File(filePath.toString());
50+
OutputStreamExamples examples = new OutputStreamExamples();
51+
examples.fileOutputStreamByteSubSequence(filePath.toString(), "Hello World!");
52+
assertTrue(file.exists());
53+
file.delete();
54+
}
55+
56+
@Test
57+
public void givenBufferedOutputStream_whenCalled_thenOutputCreated() throws IOException {
58+
59+
final File file = new File(filePath.toString());
60+
OutputStreamExamples examples = new OutputStreamExamples();
61+
examples.bufferedOutputStream(filePath.toString(), "Hello", "World!");
62+
assertTrue(file.exists());
63+
file.delete();
64+
}
65+
66+
@Test
67+
public void givenOutputStreamWriter_whenCalled_thenOutputCreated() throws IOException {
68+
69+
final File file = new File(filePath.toString());
70+
OutputStreamExamples examples = new OutputStreamExamples();
71+
examples.outputStreamWriter(filePath.toString(), "Hello World!");
72+
assertTrue(file.exists());
73+
file.delete();
74+
}
75+
76+
}

‎flyway-cdi-extension/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Relevant articles
2+
3+
- [CDI Portable Extension and Flyway](https://www.baeldung.com/cdi-portable-extension)

‎hibernate5/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
- [Bootstrapping JPA Programmatically in Java](http://www.baeldung.com/java-bootstrap-jpa)
1515
- [Optimistic Locking in JPA](http://www.baeldung.com/jpa-optimistic-locking)
1616
- [Hibernate Entity Lifecycle](https://www.baeldung.com/hibernate-entity-lifecycle)
17+
- [Mapping A Hibernate Query to a Custom Class](https://www.baeldung.com/hibernate-query-to-custom-class)
1718
- [@JoinColumn Annotation Explained](https://www.baeldung.com/jpa-join-column)

‎jackson/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118

119119
<properties>
120120
<!-- marshalling -->
121-
<jackson.version>2.9.6</jackson.version>
121+
<jackson.version>2.9.7</jackson.version>
122122
<!-- util -->
123123
<commons-lang3.version>3.8</commons-lang3.version>
124124
<joda-time.version>2.10</joda-time.version>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.jackson.xmlToJson;
2+
3+
public enum Color {
4+
PINK, BLUE, YELLOW, RED;
5+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.jackson.xmlToJson;
2+
3+
public class Flower {
4+
5+
private String name;
6+
7+
private Color color;
8+
9+
private Integer petals;
10+
11+
public Flower() { }
12+
13+
public Flower(String name, Color color, Integer petals) {
14+
this.name = name;
15+
this.color = color;
16+
this.petals = petals;
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public void setName(String name) {
24+
this.name = name;
25+
}
26+
27+
public Color getColor() {
28+
return color;
29+
}
30+
31+
public void setColor(Color color) {
32+
this.color = color;
33+
}
34+
35+
public Integer getPetals() {
36+
return petals;
37+
}
38+
39+
public void setPetals(Integer petals) {
40+
this.petals = petals;
41+
}
42+
}

0 commit comments

Comments
 (0)