Return-Path: Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: (qmail 95379 invoked from network); 25 Jan 2010 06:55:01 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 25 Jan 2010 06:55:01 -0000 Received: (qmail 61900 invoked by uid 500); 25 Jan 2010 06:55:00 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 61659 invoked by uid 500); 25 Jan 2010 06:55:00 -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 61632 invoked by uid 99); 25 Jan 2010 06:54:59 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 25 Jan 2010 06:54:59 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.140] (HELO brutus.apache.org) (140.211.11.140) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 25 Jan 2010 06:54:56 +0000 Received: from brutus.apache.org (localhost [127.0.0.1]) by brutus.apache.org (Postfix) with ESMTP id 8F177234C498 for ; Sun, 24 Jan 2010 22:54:34 -0800 (PST) Message-ID: <806129213.4901264402474584.JavaMail.jira@brutus.apache.org> Date: Mon, 25 Jan 2010 06:54:34 +0000 (UTC) From: "Henri Yandell (JIRA)" To: issues@commons.apache.org Subject: [jira] Updated: (LANG-536) Add isSorted() to ArrayUtils In-Reply-To: <1151342296.1253810596124.JavaMail.jira@brutus> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 X-Virus-Checked: Checked by ClamAV on apache.org [ https://issues.apache.org/jira/browse/LANG-536?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] Henri Yandell updated LANG-536: ------------------------------- Fix Version/s: (was: 3.0) 3.1 No backwards compat issues, so moving to 3.1. If resolved before 3.0 is released please assign to 3.0. > Add isSorted() to ArrayUtils > ---------------------------- > > Key: LANG-536 > URL: https://issues.apache.org/jira/browse/LANG-536 > Project: Commons Lang > Issue Type: New Feature > Components: lang.* > Reporter: Sergei Ivanov > Priority: Minor > Fix For: 3.1 > > > In my unit tests I often need to verify that an array is correctly sorted. > In order to achieve this, I've got two helper methods as follows. > Is it possible to integrate these methods into ArrayUtils? > {code} > /** > * Checks that the specified array of objects is in an ascending order > * according to the specified comparator. All elements in the array must be > * mutually comparable by the specified comparator (that is, > * c.compare(e1, e2) must not throw a ClassCastException > * for any elements e1 and e2 in the array). > * > * @param a the array to be checked. > * @param c the comparator to determine the order of the array. A > * null value indicates that the elements' > * {@linkplain Comparable natural ordering} should be used. > * @return {@code true}, if the array is sorted; {@code false}, otherwise. > * @throws ClassCastException if the array contains elements that are > * not mutually comparable using the specified comparator. > */ > public static boolean isSorted(final T[] a, final Comparator c) { > if (a.length <= 1) { > // Empty or singleton arrays are always sorted > return true; > } > // Otherwise, check that every element is not smaller than the previous > T previous = a[0]; > for (int i = 1, n = a.length; i < n; i++) { > final T current = a[i]; > if (c.compare(previous, current) > 0) { > return false; > } > previous = current; > } > return true; > } > /** > * Checks that the specified array of objects is in an ascending order, > * according to the {@linkplain Comparable natural ordering} of its elements. > * All elements in the array must implement the {@link Comparable} interface. > * Furthermore, all elements in the array must be mutually comparable > * (that is, e1.compareTo(e2) must not throw a ClassCastException > * for any elements e1 and e2 in the array). > * > * @param a the array to be checked. > * @return {@code true}, if the array is sorted; {@code false}, otherwise. > * @throws ClassCastException if the array contains elements that are not > * mutually comparable (for example, strings and integers). > */ > @SuppressWarnings({"unchecked"}) > public static boolean isSorted(final T[] a) { > if (a.length <= 1) { > // Empty or singleton arrays are always sorted > return true; > } > // Otherwise, check that every element is not smaller than the previous > T previous = a[0]; > for (int i = 1, n = a.length; i < n; i++) { > final T current = a[i]; > if (((Comparable) previous).compareTo(previous) > 0) { > return false; > } > previous = current; > } > return true; > } > {code} -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online.