commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Thomas Neidhart (JIRA)" <>
Subject [jira] [Commented] (COLLECTIONS-530) Rejecting items on predicate failure without throwing an Exception
Date Fri, 30 May 2014 12:22:01 GMT


Thomas Neidhart commented on COLLECTIONS-530:

The reason why the PredicatedXXX decorators throw an exception rather than silently discard
the element is related to the Collection contract which states the following (add method):

If a collection refuses to add a particular element for any reason other than that it already
contains the element, it must throw an exception (rather than returning false). This preserves
the invariant that a collection always contains the specified element after this call returns.

We did violate the Collection contract at other occasions in the past (see Bag interface),
but it was not a very wise choice imho (and we still suffer from it). So atm I do not see
a convincing argument to add something like this to a general-purpose lib as collections.
If you just want to filter certain elements from a given collection, you might better use

> Rejecting items on predicate failure without throwing an Exception
> ------------------------------------------------------------------
>                 Key: COLLECTIONS-530
>                 URL:
>             Project: Commons Collections
>          Issue Type: Improvement
>          Components: List
>    Affects Versions: 4.0
>            Reporter: Erik
> The PredicatedList class doesn't allow entries that fail the predicate, but throws an
Exception on entry.
> The problem I have with this, is that it places the onus of filtering out invalid entries
on the caller.
> I typically add items in a loop. The item added is the result of a method call (which
returns null if it can't create one).
> This problem is so common for me that I have created my own FilteredList class that simply
ignores invalid entries.
> I would like the PredicatedList class to be capable of rejecting items without throwing
an exception.
> I don't mind writing the code for this, but there are a great many ways in which this
can be done.
> So I was wondering what the interface should look like.
> Separate FilteredList class.
> Works, but seems a little verbose for the purpose
> New factory method: filteredList(List<T> list, Predicate<? super T> predicate)

> Nice and simple, but doesn't allow extension; other ways of dealing with predicate failure.
> New factory method with enum: predicatedList(List<T> list, Predicate<? super
T> predicate, PredicateFailEnum action)
> More verbose to use and adds an extra class, but allows more alternative ways to deal
with predicate failure.
> One more nice thing is that it might be less confusing, 
> because choosing between predicatedList and the above filteredList might not be so obvious.
> New factory method with interface: filteredList(List<T> list, Predicate<? super
T> predicate, PredicateFailInterface action)
> Complex, but the most flexible way of dealing with predicate failure.

This message was sent by Atlassian JIRA

View raw message