Skip to content
This repository has been archived by the owner on Nov 2, 2021. It is now read-only.

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sokomishalov committed Mar 19, 2020
1 parent 3b1fbd0 commit e635b48
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import java.util.zip.ZipInputStream
class ZipIterator(
private val zis: ZipInputStream
) : Iterator<ZipEntry> {
var next: ZipEntry? = null
private var next: ZipEntry? = null

override operator fun hasNext(): Boolean {
next = zis.nextEntry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("unused")
@file:Suppress("unused", "NOTHING_TO_INLINE")

package ru.sokomishalov.commons.core.reflection

Expand All @@ -22,7 +22,7 @@ package ru.sokomishalov.commons.core.reflection
*/


fun <T : Any> Class<T>.unwrapCompanionClass(): Class<*> {
inline fun <T : Any> Class<T>.unwrapCompanionClass(): Class<*> {
return when {
name.endsWith("\$Companion") -> enclosingClass ?: this
else -> this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,81 +24,10 @@ import kotlin.contracts.contract
* @author sokomishalov
*/

@UseExperimental(ExperimentalContracts::class)
@OptIn(ExperimentalContracts::class)
inline fun CharSequence?.isNotNullOrBlank(): Boolean {
contract {
returns(true) implies (this@isNotNullOrBlank != null)
}
return (this == null || this.isBlank()).not()
}

// https://stackoverflow.com/a/1248627/5843129
fun String.convertGlobToRegex(): Regex {
val sb = StringBuilder(length)
var inGroup = 0
var inClass = 0
var firstIndexInClass = -1
val arr = toCharArray()
var i = 0
while (i < arr.size) {
when (val ch = arr[i]) {
'\\' -> if (++i >= arr.size) {
sb.append('\\')
} else {
val next = arr[i]
when (next) {
',' -> {
}
'Q', 'E' -> {
// extra escape needed
sb.append('\\')
sb.append('\\')
}
else -> sb.append('\\')
}// escape not needed
sb.append(next)
}
'*' -> if (inClass == 0)
sb.append(".*")
else
sb.append('*')
'?' -> if (inClass == 0)
sb.append('.')
else
sb.append('?')
'[' -> {
inClass++
firstIndexInClass = i + 1
sb.append('[')
}
']' -> {
inClass--
sb.append(']')
}
'.', '(', ')', '+', '|', '^', '$', '@', '%' -> {
if (inClass == 0 || firstIndexInClass == i && ch == '^')
sb.append('\\')
sb.append(ch)
}
'!' -> if (firstIndexInClass == i)
sb.append('^')
else
sb.append('!')
'{' -> {
inGroup++
sb.append('(')
}
'}' -> {
inGroup--
sb.append(')')
}
',' -> if (inGroup > 0)
sb.append('|')
else
sb.append(',')
else -> sb.append(ch)
}
i++
}
return sb.toString().toRegex()
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("unused")
@file:Suppress("unused", "NOTHING_TO_INLINE")

package ru.sokomishalov.commons.core.time

import java.time.Duration
import java.time.Period
import java.time.temporal.TemporalAmount

/**
* @author sokomishalov
*/

fun Duration.humanReadable(): String? {
inline fun Duration.humanReadable(): String? {
return toString()
.substring(2)
.replace("(\\d[HMS])(?!$)".toRegex(), "$1 ")
.toLowerCase()
}

inline val TemporalAmount.ms: Long
get() = when (this) {
is Duration -> this.toMillis()
is Period -> this.days.toLong() * 24 * 60 * 60 * 1000
else -> Duration.from(this).toMillis()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("MemberVisibilityCanBePrivate")

package ru.sokomishalov.commons.core.log

import org.slf4j.Logger
Expand All @@ -31,15 +33,12 @@ object CustomLoggerFactory {
return getLogger(nonCompanionClazz.name)
}

@Suppress("MemberVisibilityCanBePrivate")
fun getLogger(className: String): Logger {
val logger = loggersMap[className]
return when {
logger != null -> logger
else -> {
val newLogger = loggerFor(className)
loggersMap[className] = newLogger
newLogger
else -> loggerFor(className).also {
loggersMap[className] = it
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("unused")
@file:Suppress("unused", "NOTHING_TO_INLINE")

package ru.sokomishalov.commons.core.log

Expand All @@ -26,13 +26,13 @@ import kotlin.reflect.KClass
* @author sokomishalov
*/

fun <T : Any> loggerFor(clazz: Class<T>): Logger = getLogger(clazz)
inline fun <T : Any> loggerFor(clazz: Class<T>): Logger = getLogger(clazz)

fun <T : Any> loggerFor(clazz: KClass<T>): Logger = getLogger(clazz.java)
inline fun <T : Any> loggerFor(clazz: KClass<T>): Logger = getLogger(clazz.java)

fun loggerFor(className: String): Logger = getLogger(className)
inline fun loggerFor(className: String): Logger = getLogger(className)

inline fun <reified T : Loggable> T.logger(): Logger = loggerFor(javaClass)
inline fun <reified T : Loggable> T.logger(): Logger = logger

inline fun <reified T : Any> T.loggerDelegate(): Lazy<Logger> = lazy { loggerFor(javaClass.unwrapCompanionClass()) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class LogUtilsTest {

class LogWithInterface : Loggable {
fun doJob() {
log("interface")
logInfo { "interface" }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,26 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("UNCHECKED_CAST", "unused")
@file:Suppress(
"UNCHECKED_CAST",
"unused",
"NOTHING_TO_INLINE"
)

package ru.sokomishalov.commons.spring.cache

import org.springframework.cache.Cache
import org.springframework.cache.CacheManager

/**
* @author sokomishalov
*/
// until merged
// https://github.com/spring-projects/spring-framework/pull/23927/files

fun <V> CacheManager.put(cacheName: String, key: String, value: V?) {
getCache(cacheName)?.put(key, value)
}
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
inline fun <reified T : Any> Cache.get(key: Any): T? = get(key, T::class.java)

operator fun <V> CacheManager.set(cacheName: String, key: String, value: V?) {
put(cacheName, key, value)
}
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
inline operator fun Cache.get(key: Any): Cache.ValueWrapper? = get(key)

fun CacheManager.evict(cacheName: String, key: String) {
getCache(cacheName)?.evict(key)
}
inline operator fun Cache.set(key: Any, value: Any?) = put(key, value)

operator fun <V> CacheManager.get(cacheName: String, key: String): V? {
return when (val value = getCache(cacheName)?.get(key)?.get()) {
null -> null
else -> value as V
}
}
inline operator fun CacheManager.get(name: String): Cache? = getCache(name)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("unused", "NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
@file:Suppress("unused")

package ru.sokomishalov.commons.spring.webclient

Expand All @@ -39,24 +39,23 @@ val REACTIVE_WEB_CLIENT: WebClient = createReactiveWebClient()

fun createReactiveWebClient(
baseUrl: String? = null,
fixedThreadPoolSize: Int? = null,
reactorNettyClient: HttpClient = createReactorNettyClient(baseUrl = baseUrl, fixedThreadPoolSize = fixedThreadPoolSize),
reactorNettyClient: HttpClient = createReactorNettyClient(baseUrl = baseUrl),
clientConnector: ClientHttpConnector = ReactorClientHttpConnector(reactorNettyClient),
filters: List<ExchangeFilterFunction> = emptyList(),
encoder: Jackson2JsonEncoder = JACKSON_ENCODER,
decoder: Jackson2JsonDecoder = JACKSON_DECODER,
maxBufferSize: Int? = null,
codecs: CodecConfigurer.DefaultCodecs.() -> Unit = {
jackson2JsonEncoder(encoder)
jackson2JsonDecoder(decoder)
maxBufferSize?.let { maxInMemorySize(it) }
},
filters: List<ExchangeFilterFunction> = emptyList()
}
): WebClient {
return WebClient
.builder()
.run {
when {
baseUrl.isNotNullOrBlank() -> this.baseUrl(baseUrl)
baseUrl.isNotNullOrBlank() -> baseUrl(baseUrl)
else -> this
}
}
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<properties>
<java.version>1.8</java.version>
<kotlin.version>1.3.70</kotlin.version>
<kotlinx-coroutines.version>1.3.4</kotlinx-coroutines.version>
<kotlinx-coroutines.version>1.3.5</kotlinx-coroutines.version>
<jackson.version>2.10.3</jackson.version>
<reactor.version>3.3.3.RELEASE</reactor.version>
<reactor-netty.version>0.9.5.RELEASE</reactor-netty.version>
Expand Down

0 comments on commit e635b48

Please sign in to comment.