formatting cleanup

This commit is contained in:
eugenp 2016-01-24 19:54:06 +02:00
parent b3878dada2
commit a25f807e3e
5 changed files with 31 additions and 39 deletions

View File

@ -1,6 +1,5 @@
package com.baeldung.doublecolumn; package com.baeldung.doublecolumn;
public class Computer { public class Computer {
private Integer age; private Integer age;
@ -47,21 +46,20 @@ public class Computer {
@Override @Override
public String toString() { public String toString() {
return "Computer{" + return "Computer{" + "age=" + age + ", color='" + color + '\'' + ", healty=" + healty + '}';
"age=" + age +
", color='" + color + '\'' +
", healty=" + healty +
'}';
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o)
if (o == null || getClass() != o.getClass()) return false; return true;
if (o == null || getClass() != o.getClass())
return false;
Computer computer = (Computer) o; Computer computer = (Computer) o;
if (age != null ? !age.equals(computer.age) : computer.age != null) return false; if (age != null ? !age.equals(computer.age) : computer.age != null)
return false;
return color != null ? color.equals(computer.color) : computer.color == null; return color != null ? color.equals(computer.color) : computer.color == null;
} }

View File

@ -7,11 +7,10 @@ import java.util.List;
public class ComputerUtils { public class ComputerUtils {
public static final ComputerPredicate after2010Predicate = (c) -> (c.getAge() > 2010);
public static final ComputerPredicate blackPredicate = (c) -> "black".equals(c.getColor());
public static final ComputerPredicate after2010Predicate = (c ) -> ( c.getAge() > 2010 ); public static List<Computer> filter(List<Computer> inventory, ComputerPredicate p) {
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<>(); List<Computer> result = new ArrayList<>();
inventory.stream().filter(p::filter).forEach(result::add); inventory.stream().filter(p::filter).forEach(result::add);
@ -19,12 +18,10 @@ public class ComputerUtils {
return result; return result;
} }
public static void repair(Computer computer) {
public static void repair (Computer computer){ if (computer.getHealty() < 50) {
if(computer.getHealty()<50){
computer.setHealty(100); computer.setHealty(100);
} }
} }
} }

View File

@ -2,7 +2,6 @@ package com.baeldung.doublecolumn.function;
import com.baeldung.doublecolumn.Computer; import com.baeldung.doublecolumn.Computer;
@FunctionalInterface @FunctionalInterface
public interface ComputerPredicate { public interface ComputerPredicate {

View File

@ -4,12 +4,11 @@ import java.util.Objects;
import java.util.function.Function; import java.util.function.Function;
@FunctionalInterface @FunctionalInterface
public interface TriFunction<A,B,C,R> { public interface TriFunction<A, B, C, R> {
R apply(A a, B b, C c); R apply(A a, B b, C c);
default <V> TriFunction<A, B, C, V> andThen( default <V> TriFunction<A, B, C, V> andThen(Function<? super R, ? extends V> after) {
Function<? super R, ? extends V> after) {
Objects.requireNonNull(after); Objects.requireNonNull(after);
return (A a, B b, C c) -> after.apply(apply(a, b, c)); return (A a, B b, C c) -> after.apply(apply(a, b, c));
} }

View File

@ -13,43 +13,42 @@ import static com.baeldung.doublecolumn.ComputerUtils.*;
public class TestComputerUtils { public class TestComputerUtils {
@Before @Before
public void setup (){ public void setup() {
} }
@After @After
public void tearDown(){ public void tearDown() {
} }
@Test @Test
public void testFilter(){ public void testFilter() {
Computer c1=new Computer(2015,"white"); Computer c1 = new Computer(2015, "white");
Computer c2=new Computer(2009,"black"); Computer c2 = new Computer(2009, "black");
Computer c3=new Computer(2014,"black"); Computer c3 = new Computer(2014, "black");
BiFunction<Integer,String,Computer> c4Function=Computer::new; BiFunction<Integer, String, Computer> c4Function = Computer::new;
Computer c4=c4Function.apply(2013,"white"); Computer c4 = c4Function.apply(2013, "white");
BiFunction<Integer,String,Computer> c5Function=Computer::new; BiFunction<Integer, String, Computer> c5Function = Computer::new;
Computer c5=c5Function.apply(2010,"black"); Computer c5 = c5Function.apply(2010, "black");
BiFunction<Integer,String,Computer> c6Function=Computer::new; BiFunction<Integer, String, Computer> c6Function = Computer::new;
Computer c6=c6Function.apply(2008,"black"); Computer c6 = c6Function.apply(2008, "black");
List<Computer> inventory = Arrays.asList(c1,c2,c3,c4,c5,c6); List<Computer> inventory = Arrays.asList(c1, c2, c3, c4, c5, c6);
List<Computer> blackComputer = filter(inventory, blackPredicate); List<Computer> blackComputer = filter(inventory, blackPredicate);
Assert.assertEquals("The black Computers are: ",blackComputer.size(),4); Assert.assertEquals("The black Computers are: ", blackComputer.size(), 4);
List<Computer> after2010Computer = filter(inventory, after2010Predicate); List<Computer> after2010Computer = filter(inventory, after2010Predicate);
Assert.assertEquals("The Computer bought after 2010 are: ",after2010Computer.size(),3); Assert.assertEquals("The Computer bought after 2010 are: ", after2010Computer.size(), 3);
List<Computer> before2011Computer = filter(inventory, c -> c.getAge() < 2011); List<Computer> before2011Computer = filter(inventory, c -> c.getAge() < 2011);
Assert.assertEquals("The Computer bought before 2011 are: ",before2011Computer.size(),3); Assert.assertEquals("The Computer bought before 2011 are: ", before2011Computer.size(), 3);
inventory.sort(Comparator.comparing(Computer::getAge)); inventory.sort(Comparator.comparing(Computer::getAge));
Assert.assertEquals("Oldest Computer in inventory", c6,inventory.get(0) ); Assert.assertEquals("Oldest Computer in inventory", c6, inventory.get(0));
} }