- Code: Select all
List<CardPrinted> source = getSourceList();
Predicate<CardRules> filterAllowAiDeck = CardRules.Predicates.isKeptInAiDecks;
Predicate<CardRules> filterGreen = CardRules.Predicates.Presets.isGreen;
Predicate<CardRules> filterCreatures = CardRules.Predicates.Presets.isCreature;
List<CardPrinted> cardsAiSupports = filterAllowAiDeck.select(CardDb.getAllUniqueCards(), CardPrinted.fnGetRules); // without reprints
List<CardPrinted> mightWantToPutIntoDeck = Predicate.and(filterGreen, filterCreatures).select(cardsAiSupports, CardPrinted.fnGetRules);
Predicates are like filters, but more versatile - they allow you to build an expression tree and filter the cards you need in a single pass.
Once you have built a predicate, you may filter with select method. There are some variations of select: top() - will return the 1st matching object, any() will return true as soon as a matching object is found, split() will divide objects into two lists - one with matching elements, other with non-matching ones.
There some are situations when you want to filter types that contain the one you would like to make checks about, as in this expample want to check properties of CardRules, but have a list of CardPrinted. In that case use the second overload of select which accepts a lambda besides the Iterable to be filtered. That lambda should hold a function that accesses the type the predicate was build for.
The third overload allows not only access the type inside a container, but get a list of some different types, not the container itself, for instance one can select only the names of cards that are red instants.