Predicates example

Setup

class HTMLElement {

    public HTMLElement(String tagName) {
        super();
        this.tagName = tagName;
    }

    private String tagName;

    @Override
    public String toString() {
        return Objects.toStringHelper(HTMLElement.class)
                .add("tagName", tagName)
                .toString();
    }

    public String getTagName() {
        return tagName;
    }

}

Predicate<HTMLElement> byAnchor = new Predicate<HTMLElement>() {
    public boolean apply(HTMLElement input) {
        return input.getTagName().startsWith("a");
    }
};

Filter collection

@Test
public void filter_collection_with_predicate () {

    List<HTMLElement> htmlElements = Lists.newArrayList(
            new HTMLElement("a:link"),
            new HTMLElement("a:visited"),
            new HTMLElement("a:hover"),
            new HTMLElement("a:active"),
            new HTMLElement("a:focus"),
            new HTMLElement("a:focus:hover"),
            new HTMLElement("p"),
            new HTMLElement("p:first-letter"),
            new HTMLElement("p:first-line")
        );

    Collection<HTMLElement> anchors = Collections2.filter(htmlElements, byAnchor);

    logger.info(anchors);

    assertEquals(6, anchors.size());
}

Output

[
    HTMLElement{tagName=a:link},
    HTMLElement{tagName=a:visited},
    HTMLElement{tagName=a:hover},
    HTMLElement{tagName=a:active},
    HTMLElement{tagName=a:focus},
    HTMLElement{tagName=a:focus:hover}
]

Filter empty or null elements

@Test
public void filter_empty_null_elements() {

    List<String> words = Lists.newArrayList(
            "", null, "do", "not", "cry",
            null, "over", "spilt", "beer");

    Predicate<String> NOT_EMPTY = new Predicate<String>() {
        @Override
        public boolean apply(String arg0) {
            return !arg0.isEmpty();
        }
    };

    Iterable<String> filteredWords =
            Iterables.filter(words,
                    Predicates.
                    and(Predicates.notNull(),
                            NOT_EMPTY));

    logger.info(filteredWords);

    assertThat(filteredWords, IsIterableContainingInOrder.
            <String>contains("do", "not", "cry",
                    "over", "spilt", "beer"));

}

Output

[do, not, cry, over, spilt, beer]

Charmatcher

/**
 * {@link CharMatcher} implements predicate but has special syntax.
 */
@Test
public void charmatcher_implements_predicate () {

    String theDigits = CharMatcher
            .DIGIT
            .retainFrom("ACB 132, ABC 123"); // only the digits

    assertEquals("132123", theDigits);
}