added toLinkedMap() and toSortedMap helper functions and test cases

This commit is contained in:
James Strachan
2012-04-16 19:30:13 +01:00
parent 378c95baa3
commit cabaeab972
4 changed files with 82 additions and 4 deletions
+1 -1
View File
@@ -64,7 +64,7 @@ public inline fun <K,V> sortedMap(vararg values: #(K,V)): SortedMap<K,V> {
* is the key and the second value is the value. This map preserves insertion order so iterating through
* the map's entries will be in the same order
*
* @includeFunctionBody ../../test/MapTest.kt createLinkedHashMap
* @includeFunctionBody ../../test/MapTest.kt createLinkedMap
*/
public inline fun <K,V> linkedMap(vararg values: #(K,V)): LinkedHashMap<K,V> {
val answer = LinkedHashMap<K,V>(values.size)
+34 -1
View File
@@ -6,6 +6,11 @@ import java.util.Collection
import java.util.Collections
import java.util.HashMap
import java.util.List
import java.util.LinkedHashMap
import java.util.Map
import java.util.TreeMap
import java.util.SortedMap
import java.util.Comparator
// Map APIs
@@ -121,4 +126,32 @@ public inline fun <K,V> java.util.Map<K,V>.putAll(vararg values: #(K,V)): Unit {
for (v in values) {
put(v._1, v._2)
}
}
}
/**
* Converts this [[Map]] to a [[LinkedHashMap]] so future insertion orders are maintained
*/
public inline fun <K,V> java.util.Map<K,V>.toLinkedMap(): LinkedHashMap<K,V> = toMap<K,V>(LinkedHashMap(size)) as LinkedHashMap<K,V>
/**
* Converts this [[Map]] to a [[SortedMap]] so iteration order will be in key order
*
* @includeFunctionBody ../../test/MapTest.kt toSortedMap
*/
public inline fun <K,V> java.util.Map<K,V>.toSortedMap(): SortedMap<K,V> = toMap<K,V>(TreeMap()) as SortedMap<K,V>
/**
* Converts this [[Map]] to a [[SortedMap]] using the given *comparator* so that iteration order will be in the order
* defined by the comparator
*
* @includeFunctionBody ../../test/MapTest.kt toSortedMapWithComparator
*/
public inline fun <K,V> java.util.Map<K,V>.toSortedMap(comparator: Comparator<K>): SortedMap<K,V> = toMap<K,V>(TreeMap(comparator)) as SortedMap<K,V>
/**
* Copies the entries in this [[Map]] to the given *map*
*/
public inline fun <K,V> java.util.Map<K,V>.toMap(map: Map<K,V>): Map<K,V> {
map.putAll(this)
return map
}
+25
View File
@@ -51,6 +51,7 @@ public inline fun <T> comparator(vararg functions: Function1<T,Any?>): Comparato
return FunctionComparator<T>(functions)
}
private class FunctionComparator<T>(val functions: Array<Function1<T,Any?>>): Comparator<T> {
public fun toString(): String {
@@ -61,6 +62,30 @@ private class FunctionComparator<T>(val functions: Array<Function1<T,Any?>>): C
return compareBy<T>(o1, o2, *functions)
}
public override fun equals(obj: Any?): Boolean {
return this == obj
}
}
/**
* Creates a comparator using the sequence of functions used to calculate a value to compare on
*/
public inline fun <T> comparator(fn: (T,T) -> Int): Comparator<T> {
return Function2Comparator<T>(fn)
}
private class Function2Comparator<T>(val compareFn: (T,T) -> Int): Comparator<T> {
public fun toString(): String {
return "Function2Comparator${compareFn}"
}
public override fun compare(a: T?, b: T?): Int {
if (a === b) return 0
if (a == null) return - 1
if (b == null) return 1
return (compareFn)(a, b)
}
public override fun equals(obj: Any?): Boolean {
return this == obj
}