com.cedarsoft.guava.GuavaExtensions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of guava Show documentation
Show all versions of guava Show documentation
Path: :open:commons:guava
package com.cedarsoft.guava
import com.google.common.collect.ImmutableList
import com.google.common.collect.ImmutableMap
import com.google.common.collect.ImmutableSet
/**
* Contains extension methods for guava collections
* @author Johannes Schneider ([email protected])
*/
/**
* Creates a new copy including the additional element
*/
fun ImmutableList.copyAndAdd(elementToAdd: T): ImmutableList {
return ImmutableList
.builder()
.addAll(this)
.add(elementToAdd)
.build()
}
/**
* Converts a list to an immutable list
*/
fun List.toImmutable(): ImmutableList {
return ImmutableList.copyOf(this)
}
fun List.toImmutableSet(): ImmutableSet {
return ImmutableSet.copyOf(this)
}
/**
* Converts a set to an immutable set
*/
fun Set.toImmutable(): ImmutableSet {
return ImmutableSet.copyOf(this)
}
/**
* Converts a sequence to an immutable list
*/
fun Sequence.toImmutable(): ImmutableList {
val builder = ImmutableList.builder()
for (item in this) {
builder.add(item)
}
return builder.build()
}
/**
* Converts a sequence to an immutable set
*/
fun Sequence.toImmutableSet(): ImmutableSet {
val builder = ImmutableSet.builder()
for (item in this) {
builder.add(item)
}
return builder.build()
}
/**
* Converts to an immutable map
*/
fun Map.toImmutable(): ImmutableMap {
return ImmutableMap.copyOf(this)
}
fun Collection.toImmutableList(): ImmutableList {
return ImmutableList.copyOf(this)
}