Minor tweaks with Log and MapSort

master
Ondřej Hruška 10 years ago
parent ab75bb018f
commit 812c3c3bf8
  1. 56
      src/mightypork/utils/MapSort.java
  2. 6
      src/mightypork/utils/logging/Log.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 <K extends Comparable, V> LinkedHashMap<K, V> byKeys(Map<K, V> 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 <K extends Comparable, V extends Comparable> Map<K, V> sortByKeys(Map<K, V> map, final Comparator<K> comparator)
@SuppressWarnings({ "unchecked" })
public static <K, V> LinkedHashMap<K, V> byKeys(Map<K, V> map, Comparator<K> comparator)
{
final List<K> keys = new LinkedList<>(map.keySet());
if (comparator == null) {
Collections.sort(keys);
} else {
Collections.sort(keys, comparator);
comparator = new Comparator<K>() {
@Override
public int compare(K arg0, K arg1)
{
return ((Comparable<K>) arg0).compareTo(arg1);
}
};
}
// LinkedHashMap will keep the keys in the order they are inserted
// which is currently sorted on natural ordering
final Map<K, V> sortedMap = new LinkedHashMap<>();
Collections.sort(keys, comparator);
final LinkedHashMap<K, V> 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 <K, V extends Comparable> LinkedHashMap<K, V> byValues(Map<K, V> 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 <K extends Comparable, V extends Comparable> Map<K, V> sortByValues(Map<K, V> map, final Comparator<V> comparator)
public static <K, V> LinkedHashMap<K, V> byValues(Map<K, V> map, final Comparator<V> comparator)
{
final List<Map.Entry<K, V>> entries = new LinkedList<>(map.entrySet());
@ -63,14 +93,12 @@ public class MapSort {
@Override
public int compare(Entry<K, V> o1, Entry<K, V> o2)
{
if (comparator == null) return o1.getValue().compareTo(o2.getValue());
if (comparator == null) return ((Comparable<V>) 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<K, V> sortedMap = new LinkedHashMap<>();
final LinkedHashMap<K, V> sortedMap = new LinkedHashMap<>();
for (final Map.Entry<K, V> entry : entries) {
sortedMap.put(entry.getKey(), entry.getValue());

@ -82,6 +82,12 @@ public class Log {
}
public static LogWriter getMainLogger()
{
return main;
}
public static void addMonitor(LogMonitor mon)
{
assertInited();

Loading…
Cancel
Save