Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- /**
- * @author: GeneralGDA
- * @since: 19.02.2009; 21:53:50
- */
- public final class ArrayListCleaner
- {
- private ArrayListCleaner()
- {
- throw new UnsupportedOperationException();
- }
- /**
- * Algorithm: check element. If it expired - move to it's position
- * element from containers' end.
- *
- * @param list list to clear of invalid elements.
- * @param validator invalid object detector.
- * @return true if at least one object was deleted.
- */
- public static <T> boolean removeInvalidObjects(final ArrayList<T> list, final Validator<T> validator)
- {
- if (null == list || list.isEmpty() || null == validator)
- {
- return false;
- }
- boolean atLeastOneBadFound = false;
- int lastIndex = list.size() - 1;
- for (int i = 0; i < lastIndex; i++)
- {
- final T element = list.get(i);
- if (validator.process(element))
- {
- atLeastOneBadFound = true;
- list.set(i, list.get(lastIndex));
- list.remove(lastIndex);
- lastIndex--;
- i--;
- }
- }
- final T element = list.get(lastIndex);
- if (validator.process(element))
- {
- atLeastOneBadFound = true;
- list.remove(lastIndex);
- }
- return atLeastOneBadFound;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement