Skip to content

Commit

Permalink
Add support for the method name in find() and findWhere() methods. Fi…
Browse files Browse the repository at this point in the history
…x the bug #14.
  • Loading branch information
javadev committed Aug 13, 2015
1 parent 4c296d1 commit 6e318bf
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 17 deletions.
25 changes: 8 additions & 17 deletions src/main/java/com/github/underscore/$.java
Expand Up @@ -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;
Expand Down Expand Up @@ -463,22 +469,7 @@ public static <T, E> Set<E> where(final Set<E> set,

public static <T, E> Optional<E> findWhere(final Iterable<E> iterable,
final List<Tuple<String, T>> properties) {
return find(iterable, new Predicate<E>() {
@Override
public Boolean apply(final E elem) {
for (Tuple<String, T> 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<E, T>(properties));
}

public <E> Optional<T> findWhere(final List<Tuple<String, E>> properties) {
Expand Down
58 changes: 58 additions & 0 deletions src/test/java/com/github/underscore/CollectionsTest.java
Expand Up @@ -820,6 +820,34 @@ public String toString() {
$.chain(listOfPlays).where(asList(
Tuple.<String, Object>create("author", "Shakespeare"),
Tuple.<String, Object>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<Book2> listOfPlays2 =
new ArrayList<Book2>() { {
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.<String, Object>create("getAuthor", "Shakespeare"),
Tuple.<String, Object>create("author2", "Shakespeare"),
Tuple.<String, Object>create("year", Integer.valueOf(1611)))).toString());
}

/*
Expand Down Expand Up @@ -864,6 +892,36 @@ public String toString() {
Tuple.<String, Object>create("author", "Shakespeare"),
Tuple.<String, Object>create("author2", "Shakespeare"),
Tuple.<String, Object>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<Book2> listOfPlays2 =
new ArrayList<Book2>() { {
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.<String, Object>create("getAuthor", "Shakespeare"),
Tuple.<String, Object>create("year", Integer.valueOf(1611)))).get().toString());
assertEquals(Optional.absent(),
$.findWhere(listOfPlays2, asList(
Tuple.<String, Object>create("getAuthor", "Shakespeare2"),
Tuple.<String, Object>create("year", Integer.valueOf(1611)))));
}

/*
Expand Down

0 comments on commit 6e318bf

Please sign in to comment.