Return-Path: X-Original-To: apmail-commons-issues-archive@minotaur.apache.org Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id C1A5110082 for ; Thu, 18 Apr 2013 21:30:15 +0000 (UTC) Received: (qmail 89867 invoked by uid 500); 18 Apr 2013 21:30:15 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 89772 invoked by uid 500); 18 Apr 2013 21:30:15 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 89705 invoked by uid 99); 18 Apr 2013 21:30:15 -0000 Received: from arcas.apache.org (HELO arcas.apache.org) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 18 Apr 2013 21:30:15 +0000 Date: Thu, 18 Apr 2013 21:30:15 +0000 (UTC) From: "Thomas Neidhart (JIRA)" To: issues@commons.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Updated] (COLLECTIONS-450) Iterate over the all elements excluding the last/first one MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/COLLECTIONS-450?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] Thomas Neidhart updated COLLECTIONS-450: ---------------------------------------- Fix Version/s: 4.0 > 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 > Fix For: 4.0 > > Attachments: patch_COLLECTIONS-450.txt > > > 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: You will end up in a code like above. > Proposal > ========= > The method CollectionUtils.forAllButLastDo(Collection, 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{ > 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