[BAEL-13506] - Split or move core-java-8 module (#7790)
This commit is contained in:
committed by
Josh Cummings
parent
7d65b1fb24
commit
f2cac1ab7c
-5
@@ -1,5 +0,0 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
@Deprecated
|
||||
class ClassWithAnnotation {
|
||||
}
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
class ClassWithDeprecatedMethod {
|
||||
|
||||
@Deprecated
|
||||
static void deprecatedMethod() {
|
||||
|
||||
}
|
||||
}
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
class ClassWithSafeVarargs<T> {
|
||||
|
||||
@SafeVarargs
|
||||
final void iterateOverVarargs(T... args) {
|
||||
for (T x : args) {
|
||||
// do stuff with x
|
||||
}
|
||||
}
|
||||
}
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
class ClassWithSuppressWarnings {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
void useDeprecatedMethod() {
|
||||
ClassWithDeprecatedMethod.deprecatedMethod(); // no warning is generated here
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
@FunctionalInterface
|
||||
interface IntConsumer {
|
||||
|
||||
void accept(Integer number);
|
||||
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import java.lang.annotation.Repeatable;
|
||||
|
||||
@Repeatable(Intervals.class)
|
||||
@interface Interval {
|
||||
int hour() default 1;
|
||||
}
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
public class IntervalUsage {
|
||||
|
||||
@Interval(hour = 17)
|
||||
@Interval(hour = 13)
|
||||
void doPeriodicCleanup() {
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
@interface Intervals {
|
||||
Interval[] value();
|
||||
}
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Inherited
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.LOCAL_VARIABLE, ElementType.FIELD})
|
||||
@interface MyAnnotation {
|
||||
|
||||
}
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
class MyAnnotationTarget {
|
||||
|
||||
// this is OK
|
||||
@MyAnnotation
|
||||
String someField;
|
||||
|
||||
// @MyAnnotation <- this is invalid usage!
|
||||
void doSomething() {
|
||||
|
||||
// this also works
|
||||
@MyAnnotation
|
||||
String localVariable;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
interface MyOperation {
|
||||
|
||||
void perform();
|
||||
}
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
class MyOperationImpl implements MyOperation {
|
||||
|
||||
@Override
|
||||
public void perform() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.customannotations;
|
||||
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RUNTIME)
|
||||
@Target(METHOD)
|
||||
public @interface Init {
|
||||
|
||||
}
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.customannotations;
|
||||
|
||||
import static java.lang.annotation.ElementType.FIELD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RUNTIME)
|
||||
@Target({ FIELD })
|
||||
public @interface JsonElement {
|
||||
public String key() default "";
|
||||
}
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.customannotations;
|
||||
|
||||
import static java.lang.annotation.ElementType.TYPE;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RUNTIME)
|
||||
@Target(TYPE)
|
||||
public @interface JsonSerializable {
|
||||
|
||||
}
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
package com.baeldung.customannotations;
|
||||
|
||||
public class JsonSerializationException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public JsonSerializationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
-67
@@ -1,67 +0,0 @@
|
||||
package com.baeldung.customannotations;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ObjectToJsonConverter {
|
||||
public String convertToJson(Object object) throws JsonSerializationException {
|
||||
try {
|
||||
|
||||
checkIfSerializable(object);
|
||||
initializeObject(object);
|
||||
return getJsonString(object);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new JsonSerializationException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void checkIfSerializable(Object object) {
|
||||
if (Objects.isNull(object)) {
|
||||
throw new JsonSerializationException("Can't serialize a null object");
|
||||
}
|
||||
|
||||
Class<?> clazz = object.getClass();
|
||||
if (!clazz.isAnnotationPresent(JsonSerializable.class)) {
|
||||
throw new JsonSerializationException("The class " + clazz.getSimpleName() + " is not annotated with JsonSerializable");
|
||||
}
|
||||
}
|
||||
|
||||
private void initializeObject(Object object) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
||||
Class<?> clazz = object.getClass();
|
||||
for (Method method : clazz.getDeclaredMethods()) {
|
||||
if (method.isAnnotationPresent(Init.class)) {
|
||||
method.setAccessible(true);
|
||||
method.invoke(object);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String getJsonString(Object object) throws IllegalArgumentException, IllegalAccessException {
|
||||
Class<?> clazz = object.getClass();
|
||||
Map<String, String> jsonElementsMap = new HashMap<>();
|
||||
for (Field field : clazz.getDeclaredFields()) {
|
||||
field.setAccessible(true);
|
||||
if (field.isAnnotationPresent(JsonElement.class)) {
|
||||
jsonElementsMap.put(getKey(field), (String) field.get(object));
|
||||
}
|
||||
}
|
||||
|
||||
String jsonString = jsonElementsMap.entrySet()
|
||||
.stream()
|
||||
.map(entry -> "\"" + entry.getKey() + "\":\"" + entry.getValue() + "\"")
|
||||
.collect(Collectors.joining(","));
|
||||
return "{" + jsonString + "}";
|
||||
}
|
||||
|
||||
private String getKey(Field field) {
|
||||
String value = field.getAnnotation(JsonElement.class)
|
||||
.key();
|
||||
return value.isEmpty() ? field.getName() : value;
|
||||
}
|
||||
}
|
||||
-66
@@ -1,66 +0,0 @@
|
||||
package com.baeldung.customannotations;
|
||||
|
||||
@JsonSerializable
|
||||
public class Person {
|
||||
@JsonElement
|
||||
private String firstName;
|
||||
@JsonElement
|
||||
private String lastName;
|
||||
@JsonElement(key = "personAge")
|
||||
private String age;
|
||||
|
||||
private String address;
|
||||
|
||||
public Person(String firstName, String lastName) {
|
||||
super();
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public Person(String firstName, String lastName, String age) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Init
|
||||
private void initNames() {
|
||||
this.firstName = this.firstName.substring(0, 1)
|
||||
.toUpperCase() + this.firstName.substring(1);
|
||||
this.lastName = this.lastName.substring(0, 1)
|
||||
.toUpperCase() + this.lastName.substring(1);
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(String age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
}
|
||||
-18
@@ -1,18 +0,0 @@
|
||||
package com.baeldung.defaultstaticinterfacemethods.application;
|
||||
|
||||
import com.baeldung.defaultstaticinterfacemethods.model.Car;
|
||||
import com.baeldung.defaultstaticinterfacemethods.model.Vehicle;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Vehicle car = new Car("BMW");
|
||||
System.out.println(car.getBrand());
|
||||
System.out.println(car.speedUp());
|
||||
System.out.println(car.slowDown());
|
||||
System.out.println(car.turnAlarmOn());
|
||||
System.out.println(car.turnAlarmOff());
|
||||
System.out.println(Vehicle.getHorsePower(2500, 480));
|
||||
}
|
||||
}
|
||||
-12
@@ -1,12 +0,0 @@
|
||||
package com.baeldung.defaultstaticinterfacemethods.model;
|
||||
|
||||
public interface Alarm {
|
||||
|
||||
default String turnAlarmOn() {
|
||||
return "Turning the alarm on.";
|
||||
}
|
||||
|
||||
default String turnAlarmOff() {
|
||||
return "Turning the alarm off.";
|
||||
}
|
||||
}
|
||||
-25
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.defaultstaticinterfacemethods.model;
|
||||
|
||||
public class Car implements Vehicle {
|
||||
|
||||
private final String brand;
|
||||
|
||||
public Car(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String speedUp() {
|
||||
return "The car is speeding up.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String slowDown() {
|
||||
return "The car is slowing down.";
|
||||
}
|
||||
}
|
||||
-25
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.defaultstaticinterfacemethods.model;
|
||||
|
||||
public class Motorbike implements Vehicle {
|
||||
|
||||
private final String brand;
|
||||
|
||||
public Motorbike(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String speedUp() {
|
||||
return "The motorbike is speeding up.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String slowDown() {
|
||||
return "The motorbike is slowing down.";
|
||||
}
|
||||
}
|
||||
-35
@@ -1,35 +0,0 @@
|
||||
package com.baeldung.defaultstaticinterfacemethods.model;
|
||||
|
||||
public class MultiAlarmCar implements Vehicle, Alarm {
|
||||
|
||||
private final String brand;
|
||||
|
||||
public MultiAlarmCar(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String speedUp() {
|
||||
return "The motorbike is speeding up.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String slowDown() {
|
||||
return "The mootorbike is slowing down.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String turnAlarmOn() {
|
||||
return Vehicle.super.turnAlarmOn() + " " + Alarm.super.turnAlarmOn();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String turnAlarmOff() {
|
||||
return Vehicle.super.turnAlarmOff() + " " + Alarm.super.turnAlarmOff();
|
||||
}
|
||||
}
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
package com.baeldung.defaultstaticinterfacemethods.model;
|
||||
|
||||
public interface Vehicle {
|
||||
|
||||
String getBrand();
|
||||
|
||||
String speedUp();
|
||||
|
||||
String slowDown();
|
||||
|
||||
default String turnAlarmOn() {
|
||||
return "Turning the vehice alarm on.";
|
||||
}
|
||||
|
||||
default String turnAlarmOff() {
|
||||
return "Turning the vehicle alarm off.";
|
||||
}
|
||||
|
||||
static int getHorsePower(int rpm, int torque) {
|
||||
return (rpm * torque) / 5252;
|
||||
}
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
package com.baeldung.doublecolon;
|
||||
|
||||
public class Computer {
|
||||
|
||||
private Integer age;
|
||||
private String color;
|
||||
private Integer healty;
|
||||
|
||||
Computer(final int age, final String color) {
|
||||
this.age = age;
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
Computer(final Integer age, final String color, final Integer healty) {
|
||||
this.age = age;
|
||||
this.color = color;
|
||||
this.healty = healty;
|
||||
}
|
||||
|
||||
public Computer() {
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(final Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(final String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
Integer getHealty() {
|
||||
return healty;
|
||||
}
|
||||
|
||||
void setHealty(final Integer healty) {
|
||||
this.healty = healty;
|
||||
}
|
||||
|
||||
public void turnOnPc() {
|
||||
System.out.println("Computer turned on");
|
||||
}
|
||||
|
||||
public void turnOffPc() {
|
||||
System.out.println("Computer turned off");
|
||||
}
|
||||
|
||||
public Double calculateValue(Double initialValue) {
|
||||
return initialValue / 1.50;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Computer{" + "age=" + age + ", color='" + color + '\'' + ", healty=" + healty + '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final Computer computer = (Computer) o;
|
||||
|
||||
return (age != null ? age.equals(computer.age) : computer.age == null) && (color != null ? color.equals(computer.color) : computer.color == null);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = age != null ? age.hashCode() : 0;
|
||||
result = 31 * result + (color != null ? color.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
-27
@@ -1,27 +0,0 @@
|
||||
package com.baeldung.doublecolon;
|
||||
|
||||
import com.baeldung.doublecolon.function.ComputerPredicate;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ComputerUtils {
|
||||
|
||||
static final ComputerPredicate after2010Predicate = (c) -> (c.getAge() > 2010);
|
||||
static final ComputerPredicate blackPredicate = (c) -> "black".equals(c.getColor());
|
||||
|
||||
public static List<Computer> filter(final List<Computer> inventory, final ComputerPredicate p) {
|
||||
|
||||
final List<Computer> result = new ArrayList<>();
|
||||
inventory.stream().filter(p::filter).forEach(result::add);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void repair(final Computer computer) {
|
||||
if (computer.getHealty() < 50) {
|
||||
computer.setHealty(100);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package com.baeldung.doublecolon;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class MacbookPro extends Computer {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(MacbookPro.class);
|
||||
|
||||
public MacbookPro(int age, String color) {
|
||||
super(age, color);
|
||||
}
|
||||
|
||||
MacbookPro(Integer age, String color, Integer healty) {
|
||||
super(age, color, healty);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void turnOnPc() {
|
||||
LOG.debug("MacbookPro turned on");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void turnOffPc() {
|
||||
LOG.debug("MacbookPro turned off");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double calculateValue(Double initialValue) {
|
||||
|
||||
Function<Double, Double> function = super::calculateValue;
|
||||
final Double pcValue = function.apply(initialValue);
|
||||
LOG.debug("First value is:" + pcValue);
|
||||
return pcValue + (initialValue / 10);
|
||||
|
||||
}
|
||||
}
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
package com.baeldung.doublecolon.function;
|
||||
|
||||
import com.baeldung.doublecolon.Computer;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ComputerPredicate {
|
||||
|
||||
boolean filter(Computer c);
|
||||
|
||||
}
|
||||
-15
@@ -1,15 +0,0 @@
|
||||
package com.baeldung.doublecolon.function;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface TriFunction<A, B, C, R> {
|
||||
|
||||
R apply(A a, B b, C c);
|
||||
|
||||
default <V> TriFunction<A, B, C, V> andThen(final Function<? super R, ? extends V> after) {
|
||||
Objects.requireNonNull(after);
|
||||
return (final A a, final B b, final C c) -> after.apply(apply(a, b, c));
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.optional;
|
||||
|
||||
public class Modem {
|
||||
private Double price;
|
||||
|
||||
public Modem(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
}
|
||||
-34
@@ -1,34 +0,0 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
-37
@@ -1,37 +0,0 @@
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package com.baeldung.optional;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class Person {
|
||||
private String name;
|
||||
private int age;
|
||||
private String password;
|
||||
|
||||
public Person(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Optional<String> getName() {
|
||||
return Optional.ofNullable(name);
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Optional<Integer> getAge() {
|
||||
return Optional.ofNullable(age);
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public Optional<String> getPassword() {
|
||||
return Optional.ofNullable(password);
|
||||
}
|
||||
|
||||
}
|
||||
-23
@@ -1,23 +0,0 @@
|
||||
package com.baeldung.primitive;
|
||||
|
||||
public class BenchmarkRunner {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
new IntPrimitiveLookup().run();
|
||||
new IntegerWrapperLookup().run();
|
||||
new FloatPrimitiveLookup().run();
|
||||
new FloatWrapperLookup().run();
|
||||
new DoublePrimitiveLookup().run();
|
||||
new DoubleWrapperLookup().run();
|
||||
new ShortPrimitiveLookup().run();
|
||||
new ShortWrapperLookup().run();
|
||||
new BooleanPrimitiveLookup().run();
|
||||
new BooleanWrapperLookup().run();
|
||||
new CharPrimitiveLookup().run();
|
||||
new CharacterWrapperLookup().run();
|
||||
new BytePrimitiveLookup().run();
|
||||
new ByteWrapperLookup().run();
|
||||
new LongPrimitiveLookup().run();
|
||||
new LongWrapperLookup().run();
|
||||
}
|
||||
}
|
||||
-45
@@ -1,45 +0,0 @@
|
||||
package com.baeldung.primitive;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
@State(Scope.Thread)
|
||||
public class BooleanPrimitiveLookup extends Lookup {
|
||||
|
||||
private boolean[] elements;
|
||||
private final boolean pivot = false;
|
||||
|
||||
@Setup
|
||||
@Override
|
||||
public void prepare() {
|
||||
elements = new boolean[s];
|
||||
for (int i = 0; i < s - 1; i++) {
|
||||
elements[i] = true;
|
||||
}
|
||||
elements[s - 1] = pivot;
|
||||
}
|
||||
|
||||
@TearDown
|
||||
@Override
|
||||
public void clean() {
|
||||
elements = null;
|
||||
}
|
||||
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public int findPosition() {
|
||||
int index = 0;
|
||||
while (pivot != elements[index]) {
|
||||
index++;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSimpleClassName() {
|
||||
return BooleanPrimitiveLookup.class.getSimpleName();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
-45
@@ -1,45 +0,0 @@
|
||||
package com.baeldung.primitive;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
@State(Scope.Thread)
|
||||
public class BooleanWrapperLookup extends Lookup {
|
||||
private Boolean[] elements;
|
||||
private final boolean pivot = false;
|
||||
|
||||
@Override
|
||||
@Setup
|
||||
public void prepare() {
|
||||
elements = new Boolean[s];
|
||||
for (int i = 0; i < s - 1; i++) {
|
||||
elements[i] = true;
|
||||
}
|
||||
elements[s - 1] = pivot;
|
||||
}
|
||||
|
||||
@Override
|
||||
@TearDown
|
||||
public void clean() {
|
||||
elements = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public int findPosition() {
|
||||
int index = 0;
|
||||
Boolean pivotWrapper = pivot;
|
||||
while (!pivotWrapper.equals(elements[index])) {
|
||||
index++;
|
||||
}
|
||||
return index;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSimpleClassName() {
|
||||
return BooleanWrapperLookup.class.getSimpleName();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
-46
@@ -1,46 +0,0 @@
|
||||
package com.baeldung.primitive;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
@State(Scope.Thread)
|
||||
public class BytePrimitiveLookup extends Lookup {
|
||||
|
||||
private byte[] elements;
|
||||
private final byte pivot = 2;
|
||||
|
||||
@Setup
|
||||
@Override
|
||||
public void prepare() {
|
||||
byte common = 1;
|
||||
elements = new byte[s];
|
||||
for (int i = 0; i < s - 1; i++) {
|
||||
elements[i] = common;
|
||||
}
|
||||
elements[s - 1] = pivot;
|
||||
}
|
||||
|
||||
@TearDown
|
||||
@Override
|
||||
public void clean() {
|
||||
elements = null;
|
||||
}
|
||||
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public int findPosition() {
|
||||
int index = 0;
|
||||
while (pivot != elements[index]) {
|
||||
index++;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSimpleClassName() {
|
||||
return BytePrimitiveLookup.class.getSimpleName();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
-46
@@ -1,46 +0,0 @@
|
||||
package com.baeldung.primitive;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
@State(Scope.Thread)
|
||||
public class ByteWrapperLookup extends Lookup {
|
||||
private Byte[] elements;
|
||||
private final byte pivot = 2;
|
||||
|
||||
@Override
|
||||
@Setup
|
||||
public void prepare() {
|
||||
byte common = 1;
|
||||
elements = new Byte[s];
|
||||
for (int i = 0; i < s - 1; i++) {
|
||||
elements[i] = common;
|
||||
}
|
||||
elements[s - 1] = pivot;
|
||||
}
|
||||
|
||||
@Override
|
||||
@TearDown
|
||||
public void clean() {
|
||||
elements = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public int findPosition() {
|
||||
int index = 0;
|
||||
Byte pivotWrapper = pivot;
|
||||
while (!pivotWrapper.equals(elements[index])) {
|
||||
index++;
|
||||
}
|
||||
return index;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSimpleClassName() {
|
||||
return ByteWrapperLookup.class.getSimpleName();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
-46
@@ -1,46 +0,0 @@
|
||||
package com.baeldung.primitive;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
@State(Scope.Thread)
|
||||
public class CharPrimitiveLookup extends Lookup {
|
||||
|
||||
private char[] elements;
|
||||
private final char pivot = 'b';
|
||||
|
||||
@Setup
|
||||
@Override
|
||||
public void prepare() {
|
||||
char common = 'a';
|
||||
elements = new char[s];
|
||||
for (int i = 0; i < s - 1; i++) {
|
||||
elements[i] = common;
|
||||
}
|
||||
elements[s - 1] = pivot;
|
||||
}
|
||||
|
||||
@TearDown
|
||||
@Override
|
||||
public void clean() {
|
||||
elements = null;
|
||||
}
|
||||
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public int findPosition() {
|
||||
int index = 0;
|
||||
while (pivot != elements[index]) {
|
||||
index++;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSimpleClassName() {
|
||||
return CharPrimitiveLookup.class.getSimpleName();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
-46
@@ -1,46 +0,0 @@
|
||||
package com.baeldung.primitive;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
@State(Scope.Thread)
|
||||
public class CharacterWrapperLookup extends Lookup {
|
||||
private Character[] elements;
|
||||
private final char pivot = 'b';
|
||||
|
||||
@Override
|
||||
@Setup
|
||||
public void prepare() {
|
||||
char common = 'a';
|
||||
elements = new Character[s];
|
||||
for (int i = 0; i < s - 1; i++) {
|
||||
elements[i] = common;
|
||||
}
|
||||
elements[s - 1] = pivot;
|
||||
}
|
||||
|
||||
@Override
|
||||
@TearDown
|
||||
public void clean() {
|
||||
elements = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public int findPosition() {
|
||||
int index = 0;
|
||||
Character pivotWrapper = pivot;
|
||||
while (!pivotWrapper.equals(elements[index])) {
|
||||
index++;
|
||||
}
|
||||
return index;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSimpleClassName() {
|
||||
return CharacterWrapperLookup.class.getSimpleName();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
-42
@@ -1,42 +0,0 @@
|
||||
package com.baeldung.primitive;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
@State(Scope.Thread)
|
||||
public class DoublePrimitiveLookup extends Lookup {
|
||||
private double[] elements;
|
||||
private final double pivot = 2;
|
||||
|
||||
@Setup
|
||||
@Override
|
||||
public void prepare() {
|
||||
double common = 1;
|
||||
elements = new double[s];
|
||||
for (int i = 0; i < s - 1; i++) {
|
||||
elements[i] = common;
|
||||
}
|
||||
elements[s - 1] = pivot;
|
||||
}
|
||||
|
||||
@TearDown
|
||||
@Override
|
||||
public void clean() {
|
||||
elements = null;
|
||||
}
|
||||
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public int findPosition() {
|
||||
int index = 0;
|
||||
while (pivot != elements[index]) {
|
||||
index++;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSimpleClassName() {
|
||||
return DoublePrimitiveLookup.class.getSimpleName();
|
||||
}
|
||||
}
|
||||
-45
@@ -1,45 +0,0 @@
|
||||
package com.baeldung.primitive;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
@State(Scope.Thread)
|
||||
public class DoubleWrapperLookup extends Lookup {
|
||||
private Double[] elements;
|
||||
private final double pivot = 2d;
|
||||
|
||||
@Override
|
||||
@Setup
|
||||
public void prepare() {
|
||||
double common = 1;
|
||||
elements = new Double[s];
|
||||
for (int i = 0; i < s - 1; i++) {
|
||||
elements[i] = common;
|
||||
}
|
||||
elements[s - 1] = pivot;
|
||||
}
|
||||
|
||||
@Override
|
||||
@TearDown
|
||||
public void clean() {
|
||||
elements = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public int findPosition() {
|
||||
int index = 0;
|
||||
Double pivotWrapper = pivot;
|
||||
while (!pivotWrapper.equals(elements[index])) {
|
||||
index++;
|
||||
}
|
||||
return index;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSimpleClassName() {
|
||||
return DoubleWrapperLookup.class.getSimpleName();
|
||||
}
|
||||
|
||||
}
|
||||
-42
@@ -1,42 +0,0 @@
|
||||
package com.baeldung.primitive;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
@State(Scope.Thread)
|
||||
public class FloatPrimitiveLookup extends Lookup {
|
||||
private float[] elements;
|
||||
private final float pivot = 2;
|
||||
|
||||
@Setup
|
||||
@Override
|
||||
public void prepare() {
|
||||
int common = 1;
|
||||
elements = new float[s];
|
||||
for (int i = 0; i < s - 1; i++) {
|
||||
elements[i] = common;
|
||||
}
|
||||
elements[s - 1] = pivot;
|
||||
}
|
||||
|
||||
@TearDown
|
||||
@Override
|
||||
public void clean() {
|
||||
elements = null;
|
||||
}
|
||||
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public int findPosition() {
|
||||
int index = 0;
|
||||
while (pivot != elements[index]) {
|
||||
index++;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSimpleClassName() {
|
||||
return FloatPrimitiveLookup.class.getSimpleName();
|
||||
}
|
||||
}
|
||||
-43
@@ -1,43 +0,0 @@
|
||||
package com.baeldung.primitive;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
@State(Scope.Thread)
|
||||
public class FloatWrapperLookup extends Lookup {
|
||||
private Float[] elements;
|
||||
private final float pivot = 2;
|
||||
|
||||
@Override
|
||||
@Setup
|
||||
public void prepare() {
|
||||
float common = 1;
|
||||
elements = new Float[s];
|
||||
for (int i = 0; i < s - 1; i++) {
|
||||
elements[i] = common;
|
||||
}
|
||||
elements[s - 1] = pivot;
|
||||
}
|
||||
|
||||
@Override
|
||||
@TearDown
|
||||
public void clean() {
|
||||
elements = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public int findPosition() {
|
||||
int index = 0;
|
||||
Float pivotWrapper = pivot;
|
||||
while (!pivotWrapper.equals(elements[index])) {
|
||||
index++;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSimpleClassName() {
|
||||
return FloatWrapperLookup.class.getSimpleName();
|
||||
}
|
||||
}
|
||||
-45
@@ -1,45 +0,0 @@
|
||||
package com.baeldung.primitive;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
@State(Scope.Thread)
|
||||
public class IntPrimitiveLookup extends Lookup {
|
||||
|
||||
private int[] elements;
|
||||
private final int pivot = 2;
|
||||
|
||||
@Setup
|
||||
@Override
|
||||
public void prepare() {
|
||||
int common = 1;
|
||||
elements = new int[s];
|
||||
for (int i = 0; i < s - 1; i++) {
|
||||
elements[i] = common;
|
||||
}
|
||||
elements[s - 1] = pivot;
|
||||
}
|
||||
|
||||
@TearDown
|
||||
@Override
|
||||
public void clean() {
|
||||
elements = null;
|
||||
}
|
||||
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public int findPosition() {
|
||||
int index = 0;
|
||||
while (pivot != elements[index]) {
|
||||
index++;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSimpleClassName() {
|
||||
return IntPrimitiveLookup.class.getSimpleName();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
-45
@@ -1,45 +0,0 @@
|
||||
package com.baeldung.primitive;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
@State(Scope.Thread)
|
||||
public class IntegerWrapperLookup extends Lookup {
|
||||
private Integer[] elements;
|
||||
private final int pivot = 2;
|
||||
|
||||
@Override
|
||||
@Setup
|
||||
public void prepare() {
|
||||
int common = 1;
|
||||
elements = new Integer[s];
|
||||
for (int i = 0; i < s - 1; i++) {
|
||||
elements[i] = common;
|
||||
}
|
||||
elements[s - 1] = pivot;
|
||||
}
|
||||
|
||||
@Override
|
||||
@TearDown
|
||||
public void clean() {
|
||||
elements = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public int findPosition() {
|
||||
int index = 0;
|
||||
Integer pivotWrapper = pivot;
|
||||
while (!pivotWrapper.equals(elements[index])) {
|
||||
index++;
|
||||
}
|
||||
return index;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSimpleClassName() {
|
||||
return IntegerWrapperLookup.class.getSimpleName();
|
||||
}
|
||||
|
||||
}
|
||||
-42
@@ -1,42 +0,0 @@
|
||||
package com.baeldung.primitive;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
@State(Scope.Thread)
|
||||
public class LongPrimitiveLookup extends Lookup {
|
||||
private long[] elements;
|
||||
private final long pivot = 2;
|
||||
|
||||
@Setup
|
||||
@Override
|
||||
public void prepare() {
|
||||
long common = 1;
|
||||
elements = new long[s];
|
||||
for (int i = 0; i < s - 1; i++) {
|
||||
elements[i] = common;
|
||||
}
|
||||
elements[s - 1] = pivot;
|
||||
}
|
||||
|
||||
@TearDown
|
||||
@Override
|
||||
public void clean() {
|
||||
elements = null;
|
||||
}
|
||||
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public int findPosition() {
|
||||
int index = 0;
|
||||
while (pivot != elements[index]) {
|
||||
index++;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSimpleClassName() {
|
||||
return LongPrimitiveLookup.class.getSimpleName();
|
||||
}
|
||||
}
|
||||
-45
@@ -1,45 +0,0 @@
|
||||
package com.baeldung.primitive;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
@State(Scope.Thread)
|
||||
public class LongWrapperLookup extends Lookup{
|
||||
private Long[] elements;
|
||||
private final long pivot = 2;
|
||||
|
||||
@Override
|
||||
@Setup
|
||||
public void prepare() {
|
||||
long common = 1;
|
||||
elements = new Long[s];
|
||||
for (int i = 0; i < s - 1; i++) {
|
||||
elements[i] = common;
|
||||
}
|
||||
elements[s - 1] = pivot;
|
||||
}
|
||||
|
||||
@Override
|
||||
@TearDown
|
||||
public void clean() {
|
||||
elements = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public int findPosition() {
|
||||
int index = 0;
|
||||
Long pivotWrapper = pivot;
|
||||
while (!pivotWrapper.equals(elements[index])) {
|
||||
index++;
|
||||
}
|
||||
return index;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSimpleClassName() {
|
||||
return LongWrapperLookup.class.getSimpleName();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package com.baeldung.primitive;
|
||||
|
||||
import org.openjdk.jmh.results.RunResult;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.RunnerException;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* An abstract class that is to be extended by the classes that
|
||||
* perform lookup in the arrays either of Java primitive types or their wrappers.
|
||||
*/
|
||||
public abstract class Lookup {
|
||||
/**
|
||||
* the array size
|
||||
*/
|
||||
final protected int s = 50000000;
|
||||
|
||||
/**
|
||||
* Initialize the array: fill in the array with the same
|
||||
* elements except for the last one.
|
||||
*/
|
||||
abstract public void prepare();
|
||||
|
||||
/**
|
||||
* Free the array's reference.
|
||||
*/
|
||||
abstract public void clean();
|
||||
|
||||
/**
|
||||
* Find the position of the element that is different from the others.
|
||||
* By construction, it is the last array element.
|
||||
*
|
||||
* @return array's last element index
|
||||
*/
|
||||
abstract public int findPosition();
|
||||
|
||||
/**
|
||||
* Get the name of the class that extends this one. It is needed in order
|
||||
* to set up the benchmark.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
abstract public String getSimpleClassName();
|
||||
|
||||
Collection<RunResult> run() throws RunnerException {
|
||||
Options opt = new OptionsBuilder()
|
||||
.include(getSimpleClassName())
|
||||
.forks(1)
|
||||
.build();
|
||||
return new Runner(opt).run();
|
||||
}
|
||||
|
||||
}
|
||||
-45
@@ -1,45 +0,0 @@
|
||||
package com.baeldung.primitive;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
@State(Scope.Thread)
|
||||
public class ShortPrimitiveLookup extends Lookup {
|
||||
|
||||
private short[] elements;
|
||||
private final short pivot = 2;
|
||||
|
||||
@Setup
|
||||
@Override
|
||||
public void prepare() {
|
||||
short common = 1;
|
||||
elements = new short[s];
|
||||
for (int i = 0; i < s - 1; i++) {
|
||||
elements[i] = common;
|
||||
}
|
||||
elements[s - 1] = pivot;
|
||||
}
|
||||
|
||||
@TearDown
|
||||
@Override
|
||||
public void clean() {
|
||||
elements = null;
|
||||
}
|
||||
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public int findPosition() {
|
||||
int index = 0;
|
||||
while (pivot != elements[index]) {
|
||||
index++;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSimpleClassName() {
|
||||
return ShortPrimitiveLookup.class.getSimpleName();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
-46
@@ -1,46 +0,0 @@
|
||||
package com.baeldung.primitive;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
@State(Scope.Thread)
|
||||
public class ShortWrapperLookup extends Lookup {
|
||||
private Short[] elements;
|
||||
private final short pivot = 2;
|
||||
|
||||
@Override
|
||||
@Setup
|
||||
public void prepare() {
|
||||
short common = 1;
|
||||
elements = new Short[s];
|
||||
for (int i = 0; i < s - 1; i++) {
|
||||
elements[i] = common;
|
||||
}
|
||||
elements[s - 1] = pivot;
|
||||
}
|
||||
|
||||
@Override
|
||||
@TearDown
|
||||
public void clean() {
|
||||
elements = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public int findPosition() {
|
||||
int index = 0;
|
||||
Short pivotWrapper = pivot;
|
||||
while (!pivotWrapper.equals(elements[index])) {
|
||||
index++;
|
||||
}
|
||||
return index;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSimpleClassName() {
|
||||
return ShortWrapperLookup.class.getSimpleName();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.reducingIfElse;
|
||||
|
||||
public class AddCommand implements Command {
|
||||
|
||||
private int a;
|
||||
private int b;
|
||||
|
||||
public AddCommand(int a, int b) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer execute() {
|
||||
return a + b;
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.reducingIfElse;
|
||||
|
||||
public class AddRule implements Rule {
|
||||
|
||||
private int result;
|
||||
|
||||
@Override
|
||||
public boolean evaluate(Expression expression) {
|
||||
boolean evalResult = false;
|
||||
if (expression.getOperator() == Operator.ADD) {
|
||||
this.result = expression.getX() + expression.getY();
|
||||
evalResult = true;
|
||||
}
|
||||
return evalResult;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result getResult() {
|
||||
return new Result(result);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.reducingIfElse;
|
||||
|
||||
public class Addition implements Operation {
|
||||
@Override
|
||||
public int apply(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
}
|
||||
-83
@@ -1,83 +0,0 @@
|
||||
package com.baeldung.reducingIfElse;
|
||||
|
||||
public class Calculator {
|
||||
|
||||
public int calculate(int a, int b, String operator) {
|
||||
int result = Integer.MIN_VALUE;
|
||||
|
||||
if ("add".equals(operator)) {
|
||||
result = a + b;
|
||||
} else if ("multiply".equals(operator)) {
|
||||
result = a * b;
|
||||
} else if ("divide".equals(operator)) {
|
||||
result = a / b;
|
||||
} else if ("subtract".equals(operator)) {
|
||||
result = a - b;
|
||||
} else if ("modulo".equals(operator)) {
|
||||
result = a % b;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public int calculateUsingSwitch(int a, int b, String operator) {
|
||||
int result = 0;
|
||||
switch (operator) {
|
||||
case "add":
|
||||
result = a + b;
|
||||
break;
|
||||
case "multiply":
|
||||
result = a * b;
|
||||
break;
|
||||
case "divide":
|
||||
result = a / b;
|
||||
break;
|
||||
case "subtract":
|
||||
result = a - b;
|
||||
break;
|
||||
case "modulo":
|
||||
result = a % b;
|
||||
break;
|
||||
default:
|
||||
result = Integer.MIN_VALUE;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public int calculateUsingSwitch(int a, int b, Operator operator) {
|
||||
int result = 0;
|
||||
switch (operator) {
|
||||
case ADD:
|
||||
result = a + b;
|
||||
break;
|
||||
case MULTIPLY:
|
||||
result = a * b;
|
||||
break;
|
||||
case DIVIDE:
|
||||
result = a / b;
|
||||
break;
|
||||
case SUBTRACT:
|
||||
result = a - b;
|
||||
break;
|
||||
case MODULO:
|
||||
result = a % b;
|
||||
break;
|
||||
default:
|
||||
result = Integer.MIN_VALUE;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public int calculate(int a, int b, Operator operator) {
|
||||
return operator.apply(a, b);
|
||||
}
|
||||
|
||||
public int calculateUsingFactory(int a, int b, String operation) {
|
||||
Operation targetOperation = OperatorFactory.getOperation(operation)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Invalid Operator"));
|
||||
return targetOperation.apply(a, b);
|
||||
}
|
||||
|
||||
public int calculate(Command command) {
|
||||
return command.execute();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.baeldung.reducingIfElse;
|
||||
|
||||
public interface Command {
|
||||
Integer execute();
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.baeldung.reducingIfElse;
|
||||
|
||||
public class Division implements Operation {
|
||||
@Override public int apply(int a, int b) {
|
||||
return a / b;
|
||||
}
|
||||
}
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
package com.baeldung.reducingIfElse;
|
||||
|
||||
public class Expression {
|
||||
|
||||
private Integer x;
|
||||
private Integer y;
|
||||
private Operator operator;
|
||||
|
||||
public Expression(Integer x, Integer y, Operator operator) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.operator = operator;
|
||||
}
|
||||
|
||||
public Integer getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public Integer getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public Operator getOperator() {
|
||||
return operator;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.baeldung.reducingIfElse;
|
||||
|
||||
public class Modulo implements Operation {
|
||||
@Override public int apply(int a, int b) {
|
||||
return a % b;
|
||||
}
|
||||
}
|
||||
-7
@@ -1,7 +0,0 @@
|
||||
package com.baeldung.reducingIfElse;
|
||||
|
||||
public class Multiplication implements Operation {
|
||||
@Override public int apply(int a, int b) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.baeldung.reducingIfElse;
|
||||
|
||||
public interface Operation {
|
||||
int apply(int a, int b);
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package com.baeldung.reducingIfElse;
|
||||
|
||||
public enum Operator {
|
||||
|
||||
ADD {
|
||||
@Override
|
||||
public int apply(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
},
|
||||
|
||||
MULTIPLY {
|
||||
@Override
|
||||
public int apply(int a, int b) {
|
||||
return a * b;
|
||||
}
|
||||
},
|
||||
|
||||
SUBTRACT {
|
||||
@Override
|
||||
public int apply(int a, int b) {
|
||||
return a - b;
|
||||
}
|
||||
},
|
||||
|
||||
DIVIDE {
|
||||
@Override
|
||||
public int apply(int a, int b) {
|
||||
return a / b;
|
||||
}
|
||||
},
|
||||
|
||||
MODULO {
|
||||
@Override
|
||||
public int apply(int a, int b) {
|
||||
return a % b;
|
||||
}
|
||||
};
|
||||
|
||||
public abstract int apply(int a, int b);
|
||||
}
|
||||
-21
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.reducingIfElse;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class OperatorFactory {
|
||||
|
||||
static Map<String, Operation> operationMap = new HashMap<>();
|
||||
static {
|
||||
operationMap.put("add", new Addition());
|
||||
operationMap.put("divide", new Division());
|
||||
operationMap.put("multiply", new Multiplication());
|
||||
operationMap.put("subtract", new Subtraction());
|
||||
operationMap.put("modulo", new Modulo());
|
||||
}
|
||||
|
||||
public static Optional<Operation> getOperation(String operation) {
|
||||
return Optional.ofNullable(operationMap.get(operation));
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.reducingIfElse;
|
||||
|
||||
public class Result {
|
||||
int value;
|
||||
|
||||
public Result(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.reducingIfElse;
|
||||
|
||||
public interface Rule {
|
||||
|
||||
boolean evaluate(Expression expression);
|
||||
|
||||
Result getResult();
|
||||
}
|
||||
-24
@@ -1,24 +0,0 @@
|
||||
package com.baeldung.reducingIfElse;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class RuleEngine {
|
||||
|
||||
private static List<Rule> rules = new ArrayList<>();
|
||||
|
||||
static {
|
||||
rules.add(new AddRule());
|
||||
}
|
||||
|
||||
public Result process(Expression expression) {
|
||||
|
||||
Rule rule = rules.stream()
|
||||
.filter(r -> r.evaluate(expression))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new IllegalArgumentException("Expression does not matches any Rule"));
|
||||
return rule.getResult();
|
||||
}
|
||||
}
|
||||
-7
@@ -1,7 +0,0 @@
|
||||
package com.baeldung.reducingIfElse;
|
||||
|
||||
public class Subtraction implements Operation {
|
||||
@Override public int apply(int a, int b) {
|
||||
return a - b;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user