From 812c3c3bf89140987aa452d781207c4dfd3d5eae Mon Sep 17 00:00:00 2001 From: MightyPork Date: Thu, 24 Jul 2014 21:37:47 +0200 Subject: [PATCH] Minor tweaks with Log and MapSort --- src/mightypork/utils/MapSort.java | 56 ++++++++++++++++++++------- src/mightypork/utils/logging/Log.java | 6 +++ 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/mightypork/utils/MapSort.java b/src/mightypork/utils/MapSort.java index 26d9a0d..9edfe66 100644 --- a/src/mightypork/utils/MapSort.java +++ b/src/mightypork/utils/MapSort.java @@ -17,6 +17,19 @@ import java.util.Map.Entry; */ public class MapSort { + /** + * Sort a map by keys, maintaining key-value pairs, using natural order. + * + * @param map map to be sorted + * @return linked hash map with sorted entries + */ + @SuppressWarnings({ "rawtypes" }) + public static LinkedHashMap byKeys(Map map) + { + return byKeys(map, null); + } + + /** * Sort a map by keys, maintaining key-value pairs. * @@ -24,20 +37,25 @@ public class MapSort { * @param comparator a comparator, or null for natural ordering * @return linked hash map with sorted entries */ - @SuppressWarnings({ "rawtypes", "unchecked" }) - public static Map sortByKeys(Map map, final Comparator comparator) + @SuppressWarnings({ "unchecked" }) + public static LinkedHashMap byKeys(Map map, Comparator comparator) { final List keys = new LinkedList<>(map.keySet()); if (comparator == null) { - Collections.sort(keys); - } else { - Collections.sort(keys, comparator); + comparator = new Comparator() { + + @Override + public int compare(K arg0, K arg1) + { + return ((Comparable) arg0).compareTo(arg1); + } + }; } - // LinkedHashMap will keep the keys in the order they are inserted - // which is currently sorted on natural ordering - final Map sortedMap = new LinkedHashMap<>(); + Collections.sort(keys, comparator); + + final LinkedHashMap sortedMap = new LinkedHashMap<>(); for (final K key : keys) { sortedMap.put(key, map.get(key)); } @@ -46,6 +64,19 @@ public class MapSort { } + /** + * Sort a map by values, maintaining key-value pairs, using natural order. + * + * @param map map to be sorted + * @return linked hash map with sorted entries + */ + @SuppressWarnings("rawtypes") + public static LinkedHashMap byValues(Map map) + { + return byValues(map, null); + } + + /** * Sort a map by values, maintaining key-value pairs. * @@ -53,8 +84,7 @@ public class MapSort { * @param comparator a comparator, or null for natural ordering * @return linked hash map with sorted entries */ - @SuppressWarnings("rawtypes") - public static Map sortByValues(Map map, final Comparator comparator) + public static LinkedHashMap byValues(Map map, final Comparator comparator) { final List> entries = new LinkedList<>(map.entrySet()); @@ -63,14 +93,12 @@ public class MapSort { @Override public int compare(Entry o1, Entry o2) { - if (comparator == null) return o1.getValue().compareTo(o2.getValue()); + if (comparator == null) return ((Comparable) o1.getValue()).compareTo(o2.getValue()); return comparator.compare(o1.getValue(), o2.getValue()); } }); - // LinkedHashMap will keep the keys in the order they are inserted - // which is currently sorted on natural ordering - final Map sortedMap = new LinkedHashMap<>(); + final LinkedHashMap sortedMap = new LinkedHashMap<>(); for (final Map.Entry entry : entries) { sortedMap.put(entry.getKey(), entry.getValue()); diff --git a/src/mightypork/utils/logging/Log.java b/src/mightypork/utils/logging/Log.java index 6b6eb9b..152e67f 100644 --- a/src/mightypork/utils/logging/Log.java +++ b/src/mightypork/utils/logging/Log.java @@ -82,6 +82,12 @@ public class Log { } + public static LogWriter getMainLogger() + { + return main; + } + + public static void addMonitor(LogMonitor mon) { assertInited();