Skip to content

Commit 31a0f7b

Browse files
Ahmed TawilaAhmed Tawila
authored andcommitted
Merge branch 'master' of https://github.com/eugenp/tutorials into future-vavr
2 parents a523437 + 63d4e7d commit 31a0f7b

8 files changed

Lines changed: 649 additions & 257 deletions

File tree

‎core-java-8/pom.xml‎

Lines changed: 275 additions & 257 deletions
Large diffs are not rendered by default.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.counter;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.openjdk.jmh.annotations.Benchmark;
7+
import org.openjdk.jmh.annotations.BenchmarkMode;
8+
import org.openjdk.jmh.annotations.Fork;
9+
import org.openjdk.jmh.annotations.Mode;
10+
11+
import com.baeldung.counter.CounterUtil.MutableInteger;
12+
13+
@Fork(value = 1, warmups = 3)
14+
@BenchmarkMode(Mode.All)
15+
public class CounterStatistics {
16+
17+
private static final Map<String, Integer> counterMap = new HashMap<>();
18+
private static final Map<String, MutableInteger> counterWithMutableIntMap = new HashMap<>();
19+
private static final Map<String, int[]> counterWithIntArrayMap = new HashMap<>();
20+
private static final Map<String, Long> counterWithLongWrapperMap = new HashMap<>();
21+
22+
@Benchmark
23+
public void wrapperAsCounter() {
24+
CounterUtil.counterWithWrapperObject(counterMap);
25+
}
26+
27+
@Benchmark
28+
public void lambdaExpressionWithWrapper() {
29+
CounterUtil.counterWithLambdaAndWrapper(counterWithLongWrapperMap);
30+
}
31+
32+
@Benchmark
33+
public void mutableIntegerAsCounter() {
34+
CounterUtil.counterWithMutableInteger(counterWithMutableIntMap);
35+
}
36+
37+
@Benchmark
38+
public void primitiveArrayAsCounter() {
39+
CounterUtil.counterWithPrimitiveArray(counterWithIntArrayMap);
40+
}
41+
42+
public static void main(String[] args) throws Exception {
43+
org.openjdk.jmh.Main.main(args);
44+
}
45+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.counter;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import static org.junit.Assert.*;
7+
8+
import org.junit.Test;
9+
10+
import com.baeldung.counter.CounterUtil.MutableInteger;
11+
12+
public class CounterTest {
13+
14+
@Test
15+
public void whenMapWithWrapperAsCounter_runsSuccessfully() {
16+
Map<String, Integer> counterMap = new HashMap<>();
17+
CounterUtil.counterWithWrapperObject(counterMap);
18+
19+
assertEquals(3, counterMap.get("China")
20+
.intValue());
21+
assertEquals(2, counterMap.get("India")
22+
.intValue());
23+
}
24+
25+
@Test
26+
public void whenMapWithLambdaAndWrapperCounter_runsSuccessfully() {
27+
Map<String, Long> counterMap = new HashMap<>();
28+
CounterUtil.counterWithLambdaAndWrapper(counterMap);
29+
30+
assertEquals(3l, counterMap.get("China")
31+
.longValue());
32+
assertEquals(2l, counterMap.get("India")
33+
.longValue());
34+
}
35+
36+
@Test
37+
public void whenMapWithMutableIntegerCounter_runsSuccessfully() {
38+
Map<String, MutableInteger> counterMap = new HashMap<>();
39+
CounterUtil.counterWithMutableInteger(counterMap);
40+
assertEquals(3, counterMap.get("China")
41+
.getCount());
42+
assertEquals(2, counterMap.get("India")
43+
.getCount());
44+
}
45+
46+
@Test
47+
public void whenMapWithPrimitiveArray_runsSuccessfully() {
48+
Map<String, int[]> counterMap = new HashMap<>();
49+
CounterUtil.counterWithPrimitiveArray(counterMap);
50+
assertEquals(3, counterMap.get("China")[0]);
51+
assertEquals(2, counterMap.get("India")[0]);
52+
}
53+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.counter;
2+
3+
import java.util.Map;
4+
import java.util.stream.Collectors;
5+
import java.util.stream.Stream;
6+
7+
public class CounterUtil {
8+
9+
private final static String[] COUNTRY_NAMES = { "China", "Australia", "India", "USA", "USSR", "UK", "China", "France", "Poland", "Austria", "India", "USA", "Egypt", "China" };
10+
11+
public static void counterWithWrapperObject(Map<String, Integer> counterMap) {
12+
for (String country : COUNTRY_NAMES) {
13+
counterMap.compute(country, (k, v) -> v == null ? 1 : v + 1);
14+
}
15+
}
16+
17+
public static void counterWithLambdaAndWrapper(Map<String, Long> counterMap) {
18+
counterMap.putAll(Stream.of(COUNTRY_NAMES)
19+
.parallel()
20+
.collect(Collectors.groupingBy(k -> k, Collectors.counting())));
21+
}
22+
23+
public static class MutableInteger {
24+
int count;
25+
26+
public MutableInteger(int count) {
27+
this.count = count;
28+
}
29+
30+
public void increment() {
31+
this.count++;
32+
}
33+
34+
public int getCount() {
35+
return this.count;
36+
}
37+
}
38+
39+
public static void counterWithMutableInteger(Map<String, MutableInteger> counterMap) {
40+
for (String country : COUNTRY_NAMES) {
41+
MutableInteger oldValue = counterMap.get(country);
42+
if (oldValue != null) {
43+
oldValue.increment();
44+
} else {
45+
counterMap.put(country, new MutableInteger(1));
46+
}
47+
}
48+
}
49+
50+
public static void counterWithPrimitiveArray(Map<String, int[]> counterMap) {
51+
for (String country : COUNTRY_NAMES) {
52+
int[] oldCounter = counterMap.get(country);
53+
if (oldCounter != null) {
54+
oldCounter[0] += 1;
55+
} else {
56+
counterMap.put(country, new int[] { 1 });
57+
}
58+
}
59+
}
60+
61+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.kotlin
2+
3+
import org.junit.Assert
4+
import org.junit.Test
5+
6+
class ExtensionMethods {
7+
@Test
8+
fun simpleExtensionMethod() {
9+
fun String.escapeForXml() : String {
10+
return this
11+
.replace("&", "&amp;")
12+
.replace("<", "&lt;")
13+
.replace(">", "&gt;")
14+
}
15+
16+
Assert.assertEquals("Nothing", "Nothing".escapeForXml())
17+
Assert.assertEquals("&lt;Tag&gt;", "<Tag>".escapeForXml())
18+
Assert.assertEquals("a&amp;b", "a&b".escapeForXml())
19+
}
20+
21+
@Test
22+
fun genericExtensionMethod() {
23+
fun <T> T.concatAsString(b: T) : String {
24+
return this.toString() + b.toString()
25+
}
26+
27+
Assert.assertEquals("12", "1".concatAsString("2"))
28+
Assert.assertEquals("12", 1.concatAsString(2))
29+
// This doesn't compile
30+
// Assert.assertEquals("12", 1.concatAsString(2.0))
31+
}
32+
33+
@Test
34+
fun infixExtensionMethod() {
35+
infix fun Number.toPowerOf(exponent: Number): Double {
36+
return Math.pow(this.toDouble(), exponent.toDouble())
37+
}
38+
39+
Assert.assertEquals(9.0, 3 toPowerOf 2, 0.1)
40+
Assert.assertEquals(3.0, 9 toPowerOf 0.5, 0.1)
41+
}
42+
43+
@Test
44+
fun operatorExtensionMethod() {
45+
operator fun List<Int>.times(by: Int): List<Int> {
46+
return this.map { it * by }
47+
}
48+
49+
Assert.assertEquals(listOf(2, 4, 6), listOf(1, 2, 3) * 2)
50+
}
51+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package org.baeldung.bddmockito;
2+
3+
import static org.junit.Assert.fail;
4+
import static org.mockito.BDDMockito.*;
5+
6+
import org.junit.Assert;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.mockito.Mockito;
10+
import org.mockito.invocation.InvocationOnMock;
11+
12+
13+
public class BDDMockitoTest {
14+
15+
PhoneBookService phoneBookService;
16+
PhoneBookRepository phoneBookRepository;
17+
18+
String momContactName = "Mom";
19+
String momPhoneNumber = "01234";
20+
String xContactName = "x";
21+
String tooLongPhoneNumber = "01111111111111";
22+
23+
@Before
24+
public void init() {
25+
phoneBookRepository = Mockito.mock(PhoneBookRepository.class);
26+
phoneBookService = new PhoneBookService(phoneBookRepository);
27+
}
28+
29+
@Test
30+
public void givenValidContactName_whenSearchInPhoneBook_thenRetunPhoneNumber() {
31+
given(phoneBookRepository.contains(momContactName)).willReturn(true);
32+
given(phoneBookRepository.getPhoneNumberByContactName(momContactName))
33+
.will((InvocationOnMock invocation) -> {
34+
if(invocation.getArgument(0).equals(momContactName)) {
35+
return momPhoneNumber;
36+
} else {
37+
return null;
38+
}
39+
});
40+
41+
String phoneNumber = phoneBookService.search(momContactName);
42+
43+
then(phoneBookRepository).should().contains(momContactName);
44+
then(phoneBookRepository).should().getPhoneNumberByContactName(momContactName);
45+
Assert.assertEquals(phoneNumber, momPhoneNumber);
46+
}
47+
48+
@Test
49+
public void givenInvalidContactName_whenSearch_thenRetunNull() {
50+
given(phoneBookRepository.contains(xContactName)).willReturn(false);
51+
52+
String phoneNumber = phoneBookService.search(xContactName);
53+
54+
then(phoneBookRepository).should().contains(xContactName);
55+
then(phoneBookRepository).should(never()).getPhoneNumberByContactName(xContactName);
56+
Assert.assertEquals(phoneNumber, null);
57+
}
58+
59+
@Test
60+
public void givenValidContactNameAndPhoneNumber_whenRegister_thenSucceed() {
61+
given(phoneBookRepository.contains(momContactName)).willReturn(false);
62+
63+
phoneBookService.register(momContactName, momPhoneNumber);
64+
65+
verify(phoneBookRepository).insert(momContactName, momPhoneNumber);
66+
}
67+
68+
@Test
69+
public void givenEmptyPhoneNumber_whenRegister_thenFail() {
70+
given(phoneBookRepository.contains(momContactName)).willReturn(false);
71+
72+
phoneBookService.register(xContactName, "");
73+
74+
then(phoneBookRepository).should(never()).insert(momContactName, momPhoneNumber);
75+
}
76+
77+
@Test
78+
public void givenLongPhoneNumber_whenRegister_thenFail() {
79+
given(phoneBookRepository.contains(xContactName)).willReturn(false);
80+
willThrow(new RuntimeException())
81+
.given(phoneBookRepository).insert(any(String.class), eq(tooLongPhoneNumber));
82+
83+
try {
84+
phoneBookService.register(xContactName, tooLongPhoneNumber);
85+
fail("Should throw exception");
86+
} catch (RuntimeException ex) { }
87+
88+
then(phoneBookRepository).should(never()).insert(momContactName, tooLongPhoneNumber);
89+
}
90+
91+
@Test
92+
public void givenExistentContactName_whenRegister_thenFail() {
93+
given(phoneBookRepository.contains(momContactName))
94+
.willThrow(new RuntimeException("Name already exist"));
95+
96+
try {
97+
phoneBookService.register(momContactName, momPhoneNumber);
98+
fail("Should throw exception");
99+
} catch(Exception ex) { }
100+
101+
then(phoneBookRepository).should(never()).insert(momContactName, momPhoneNumber);
102+
}
103+
104+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.baeldung.bddmockito;
2+
3+
public interface PhoneBookRepository {
4+
5+
/**
6+
* Insert phone record
7+
* @param name Contact name
8+
* @param phone Phone number
9+
*/
10+
void insert(String name, String phone);
11+
12+
/**
13+
* Search for contact phone number
14+
* @param name Contact name
15+
* @return phone number
16+
*/
17+
String getPhoneNumberByContactName(String name);
18+
19+
/**
20+
* Check if the phonebook contains this contact
21+
* @param name Contact name
22+
* @return true if this contact name exists
23+
*/
24+
boolean contains(String name);
25+
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.baeldung.bddmockito;
2+
3+
public class PhoneBookService {
4+
5+
private PhoneBookRepository phoneBookRepository;
6+
7+
public PhoneBookService(PhoneBookRepository phoneBookRepository) {
8+
this.phoneBookRepository = phoneBookRepository;
9+
}
10+
11+
/**
12+
* Register a contact
13+
* @param name Contact name
14+
* @param phone Phone number
15+
*/
16+
public void register(String name, String phone) {
17+
if(!name.isEmpty() && !phone.isEmpty() && !phoneBookRepository.contains(name)) {
18+
phoneBookRepository.insert(name, phone);
19+
}
20+
}
21+
22+
/**
23+
* Search for a phone number by contact name
24+
* @param name Contact name
25+
* @return Phone number
26+
*/
27+
public String search(String name) {
28+
if(!name.isEmpty() && phoneBookRepository.contains(name)) {
29+
return phoneBookRepository.getPhoneNumberByContactName(name);
30+
}
31+
return null;
32+
}
33+
34+
}

0 commit comments

Comments
 (0)