All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cedarsoft.guava.GuavaExtensions.kt Maven / Gradle / Ivy

There is a newer version: 8.9.2
Show newest version
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)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy