Skip to content

Commit 3f1d94f

Browse files
committed
Merge remote-tracking branch 'eugenp/master'
2 parents d76aa38 + 15666e8 commit 3f1d94f

78 files changed

Lines changed: 1730 additions & 424 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.

‎algorithms/src/main/java/com/baeldung/algorithms/BinarySearch.java‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import java.util.Arrays;
2+
import java.util.Collections;
3+
import java.util.List;
4+
15
public class BinarySearch {
26

3-
public int runBinarySearch() {
4-
int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
5-
int key = 6;
7+
public int runBinarySearchIteratively(int[] sortedArray, int key, int low, int high) {
68

7-
int low = 0;
8-
int high = sortedArray.length - 1;
99
int index = Integer.MAX_VALUE;
1010

1111
while (low <= high) {
@@ -23,4 +23,31 @@ public int runBinarySearch() {
2323
}
2424
return index;
2525
}
26+
27+
public int runBinarySearchRecursively(int[] sortedArray, int key, int low, int high) {
28+
29+
int middle = (low + high) / 2;
30+
if (high < low) {
31+
return -1;
32+
}
33+
34+
if (key == sortedArray[middle]) {
35+
return middle;
36+
} else if (key < sortedArray[middle]) {
37+
return runBinarySearchRecursively(sortedArray, key, low, middle - 1);
38+
} else {
39+
return runBinarySearchRecursively(sortedArray, key, middle + 1, high);
40+
}
41+
}
42+
43+
public int runBinarySearchUsingJavaArrays(int[] sortedArray, Integer key) {
44+
int index = Arrays.binarySearch(sortedArray, key);
45+
return index;
46+
}
47+
48+
public int runBinarySearchUsingJavaCollections(List<Integer> sortedList, Integer key) {
49+
int index = Collections.binarySearch(sortedList, key);
50+
return index;
51+
}
52+
2653
}
Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
1+
import java.util.Arrays;
2+
import java.util.List;
3+
14
import org.junit.Assert;
25
import org.junit.Test;
36

47

58
public class BinarySearchTest {
69

10+
int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
11+
int key = 6;
12+
int expectedIndexForSearchKey = 7;
13+
int low = 0;
14+
int high = sortedArray.length - 1;
15+
List<Integer> sortedList = Arrays.asList(0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9);
16+
717
@Test
8-
public void givenASortedArrayOfIntegers_whenBinarySearchRunForANumber_thenGetIndexOfTheNumber() {
18+
public void givenASortedArrayOfIntegers_whenBinarySearchRunIterativelyForANumber_thenGetIndexOfTheNumber() {
919
BinarySearch binSearch = new BinarySearch();
10-
int expectedIndexForSearchKey = 7;
11-
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearch());
20+
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchIteratively(sortedArray, key, low, high));
1221
}
1322

23+
@Test
24+
public void givenASortedArrayOfIntegers_whenBinarySearchRunRecursivelyForANumber_thenGetIndexOfTheNumber() {
25+
BinarySearch binSearch = new BinarySearch();
26+
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchRecursively(sortedArray, key, low, high));
27+
}
28+
29+
@Test
30+
public void givenASortedArrayOfIntegers_whenBinarySearchRunUsingArraysClassStaticMethodForANumber_thenGetIndexOfTheNumber() {
31+
BinarySearch binSearch = new BinarySearch();
32+
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchUsingJavaArrays(sortedArray, key));
33+
}
34+
35+
@Test
36+
public void givenASortedListOfIntegers_whenBinarySearchRunUsingCollectionsClassStaticMethodForANumber_thenGetIndexOfTheNumber() {
37+
BinarySearch binSearch = new BinarySearch();
38+
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchUsingJavaCollections(sortedList, key));
39+
}
40+
1441
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.string;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.assertNotEquals;
7+
import static org.junit.Assert.assertTrue;
8+
9+
public class CharSequenceVsStringUnitTest {
10+
11+
@Test
12+
public void givenUsingString_whenInstantiatingString_thenWrong() {
13+
CharSequence firstString = "bealdung";
14+
String secondString = "baeldung";
15+
16+
assertNotEquals(firstString, secondString);
17+
}
18+
19+
@Test
20+
public void givenIdenticalCharSequences_whenCastToString_thenEqual() {
21+
CharSequence charSequence1 = "baeldung_1";
22+
CharSequence charSequence2 = "baeldung_2";
23+
24+
assertTrue(charSequence1.toString().compareTo(charSequence2.toString()) > 0);
25+
}
26+
27+
@Test
28+
public void givenString_whenAppended_thenUnmodified() {
29+
String test = "a";
30+
int firstAddressOfTest = System.identityHashCode(test);
31+
test += "b";
32+
int secondAddressOfTest = System.identityHashCode(test);
33+
34+
assertEquals(firstAddressOfTest, secondAddressOfTest);
35+
}
36+
37+
@Test
38+
public void givenStringBuilder_whenAppended_thenModified() {
39+
StringBuilder test = new StringBuilder();
40+
test.append("a");
41+
int firstAddressOfTest = System.identityHashCode(test);
42+
test.append("b");
43+
int secondAddressOfTest = System.identityHashCode(test);
44+
45+
assertEquals(firstAddressOfTest, secondAddressOfTest);
46+
}
47+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.baeldung.java.lists;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
import org.apache.commons.lang3.StringUtils;
8+
import org.junit.Test;
9+
10+
public class ListToSTring {
11+
12+
@Test
13+
public void whenListToString_thenPrintDefault() {
14+
List<Integer> intLIst = Arrays.asList(1, 2, 3);
15+
System.out.println(intLIst);
16+
}
17+
18+
@Test
19+
public void whenCollectorsJoining_thenPrintCustom() {
20+
List<Integer> intList = Arrays.asList(1, 2, 3);
21+
System.out.println(intList.stream()
22+
.map(n -> String.valueOf(n))
23+
.collect(Collectors.joining("-", "{", "}")));
24+
}
25+
26+
@Test
27+
public void whenStringUtilsJoin_thenPrintCustom() {
28+
List<Integer> intList = Arrays.asList(1, 2, 3);
29+
System.out.println(StringUtils.join(intList, "|"));
30+
}
31+
}

‎easy-rules/pom.xml‎

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎easy-rules/src/main/java/com/baeldung/easyrules/HelloWorldRule.java‎

Lines changed: 0 additions & 20 deletions
This file was deleted.

‎easy-rules/src/main/java/com/baeldung/easyrules/Launcher.java‎

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎guest/logback-example/pom.xml‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.stackify</groupId>
5+
<artifactId>logback-example</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
8+
<dependencies>
9+
<dependency>
10+
<groupId>ch.qos.logback</groupId>
11+
<artifactId>logback-classic</artifactId>
12+
<version>1.2.3</version>
13+
</dependency>
14+
15+
<dependency>
16+
<groupId>junit</groupId>
17+
<artifactId>junit</artifactId>
18+
<version>4.12</version>
19+
</dependency>
20+
21+
<dependency>
22+
<groupId>org.codehaus.janino</groupId>
23+
<artifactId>janino</artifactId>
24+
<version>3.0.7</version>
25+
</dependency>
26+
27+
</dependencies>
28+
29+
<build>
30+
<plugins>
31+
<plugin>
32+
<artifactId>maven-compiler-plugin</artifactId>
33+
<version>3.5</version>
34+
<configuration>
35+
<source>1.8</source>
36+
<target>1.8</target>
37+
</configuration>
38+
</plugin>
39+
</plugins>
40+
</build>
41+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.stackify.logging;
2+
3+
import org.slf4j.Marker;
4+
5+
import ch.qos.logback.classic.Level;
6+
import ch.qos.logback.classic.Logger;
7+
import ch.qos.logback.classic.turbo.TurboFilter;
8+
import ch.qos.logback.core.spi.FilterReply;
9+
10+
public class IgnoreLoggerFilter extends TurboFilter {
11+
12+
private String loggerName;
13+
14+
@Override
15+
public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
16+
if (loggerName == null) {
17+
return FilterReply.NEUTRAL;
18+
} else if (loggerName.equals(logger.getName())) {
19+
return FilterReply.DENY;
20+
} else
21+
return FilterReply.NEUTRAL;
22+
}
23+
24+
public void setLoggerName(String loggerName) {
25+
this.loggerName = loggerName;
26+
}
27+
28+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.stackify.models;
2+
3+
public class Employee {
4+
5+
private String email;
6+
private String name;
7+
8+
private double salary;
9+
10+
public Employee() {
11+
}
12+
13+
public Employee(String email, String name, double salary) {
14+
this.email = email;
15+
this.name = name;
16+
this.salary = salary;
17+
}
18+
19+
public String getEmail() {
20+
return email;
21+
}
22+
23+
public void setEmail(String email) {
24+
this.email = email;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
35+
public double getSalary() {
36+
return salary;
37+
}
38+
39+
public void setSalary(double salary) {
40+
this.salary = salary;
41+
}
42+
43+
}

0 commit comments

Comments
 (0)