package org.mariotaku.ktextension /** * Created by mariotaku on 2016/12/24. */ fun Collection<*>?.isNotNullOrEmpty(): Boolean { return this != null && this.isNotEmpty() } fun Collection<*>?.isNullOrEmpty(): Boolean { return this == null || this.isEmpty() } fun MutableCollection.addAllEnhanced(collection: Collection, ignoreDuplicates: Boolean): Boolean { return if (ignoreDuplicates) { addAll(collection.filter { it !in this }) } else { addAll(collection) } } fun Collection.addAllTo(collection: MutableCollection): Boolean { return collection.addAll(this) } fun Array.addAllTo(collection: MutableCollection): Boolean { return collection.addAll(this) } fun Collection?.nullableContentEquals(other: Collection?): Boolean { if (this == null) return other.isNullOrEmpty() return contentEquals(other!!) } fun Collection.contentEquals(other: Collection): Boolean { if (this === other) return true if (this.size != other.size) return false return this.containsAll(other) && other.containsAll(this) } inline fun List.subArray(range: IntRange): Array { return Array(range.count()) { this[range.first + it] } } fun T.addTo(collection: MutableCollection): Boolean { return collection.add(this) } inline fun Collection.mapToArray(transform: (T) -> R): Array { return map(transform).toTypedArray() } inline fun List.mapToArray(transform: (T) -> R): Array { return Array(size) { transform(this[it]) } }