Return-Path: X-Original-To: apmail-commons-dev-archive@www.apache.org Delivered-To: apmail-commons-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0103FEDD5 for ; Tue, 5 Mar 2013 10:31:52 +0000 (UTC) Received: (qmail 38455 invoked by uid 500); 5 Mar 2013 10:31:51 -0000 Delivered-To: apmail-commons-dev-archive@commons.apache.org Received: (qmail 38114 invoked by uid 500); 5 Mar 2013 10:31:50 -0000 Mailing-List: contact dev-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Developers List" Delivered-To: mailing list dev@commons.apache.org Received: (qmail 38063 invoked by uid 99); 5 Mar 2013 10:31:49 -0000 Received: from minotaur.apache.org (HELO minotaur.apache.org) (140.211.11.9) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 05 Mar 2013 10:31:49 +0000 Received: from localhost (HELO mail-we0-f174.google.com) (127.0.0.1) (smtp-auth username britter, mechanism plain) by minotaur.apache.org (qpsmtpd/0.29) with ESMTP; Tue, 05 Mar 2013 10:31:48 +0000 Received: by mail-we0-f174.google.com with SMTP id r6so5863449wey.19 for ; Tue, 05 Mar 2013 02:31:47 -0800 (PST) MIME-Version: 1.0 X-Received: by 10.194.178.9 with SMTP id cu9mr17487662wjc.39.1362479506886; Tue, 05 Mar 2013 02:31:46 -0800 (PST) Received: by 10.194.7.42 with HTTP; Tue, 5 Mar 2013 02:31:46 -0800 (PST) In-Reply-To: References: <20130302181246.9EB2D2388847@eris.apache.org> <513275B1.5080108@gmail.com> Date: Tue, 5 Mar 2013 11:31:46 +0100 Message-ID: Subject: Re: svn commit: r1451914 - /commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ListUtils.java From: Benedikt Ritter To: Commons Developers List Content-Type: multipart/alternative; boundary=089e013d1f28059b2d04d72afb2d --089e013d1f28059b2d04d72afb2d Content-Type: text/plain; charset=ISO-8859-1 2013/3/3 Benedikt Ritter > > > > 2013/3/2 Thomas Neidhart > >> On 03/02/2013 07:33 PM, Benedikt Ritter wrote: >> > 2013/3/2 >> > >> >> Author: tn >> >> Date: Sat Mar 2 18:12:46 2013 >> >> New Revision: 1451914 >> >> >> >> URL: http://svn.apache.org/r1451914 >> >> Log: >> >> [COLLECTIONS-366] Added ListUtils.range methods. >> >> >> >> Modified: >> >> >> >> >> commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ListUtils.java >> >> >> >> Modified: >> >> >> commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ListUtils.java >> >> URL: >> >> >> http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ListUtils.java?rev=1451914&r1=1451913&r2=1451914&view=diff >> >> >> >> >> ============================================================================== >> >> --- >> >> >> commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ListUtils.java >> >> (original) >> >> +++ >> >> >> commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ListUtils.java >> >> Sat Mar 2 18:12:46 2013 >> >> @@ -507,6 +507,9 @@ public class ListUtils { >> >> return -1; >> >> } >> >> >> >> + // partition >> >> + >> >> >> //------------------------------------------------------------------------- >> >> + >> >> /** >> >> * Returns consecutive {@link List#subList(int, int) sublists} of >> a >> >> * list, each of the same size (the final list may be smaller). >> For >> >> example, >> >> @@ -579,4 +582,99 @@ public class ListUtils { >> >> return list.isEmpty(); >> >> } >> >> } >> >> + >> >> + // range >> >> + >> >> >> //------------------------------------------------------------------------- >> >> + >> >> + /** >> >> + * Returns an unmodifiable List of integers in the range [0, size >> - >> >> 1]. >> >> + *

>> >> + * The returned list does not store the actual numbers, but checks >> >> + * if a given number would be contained in the defined range. A >> call >> >> + * to {@link #contains(Object)} is very fast - O(1). >> >> + * >> >> + * @see #range(int,int) >> >> + * >> >> + * @param size the size of the returned list >> >> + * @return an unmodifiable list of integers in the range [0, size >> - 1] >> >> + * @throws IllegalArgumentException if from > to >> >> + * @since 4.0 >> >> + */ >> >> + public static List range(final int size) { >> >> + return range(0, size - 1); >> >> + } >> >> + >> >> + /** >> >> + * Returns an unmodifiable List of integers in the range [from, >> to]. >> >> + *

>> >> + * The returned list does not store the actual numbers, but checks >> >> + * if a given number would be contained in the defined range. A >> call >> >> + * to {@link #contains(Object)} is very fast - O(1). >> >> + *

>> >> + * The bounds of the range are allowed to be negative. >> >> + * >> >> + * @param from the start of the range >> >> + * @param to the end of the range (inclusive) >> >> + * @return an unmodifiable list of integers in the specified range >> >> + * @throws IllegalArgumentException if from > to >> >> + * @since 4.0 >> >> + */ >> >> + public static List range(final int from, final int to) { >> >> + return ListUtils.unmodifiableList(new RangeList(from, to)); >> >> + } >> >> + >> >> + /** >> >> + * Provides a memory-efficient implementation of a fixed range >> list. >> >> + * @since 4.0 >> >> + */ >> >> + private static final class RangeList extends >> AbstractList { >> >> + private final int from; >> >> + private final int to; >> >> + >> >> + /** >> >> + * Creates a list of integers with a given range, inclusive. >> >> + * >> >> + * @param from the start of the range >> >> + * @param to the end of the range (inclusive) >> >> + * @throws IllegalArgumentException if from > to >> >> + */ >> >> + private RangeList(final int from, final int to) { >> >> + if (to < from) { >> >> + throw new IllegalArgumentException("from(" + from + >> ") > >> >> to(" + to + ")"); >> >> + } >> >> + >> >> + this.from = from; >> >> + this.to = to; >> >> + } >> >> + >> >> + public int size() { >> >> + return to - from + 1; >> >> + } >> >> + >> >> + public Integer get(final int index) { >> >> + final int sz = size(); >> >> + if (index >= sz || index < 0) { >> >> + throw new IndexOutOfBoundsException("Index: " + index >> + >> >> ", Size: " + sz); >> >> + } >> >> + return Integer.valueOf(index + from); >> >> + } >> >> + >> >> + public int indexOf(Object o) { >> >> + if (o instanceof Number) { >> > >> > + final int value = ((Number) o).intValue(); >> >> >> > >> > Why do we cast to Number instead of Integer? >> > >> > With this implementation the following will fail: >> > >> > public void testRange() { >> > List range = ListUtils.range(0, 1); >> > assertFalse(range.contains(Double.valueOf(1.2d))); >> > } >> >> because it was like this in the original patch. >> I looked at it and found it ok, but it has to be documented in the >> contains method that any number passes as argument is converted to int, >> which I forgot (still need to commit the unit tests). >> >> > Okay, but even if this was documented it feels strange that 1.2 is > contained in [0,1] or am I missing something here? > Thomas, can you please comment on this? I don't understand why 1.2 should be contained in [0,1] or is this a result of the incomplete implementation and will be changed? TIA! Benedikt > Would be nice to have some unit tests in place. > > >> Right now, the returned list is not a full-blown Range implementation, >> but this maybe a useful addition in the future. >> > > No questions about that! As I commented in the issue, I like this feature! > :) > > >> >> Thomas >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org >> For additional commands, e-mail: dev-help@commons.apache.org >> >> > > > -- > http://people.apache.org/~britter/ > http://www.systemoutprintln.de/ > http://twitter.com/BenediktRitter > http://github.com/britter > -- http://people.apache.org/~britter/ http://www.systemoutprintln.de/ http://twitter.com/BenediktRitter http://github.com/britter --089e013d1f28059b2d04d72afb2d--