Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
5f89c098bd
|
@ -31,8 +31,8 @@ public class CollectorImprovementTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFiltering() {
|
||||
@Test
|
||||
public void givenList_whenSatifyPredicate_thenMapValueWithOccurences() {
|
||||
List<Integer> numbers = List.of(1, 2, 3, 5, 5);
|
||||
|
||||
Map<Integer, Long> result = numbers.stream()
|
||||
|
@ -49,7 +49,7 @@ public class CollectorImprovementTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testFlatMapping() {
|
||||
public void givenListOfBlogs_whenAuthorName_thenMapAuthorWithComments() {
|
||||
Blog blog1 = new CollectorImprovementTest.Blog("1");
|
||||
blog1.addComment("Nice");
|
||||
blog1.addComment("Very Nice");
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.concurrent.blockingqueue;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
public class BlockingQueueUsage {
|
||||
public static void main(String[] args) {
|
||||
int BOUND = 10;
|
||||
int N_PRODUCERS = 4;
|
||||
int N_CONSUMERS = Runtime.getRuntime().availableProcessors();
|
||||
|
||||
BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(BOUND);
|
||||
|
||||
for (int i = 0; i < N_PRODUCERS; i++) {
|
||||
new Thread(new NumbersProducer(queue)).start();
|
||||
}
|
||||
|
||||
for (int j = 0; j < N_CONSUMERS; j++) {
|
||||
new Thread(new NumbersConsumer(queue)).start();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.concurrent.blockingqueue;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
|
||||
public class NumbersConsumer implements Runnable {
|
||||
private final BlockingQueue<Integer> queue;
|
||||
|
||||
public NumbersConsumer(BlockingQueue<Integer> queue) {
|
||||
this.queue = queue;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
while (true) {
|
||||
Integer number = queue.take();
|
||||
String result = number.toString();
|
||||
System.out.println(Thread.currentThread().getName() + " result: " + result);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.concurrent.blockingqueue;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class NumbersProducer implements Runnable {
|
||||
|
||||
private final BlockingQueue<Integer> numbersQueue;
|
||||
|
||||
public NumbersProducer(BlockingQueue<Integer> numbersQueue) {
|
||||
this.numbersQueue = numbersQueue;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
generateNumbers();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
private void generateNumbers() throws InterruptedException {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
numbersQueue.put(ThreadLocalRandom.current().nextInt(100));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
package com.baeldung.java.concurrentmap;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
public class ConcurrentMapAggregateStatusTest {
|
||||
|
||||
private ExecutorService executorService;
|
||||
private Map<String, Integer> concurrentMap;
|
||||
private List<Integer> mapSizes;
|
||||
private int MAX_SIZE = 100000;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
executorService = Executors.newFixedThreadPool(2);
|
||||
concurrentMap = new ConcurrentHashMap<>();
|
||||
mapSizes = new ArrayList<>(MAX_SIZE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenConcurrentMap_whenSizeWithoutConcurrentUpdates_thenCorrect() throws InterruptedException {
|
||||
Runnable collectMapSizes = () -> {
|
||||
for (int i = 0; i < MAX_SIZE; i++) {
|
||||
concurrentMap.put(String.valueOf(i), i);
|
||||
mapSizes.add(concurrentMap.size());
|
||||
}
|
||||
};
|
||||
Runnable retrieveMapData = () -> {
|
||||
for (int i = 0; i < MAX_SIZE; i++) {
|
||||
concurrentMap.get(String.valueOf(i));
|
||||
}
|
||||
};
|
||||
executorService.execute(retrieveMapData);
|
||||
executorService.execute(collectMapSizes);
|
||||
executorService.shutdown();
|
||||
executorService.awaitTermination(1, TimeUnit.MINUTES);
|
||||
|
||||
for (int i = 1; i <= MAX_SIZE; i++) {
|
||||
assertEquals("map size should be consistently reliable", i, mapSizes
|
||||
.get(i - 1)
|
||||
.intValue());
|
||||
}
|
||||
assertEquals(MAX_SIZE, concurrentMap.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenConcurrentMap_whenUpdatingAndGetSize_thenError() throws InterruptedException {
|
||||
Runnable collectMapSizes = () -> {
|
||||
for (int i = 0; i < MAX_SIZE; i++) {
|
||||
mapSizes.add(concurrentMap.size());
|
||||
}
|
||||
};
|
||||
Runnable updateMapData = () -> {
|
||||
for (int i = 0; i < MAX_SIZE; i++) {
|
||||
concurrentMap.put(String.valueOf(i), i);
|
||||
}
|
||||
};
|
||||
executorService.execute(updateMapData);
|
||||
executorService.execute(collectMapSizes);
|
||||
executorService.shutdown();
|
||||
executorService.awaitTermination(1, TimeUnit.MINUTES);
|
||||
|
||||
assertNotEquals("map size collected with concurrent updates not reliable", MAX_SIZE, mapSizes
|
||||
.get(MAX_SIZE - 1)
|
||||
.intValue());
|
||||
assertEquals(MAX_SIZE, concurrentMap.size());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,160 @@
|
|||
package com.baeldung.java.concurrentmap;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public class ConcurrentMapNullKeyValueTest {
|
||||
|
||||
ConcurrentMap<String, Object> concurrentMap;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
concurrentMap = new ConcurrentHashMap<>();
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenGetWithNullKey_thenThrowsNPE() {
|
||||
concurrentMap.get(null);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenGetOrDefaultWithNullKey_thenThrowsNPE() {
|
||||
concurrentMap.getOrDefault(null, new Object());
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenPutWithNullKey_thenThrowsNPE() {
|
||||
concurrentMap.put(null, new Object());
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenPutNullValue_thenThrowsNPE() {
|
||||
concurrentMap.put("test", null);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMapAndKeyAbsent_whenPutWithNullKey_thenThrowsNPE() {
|
||||
concurrentMap.putIfAbsent(null, new Object());
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMapAndMapWithNullKey_whenPutNullKeyMap_thenThrowsNPE() {
|
||||
Map<String, Object> nullKeyMap = new HashMap<>();
|
||||
nullKeyMap.put(null, new Object());
|
||||
concurrentMap.putAll(nullKeyMap);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMapAndMapWithNullValue_whenPutNullValueMap_thenThrowsNPE() {
|
||||
Map<String, Object> nullValueMap = new HashMap<>();
|
||||
nullValueMap.put("test", null);
|
||||
concurrentMap.putAll(nullValueMap);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenReplaceNullKeyWithValues_thenThrowsNPE() {
|
||||
concurrentMap.replace(null, new Object(), new Object());
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenReplaceWithNullNewValue_thenThrowsNPE() {
|
||||
Object o = new Object();
|
||||
concurrentMap.replace("test", o, null);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenReplaceOldNullValue_thenThrowsNPE() {
|
||||
Object o = new Object();
|
||||
concurrentMap.replace("test", null, o);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenReplaceWithNullValue_thenThrowsNPE() {
|
||||
concurrentMap.replace("test", null);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenReplaceNullKey_thenThrowsNPE() {
|
||||
concurrentMap.replace(null, "test");
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenReplaceAllMappingNull_thenThrowsNPE() {
|
||||
concurrentMap.put("test", new Object());
|
||||
concurrentMap.replaceAll((s, o) -> null);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenRemoveNullKey_thenThrowsNPE() {
|
||||
concurrentMap.remove(null);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenRemoveNullKeyWithValue_thenThrowsNPE() {
|
||||
concurrentMap.remove(null, new Object());
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenMergeNullKeyWithValue_thenThrowsNPE() {
|
||||
concurrentMap.merge(null, new Object(), (o, o2) -> o2);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenMergeKeyWithNullValue_thenThrowsNPE() {
|
||||
concurrentMap.put("test", new Object());
|
||||
concurrentMap.merge("test", null, (o, o2) -> o2);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMapAndAssumeKeyAbsent_whenComputeWithNullKey_thenThrowsNPE() {
|
||||
concurrentMap.computeIfAbsent(null, s -> s);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMapAndAssumeKeyPresent_whenComputeWithNullKey_thenThrowsNPE() {
|
||||
concurrentMap.computeIfPresent(null, (s, o) -> o);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenConcurrentHashMap_whenComputeWithNullKey_thenThrowsNPE() {
|
||||
concurrentMap.compute(null, (s, o) -> o);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenConcurrentHashMap_whenMergeKeyRemappingNull_thenRemovesMapping() {
|
||||
Object oldValue = new Object();
|
||||
concurrentMap.put("test", oldValue);
|
||||
concurrentMap.merge("test", new Object(), (o, o2) -> null);
|
||||
assertNull(concurrentMap.get("test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenConcurrentHashMapAndKeyAbsent_whenComputeWithKeyRemappingNull_thenRemainsAbsent() {
|
||||
concurrentMap.computeIfPresent("test", (s, o) -> null);
|
||||
assertNull(concurrentMap.get("test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenKeyPresent_whenComputeIfPresentRemappingNull_thenMappingRemoved() {
|
||||
Object oldValue = new Object();
|
||||
concurrentMap.put("test", oldValue);
|
||||
concurrentMap.computeIfPresent("test", (s, o) -> null);
|
||||
assertNull(concurrentMap.get("test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenKeyPresent_whenComputeRemappingNull_thenMappingRemoved() {
|
||||
Object oldValue = new Object();
|
||||
concurrentMap.put("test", oldValue);
|
||||
concurrentMap.compute("test", (s, o) -> null);
|
||||
assertNull(concurrentMap.get("test"));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
package com.baeldung.java.concurrentmap;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ConcurrentMapPerformanceTest {
|
||||
|
||||
@Test
|
||||
public void givenMaps_whenGetPut500KTimes_thenConcurrentMapFaster() throws Exception {
|
||||
Map<String, Object> hashtable = new Hashtable<>();
|
||||
Map<String, Object> synchronizedHashMap = Collections.synchronizedMap(new HashMap<>());
|
||||
Map<String, Object> concurrentHashMap = new ConcurrentHashMap<>();
|
||||
|
||||
long hashtableAvgRuntime = timeElapseForGetPut(hashtable);
|
||||
long syncHashMapAvgRuntime = timeElapseForGetPut(synchronizedHashMap);
|
||||
long concurrentHashMapAvgRuntime = timeElapseForGetPut(concurrentHashMap);
|
||||
|
||||
assertTrue(hashtableAvgRuntime > concurrentHashMapAvgRuntime);
|
||||
assertTrue(syncHashMapAvgRuntime > concurrentHashMapAvgRuntime);
|
||||
|
||||
System.out.println(String.format("Hashtable: %s, syncHashMap: %s, ConcurrentHashMap: %s", hashtableAvgRuntime, syncHashMapAvgRuntime, concurrentHashMapAvgRuntime));
|
||||
|
||||
}
|
||||
|
||||
private long timeElapseForGetPut(Map<String, Object> map) throws InterruptedException {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(4);
|
||||
long startTime = System.nanoTime();
|
||||
for (int i = 0; i < 4; i++) {
|
||||
executorService.execute(() -> {
|
||||
for (int j = 0; j < 500_000; j++) {
|
||||
int value = ThreadLocalRandom
|
||||
.current()
|
||||
.nextInt(10000);
|
||||
String key = String.valueOf(value);
|
||||
map.put(key, value);
|
||||
map.get(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
executorService.shutdown();
|
||||
executorService.awaitTermination(1, TimeUnit.MINUTES);
|
||||
return (System.nanoTime() - startTime) / 500_000;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenConcurrentMap_whenKeyWithSameHashCode_thenPerformanceDegrades() throws InterruptedException {
|
||||
class SameHash {
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
int executeTimes = 5000;
|
||||
|
||||
Map<SameHash, Integer> mapOfSameHash = new ConcurrentHashMap<>();
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(2);
|
||||
long sameHashStartTime = System.currentTimeMillis();
|
||||
for (int i = 0; i < 2; i++) {
|
||||
executorService.execute(() -> {
|
||||
for (int j = 0; j < executeTimes; j++) {
|
||||
mapOfSameHash.put(new SameHash(), 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
executorService.shutdown();
|
||||
executorService.awaitTermination(5, TimeUnit.SECONDS);
|
||||
|
||||
long mapOfSameHashDuration = System.currentTimeMillis() - sameHashStartTime;
|
||||
Map<Object, Integer> mapOfDefaultHash = new ConcurrentHashMap<>();
|
||||
executorService = Executors.newFixedThreadPool(2);
|
||||
long defaultHashStartTime = System.currentTimeMillis();
|
||||
for (int i = 0; i < 2; i++) {
|
||||
executorService.execute(() -> {
|
||||
for (int j = 0; j < executeTimes; j++) {
|
||||
mapOfDefaultHash.put(new Object(), 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
executorService.shutdown();
|
||||
executorService.awaitTermination(5, TimeUnit.SECONDS);
|
||||
|
||||
long mapOfDefaultHashDuration = System.currentTimeMillis() - defaultHashStartTime;
|
||||
assertEquals(executeTimes * 2, mapOfDefaultHash.size());
|
||||
assertNotEquals(executeTimes * 2, mapOfSameHash.size());
|
||||
System.out.println(String.format("same-hash: %s, default-hash: %s", mapOfSameHashDuration, mapOfDefaultHashDuration));
|
||||
assertTrue("same hashCode() should greatly degrade performance", mapOfSameHashDuration > mapOfDefaultHashDuration * 10);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
package com.baeldung.java.concurrentmap;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ConcurretMapMemoryConsistencyTest {
|
||||
|
||||
@Test
|
||||
public void givenConcurrentMap_whenSumParallel_thenCorrect() throws Exception {
|
||||
Map<String, Integer> map = new ConcurrentHashMap<>();
|
||||
List<Integer> sumList = parallelSum100(map, 1000);
|
||||
assertEquals(1, sumList
|
||||
.stream()
|
||||
.distinct()
|
||||
.count());
|
||||
long wrongResultCount = sumList
|
||||
.stream()
|
||||
.filter(num -> num != 100)
|
||||
.count();
|
||||
assertEquals(0, wrongResultCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHashtable_whenSumParallel_thenCorrect() throws Exception {
|
||||
Map<String, Integer> map = new Hashtable<>();
|
||||
List<Integer> sumList = parallelSum100(map, 1000);
|
||||
assertEquals(1, sumList
|
||||
.stream()
|
||||
.distinct()
|
||||
.count());
|
||||
long wrongResultCount = sumList
|
||||
.stream()
|
||||
.filter(num -> num != 100)
|
||||
.count();
|
||||
assertEquals(0, wrongResultCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHashMap_whenSumParallel_thenError() throws Exception {
|
||||
Map<String, Integer> map = new HashMap<>();
|
||||
List<Integer> sumList = parallelSum100(map, 100);
|
||||
assertNotEquals(1, sumList
|
||||
.stream()
|
||||
.distinct()
|
||||
.count());
|
||||
long wrongResultCount = sumList
|
||||
.stream()
|
||||
.filter(num -> num != 100)
|
||||
.count();
|
||||
assertTrue(wrongResultCount > 0);
|
||||
}
|
||||
|
||||
private List<Integer> parallelSum100(Map<String, Integer> map, int executionTimes) throws InterruptedException {
|
||||
List<Integer> sumList = new ArrayList<>(1000);
|
||||
for (int i = 0; i < executionTimes; i++) {
|
||||
map.put("test", 0);
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(4);
|
||||
for (int j = 0; j < 10; j++) {
|
||||
executorService.execute(() -> {
|
||||
for (int k = 0; k < 10; k++)
|
||||
map.computeIfPresent("test", (key, value) -> value + 1);
|
||||
});
|
||||
}
|
||||
executorService.shutdown();
|
||||
executorService.awaitTermination(5, TimeUnit.SECONDS);
|
||||
sumList.add(map.get("test"));
|
||||
}
|
||||
return sumList;
|
||||
}
|
||||
|
||||
}
|
|
@ -38,6 +38,12 @@
|
|||
<version>${kotlin-maven-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<configuration>
|
||||
<sourceDirs>
|
||||
<source>src/main/java</source>
|
||||
<source>src/main/kotlin</source>
|
||||
</sourceDirs>
|
||||
</configuration>
|
||||
<id>compile</id>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
|
@ -45,6 +51,12 @@
|
|||
</execution>
|
||||
|
||||
<execution>
|
||||
<configuration>
|
||||
<sourceDirs>
|
||||
<source>src/test/java</source>
|
||||
<source>src/test/kotlin</source>
|
||||
</sourceDirs>
|
||||
</configuration>
|
||||
<id>test-compile</id>
|
||||
<goals>
|
||||
<goal>test-compile</goal>
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.java;
|
||||
|
||||
public class StringUtils {
|
||||
public static String toUpperCase(String name) {
|
||||
return name.toUpperCase();
|
||||
}
|
||||
}
|
|
@ -24,29 +24,41 @@ class ItemManager(val categoryId: String, val dbConnection: String) {
|
|||
fun makeAnalyisOfCategory(catId: String): Unit {
|
||||
val result = if (catId == "100") "Yes" else "No"
|
||||
println(result)
|
||||
|
||||
`object`()
|
||||
}
|
||||
|
||||
fun sum(a: Int, b: Int): Int {
|
||||
return a + b
|
||||
}
|
||||
|
||||
fun `object`(): String {
|
||||
return "this is object"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val numbers = arrayOf("first", "second", "third", "fourth")
|
||||
|
||||
var concat = ""
|
||||
for (n in numbers) {
|
||||
concat += n
|
||||
println(n)
|
||||
}
|
||||
|
||||
var sum = 0
|
||||
for (i in 2..9) {
|
||||
sum += i
|
||||
for (i in 2..9 step 2) {
|
||||
println(i)
|
||||
}
|
||||
|
||||
val res = 1.rangeTo(10).map { it * 2 }
|
||||
println(res)
|
||||
|
||||
val firstName = "Tom"
|
||||
val secondName = "Mary"
|
||||
val concatOfNames = "$firstName + $secondName"
|
||||
println("Names: $concatOfNames")
|
||||
val sum = "four: ${2 + 2}"
|
||||
|
||||
val itemManager = ItemManager("cat_id", "db://connection")
|
||||
ItemManager(categoryId = "catId", dbConnection = "db://Connection")
|
||||
val result = "function result: ${itemManager.isFromSpecificCategory("1")}"
|
||||
println(result)
|
||||
|
||||
|
@ -63,4 +75,9 @@ fun main(args: Array<String>) {
|
|||
"Alice" -> println("Hi lady")
|
||||
}
|
||||
|
||||
val items = listOf(1, 2, 3, 4)
|
||||
|
||||
|
||||
val rwList = mutableListOf(1, 2, 3)
|
||||
rwList.add(5)
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.kotlin
|
||||
|
||||
class MathematicsOperations {
|
||||
fun addTwoNumbers(a: Int, b: Int): Int {
|
||||
return a + b
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.kotlin;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class JavaCallToKotlinTest {
|
||||
@Test
|
||||
public void givenKotlinClass_whenCallFromJava_shouldProduceResults() {
|
||||
//when
|
||||
int res = new MathematicsOperations().addTwoNumbers(2, 4);
|
||||
|
||||
//then
|
||||
assertEquals(6, res);
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.kotlin
|
||||
|
||||
import com.baeldung.java.StringUtils
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
|
||||
class KotlinScalaInteroperabilityTest {
|
||||
@Test
|
||||
fun givenLowercaseString_whenExecuteMethodFromJavaStringUtils_shouldReturnStringUppercase() {
|
||||
//given
|
||||
val name = "tom"
|
||||
|
||||
//when
|
||||
val res = StringUtils.toUpperCase(name)
|
||||
|
||||
//then
|
||||
assertEquals(res, "TOM")
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.kotlin
|
||||
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
|
||||
class LambdaTest {
|
||||
@Test
|
||||
fun givenListOfNumber_whenDoingOperationsUsingLambda_shouldReturnProperResult() {
|
||||
//given
|
||||
val listOfNumbers = listOf(1, 2, 3)
|
||||
|
||||
//when
|
||||
val sum = listOfNumbers.reduce { a, b -> a + b }
|
||||
|
||||
//then
|
||||
assertEquals(6, sum)
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
spring.application.name=resource
|
||||
server.port=0
|
||||
#server.port=0
|
||||
|
||||
#### cloud
|
||||
eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://system:systemPass@localhost:8761/eureka}
|
||||
|
|
|
@ -1,3 +1,9 @@
|
|||
spring.cloud.config.name=resource
|
||||
spring.cloud.config.discovery.service-id=config
|
||||
spring.cloud.config.discovery.enabled=true
|
||||
spring.cloud.config.discovery.enabled=true
|
||||
spring.cloud.config.username=configUser
|
||||
spring.cloud.config.password=configPassword
|
||||
|
||||
eureka.client.serviceUrl.defaultZone=http://system:systemPass@localhost:8761/eureka
|
||||
|
||||
server.port=8084
|
|
@ -1,19 +0,0 @@
|
|||
#### cloud
|
||||
spring.application.name=spring-cloud-eureka-client
|
||||
server.port=0
|
||||
eureka.client.serviceUrl.defaultZone=http://system:systemPass@localhost:8761/eureka
|
||||
eureka.instance.preferIpAddress=true
|
||||
|
||||
#### persistence
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.datasource.url=jdbc:h2:mem:cloud_rest;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
|
||||
#### security
|
||||
security.basic.enabled=true
|
||||
security.basic.path=/**
|
||||
security.user.name=user
|
||||
security.user.password=userPass
|
||||
security.user.role=USER
|
||||
security.sessions=always
|
|
@ -0,0 +1,9 @@
|
|||
spring.cloud.config.name=resource
|
||||
spring.cloud.config.discovery.service-id=config
|
||||
spring.cloud.config.discovery.enabled=true
|
||||
spring.cloud.config.username=configUser
|
||||
spring.cloud.config.password=configPassword
|
||||
|
||||
eureka.client.serviceUrl.defaultZone=http://system:systemPass@localhost:8761/eureka
|
||||
|
||||
server.port=8085
|
|
@ -22,7 +22,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||
@SpringBootTest(classes = { SpringCloudRestClientApplication.class }, webEnvironment = WebEnvironment.DEFINED_PORT)
|
||||
public class RestApiLiveTest {
|
||||
|
||||
private static final String API_URI = "http://localhost:8084/reviews";
|
||||
private static final String API_URI = "http://localhost:8085/reviews";
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#### cloud
|
||||
spring.application.name=spring-cloud-eureka-client
|
||||
server.port=8084
|
||||
server.port=8085
|
||||
eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://system:systemPass@localhost:8761/eureka}
|
||||
eureka.instance.preferIpAddress=true
|
||||
|
||||
|
|
|
@ -34,6 +34,10 @@
|
|||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-eureka</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
|
@ -3,10 +3,11 @@ package org.baeldung;
|
|||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.config.server.EnableConfigServer;
|
||||
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableConfigServer
|
||||
// @EnableEurekaClient
|
||||
@EnableEurekaClient
|
||||
public class SpringCloudRestConfigApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
server.port=8081
|
||||
spring.application.name=config
|
||||
|
||||
spring.cloud.config.server.git.uri=https://github.com/eugenp/tutorials/tree/master/spring-cloud/spring-cloud-rest/application-config
|
||||
spring.cloud.config.server.git.uri=${HOME}/application-config
|
||||
|
||||
#eureka.client.region = default
|
||||
#eureka.client.registryFetchIntervalSeconds = 5
|
||||
#eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://system:systemPass@localhost:8761/eureka}
|
||||
eureka.client.region = default
|
||||
eureka.client.registryFetchIntervalSeconds = 5
|
||||
eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://system:systemPass@localhost:8761/eureka}
|
||||
|
||||
security.user.name=configUser
|
||||
security.user.password=configPassword
|
|
@ -1,2 +1,4 @@
|
|||
spring.cloud.config.name=spring-cloud-rest-server
|
||||
spring.cloud.config.uri=http://localhost:8081
|
||||
spring.cloud.config.uri=http://localhost:8081
|
||||
spring.cloud.config.username=configUser
|
||||
spring.cloud.config.password=configPassword
|
Loading…
Reference in New Issue