Merge pull request #15 from TouchInstinct/common-measure
add common-measure
This commit is contained in:
commit
e2b51ac761
|
|
@ -87,3 +87,11 @@ Interceptor для логирования запросов/ответов.
|
|||
## response-wrapper-spring-web
|
||||
|
||||
Добавляет обертку для успешного ответа
|
||||
|
||||
## common-measure
|
||||
|
||||
Утилиты для работы с `measure`
|
||||
|
||||
## common-measure-spring
|
||||
|
||||
Возможность задавать `measure` через `properties`
|
||||
|
|
|
|||
|
|
@ -61,6 +61,9 @@ subprojects {
|
|||
dependency("org.mockito:mockito-inline:2.13.0")
|
||||
|
||||
dependency("com.github.zafarkhaja:java-semver:0.9.0")
|
||||
|
||||
dependency("javax.measure:unit-api:2.1.2")
|
||||
dependency("tech.units:indriya:2.1.2")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
plugins {
|
||||
id("kotlin")
|
||||
id("kotlin-spring")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(project(":common-measure"))
|
||||
|
||||
implementation("org.springframework.boot:spring-boot")
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package ru.touchin.common.measure
|
||||
|
||||
import org.springframework.context.annotation.Import
|
||||
import ru.touchin.common.measure.configurations.MeasureConfiguration
|
||||
|
||||
@Suppress("unused")
|
||||
@Import(value = [MeasureConfiguration::class])
|
||||
annotation class EnableTouchinMeasure
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
@file:Suppress("unused")
|
||||
|
||||
package ru.touchin.common.measure.components
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding
|
||||
import org.springframework.core.convert.converter.Converter
|
||||
import org.springframework.stereotype.Component
|
||||
import tech.units.indriya.quantity.Quantities
|
||||
import javax.measure.Quantity
|
||||
import javax.measure.quantity.Length
|
||||
|
||||
@Component
|
||||
@ConfigurationPropertiesBinding
|
||||
class DistancePropertyConverter : Converter<String, Quantity<Length>> {
|
||||
|
||||
override fun convert(source: String): Quantity<Length>? {
|
||||
if (source.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
|
||||
return Quantities.getQuantity(source).asType(Length::class.java)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package ru.touchin.common.measure.configurations
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan
|
||||
import org.springframework.context.annotation.Configuration
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("ru.touchin.common.measure")
|
||||
class MeasureConfiguration
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
plugins {
|
||||
id("kotlin")
|
||||
id("kotlin-spring")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api("javax.measure:unit-api")
|
||||
api("tech.units:indriya")
|
||||
|
||||
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
@file:Suppress("unused")
|
||||
|
||||
package ru.touchin.common.measure
|
||||
|
||||
import tech.units.indriya.unit.Units
|
||||
import javax.measure.MetricPrefix
|
||||
import javax.measure.Quantity
|
||||
import javax.measure.Unit
|
||||
import javax.measure.quantity.Length
|
||||
|
||||
object MeasureUtils {
|
||||
|
||||
private val Kilometer: Unit<Length> = MetricPrefix.KILO(Units.METRE)
|
||||
|
||||
fun Quantity<Length>.toKilometers(): Double {
|
||||
return to(Kilometer).value.toDouble()
|
||||
}
|
||||
|
||||
fun Quantity<Length>.isMore(quantity: Quantity<Length>): Boolean {
|
||||
return this.subtract(quantity).value.toDouble() > 0
|
||||
}
|
||||
|
||||
fun Quantity<Length>.isMoreOrEquals(quantity: Quantity<Length>): Boolean {
|
||||
return this.subtract(quantity).value.toDouble() >= 0
|
||||
}
|
||||
|
||||
fun Quantity<Length>.isLess(quantity: Quantity<Length>): Boolean {
|
||||
return !isMoreOrEquals(quantity)
|
||||
}
|
||||
|
||||
fun Quantity<Length>.isLessOrEquals(quantity: Quantity<Length>): Boolean {
|
||||
return !isMore(quantity)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -25,6 +25,8 @@ include("common-spring-jpa")
|
|||
include("common-spring-web")
|
||||
include("common-spring-test")
|
||||
include("common-spring-test-jpa")
|
||||
include("common-measure")
|
||||
include("common-measure-spring")
|
||||
include("logger")
|
||||
include("logger-spring")
|
||||
include("logger-spring-web")
|
||||
|
|
|
|||
Loading…
Reference in New Issue