Created
February 9, 2011 08:33
-
-
Save csamuel/818148 to your computer and use it in GitHub Desktop.
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
import com.google.common.base.Predicate; | |
public class FilterCriteria { | |
public static Predicate<Item> freeShipping() { | |
return new Predicate<Item>() { | |
public boolean apply(Item item) { | |
return item.getPrice() >= 25 && item.isAvailable(); | |
} | |
}; | |
} | |
} |
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
import com.google.common.collect.Iterables; | |
import com.google.common.collect.Lists; | |
import java.util.List; | |
public class FreeShippingFilterExample { | |
public static void main (String[] args) { | |
List<Item> searchResults | |
= Lists.newArrayList(new Item("item1", 30, true), | |
new Item("item2", 20, true), | |
new Item("item3", 100, false)); | |
// Prints "[item1]" | |
System.out.println( | |
Iterables.filter(searchResults, FilterCriteria.freeShipping()) | |
); | |
} | |
} |
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
public class Item { | |
private String name; | |
private int price; | |
private boolean available; | |
public Item(String name, int price, boolean available) { | |
this.name = name; | |
this.price = price; | |
this.available = available; | |
} | |
public String getName() { | |
return name; | |
} | |
public int getPrice() { | |
return price; | |
} | |
public boolean isAvailable() { | |
return available; | |
} | |
@Override | |
public String toString() { | |
return getName(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment