Merge pull request #338 from giuseppebueti/master

Double column operator
This commit is contained in:
Eugen 2016-01-24 19:52:42 +02:00
commit b3878dada2
5 changed files with 202 additions and 0 deletions

View File

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

View File

@ -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<Computer> filter(List<Computer> inventory, ComputerPredicate p){
List<Computer> 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);
}
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.doublecolumn.function;
import com.baeldung.doublecolumn.Computer;
@FunctionalInterface
public interface ComputerPredicate {
boolean filter(Computer c);
}

View File

@ -0,0 +1,16 @@
package com.baeldung.doublecolumn.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(
Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (A a, B b, C c) -> after.apply(apply(a, b, c));
}
}

View File

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