Skip to content

Instantly share code, notes, and snippets.

@MoriTanosuke MoriTanosuke/Apple.java
Last active Aug 29, 2015

Embed
What would you like to do?
Simple junit to demonstrate Predicates.
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;
}
}
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);
}
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);
}
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
You can’t perform that action at this time.