Last active
August 29, 2015 14:20
-
-
Save MoriTanosuke/2f059e955cb12c5b9783 to your computer and use it in GitHub Desktop.
Simple junit to demonstrate Predicates.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.kopis.java8; | |
public class Apple { | |
private String color; | |
private double weight; | |
public Apple(String color, double weight) { | |
this.color = color; | |
this.weight = weight; | |
} | |
public String getColor() { | |
return color; | |
} | |
public void setColor(String color) { | |
this.color = color; | |
} | |
public double getWeight() { | |
return weight; | |
} | |
public void setWeight(double weight) { | |
this.weight = weight; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.kopis.java8; | |
import static org.junit.Assert.assertEquals; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class SortApplesTest { | |
private List<Apple> unsorted; | |
@Before | |
public void createApples() { | |
unsorted = new ArrayList<>(); | |
for (int i = 1; i < 10; i++) { | |
unsorted.add(new Apple("red", 1.23)); | |
} | |
unsorted.add(new Apple("green", 2.34)); | |
} | |
@Test | |
public void canSortApplesByColor() { | |
List<Apple> green = filterApples(unsorted, new ApplePredicate() { | |
@Override | |
public boolean test(Apple apple) { | |
return apple.getColor().equals("green"); | |
} | |
}); | |
assertEquals(1, green.size()); | |
} | |
@Test | |
public void canSortApplesByWeight() { | |
List<Apple> green = filterApples(unsorted, new ApplePredicate() { | |
@Override | |
public boolean test(Apple apple) { | |
return apple.getWeight() > 2.0; | |
} | |
}); | |
assertEquals(1, green.size()); | |
} | |
private List<Apple> filterApples(List<Apple> unsorted, ApplePredicate colorPredicate) { | |
List<Apple> sorted = new ArrayList<>(); | |
for (Apple apple : unsorted) { | |
if (colorPredicate.test(apple)) { | |
sorted.add(apple); | |
} | |
} | |
return sorted; | |
} | |
} | |
interface ApplePredicate { | |
public boolean test(Apple apple); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.kopis.java8; | |
import static org.junit.Assert.assertEquals; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class SortApplesTest { | |
private List<Apple> unsorted; | |
@Before | |
public void createApples() { | |
unsorted = new ArrayList<>(); | |
for (int i = 1; i < 10; i++) { | |
unsorted.add(new Apple("red", 1.23)); | |
} | |
unsorted.add(new Apple("green", 2.34)); | |
} | |
@Test | |
public void canSortApplesByColor() { | |
List<Apple> green = filter(unsorted, (Apple apple) -> apple.getColor().equals("green")); | |
assertEquals(1, green.size()); | |
} | |
@Test | |
public void canSortApplesByWeight() { | |
List<Apple> green = filter(unsorted, (Apple apple) -> apple.getWeight() > 2.0); | |
assertEquals(1, green.size()); | |
} | |
private <T> List<T> filter(List<T> unsorted, Predicate<T> predicate) { | |
List<T> sorted = new ArrayList<>(); | |
for (T element : unsorted) { | |
if (predicate.test(element)) { | |
sorted.add(element); | |
} | |
} | |
return sorted; | |
} | |
} | |
interface Predicate<T> { | |
boolean test(T t); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.kopis.java8; | |
import static org.junit.Assert.assertEquals; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class SortApplesTest { | |
private List<Apple> unsorted; | |
@Before | |
public void createApples() { | |
unsorted = new ArrayList<>(); | |
for (int i = 1; i < 10; i++) { | |
unsorted.add(new Apple("red", 1.23)); | |
} | |
unsorted.add(new Apple("green", 2.34)); | |
} | |
@Test | |
public void canSortApplesByColor() { | |
List<Apple> green = filterApples(unsorted, new AppleColorPredicate("green")); | |
assertEquals(1, green.size()); | |
} | |
@Test | |
public void canSortApplesByWeight() { | |
List<Apple> green = filterApples(unsorted, new AppleMinimumWeightPredicate(2.0)); | |
assertEquals(1, green.size()); | |
} | |
private List<Apple> filterApples(List<Apple> unsorted, ApplePredicate colorPredicate) { | |
List<Apple> sorted = new ArrayList<>(); | |
for (Apple apple : unsorted) { | |
if (colorPredicate.test(apple)) { | |
sorted.add(apple); | |
} | |
} | |
return sorted; | |
} | |
} | |
interface ApplePredicate { | |
public boolean test(Apple apple); | |
} | |
class AppleColorPredicate implements ApplePredicate { | |
private final String color; | |
public AppleColorPredicate(String color) { | |
this.color = color; | |
} | |
public String getColor() { | |
return color; | |
} | |
@Override | |
public boolean test(Apple apple) { | |
return apple.getColor().equals(color); | |
} | |
} | |
class AppleMinimumWeightPredicate implements ApplePredicate { | |
private final double weight; | |
public double getWeight() { | |
return weight; | |
} | |
public AppleMinimumWeightPredicate(double weight) { | |
super(); | |
this.weight = weight; | |
} | |
@Override | |
public boolean test(Apple apple) { | |
return apple.getWeight() > weight; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment