commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "James Sawle (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (LANG-536) Add isSorted() to ArrayUtils
Date Fri, 10 Oct 2014 10:50:33 GMT

    [ https://issues.apache.org/jira/browse/LANG-536?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14166622#comment-14166622
] 

James Sawle edited comment on LANG-536 at 10/10/14 10:50 AM:
-------------------------------------------------------------

A patch for the proposed new methods. Personally, I believe that we should add this functionality,
even if a similar functionality is within Guava, for completeness. There are already a number
of places where the libraries overlap.

Have not implemented the Iterator version of the methods as this would fit better within another
class.

All methods eventually delegate to public static '<T> boolean isSorted(T[] a, Comparator<?
super T> c)' to aid code reuse; however this has meant that an anonymous implementation
of the Comparable class is generated on each primitive method and the isSorted(T[]) method.
Personally, this leads to a cleaner implementation than having the same logic in each of the
two methods.


was (Author: jamessawle):
A patch for the proposed new methods.

Have not implemented the Iterator version of the methods as this would fit better within another
class.

All methods eventually delegate to public static '<T> boolean isSorted(T[] a, Comparator<?
super T> c)' to aid code reuse; however this has meant that an anonymous implementation
of the Comparable class is generated on each primitive method and the isSorted(T[]) method.
Personally, this leads to a cleaner implementation than having the same logic in each of the
two methods.

> 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: Review Patch
>
>         Attachments: LANG-536.patch
>
>
> 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
>      * <i>mutually comparable</i> by the specified comparator (that is,
>      * <tt>c.compare(e1, e2)</tt> must not throw a <tt>ClassCastException</tt>
>      * for any elements <tt>e1</tt> and <tt>e2</tt> in the array).
>      *
>      * @param a the array to be checked.
>      * @param c the comparator to determine the order of the array.  A
>      * <tt>null</tt> 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 <i>mutually comparable</i> using the specified comparator.
>      */
>     public static <T> boolean isSorted(final T[] a, final Comparator<? super
T> 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 <i>mutually comparable</i>
>      * (that is, <tt>e1.compareTo(e2)</tt> must not throw a <tt>ClassCastException</tt>
>      * for any elements <tt>e1</tt> and <tt>e2</tt> 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
>      * <i>mutually comparable</i> (for example, strings and integers).
>      */
>     @SuppressWarnings({"unchecked"})
>     public static <T> 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<? super T>) previous).compareTo(previous) > 0)
{
>                 return false;
>             }
>             previous = current;
>         }
>         return true;
>     }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message