added toLinkedMap() and toSortedMap helper functions and test cases
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -121,7 +121,6 @@ class MapTest {
|
||||
|
||||
test fun createLinkedMap() {
|
||||
val map = linkedMap(#("c", 3), #("b", 2), #("a", 1))
|
||||
assertEquals(3, map.size)
|
||||
assertEquals(1, map.get("a"))
|
||||
assertEquals(2, map.get("b"))
|
||||
assertEquals(3, map.get("c"))
|
||||
@@ -130,13 +129,34 @@ class MapTest {
|
||||
|
||||
test fun createSortedMap() {
|
||||
val map = sortedMap(#("c", 3), #("b", 2), #("a", 1))
|
||||
assertEquals(3, map.size)
|
||||
assertEquals(1, map.get("a"))
|
||||
assertEquals(2, map.get("b"))
|
||||
assertEquals(3, map.get("c"))
|
||||
assertEquals(arrayList("a", "b", "c"), map.keySet()!!.toList())
|
||||
}
|
||||
|
||||
test fun toSortedMap() {
|
||||
val map = hashMap<String,Int>(#("c", 3), #("b", 2), #("a", 1))
|
||||
val sorted = map.toSortedMap<String,Int>()
|
||||
assertEquals(1, sorted.get("a"))
|
||||
assertEquals(2, sorted.get("b"))
|
||||
assertEquals(3, sorted.get("c"))
|
||||
assertEquals(arrayList("a", "b", "c"), sorted.keySet()!!.toList())
|
||||
}
|
||||
|
||||
test fun toSortedMapWithComparator() {
|
||||
val map = hashMap(#("c", 3), #("bc", 2), #("bd", 4), #("abc", 1))
|
||||
val c = comparator<String>{ a, b ->
|
||||
val answer = a.length() - b.length()
|
||||
if (answer == 0) a.compareTo(b) else answer
|
||||
}
|
||||
val sorted = map.toSortedMap(c)
|
||||
assertEquals(arrayList("c", "bc", "bd", "abc"), sorted.keySet()!!.toList())
|
||||
assertEquals(1, sorted.get("abc"))
|
||||
assertEquals(2, sorted.get("bc"))
|
||||
assertEquals(3, sorted.get("c"))
|
||||
}
|
||||
|
||||
/**
|
||||
TODO
|
||||
test case for http://youtrack.jetbrains.com/issue/KT-1773
|
||||
|
||||
Reference in New Issue
Block a user