[BAEL-13506] - Split or move core-java-8 module (#7790)
This commit is contained in:
committed by
Josh Cummings
parent
7d65b1fb24
commit
f2cac1ab7c
@@ -6,3 +6,4 @@
|
||||
- [Lambda Expressions and Functional Interfaces: Tips and Best Practices](http://www.baeldung.com/java-8-lambda-expressions-tips)
|
||||
- [Exceptions in Java 8 Lambda Expressions](http://www.baeldung.com/java-lambda-exceptions)
|
||||
- [Method References in Java](https://www.baeldung.com/java-method-references)
|
||||
- [The Double Colon Operator in Java 8](https://www.baeldung.com/java-8-double-colon-operator)
|
||||
@@ -0,0 +1,85 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.doublecolon;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.doublecolon.function.ComputerPredicate;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.doublecolon.function;
|
||||
|
||||
import com.baeldung.doublecolon.Computer;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ComputerPredicate {
|
||||
|
||||
boolean filter(Computer c);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.baeldung.doublecolon;
|
||||
|
||||
import com.baeldung.doublecolon.function.TriFunction;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
import static com.baeldung.doublecolon.ComputerUtils.*;
|
||||
|
||||
public class ComputerUtilsUnitTest {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorReference() {
|
||||
|
||||
Computer c1 = new Computer(2015, "white");
|
||||
Computer c2 = new Computer(2009, "black");
|
||||
Computer c3 = new Computer(2014, "black");
|
||||
|
||||
BiFunction<Integer, String, Computer> c4Function = Computer::new;
|
||||
Computer c4 = c4Function.apply(2013, "white");
|
||||
BiFunction<Integer, String, Computer> c5Function = Computer::new;
|
||||
Computer c5 = c5Function.apply(2010, "black");
|
||||
BiFunction<Integer, String, Computer> c6Function = Computer::new;
|
||||
Computer c6 = c6Function.apply(2008, "black");
|
||||
|
||||
List<Computer> inventory = Arrays.asList(c1, c2, c3, c4, c5, c6);
|
||||
|
||||
List<Computer> blackComputer = filter(inventory, blackPredicate);
|
||||
Assert.assertEquals("The black Computers are: ", blackComputer.size(), 4);
|
||||
|
||||
List<Computer> after2010Computer = filter(inventory, after2010Predicate);
|
||||
Assert.assertEquals("The Computer bought after 2010 are: ", after2010Computer.size(), 3);
|
||||
|
||||
List<Computer> before2011Computer = filter(inventory, c -> c.getAge() < 2011);
|
||||
Assert.assertEquals("The Computer bought before 2011 are: ", before2011Computer.size(), 3);
|
||||
|
||||
inventory.sort(Comparator.comparing(Computer::getAge));
|
||||
|
||||
Assert.assertEquals("Oldest Computer in inventory", c6, inventory.get(0));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStaticMethodReference() {
|
||||
|
||||
Computer c1 = new Computer(2015, "white", 35);
|
||||
Computer c2 = new Computer(2009, "black", 65);
|
||||
TriFunction<Integer, String, Integer, Computer> c6Function = Computer::new;
|
||||
Computer c3 = c6Function.apply(2008, "black", 90);
|
||||
|
||||
List<Computer> inventory = Arrays.asList(c1, c2, c3);
|
||||
inventory.forEach(ComputerUtils::repair);
|
||||
|
||||
Assert.assertEquals("Computer repaired", new Integer(100), c1.getHealty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInstanceMethodArbitraryObjectParticularType() {
|
||||
|
||||
Computer c1 = new Computer(2015, "white", 35);
|
||||
Computer c2 = new MacbookPro(2009, "black", 65);
|
||||
List<Computer> inventory = Arrays.asList(c1, c2);
|
||||
inventory.forEach(Computer::turnOnPc);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuperMethodReference() {
|
||||
|
||||
final TriFunction<Integer, String, Integer, MacbookPro> integerStringIntegerObjectTriFunction = MacbookPro::new;
|
||||
final MacbookPro macbookPro = integerStringIntegerObjectTriFunction.apply(2010, "black", 100);
|
||||
Double initialValue = 999.99;
|
||||
final Double actualValue = macbookPro.calculateValue(initialValue);
|
||||
Assert.assertEquals(766.659, actualValue, 0.0);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user