commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sergei Ivanov (JIRA)" <j...@apache.org>
Subject [jira] Commented: (LANG-536) Add isSorted() to ArrayUtils
Date Tue, 13 Oct 2009 23:29:31 GMT

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

Sergei Ivanov commented on LANG-536:
------------------------------------

Well, the only problem I have with commons-collections is that it's grown to be a behemoth
of a library. commons-lang managed to stay slimmer over time. Having to import the entire
bulk of commons-collections will always be a major deterrent for me. If they could only mavenise
it properly and split into smaller modules! Apart from that I don't mind the method that takes
Iterable<T> -- makes all sense.

Secondly, Arrays.asList() only works for arrays of objects. Does not work for arrays of primitives
such as int[]. Also, doubles and floats have a special comparison order, implemented since
java 1.4 in Double.compare(double, double) and Float.compare(float, float) respectively.

Thirdly, as arrays are not generic-friendly, the code above (while perfectly valid) produces
compiler warnings (visible if you specify -Xlint:unchecked for javac). The annotation suppresses
these warnings and warning highlighting in IntelliJ IDEA too.



> Add isSorted() to ArrayUtils
> ----------------------------
>
>                 Key: LANG-536
>                 URL: https://issues.apache.org/jira/browse/LANG-536
>             Project: Commons Lang
>          Issue Type: New Feature
>            Reporter: Sergei Ivanov
>            Priority: Minor
>             Fix For: 3.0
>
>
> 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 is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message