diff --git a/stdlib/ktSrc/JavaCollections.kt b/stdlib/ktSrc/JavaCollections.kt index c213215cb32..bcd9f001b4a 100644 --- a/stdlib/ktSrc/JavaCollections.kt +++ b/stdlib/ktSrc/JavaCollections.kt @@ -10,4 +10,8 @@ inline fun java.util.Collection.map(result: Collection = ArrayList< } /** Returns true if the collection is not empty */ -inline fun java.util.Collection.notEmpty() : Boolean = !this.isEmpty() \ No newline at end of file +inline fun java.util.Collection.notEmpty() : Boolean = !this.isEmpty() + +/** Converts the nullable collection into an empty collection if its null */ +inline fun java.util.Collection?.notNull() : Collection + = if (this != null) this else Collections.EMPTY_LIST as Collection diff --git a/stdlib/ktSrc/JavaIterables.kt b/stdlib/ktSrc/JavaIterables.kt index 65c73948714..9a63529c57c 100644 --- a/stdlib/ktSrc/JavaIterables.kt +++ b/stdlib/ktSrc/JavaIterables.kt @@ -141,18 +141,24 @@ inline fun java.lang.Iterable.reverse() : List { return answer } +/* Copies the collection into the given collection */ inline fun > java.lang.Iterable.to(result: C) : C { for (elem in this) result.add(elem) return result } +/* Converts the collection into a LinkedList */ inline fun java.lang.Iterable.toLinkedList() : LinkedList = this.to(LinkedList()) +/* Converts the collection into a List */ inline fun java.lang.Iterable.toList() : List = this.to(ArrayList()) +/* Converts the collection into a Set */ inline fun java.lang.Iterable.toSet() : Set = this.to(HashSet()) +/* Converts the collection into a SortedSet */ +inline fun java.lang.Iterable.toSortedSet() : SortedSet = this.to(TreeSet()) /** TODO figure out necessary variance/generics ninja stuff... :) inline fun java.lang.Iterable.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List { diff --git a/stdlib/ktSrc/JavaUtil.kt b/stdlib/ktSrc/JavaUtil.kt index 83a3f08d897..6d0c7ce2de8 100644 --- a/stdlib/ktSrc/JavaUtil.kt +++ b/stdlib/ktSrc/JavaUtil.kt @@ -54,6 +54,10 @@ inline fun > List.sort(comparator: java.util.Co return this } +/** Converts the nullable List into an empty List if its null */ +inline fun java.util.List?.notNull() : List + = if (this != null) this else Collections.EMPTY_LIST as List + /** TODO figure out necessary variance/generics ninja stuff... :) inline fun List.sort(transform: fun(T) : java.lang.Comparable<*>) : List {