This is for Java 6 and uses Guava.
It's a little ugly and I'm worried I missed some obvious way to do it a lot better.
- If there is more than one element satisfying either condition I am saying it's false because the condition I am checking should be unique among the list.
- If and only if the one element satisfying
condition1
is in the list with a lower index than the one element satisfyingcondition2
should this return true.
condition1
and condition2
will never both be true for the same element.
private static <T> boolean isItemWithConditionBeforeAnother(Predicate<T> condition1, Predicate<T> condition2, List<T> list)
{
if (Collections2.filter(list, condition1).size() != 1 || Collections2.filter(list, condition2).size() != 1)
{
return false;
}
boolean found1 = false;
for (T t: list)
{
if (!found1)
{
if (condition1.apply(t))
{
found1 = true;
}
else if (condition2.apply(t))
{
return false;
}
}
else
{
if (condition2.apply(t))
{
return true;
}
}
}
return false;
}
condition1
andcondition2
, should that return true or false? \$\endgroup\$isItemWithMutuallyExclusiveConditionBeforeAnother
? \$\endgroup\$