@SuppressWarnings("unchecked") private void deepSort(DefaultMutableTreeNode parent, TreeObjectSortBy sortBy, SortOrder order) { Enumeration<DefaultMutableTreeNode> treeObjectNodes = parent.children(); List<DefaultMutableTreeNode> treeNodes = EnumerationUtils .toList(treeObjectNodes); BeanToPropertyValueTransformer sortProperty = getSortByTransformer(sortBy); Comparator<DefaultMutableTreeNode> comparator = new TransformingComparator( sortProperty); if (SortOrder.DESC.equals(order)) { comparator = new ReverseComparator<DefaultMutableTreeNode>( comparator); } Collections.sort(treeNodes, comparator); parent.removeAllChildren(); for (DefaultMutableTreeNode childNode : treeNodes) { parent.add(childNode); deepSort(childNode, sortBy, order); } }
public SortedMap(Map<K, V> map, SortBy sortBy, SortOrder sortOrder) { List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>( map.entrySet()); Comparator<Map.Entry<K, V>> c = getMapEntryComparator(sortBy); if (SortOrder.DESC.equals(sortOrder)) { c = new ReverseComparator<Map.Entry<K, V>>(c); } Collections.sort(list, c); for (Iterator<Map.Entry<K, V>> it = list.iterator(); it.hasNext();) { Map.Entry<K, V> entry = it.next(); put(entry.getKey(), entry.getValue()); } }
@org.junit.Test(timeout = 1000) public void testSerializeDeserializeCheckComparator() throws Exception { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testSerializeDeserializeCheckComparator"); final SortedBidiMap<?, ?> obj = makeObject(); if ((obj instanceof Serializable) && (isTestSerialization())) { final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); final ObjectOutputStream out = new ObjectOutputStream(buffer); out.writeObject(obj); out.writeObject(obj); out.close(); final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); final Object dest = in.readObject(); in.close(); final SortedBidiMap<?, ?> bidi = ((SortedBidiMap<?, ?>)(dest)); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5541,obj,5540,obj.comparator()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5543,bidi,5542,bidi.comparator()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5544,((bidi.comparator()) instanceof org.apache.commons.collections4.comparators.ReverseComparator)); } fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@org.junit.Test(timeout = 1000) public void testSerializeDeserializeCheckComparator_add1648() throws Exception { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testSerializeDeserializeCheckComparator_add1648"); final SortedBidiMap<?, ?> obj = makeObject(); if ((obj instanceof Serializable) && (isTestSerialization())) { final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); final ObjectOutputStream out = new ObjectOutputStream(buffer); out.writeObject(obj); out.close(); out.close(); final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); final Object dest = in.readObject(); in.close(); final SortedBidiMap<?, ?> bidi = ((SortedBidiMap<?, ?>)(dest)); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5541,obj,5540,obj.comparator()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5543,bidi,5542,bidi.comparator()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5544,((bidi.comparator()) instanceof org.apache.commons.collections4.comparators.ReverseComparator)); } fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@org.junit.Test(timeout = 1000) public void testSerializeDeserializeCheckComparator_add1649() throws Exception { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testSerializeDeserializeCheckComparator_add1649"); final SortedBidiMap<?, ?> obj = makeObject(); if ((obj instanceof Serializable) && (isTestSerialization())) { final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); final ObjectOutputStream out = new ObjectOutputStream(buffer); out.writeObject(obj); out.close(); final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); final Object dest = in.readObject(); in.close(); in.close(); final SortedBidiMap<?, ?> bidi = ((SortedBidiMap<?, ?>)(dest)); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5541,obj,5540,obj.comparator()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5543,bidi,5542,bidi.comparator()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5544,((bidi.comparator()) instanceof org.apache.commons.collections4.comparators.ReverseComparator)); } fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@org.junit.Test(timeout = 1000) public void testSerializeDeserializeCheckComparator_remove1246() throws Exception { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testSerializeDeserializeCheckComparator_remove1246"); final SortedBidiMap<?, ?> obj = makeObject(); if ((obj instanceof Serializable) && (isTestSerialization())) { final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); final ObjectOutputStream out = new ObjectOutputStream(buffer); out.close(); final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); final Object dest = in.readObject(); in.close(); final SortedBidiMap<?, ?> bidi = ((SortedBidiMap<?, ?>)(dest)); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5541,obj,5540,obj.comparator()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5543,bidi,5542,bidi.comparator()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5544,((bidi.comparator()) instanceof org.apache.commons.collections4.comparators.ReverseComparator)); } fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@org.junit.Test(timeout = 1000) public void testSerializeDeserializeCheckComparator_remove1247() throws Exception { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testSerializeDeserializeCheckComparator_remove1247"); final SortedBidiMap<?, ?> obj = makeObject(); if ((obj instanceof Serializable) && (isTestSerialization())) { final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); final ObjectOutputStream out = new ObjectOutputStream(buffer); out.writeObject(obj); final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); final Object dest = in.readObject(); in.close(); final SortedBidiMap<?, ?> bidi = ((SortedBidiMap<?, ?>)(dest)); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5541,obj,5540,obj.comparator()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5543,bidi,5542,bidi.comparator()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5544,((bidi.comparator()) instanceof org.apache.commons.collections4.comparators.ReverseComparator)); } fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@org.junit.Test(timeout = 1000) public void testSerializeDeserializeCheckComparator_remove1248() throws Exception { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testSerializeDeserializeCheckComparator_remove1248"); final SortedBidiMap<?, ?> obj = makeObject(); if ((obj instanceof Serializable) && (isTestSerialization())) { final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); final ObjectOutputStream out = new ObjectOutputStream(buffer); out.writeObject(obj); out.close(); final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); final Object dest = in.readObject(); final SortedBidiMap<?, ?> bidi = ((SortedBidiMap<?, ?>)(dest)); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5541,obj,5540,obj.comparator()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5543,bidi,5542,bidi.comparator()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5544,((bidi.comparator()) instanceof org.apache.commons.collections4.comparators.ReverseComparator)); } fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
public void testSortOrder() throws Exception { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testSortOrder"); final SortedBidiMap<K, V> sm = makeFullMap(); List<K> newSortedKeys = getAsList(getSampleKeys()); java.util.Collections.sort(newSortedKeys, new ReverseComparator<K>(org.apache.commons.collections4.comparators.ComparableComparator.<K>comparableComparator())); newSortedKeys = java.util.Collections.unmodifiableList(newSortedKeys); final Iterator<K> mapIter = sm.keySet().iterator(); final Iterator<K> expectedIter = newSortedKeys.iterator(); while (expectedIter.hasNext()) { final K expectedKey = expectedIter.next(); final K mapKey = mapIter.next(); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5545,expectedKey); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5546,mapKey); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5547,expectedKey); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5548,mapKey); } fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@org.junit.Test(timeout = 1000) public void testSortOrder_add1650() throws Exception { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testSortOrder_add1650"); final SortedBidiMap<K, V> sm = makeFullMap(); List<K> newSortedKeys = getAsList(getSampleKeys()); java.util.Collections.sort(newSortedKeys, new ReverseComparator<K>(org.apache.commons.collections4.comparators.ComparableComparator.<K>comparableComparator())); java.util.Collections.sort(newSortedKeys, new ReverseComparator<K>(org.apache.commons.collections4.comparators.ComparableComparator.<K>comparableComparator())); newSortedKeys = java.util.Collections.unmodifiableList(newSortedKeys); final Iterator<K> mapIter = sm.keySet().iterator(); final Iterator<K> expectedIter = newSortedKeys.iterator(); while (expectedIter.hasNext()) { final K expectedKey = expectedIter.next(); final K mapKey = mapIter.next(); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5545,expectedKey); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5546,mapKey); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5547,expectedKey); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5548,mapKey); } fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@org.junit.Test(timeout = 1000) public void testComparator_add1644() { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testComparator_add1644"); resetEmpty(); resetEmpty(); final SortedBidiMap<K, V> bidi = ((SortedBidiMap<K, V>)(map)); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5538,bidi,5537,bidi.comparator()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5539,((bidi.comparator()) instanceof org.apache.commons.collections4.comparators.ReverseComparator)); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@org.junit.Test(timeout = 1000) public void testComparator_remove1243() { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testComparator_remove1243"); final SortedBidiMap<K, V> bidi = ((SortedBidiMap<K, V>)(map)); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5538,bidi,5537,bidi.comparator()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),5539,((bidi.comparator()) instanceof org.apache.commons.collections4.comparators.ReverseComparator)); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@Override public DualTreeBidiMap<K, V> makeObject() { return new DualTreeBidiMap<K, V>(new ReverseComparator<K>(org.apache.commons.collections4.comparators.ComparableComparator.<K>comparableComparator()) , new ReverseComparator<V>(org.apache.commons.collections4.comparators.ComparableComparator.<V>comparableComparator())); }
@Override public TreeMap<K, V> makeConfirmedMap() { return new TreeMap<K, V>(new ReverseComparator<K>(org.apache.commons.collections4.comparators.ComparableComparator.<K>comparableComparator())); }
/** * 按照key desc 倒序排序. * * <h3>注意:</h3> * <blockquote> * <ol> * <li>原 <code>map</code> 的顺序不变</li> * <li>该方法使用了 {@link PropertyComparator},允许 null key,<b>null key排在最后面</b></li> * </ol> * </blockquote> * * <h3>示例:</h3> * <blockquote> * * <pre class="code"> * Map{@code <String, Comparable>} map = new HashMap{@code <>}(); * * map.put("a", 123); * map.put("c", 345); * map.put(null, 88); * map.put("b", 8); * * LOGGER.debug(JsonUtil.format(SortUtil.sortMapByKeyDesc(map))); * </pre> * * <b>返回:</b> * * <pre class="code"> * { * "c": 345, * "b": 8, * "a": 123, * null: 88 * } * </pre> * * </blockquote> * * @param <K> * the key type * @param <V> * the value type * @param map * the map * @return 如果 <code>map</code> 是null,返回 {@link Collections#emptyMap()}<br> * @see ReverseComparator#ReverseComparator(Comparator) * @see PropertyComparator#PropertyComparator(String) * @see #sortMap(Map, Comparator) * @since 1.8.0 move from MapUtil * @since 1.8.7 change method name */ public static <K, V> Map<K, V> sortMapByKeyDesc(Map<K, V> map){ if (null == map){ return emptyMap(); } return sortMap(map, new ReverseComparator<Map.Entry<K, V>>(new PropertyComparator<Map.Entry<K, V>>("key"))); }
/** * 根据value 来倒序排序(desc). * * <h3>注意:</h3> * <blockquote> * <ol> * <li>原 <code>map</code> 的顺序不变</li> * </ol> * </blockquote> * * <h3>示例:</h3> * <blockquote> * * <pre class="code"> * Map{@code <String, Comparable>} map = new LinkedHashMap{@code <>}(); * * map.put("a", 123); * map.put("c", 345); * map.put("b", 8); * * LOGGER.debug(JsonUtil.format(SortUtil.sortMapByValueDesc(map))); * </pre> * * <b>返回:</b> * * <pre class="code"> * { * "c": 345, * "a": 123, * "b": 8 * } * </pre> * * </blockquote> * * @param <K> * the key type * @param <V> * the value type * @param map * the map * @return 如果 <code>map</code> 是null,返回 {@link Collections#emptyMap()}<br> * @see #sortMap(Map, Comparator) * @since 1.8.0 move from MapUtil * @since 1.8.7 change method name from sortByValueDesc */ public static <K, V extends Comparable<V>> Map<K, V> sortMapByValueDesc(Map<K, V> map){ if (null == map){ return emptyMap(); } return sortMap(map, new ReverseComparator<Map.Entry<K, V>>(new PropertyComparator<Map.Entry<K, V>>("value"))); }
/** * Gets a comparator that reverses the order of the given comparator. * * @param <E> the object type to compare * @param comparator the comparator to reverse * @return a comparator that reverses the order of the input comparator * @see ReverseComparator */ public static <E> Comparator<E> reversedComparator(final Comparator<E> comparator) { return new ReverseComparator<E>(comparator); }