Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the method name in where() and findWhere() methods. #14 #15

Merged
merged 1 commit into from
Aug 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 8 additions & 17 deletions src/main/java/com/github/underscore/$.java
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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