Skip to content

Commit b3878da

Browse files
author
Eugen
committed
Merge pull request eugenp#338 from giuseppebueti/master
Double column operator
2 parents 7653328 + e37f3ae commit b3878da

File tree

5 files changed

+202
-0
lines changed

5 files changed

+202
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.baeldung.doublecolumn;
2+
3+
4+
public class Computer {
5+
6+
private Integer age;
7+
private String color;
8+
private Integer healty;
9+
10+
public Computer(int age, String color) {
11+
this.age = age;
12+
this.color = color;
13+
}
14+
15+
public Computer(Integer age, String color, Integer healty) {
16+
this.age = age;
17+
this.color = color;
18+
this.healty = healty;
19+
}
20+
21+
public Computer() {
22+
}
23+
24+
public Integer getAge() {
25+
return age;
26+
}
27+
28+
public void setAge(Integer age) {
29+
this.age = age;
30+
}
31+
32+
public String getColor() {
33+
return color;
34+
}
35+
36+
public void setColor(String color) {
37+
this.color = color;
38+
}
39+
40+
public Integer getHealty() {
41+
return healty;
42+
}
43+
44+
public void setHealty(Integer healty) {
45+
this.healty = healty;
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return "Computer{" +
51+
"age=" + age +
52+
", color='" + color + '\'' +
53+
", healty=" + healty +
54+
'}';
55+
}
56+
57+
@Override
58+
public boolean equals(Object o) {
59+
if (this == o) return true;
60+
if (o == null || getClass() != o.getClass()) return false;
61+
62+
Computer computer = (Computer) o;
63+
64+
if (age != null ? !age.equals(computer.age) : computer.age != null) return false;
65+
return color != null ? color.equals(computer.color) : computer.color == null;
66+
67+
}
68+
69+
@Override
70+
public int hashCode() {
71+
int result = age != null ? age.hashCode() : 0;
72+
result = 31 * result + (color != null ? color.hashCode() : 0);
73+
return result;
74+
}
75+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.doublecolumn;
2+
3+
import com.baeldung.doublecolumn.function.ComputerPredicate;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class ComputerUtils {
9+
10+
11+
public static final ComputerPredicate after2010Predicate = (c ) -> ( c.getAge() > 2010 );
12+
public static final ComputerPredicate blackPredicate = ( c ) -> "black".equals(c.getColor());
13+
14+
public static List<Computer> filter(List<Computer> inventory, ComputerPredicate p){
15+
16+
List<Computer> result = new ArrayList<>();
17+
inventory.stream().filter(p::filter).forEach(result::add);
18+
19+
return result;
20+
}
21+
22+
23+
public static void repair (Computer computer){
24+
if(computer.getHealty()<50){
25+
computer.setHealty(100);
26+
}
27+
}
28+
29+
30+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.doublecolumn.function;
2+
3+
import com.baeldung.doublecolumn.Computer;
4+
5+
6+
@FunctionalInterface
7+
public interface ComputerPredicate {
8+
9+
boolean filter(Computer c);
10+
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.doublecolumn.function;
2+
3+
import java.util.Objects;
4+
import java.util.function.Function;
5+
6+
@FunctionalInterface
7+
public interface TriFunction<A,B,C,R> {
8+
9+
R apply(A a, B b, C c);
10+
11+
default <V> TriFunction<A, B, C, V> andThen(
12+
Function<? super R, ? extends V> after) {
13+
Objects.requireNonNull(after);
14+
return (A a, B b, C c) -> after.apply(apply(a, b, c));
15+
}
16+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.baeldung.doublecolumn;
2+
3+
import com.baeldung.doublecolumn.function.TriFunction;
4+
import org.junit.After;
5+
import org.junit.Assert;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
import java.util.*;
10+
import java.util.function.BiFunction;
11+
12+
import static com.baeldung.doublecolumn.ComputerUtils.*;
13+
14+
public class TestComputerUtils {
15+
16+
17+
@Before
18+
public void setup (){
19+
}
20+
21+
@After
22+
public void tearDown(){
23+
}
24+
25+
@Test
26+
public void testFilter(){
27+
28+
Computer c1=new Computer(2015,"white");
29+
Computer c2=new Computer(2009,"black");
30+
Computer c3=new Computer(2014,"black");
31+
32+
BiFunction<Integer,String,Computer> c4Function=Computer::new;
33+
Computer c4=c4Function.apply(2013,"white");
34+
BiFunction<Integer,String,Computer> c5Function=Computer::new;
35+
Computer c5=c5Function.apply(2010,"black");
36+
BiFunction<Integer,String,Computer> c6Function=Computer::new;
37+
Computer c6=c6Function.apply(2008,"black");
38+
39+
List<Computer> inventory = Arrays.asList(c1,c2,c3,c4,c5,c6);
40+
41+
List<Computer> blackComputer = filter(inventory, blackPredicate);
42+
Assert.assertEquals("The black Computers are: ",blackComputer.size(),4);
43+
44+
List<Computer> after2010Computer = filter(inventory, after2010Predicate);
45+
Assert.assertEquals("The Computer bought after 2010 are: ",after2010Computer.size(),3);
46+
47+
List<Computer> before2011Computer = filter(inventory, c -> c.getAge() < 2011);
48+
Assert.assertEquals("The Computer bought before 2011 are: ",before2011Computer.size(),3);
49+
50+
inventory.sort(Comparator.comparing(Computer::getAge));
51+
52+
Assert.assertEquals("Oldest Computer in inventory", c6,inventory.get(0) );
53+
54+
}
55+
56+
@Test
57+
public void testRepair() {
58+
59+
Computer c1 = new Computer(2015, "white", 35);
60+
Computer c2 = new Computer(2009, "black", 65);
61+
TriFunction<Integer, String, Integer, Computer> c6Function = Computer::new;
62+
Computer c3 = c6Function.apply(2008, "black", 90);
63+
64+
List<Computer> inventory = Arrays.asList(c1, c2, c3);
65+
inventory.forEach(ComputerUtils::repair);
66+
67+
Assert.assertEquals("Computer repaired", new Integer(100), c1.getHealty());
68+
}
69+
70+
}

0 commit comments

Comments
 (0)