From e37f3aebc386a911ffa2ef3a6dd00abc8fd698ae Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Sun, 24 Jan 2016 18:26:44 +0100 Subject: [PATCH] Examples of Method Reference and Constructor Reference --- .../com/baeldung/doublecolumn/Computer.java | 75 +++++++++++++++++++ .../baeldung/doublecolumn/ComputerUtils.java | 30 ++++++++ .../function/ComputerPredicate.java | 11 +++ .../doublecolumn/function/TriFunction.java | 16 ++++ .../doublecolumn/TestComputerUtils.java | 70 +++++++++++++++++ 5 files changed, 202 insertions(+) create mode 100644 core-java-8/src/main/java/com/baeldung/doublecolumn/Computer.java create mode 100644 core-java-8/src/main/java/com/baeldung/doublecolumn/ComputerUtils.java create mode 100644 core-java-8/src/main/java/com/baeldung/doublecolumn/function/ComputerPredicate.java create mode 100644 core-java-8/src/main/java/com/baeldung/doublecolumn/function/TriFunction.java create mode 100644 core-java-8/src/test/java/com/baeldung/doublecolumn/TestComputerUtils.java diff --git a/core-java-8/src/main/java/com/baeldung/doublecolumn/Computer.java b/core-java-8/src/main/java/com/baeldung/doublecolumn/Computer.java new file mode 100644 index 0000000000..d2a72ca04d --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/doublecolumn/Computer.java @@ -0,0 +1,75 @@ +package com.baeldung.doublecolumn; + + +public class Computer { + + private Integer age; + private String color; + private Integer healty; + + public Computer(int age, String color) { + this.age = age; + this.color = color; + } + + public Computer(Integer age, String color, Integer healty) { + this.age = age; + this.color = color; + this.healty = healty; + } + + public Computer() { + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public Integer getHealty() { + return healty; + } + + public void setHealty(Integer healty) { + this.healty = healty; + } + + @Override + public String toString() { + return "Computer{" + + "age=" + age + + ", color='" + color + '\'' + + ", healty=" + healty + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Computer computer = (Computer) o; + + if (age != null ? !age.equals(computer.age) : computer.age != null) return false; + return 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; + } +} diff --git a/core-java-8/src/main/java/com/baeldung/doublecolumn/ComputerUtils.java b/core-java-8/src/main/java/com/baeldung/doublecolumn/ComputerUtils.java new file mode 100644 index 0000000000..eac4db7577 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/doublecolumn/ComputerUtils.java @@ -0,0 +1,30 @@ +package com.baeldung.doublecolumn; + +import com.baeldung.doublecolumn.function.ComputerPredicate; + +import java.util.ArrayList; +import java.util.List; + +public class ComputerUtils { + + + public static final ComputerPredicate after2010Predicate = (c ) -> ( c.getAge() > 2010 ); + public static final ComputerPredicate blackPredicate = ( c ) -> "black".equals(c.getColor()); + + public static List filter(List inventory, ComputerPredicate p){ + + List result = new ArrayList<>(); + inventory.stream().filter(p::filter).forEach(result::add); + + return result; + } + + + public static void repair (Computer computer){ + if(computer.getHealty()<50){ + computer.setHealty(100); + } + } + + +} diff --git a/core-java-8/src/main/java/com/baeldung/doublecolumn/function/ComputerPredicate.java b/core-java-8/src/main/java/com/baeldung/doublecolumn/function/ComputerPredicate.java new file mode 100644 index 0000000000..f73e985d9c --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/doublecolumn/function/ComputerPredicate.java @@ -0,0 +1,11 @@ +package com.baeldung.doublecolumn.function; + +import com.baeldung.doublecolumn.Computer; + + +@FunctionalInterface +public interface ComputerPredicate { + + boolean filter(Computer c); + +} diff --git a/core-java-8/src/main/java/com/baeldung/doublecolumn/function/TriFunction.java b/core-java-8/src/main/java/com/baeldung/doublecolumn/function/TriFunction.java new file mode 100644 index 0000000000..f7bbc64090 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/doublecolumn/function/TriFunction.java @@ -0,0 +1,16 @@ +package com.baeldung.doublecolumn.function; + +import java.util.Objects; +import java.util.function.Function; + +@FunctionalInterface +public interface TriFunction { + + R apply(A a, B b, C c); + + default TriFunction andThen( + Function after) { + Objects.requireNonNull(after); + return (A a, B b, C c) -> after.apply(apply(a, b, c)); + } +} \ No newline at end of file diff --git a/core-java-8/src/test/java/com/baeldung/doublecolumn/TestComputerUtils.java b/core-java-8/src/test/java/com/baeldung/doublecolumn/TestComputerUtils.java new file mode 100644 index 0000000000..260074dde6 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/doublecolumn/TestComputerUtils.java @@ -0,0 +1,70 @@ +package com.baeldung.doublecolumn; + +import com.baeldung.doublecolumn.function.TriFunction; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.*; +import java.util.function.BiFunction; + +import static com.baeldung.doublecolumn.ComputerUtils.*; + +public class TestComputerUtils { + + + @Before + public void setup (){ + } + + @After + public void tearDown(){ + } + + @Test + public void testFilter(){ + + Computer c1=new Computer(2015,"white"); + Computer c2=new Computer(2009,"black"); + Computer c3=new Computer(2014,"black"); + + BiFunction c4Function=Computer::new; + Computer c4=c4Function.apply(2013,"white"); + BiFunction c5Function=Computer::new; + Computer c5=c5Function.apply(2010,"black"); + BiFunction c6Function=Computer::new; + Computer c6=c6Function.apply(2008,"black"); + + List inventory = Arrays.asList(c1,c2,c3,c4,c5,c6); + + List blackComputer = filter(inventory, blackPredicate); + Assert.assertEquals("The black Computers are: ",blackComputer.size(),4); + + List after2010Computer = filter(inventory, after2010Predicate); + Assert.assertEquals("The Computer bought after 2010 are: ",after2010Computer.size(),3); + + List 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 testRepair() { + + Computer c1 = new Computer(2015, "white", 35); + Computer c2 = new Computer(2009, "black", 65); + TriFunction c6Function = Computer::new; + Computer c3 = c6Function.apply(2008, "black", 90); + + List inventory = Arrays.asList(c1, c2, c3); + inventory.forEach(ComputerUtils::repair); + + Assert.assertEquals("Computer repaired", new Integer(100), c1.getHealty()); + } + +}