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

com.autonomousapps.internal.utils.collections.kt Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package com.autonomousapps.internal.utils

import org.gradle.api.file.FileCollection
import org.w3c.dom.Node
import org.w3c.dom.NodeList
import java.util.*
import java.util.zip.ZipEntry
import java.util.zip.ZipFile
import kotlin.collections.HashSet
import kotlin.collections.LinkedHashSet

/**
 * Transforms a [ZipFile] into a collection of [ZipEntry]s, which contains only class files (and not
 * the module-info.class file).
 */
internal fun ZipFile.asClassFiles(): Set {
  return entries().toList().filterToSetOfClassFiles()
}

/**
 * Filters a collection of [ZipEntry]s to contain only class files (and not the module-info.class
 * file).
 */
internal fun Iterable.filterToSetOfClassFiles(): Set {
  return filterToSet {
    it.name.endsWith(".class") && it.name != "module-info.class"
  }
}

/**
 * Filters a [FileCollection] to contain only class files (and not the module-info.class file).
 */
internal fun FileCollection.filterToClassFiles(): FileCollection {
  return filter {
    it.isFile && it.name.endsWith(".class") && it.name != "module-info.class"
  }
}

internal inline fun  Iterable.filterToSet(predicate: (T) -> Boolean): Set {
  return filterTo(HashSet(), predicate)
}

internal inline fun  Iterable.filterToOrderedSet(predicate: (T) -> Boolean): Set {
  return filterTo(TreeSet(), predicate)
}

internal inline fun  Iterable.filterToOrderedSet(
  comparator: Comparator, predicate: (T) -> Boolean
): Set {
  return filterTo(TreeSet(comparator), predicate)
}

internal fun  Iterable.filterNoneMatchingSorted(unwanted: Iterable): Set {
  return filterToOrderedSet { a ->
    unwanted.none { b ->
      a == b
    }
  }
}

internal inline fun  Iterable.mapToSet(transform: (T) -> R): Set {
  return mapTo(LinkedHashSet(collectionSizeOrDefault(10)), transform)
}

internal inline fun  Iterable.mapToOrderedSet(transform: (T) -> R): Set {
  return mapTo(TreeSet(), transform)
}

internal inline fun  Iterable.flatMapToSet(transform: (T) -> Iterable): Set {
  return flatMapToMutableSet(transform)
}

internal inline fun  Iterable.flatMapToMutableSet(transform: (T) -> Iterable): MutableSet {
  return flatMapTo(HashSet(collectionSizeOrDefault(10)), transform)
}

internal inline fun  Iterable.flatMapToOrderedSet(transform: (T) -> Iterable): Set {
  return flatMapTo(TreeSet(), transform)
}

internal fun  Iterable.collectionSizeOrDefault(default: Int): Int =
  if (this is Collection<*>) this.size
  else default

internal inline fun  Iterable.mapNotNullToSet(transform: (T) -> R?): Set {
  return mapNotNullTo(HashSet(), transform)
}

internal inline fun  Iterable.mapNotNullToOrderedSet(transform: (T) -> R?): Set {
  return mapNotNullTo(TreeSet(), transform)
}

internal fun  Iterable>.flattenToSet(): Set {
  val result = HashSet()
  for (element in this) {
    result.addAll(element)
  }
  return result
}

internal inline fun  NodeList.mapNotNull(transform: (Node) -> R?): List {
  val destination = ArrayList(length)
  for (i in 0 until length) {
    transform(item(i))?.let { destination.add(it) }
  }
  return destination
}

internal inline fun  NodeList.map(transform: (Node) -> R): List {
  val destination = ArrayList(length)
  for (i in 0 until length) {
    destination.add(transform(item(i)))
  }
  return destination
}

internal inline fun NodeList.filter(predicate: (Node) -> Boolean): List {
  val destination = ArrayList(length)
  for (i in 0 until length) {
    if (predicate(item(i))) destination.add(item(i))
  }
  return destination
}

internal inline fun NodeList.filterToSet(predicate: (Node) -> Boolean): Set {
  val destination = LinkedHashSet(length)
  for (i in 0 until length) {
    if (predicate(item(i))) destination.add(item(i))
  }
  return destination
}

// standard `all` function returns true if collection is empty!
internal inline fun  Collection.reallyAll(predicate: (T) -> Boolean): Boolean {
  if (isEmpty()) return false
  for (element in this) if (!predicate(element)) return false
  return true
}

internal fun  Set.efficient(): Set {
  return when {
    isEmpty() -> emptySet()
    size == 1 -> Collections.singleton(first())
    else -> this
  }
}

/**
 * Given a list of pairs, where the pairs are key -> value pairs, merge into a map (not losing any
 * keys).
 */
internal fun  List>>.mergedMap(): Map> {
  return foldRight(linkedMapOf>()) { (key, values), map ->
    map.apply {
      merge(key, values) { old, new -> old.apply { addAll(new) } }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy