Changeset 6193e48


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

Util: Add sort methods that catch IAE

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • apps/i2psnark/java/src/org/klomp/snark/web/I2PSnarkServlet.java

    r1826fce r6193e48  
    14911491            else
    14921492                Sorters.setPattern(null);
    1493             try {
    1494                 Collections.sort(rv, Sorters.getComparator(sort, this));
    1495             } catch (IllegalArgumentException iae) {
    1496                 // Java 7 TimSort - may be unstable
    1497             }
     1493            // Java 7 TimSort - may be unstable
     1494            DataHelper.sort(rv, Sorters.getComparator(sort, this));
    14981495        }
    14991496        return rv;
  • apps/susimail/src/src/i2p/susi/util/Folder.java

    r1826fce r6193e48  
    3535import java.util.Map;
    3636
     37import net.i2p.data.DataHelper;
     38
    3739/**
    3840 * Folder object manages a array Object[] to support
     
    161163        {
    162164                if (currentSorter != null && elements != null && elements.length > 1)
    163                         Arrays.sort( elements, currentSorter );
     165                        DataHelper.sort(elements, currentSorter);
    164166        }
    165167       
  • 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}
  • router/java/src/net/i2p/router/tunnel/pool/BuildExecutor.java

    r1826fce r6193e48  
    99import java.util.concurrent.ConcurrentHashMap;
    1010
     11import net.i2p.data.DataHelper;
    1112import net.i2p.data.Hash;
    1213import net.i2p.data.i2np.I2NPMessage;
     
    383384                            // for several minutes.
    384385                            boolean preferEmpty = _context.random().nextInt(4) != 0;
    385                             try {
    386                                 Collections.sort(wanted, new TunnelPoolComparator(preferEmpty));
    387                             } catch (IllegalArgumentException iae) {
    388                                 // Java 7 TimSort - see info in TunnelPoolComparator
    389                                 continue;
    390                             }
     386                            // Java 7 TimSort - see info in TunnelPoolComparator
     387                            DataHelper.sort(wanted, new TunnelPoolComparator(preferEmpty));
    391388                        }
    392389
Note: See TracChangeset for help on using the changeset viewer.