com.mayabot.nlp.common.Guava.kt Maven / Gradle / Ivy
package com.mayabot.nlp.common
import java.io.File
import java.io.InputStream
import java.net.URL
import java.nio.charset.Charset
import java.util.*
import java.util.regex.Pattern
import kotlin.collections.ArrayList
fun checkNotNull(obj: Any?) {
kotlin.checkNotNull(obj)
}
object Guava {
@JvmStatic
fun join(iterable: Iterable, sp: String): String {
return iterable.joinToString(sp)
}
//
// fun trans(iterable: Iterable): Iterable {
//
// }
@JvmStatic
fun split(text: String, sp: String): List {
return text.split(sp).map { it.trim() }.filter { it.isNotEmpty() }
}
@JvmStatic
fun split(text: String, sp: Pattern): List {
return text.split(sp).map { it.trim() }.filter { it.isNotEmpty() }
}
@JvmStatic
fun openBufIns(file: File) = file.inputStream().buffered()
@JvmStatic
fun openResource(name: String): InputStream? = Guava::class.java.classLoader.getResourceAsStream(name)
/**
* Given a `resourceName` that is relative to `contextClass`,
* returns a `URL` pointing to the named resource.
*
* @throws IllegalArgumentException if the resource is not found
*/
@JvmStatic
fun getResource(contextClass: Class<*>, resourceName: String?): URL? {
val url = contextClass.getResource(resourceName)
return url
}
@JvmStatic
public fun mutiadd(map: MutableMap>, key: String, value: T) {
if (!map.containsKey(key)) {
map.put(key, ArrayList())
}
map[key]!!.add(value)
}
@JvmStatic
fun readLines(url: URL): List {
return url.readText(Charsets.UTF_8).lines()
}
@JvmStatic
fun concatIterables(vararg iter: Iterable): Iterable {
return sequenceOf(*iter).flatten().asIterable()
}
@JvmStatic
fun readLines(file: File, charset: Charset): List {
return file.readLines(charset)
}
@JvmStatic
fun checkNotNull(obj: Any?): Any {
return obj!!
}
}
object Lists {
@JvmStatic
fun newArrayList(): ArrayList {
return ArrayList()
}
@JvmStatic
fun newArrayList(vararg es: T): ArrayList {
return ArrayList(es.toList())
}
@JvmStatic
fun newArrayList(iter: Iterable): ArrayList {
val al = ArrayList()
iter.forEach {
al += it
}
return al
}
@JvmStatic
fun newArrayList(iter: Iterator): ArrayList {
val al = ArrayList()
iter.forEach {
al += it
}
return al
}
@JvmStatic
fun newArrayListWithExpectedSize(size: Int): ArrayList {
return ArrayList(computeArrayListCapacity(size))
}
@JvmStatic
fun newArrayListWithCapacity(size: Int): ArrayList {
return ArrayList(size)
}
private fun computeArrayListCapacity(arraySize: Int): Int {
return saturatedCast(5L + arraySize + arraySize / 10)
}
private fun saturatedCast(value: Long): Int {
if (value > Int.MAX_VALUE) {
return Int.MAX_VALUE
}
return if (value < Int.MIN_VALUE) {
Int.MIN_VALUE
} else value.toInt()
}
}
object Maps{
@JvmStatic
fun newHashMap(): HashMap {
return java.util.HashMap()
}
@JvmStatic
fun newHashMap(from: Map): HashMap {
return java.util.HashMap(from)
}
}
//TreeMap>
class TreeBasedTable {
private val map = TreeMap>()
fun put(row: R, col: C, v: V) {
val rowmap = map.getOrPut(row) { TreeMap() }
rowmap[col] = v
}
/**
* Returns the number of row key / column key / value mappings in the table.
*/
fun size(): Int {
var count = 0
map.values.forEach { count += it.size }
return count
}
fun rowKeySet(): Set {
return map.keys.toSet()
}
fun row(row: R): TreeMap? {
return map[row]
}
}