commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "J. Moldawski (JIRA)" <j...@apache.org>
Subject [jira] [Created] (COLLECTIONS-450) Iterate over the all elements excluding the last/first one
Date Sat, 16 Mar 2013 22:30:13 GMT
J. Moldawski created COLLECTIONS-450:
----------------------------------------

             Summary: Iterate over the all elements excluding the last/first one
                 Key: COLLECTIONS-450
                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-450
             Project: Commons Collections
          Issue Type: Wish
          Components: Collection
            Reporter: J. Moldawski
            Priority: Minor


The Problem
============
In many applications you will extremly often find this sort of code:
int i=0;
for (element:elements)
{
  i++;
  if (i!=elemets.size)
  {
     processLastElement(element);
  }else
  {
     // Just for last element
     processLastElement(element);
  }
}
It happens often, if not just all collections's elements themself must be processed, but some
actions must be performed on going from one element to the next. Since the last element has
no successor, this actions must be skipped when processing the last element. A very famous
example is, if your are going the generate a comma-separated-vector from a Collection<String>:
You will end up in a code like above.

Proposal
=========
The method <T>CollectionUtils.forAllButLastDo(Collection<T>, C)
should be introduced, which process all elements of a collection, but skips the last one,
which will be just returned. The above code can be then re-written:

processLastElement(forAllButLastDo(elements, 
   new Closure<T>{
      execute(T element){
        processAllButTheLast(element)
      }
   }
  ))

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Mime
View raw message