From bdb937e68b2cf4a18776caea2be2e2ff2ee1f69f Mon Sep 17 00:00:00 2001 From: Shubhra Srivastava Date: Tue, 15 May 2018 23:50:55 +0530 Subject: [PATCH] BAEL 1748 - Optional - orElse() vs orElseGet() (#4240) * Types of Bean Injection in Spring * Changing config file name * BAEL-1584 : Find an element in list * Revert "Changing config file name" This reverts commit d857db9f65b1cf89773348e3901385ce59d9e1f8. * Revert "Types of Bean Injection in Spring" This reverts commit e9efcb8e70f37e7488aa2371bb3ee62c676996f4. * BAEL-1584 : Find an Element in Given List * BAEL-1584 : Hashcode impl changed * BAEL:1584 : ListIterator to Iterator change * Method name refactoring * BAEL 1748 - Optional OrElse vs OrElseGet * BAEL-1748 Benchmark Runner --- .../baeldung/optional/OrElseAndOrElseGet.java | 34 ++++++++++++++ .../OrElseAndOrElseGetBenchmarkRunner.java | 37 ++++++++++++++++ .../optional/OrElseAndOrElseGetTest.java | 44 +++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 core-java-8/src/main/java/com/baeldung/optional/OrElseAndOrElseGet.java create mode 100644 core-java-8/src/main/java/com/baeldung/optional/OrElseAndOrElseGetBenchmarkRunner.java create mode 100644 core-java-8/src/test/java/com/baeldung/java8/optional/OrElseAndOrElseGetTest.java diff --git a/core-java-8/src/main/java/com/baeldung/optional/OrElseAndOrElseGet.java b/core-java-8/src/main/java/com/baeldung/optional/OrElseAndOrElseGet.java new file mode 100644 index 0000000000..d4d07291d5 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/optional/OrElseAndOrElseGet.java @@ -0,0 +1,34 @@ +package com.baeldung.optional; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.Random; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class OrElseAndOrElseGet { + + public static List names = Arrays.asList("John", "Jones", "Kelly", "Cristina", "Raven"); + + private static final Logger LOG = LoggerFactory.getLogger(OrElseAndOrElseGet.class); + + public String getRandomName() { + LOG.info("getRandomName() method - start"); + Random random = new Random(); + int index = random.nextInt(5); + LOG.info("getRandomName() method - end"); + return names.get(index); + } + + public String getNameUsingOrElse(String name) { + return Optional.ofNullable(name) + .orElse(getRandomName()); + } + + public String getNameUsingOrElseGet(String name) { + return Optional.ofNullable(name) + .orElseGet(() -> getRandomName()); + } +} diff --git a/core-java-8/src/main/java/com/baeldung/optional/OrElseAndOrElseGetBenchmarkRunner.java b/core-java-8/src/main/java/com/baeldung/optional/OrElseAndOrElseGetBenchmarkRunner.java new file mode 100644 index 0000000000..8bf0b0b25d --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/optional/OrElseAndOrElseGetBenchmarkRunner.java @@ -0,0 +1,37 @@ +package com.baeldung.optional; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.runner.RunnerException; + +@Fork(1) +@State(Scope.Benchmark) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +public class OrElseAndOrElseGetBenchmarkRunner { + + private OrElseAndOrElseGet orElsevsOrElseGet = new OrElseAndOrElseGet(); + + public static void main(String[] args) throws RunnerException, IOException { + org.openjdk.jmh.Main.main(args); + } + + @Benchmark + public String orElseBenchmark() { + return orElsevsOrElseGet.getNameUsingOrElse("baeldung"); + } + + @Benchmark + public String orElseGetBenchmark() { + return orElsevsOrElseGet.getNameUsingOrElseGet("baeldung"); + } + +} diff --git a/core-java-8/src/test/java/com/baeldung/java8/optional/OrElseAndOrElseGetTest.java b/core-java-8/src/test/java/com/baeldung/java8/optional/OrElseAndOrElseGetTest.java new file mode 100644 index 0000000000..2519a77f1f --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/java8/optional/OrElseAndOrElseGetTest.java @@ -0,0 +1,44 @@ +package com.baeldung.java8.optional; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.junit.Assert.*; + +import com.baeldung.optional.OrElseAndOrElseGet; + +public class OrElseAndOrElseGetTest { + + private OrElseAndOrElseGet orElsevsOrElseGet = new OrElseAndOrElseGet(); + + private static final Logger LOG = LoggerFactory.getLogger(OrElseAndOrElseGetTest.class); + + @Test + public void givenNonEmptyOptional_whenOrElseUsed_thenGivenStringReturned() { + LOG.info("In givenNonEmptyOptional_whenOrElseUsed_thenGivenStringReturned()"); + String name = orElsevsOrElseGet.getNameUsingOrElse("baeldung"); + assertEquals(name, "baeldung"); + } + + @Test + public void givenEmptyOptional_whenOrElseUsed_thenRandomStringReturned() { + LOG.info("In givenEmptyOptional_whenOrElseUsed_thenRandomStringReturned()"); + String name = orElsevsOrElseGet.getNameUsingOrElse(null); + assertTrue(orElsevsOrElseGet.names.contains(name)); + } + + @Test + public void givenNonEmptyOptional_whenOrElseGetUsed_thenGivenStringReturned() { + LOG.info("In givenNonEmptyOptional_whenOrElseGetUsed_thenGivenStringReturned()"); + String name = orElsevsOrElseGet.getNameUsingOrElseGet("baeldung"); + assertEquals(name, "baeldung"); + } + + @Test + public void givenEmptyOptional_whenOrElseGetUsed_thenRandomStringReturned() { + LOG.info("In givenEmptyOptional_whenOrElseGetUsed_thenRandomStringReturned()"); + String name = orElsevsOrElseGet.getNameUsingOrElseGet(null); + assertTrue(orElsevsOrElseGet.names.contains(name)); + } +}