From e635b484f75e9aeb0a9ec515831034239d72ecb2 Mon Sep 17 00:00:00 2001 From: sokomishalov Date: Thu, 19 Mar 2020 13:09:51 +0300 Subject: [PATCH] minor fixes --- .../commons/core/io/ZipIterator.kt | 2 +- .../core/reflection/ReflectionUtils.kt | 4 +- .../commons/core/string/StringExtensions.kt | 73 +------------------ .../commons/core/time/DateTimeExtensions.kt | 15 +--- .../commons/core/log/CustomLoggerFactory.kt | 9 +-- .../sokomishalov/commons/core/log/LogUtils.kt | 10 +-- .../commons/core/log/LogUtilsTest.kt | 2 +- .../spring/cache/CacheManagerExtensions.kt | 33 ++++----- .../webclient/ReactiveWebClientUtils.kt | 11 ++- pom.xml | 2 +- 10 files changed, 37 insertions(+), 124 deletions(-) diff --git a/commons-core/src/main/kotlin/ru/sokomishalov/commons/core/io/ZipIterator.kt b/commons-core/src/main/kotlin/ru/sokomishalov/commons/core/io/ZipIterator.kt index b6e7e6a..720a3f5 100644 --- a/commons-core/src/main/kotlin/ru/sokomishalov/commons/core/io/ZipIterator.kt +++ b/commons-core/src/main/kotlin/ru/sokomishalov/commons/core/io/ZipIterator.kt @@ -24,7 +24,7 @@ import java.util.zip.ZipInputStream class ZipIterator( private val zis: ZipInputStream ) : Iterator { - var next: ZipEntry? = null + private var next: ZipEntry? = null override operator fun hasNext(): Boolean { next = zis.nextEntry diff --git a/commons-core/src/main/kotlin/ru/sokomishalov/commons/core/reflection/ReflectionUtils.kt b/commons-core/src/main/kotlin/ru/sokomishalov/commons/core/reflection/ReflectionUtils.kt index 7f06d79..b40f1d0 100644 --- a/commons-core/src/main/kotlin/ru/sokomishalov/commons/core/reflection/ReflectionUtils.kt +++ b/commons-core/src/main/kotlin/ru/sokomishalov/commons/core/reflection/ReflectionUtils.kt @@ -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 @@ -22,7 +22,7 @@ package ru.sokomishalov.commons.core.reflection */ -fun Class.unwrapCompanionClass(): Class<*> { +inline fun Class.unwrapCompanionClass(): Class<*> { return when { name.endsWith("\$Companion") -> enclosingClass ?: this else -> this diff --git a/commons-core/src/main/kotlin/ru/sokomishalov/commons/core/string/StringExtensions.kt b/commons-core/src/main/kotlin/ru/sokomishalov/commons/core/string/StringExtensions.kt index 5ac8a19..0283f77 100644 --- a/commons-core/src/main/kotlin/ru/sokomishalov/commons/core/string/StringExtensions.kt +++ b/commons-core/src/main/kotlin/ru/sokomishalov/commons/core/string/StringExtensions.kt @@ -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() } \ No newline at end of file diff --git a/commons-core/src/main/kotlin/ru/sokomishalov/commons/core/time/DateTimeExtensions.kt b/commons-core/src/main/kotlin/ru/sokomishalov/commons/core/time/DateTimeExtensions.kt index 5cd3dbf..1ca58e9 100644 --- a/commons-core/src/main/kotlin/ru/sokomishalov/commons/core/time/DateTimeExtensions.kt +++ b/commons-core/src/main/kotlin/ru/sokomishalov/commons/core/time/DateTimeExtensions.kt @@ -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() - } \ No newline at end of file +} \ No newline at end of file diff --git a/commons-logging/src/main/kotlin/ru/sokomishalov/commons/core/log/CustomLoggerFactory.kt b/commons-logging/src/main/kotlin/ru/sokomishalov/commons/core/log/CustomLoggerFactory.kt index d6fb660..fbf58d7 100644 --- a/commons-logging/src/main/kotlin/ru/sokomishalov/commons/core/log/CustomLoggerFactory.kt +++ b/commons-logging/src/main/kotlin/ru/sokomishalov/commons/core/log/CustomLoggerFactory.kt @@ -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 @@ -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 } } } diff --git a/commons-logging/src/main/kotlin/ru/sokomishalov/commons/core/log/LogUtils.kt b/commons-logging/src/main/kotlin/ru/sokomishalov/commons/core/log/LogUtils.kt index 39e073e..842b53c 100644 --- a/commons-logging/src/main/kotlin/ru/sokomishalov/commons/core/log/LogUtils.kt +++ b/commons-logging/src/main/kotlin/ru/sokomishalov/commons/core/log/LogUtils.kt @@ -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 @@ -26,13 +26,13 @@ import kotlin.reflect.KClass * @author sokomishalov */ -fun loggerFor(clazz: Class): Logger = getLogger(clazz) +inline fun loggerFor(clazz: Class): Logger = getLogger(clazz) -fun loggerFor(clazz: KClass): Logger = getLogger(clazz.java) +inline fun loggerFor(clazz: KClass): Logger = getLogger(clazz.java) -fun loggerFor(className: String): Logger = getLogger(className) +inline fun loggerFor(className: String): Logger = getLogger(className) -inline fun T.logger(): Logger = loggerFor(javaClass) +inline fun T.logger(): Logger = logger inline fun T.loggerDelegate(): Lazy = lazy { loggerFor(javaClass.unwrapCompanionClass()) } diff --git a/commons-logging/src/test/kotlin/ru/sokomishalov/commons/core/log/LogUtilsTest.kt b/commons-logging/src/test/kotlin/ru/sokomishalov/commons/core/log/LogUtilsTest.kt index 05a4f9e..1ae849f 100644 --- a/commons-logging/src/test/kotlin/ru/sokomishalov/commons/core/log/LogUtilsTest.kt +++ b/commons-logging/src/test/kotlin/ru/sokomishalov/commons/core/log/LogUtilsTest.kt @@ -34,7 +34,7 @@ class LogUtilsTest { class LogWithInterface : Loggable { fun doJob() { - log("interface") + logInfo { "interface" } } } diff --git a/commons-spring/src/main/kotlin/ru/sokomishalov/commons/spring/cache/CacheManagerExtensions.kt b/commons-spring/src/main/kotlin/ru/sokomishalov/commons/spring/cache/CacheManagerExtensions.kt index b5a23c6..7a2778c 100644 --- a/commons-spring/src/main/kotlin/ru/sokomishalov/commons/spring/cache/CacheManagerExtensions.kt +++ b/commons-spring/src/main/kotlin/ru/sokomishalov/commons/spring/cache/CacheManagerExtensions.kt @@ -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 CacheManager.put(cacheName: String, key: String, value: V?) { - getCache(cacheName)?.put(key, value) -} +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +inline fun Cache.get(key: Any): T? = get(key, T::class.java) -operator fun 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 CacheManager.get(cacheName: String, key: String): V? { - return when (val value = getCache(cacheName)?.get(key)?.get()) { - null -> null - else -> value as V - } -} \ No newline at end of file +inline operator fun CacheManager.get(name: String): Cache? = getCache(name) \ No newline at end of file diff --git a/commons-spring/src/main/kotlin/ru/sokomishalov/commons/spring/webclient/ReactiveWebClientUtils.kt b/commons-spring/src/main/kotlin/ru/sokomishalov/commons/spring/webclient/ReactiveWebClientUtils.kt index 78a4171..cd73a91 100644 --- a/commons-spring/src/main/kotlin/ru/sokomishalov/commons/spring/webclient/ReactiveWebClientUtils.kt +++ b/commons-spring/src/main/kotlin/ru/sokomishalov/commons/spring/webclient/ReactiveWebClientUtils.kt @@ -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 @@ -39,9 +39,9 @@ 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 = emptyList(), encoder: Jackson2JsonEncoder = JACKSON_ENCODER, decoder: Jackson2JsonDecoder = JACKSON_DECODER, maxBufferSize: Int? = null, @@ -49,14 +49,13 @@ fun createReactiveWebClient( jackson2JsonEncoder(encoder) jackson2JsonDecoder(decoder) maxBufferSize?.let { maxInMemorySize(it) } - }, - filters: List = emptyList() + } ): WebClient { return WebClient .builder() .run { when { - baseUrl.isNotNullOrBlank() -> this.baseUrl(baseUrl) + baseUrl.isNotNullOrBlank() -> baseUrl(baseUrl) else -> this } } diff --git a/pom.xml b/pom.xml index e765c5f..303e879 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ 1.8 1.3.70 - 1.3.4 + 1.3.5 2.10.3 3.3.3.RELEASE 0.9.5.RELEASE