com.increase.api.core.Utils.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of increase-java-core Show documentation
Show all versions of increase-java-core Show documentation
An SDK library for increase
@file:JvmName("Utils")
package com.increase.api.core
import com.google.common.collect.ImmutableListMultimap
import com.google.common.collect.ListMultimap
import com.google.common.collect.Multimaps
import com.increase.api.errors.IncreaseInvalidDataException
import java.util.Collections
@JvmSynthetic
internal fun T?.getOrThrow(name: String): T {
if (this == null) {
throw IncreaseInvalidDataException("'${name}' is not present")
}
return this
}
@JvmSynthetic
internal fun List.toUnmodifiable(): List {
if (isEmpty()) {
return Collections.emptyList()
}
return Collections.unmodifiableList(this)
}
@JvmSynthetic
internal fun Map.toUnmodifiable(): Map {
if (isEmpty()) {
return Collections.emptyMap()
}
return Collections.unmodifiableMap(this)
}
@JvmSynthetic
internal fun ListMultimap.toUnmodifiable(): ListMultimap {
if (isEmpty()) {
return ImmutableListMultimap.of()
}
return Multimaps.unmodifiableListMultimap(this)
}
@JvmSynthetic
internal fun ListMultimap.getRequiredHeader(header: String): String {
val value =
entries()
.stream()
.filter { entry -> entry.key.equals(header, ignoreCase = true) }
.map { entry -> entry.value }
.findFirst()
if (!value.isPresent) {
throw IncreaseInvalidDataException("Could not find $header header")
}
return value.get()
}
internal interface Enum