diff --git a/src/main/java/com/github/underscore/$.java b/src/main/java/com/github/underscore/$.java index 22c9643e..b395986a 100644 --- a/src/main/java/com/github/underscore/$.java +++ b/src/main/java/com/github/underscore/$.java @@ -88,7 +88,13 @@ public Boolean apply(final E elem) { return false; } } catch (Exception ex) { - ex.getMessage(); + try { + if (!elem.getClass().getMethod(prop.fst()).invoke(elem).equals(prop.snd())) { + return false; + } + } catch (Exception e) { + e.getMessage(); + } } } return true; @@ -463,22 +469,7 @@ public static Set where(final Set set, public static Optional findWhere(final Iterable iterable, final List> properties) { - return find(iterable, new Predicate() { - @Override - public Boolean apply(final E elem) { - for (Tuple prop : properties) { - try { - if (!elem.getClass().getField(prop.fst()).get(elem) - .equals(prop.snd())) { - return false; - } - } catch (Exception ex) { - ex.getMessage(); - } - } - return true; - } - }); + return find(iterable, new WherePredicate(properties)); } public Optional findWhere(final List> properties) { diff --git a/src/test/java/com/github/underscore/CollectionsTest.java b/src/test/java/com/github/underscore/CollectionsTest.java index 669fb2be..f79e2fcc 100644 --- a/src/test/java/com/github/underscore/CollectionsTest.java +++ b/src/test/java/com/github/underscore/CollectionsTest.java @@ -820,6 +820,34 @@ public String toString() { $.chain(listOfPlays).where(asList( Tuple.create("author", "Shakespeare"), Tuple.create("year", Integer.valueOf(1611)))).value().toString()); + class Book2 { + public final String title; + private final String author; + public final Integer year; + public Book2(final String title, final String author, final Integer year) { + this.title = title; + this.author = author; + this.year = year; + } + public String getAuthor() { + return author; + } + public String toString() { + return "title: " + title + ", author: " + author + ", year: " + year; + } + } + List listOfPlays2 = + new ArrayList() { { + add(new Book2("Cymbeline2", "Shakespeare", 1614)); + add(new Book2("Cymbeline", "Shakespeare", 1611)); + add(new Book2("The Tempest", "Shakespeare", 1611)); + } }; + assertEquals("[title: Cymbeline, author: Shakespeare, year: 1611," + + " title: The Tempest, author: Shakespeare, year: 1611]", + $.where(listOfPlays2, asList( + Tuple.create("getAuthor", "Shakespeare"), + Tuple.create("author2", "Shakespeare"), + Tuple.create("year", Integer.valueOf(1611)))).toString()); } /* @@ -864,6 +892,36 @@ public String toString() { Tuple.create("author", "Shakespeare"), Tuple.create("author2", "Shakespeare"), Tuple.create("year", Integer.valueOf(1611)))).get().toString()); + class Book2 { + public final String title; + private final String author; + public final Integer year; + public Book2(final String title, final String author, final Integer year) { + this.title = title; + this.author = author; + this.year = year; + } + public String getAuthor() { + return author; + } + public String toString() { + return "title: " + title + ", author: " + author + ", year: " + year; + } + } + List listOfPlays2 = + new ArrayList() { { + add(new Book2("Cymbeline2", "Shakespeare", 1614)); + add(new Book2("Cymbeline", "Shakespeare", 1611)); + add(new Book2("The Tempest", "Shakespeare", 1611)); + } }; + assertEquals("title: Cymbeline, author: Shakespeare, year: 1611", + $.findWhere(listOfPlays2, asList( + Tuple.create("getAuthor", "Shakespeare"), + Tuple.create("year", Integer.valueOf(1611)))).get().toString()); + assertEquals(Optional.absent(), + $.findWhere(listOfPlays2, asList( + Tuple.create("getAuthor", "Shakespeare2"), + Tuple.create("year", Integer.valueOf(1611))))); } /*