diff --git a/stdlib/ktSrc/JavaUtil.kt b/stdlib/ktSrc/JavaUtil.kt index f275339cbf2..f1043599bd1 100644 --- a/stdlib/ktSrc/JavaUtil.kt +++ b/stdlib/ktSrc/JavaUtil.kt @@ -82,37 +82,3 @@ val List.last : T? -// Map APIs -/** Returns the size of the map */ -/* TODO get redeclaration errors -val Map<*,*>.size : Int - get() = size() -*/ - -/** Returns true if this map is empty */ -/* TODO get redeclaration errors -val Map<*,*>.empty : Boolean - get() = isEmpty() -*/ - -/** Returns the value for the given key or returns the result of the defaultValue function if there was no entry for the given key */ -inline fun java.util.Map.getOrElse(key: K, defaultValue: ()-> V) : V { - val current = this.get(key) - if (current != null) { - return current - } else { - return defaultValue() - } -} - -/** Returns the value for the given key or the result of the defaultValue function is put into the map for the given value and returned */ -inline fun java.util.Map.getOrElseUpdate(key: K, defaultValue: ()-> V) : V { - val current = this.get(key) - if (current != null) { - return current - } else { - val answer = defaultValue() - this.put(key, answer) - return answer - } -} diff --git a/stdlib/ktSrc/JavaUtilMap.kt b/stdlib/ktSrc/JavaUtilMap.kt new file mode 100644 index 00000000000..4dcd2e9bd19 --- /dev/null +++ b/stdlib/ktSrc/JavaUtilMap.kt @@ -0,0 +1,39 @@ +package std.util + +import java.util.* + +// Map APIs + +/** Returns the size of the map */ +/* TODO get redeclaration errors +val Map<*,*>.size : Int + get() = size() +*/ + +/** Returns true if this map is empty */ +/* TODO get redeclaration errors +val Map<*,*>.empty : Boolean + get() = isEmpty() +*/ + +/** Returns the value for the given key or returns the result of the defaultValue function if there was no entry for the given key */ +inline fun java.util.Map.getOrElse(key: K, defaultValue: ()-> V) : V { + val current = this.get(key) + if (current != null) { + return current + } else { + return defaultValue() + } +} + +/** Returns the value for the given key or the result of the defaultValue function is put into the map for the given value and returned */ +inline fun java.util.Map.getOrElseUpdate(key: K, defaultValue: ()-> V) : V { + val current = this.get(key) + if (current != null) { + return current + } else { + val answer = defaultValue() + this.put(key, answer) + return answer + } +} diff --git a/testlib/test/MapTest.kt b/testlib/test/MapTest.kt index 18b8b527b63..90fc8110b8e 100644 --- a/testlib/test/MapTest.kt +++ b/testlib/test/MapTest.kt @@ -32,4 +32,12 @@ class MapTest() : TestSupport() { assertEquals(1, data.size()) } + + /** TODO can't seem to define size/empty properties for Map + fun testSizeAndEmpty() { + assert{ data.empty } + + assertEquals(data.size, 0) + } + */ } \ No newline at end of file