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
This commit is contained in:
Shubhra Srivastava 2018-05-15 23:50:55 +05:30 committed by maibin
parent 8b324a315b
commit bdb937e68b
3 changed files with 115 additions and 0 deletions

View File

@ -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<String> 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());
}
}

View File

@ -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");
}
}

View File

@ -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));
}
}