added collection.toSortedSet() for easier conversion to sorted setsand collection.notNull() so its easy to treat a nullable collection/list as a collection

This commit is contained in:
James Strachan
2012-02-23 18:10:53 +00:00
parent 80e728b6f6
commit cb7d23c420
3 changed files with 15 additions and 1 deletions
+5 -1
View File
@@ -10,4 +10,8 @@ inline fun <T, R> java.util.Collection<T>.map(result: Collection<R> = ArrayList<
}
/** Returns true if the collection is not empty */
inline fun <T> java.util.Collection<T>.notEmpty() : Boolean = !this.isEmpty()
inline fun <T> java.util.Collection<T>.notEmpty() : Boolean = !this.isEmpty()
/** Converts the nullable collection into an empty collection if its null */
inline fun <T> java.util.Collection<T>?.notNull() : Collection<T>
= if (this != null) this else Collections.EMPTY_LIST as Collection<T>
+6
View File
@@ -141,18 +141,24 @@ inline fun <T> java.lang.Iterable<T>.reverse() : List<T> {
return answer
}
/* Copies the collection into the given collection */
inline fun <T, C: Collection<T>> java.lang.Iterable<T>.to(result: C) : C {
for (elem in this)
result.add(elem)
return result
}
/* Converts the collection into a LinkedList */
inline fun <T> java.lang.Iterable<T>.toLinkedList() : LinkedList<T> = this.to(LinkedList<T>())
/* Converts the collection into a List */
inline fun <T> java.lang.Iterable<T>.toList() : List<T> = this.to(ArrayList<T>())
/* Converts the collection into a Set */
inline fun <T> java.lang.Iterable<T>.toSet() : Set<T> = this.to(HashSet<T>())
/* Converts the collection into a SortedSet */
inline fun <T> java.lang.Iterable<T>.toSortedSet() : SortedSet<T> = this.to(TreeSet<T>())
/**
TODO figure out necessary variance/generics ninja stuff... :)
inline fun <in T> java.lang.Iterable<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> {
+4
View File
@@ -54,6 +54,10 @@ inline fun <in T: java.lang.Comparable<T>> List<T>.sort(comparator: java.util.Co
return this
}
/** Converts the nullable List into an empty List if its null */
inline fun <T> java.util.List<T>?.notNull() : List<T>
= if (this != null) this else Collections.EMPTY_LIST as List<T>
/**
TODO figure out necessary variance/generics ninja stuff... :)
inline fun <in T> List<T>.sort(transform: fun(T) : java.lang.Comparable<*>) : List<T> {