I'm learning Guava right now and I have a problem. I have three possible string filters. The thing is that I only want to filter the object collection when the string is not ""
(empty string). My other question is how can I filter different members from the object like object.getName() == firstStringFilter
. If anyone know how to do this I would be truly appreciatted.
SOLUTION
This is what I finally did with my code. If my firstString filter is not empty, then apply that filter, and the same with the other two string filters. I'm using the same List and overwriting it with the results of the filters.
List<Field> fieldList = mLand.getFields();
if(!filterPattern.equalsIgnoreCase(""))
{
Predicate predicate = new Predicate<Field>()
{
@Override
public boolean apply(Field input)
{
return input.getName()
.toLowerCase()
.contains(filterPattern);
}
};
Collection<Field> result = Collections2.filter(fieldList, predicate);
fieldList = new ArrayList<>(result);
}
if(!cropStringPattern.equalsIgnoreCase(""))
{
Predicate predicate = new Predicate<Field>()
{
@Override
public boolean apply(Field input)
{
return input.getCrop().getName()
.toLowerCase()
.contains(cropStringPattern);
}
};
Collection<Field> result = Collections2.filter(fieldList, predicate);
fieldList = new ArrayList<>(result);
}
if(!varietyStringPattern.equalsIgnoreCase(""))
{
Predicate predicate = new Predicate<Field>()
{
@Override
public boolean apply(Field input)
{
return input.getCrop().getVariety().getName()
.toLowerCase()
.contains(varietyStringPattern);
}
};
Collection<Field> result = Collections2.filter(fieldList, predicate);
fieldList = new ArrayList<>(result);
}