Changeset 6193e48 for core/java


Ignore:
Timestamp:
Feb 12, 2018 6:49:01 PM (3 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
507c1d5
Parents:
1826fce
Message:

Util: Add sort methods that catch IAE

File:
1 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/data/DataHelper.java

    r1826fce r6193e48  
    3030import java.util.Arrays;
    3131import java.util.Collection;
     32import java.util.Collections;
     33import java.util.Comparator;
    3234import java.util.Date;
    3335import java.util.HashMap;
    3436import java.util.Iterator;
     37import java.util.List;
    3538import java.util.Locale;
    3639import java.util.Map;
     
    19982001        }
    19992002    }
     2003
     2004    /**
     2005      * Same as Collections.sort(), but guaranteed not to throw an IllegalArgumentException if the
     2006      * sort is unstable. As of Java 7, TimSort will throw an IAE if the underlying sort order
     2007      * changes during the sort.
     2008      *
     2009      * This catches the IAE, retries once, and then returns.
     2010      * If an IAE is thrown twice, this method will return, with the list possibly unsorted.
     2011      *
     2012      * @param list the list to be sorted.
     2013      * @param c the comparator to determine the order of the list. A null value indicates that the elements' natural ordering should be used.
     2014      * @since 0.9.34
     2015      */
     2016    public static <T> void sort(List<T> list, Comparator<? super T> c) {
     2017        try {
     2018            Collections.sort(list, c);
     2019        } catch (IllegalArgumentException iae1) {
     2020            try {
     2021                Thread.sleep(5);
     2022            } catch (InterruptedException ie) {}
     2023            try {
     2024                Collections.sort(list, c);
     2025            } catch (IllegalArgumentException iae2) {}
     2026        }
     2027    }
     2028
     2029    /**
     2030      * Same as Arrays.sort(), but guaranteed not to throw an IllegalArgumentException if the
     2031      * sort is unstable. As of Java 7, TimSort will throw an IAE if the underlying sort order
     2032      * changes during the sort.
     2033      *
     2034      * This catches the IAE, retries once, and then returns.
     2035      * If an IAE is thrown twice, this method will return, with the array possibly unsorted.
     2036      *
     2037      * @param a the array to be sorted.
     2038      * @param c the comparator to determine the order of the array. A null value indicates that the elements' natural ordering should be used.
     2039      * @since 0.9.34
     2040      */
     2041    public static <T> void sort(T[] a, Comparator<? super T> c) {
     2042        try {
     2043            Arrays.sort(a, c);
     2044        } catch (IllegalArgumentException iae1) {
     2045            try {
     2046                Thread.sleep(5);
     2047            } catch (InterruptedException ie) {}
     2048            try {
     2049                Arrays.sort(a, c);
     2050            } catch (IllegalArgumentException iae2) {}
     2051        }
     2052    }
    20002053}
Note: See TracChangeset for help on using the changeset viewer.