mirror of
https://github.com/dyrkin/luxmed-bot.git
synced 2025-12-21 13:23:05 +01:00
Initial commit
This commit is contained in:
5
common/build.gradle
Normal file
5
common/build.gradle
Normal file
@@ -0,0 +1,5 @@
|
||||
dependencies {
|
||||
compile group: "org.slf4j", name: "slf4j-api", version: "1.7.25"
|
||||
compile group: "ch.qos.logback", name: "logback-classic", version: "1.2.3"
|
||||
compile group: "ch.qos.logback", name: "logback-core", version: "1.2.3"
|
||||
}
|
||||
38
common/src/main/scala/com/lbs/common/Implicits.scala
Normal file
38
common/src/main/scala/com/lbs/common/Implicits.scala
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2018 Yevhen Zadyra
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package com.lbs.common
|
||||
|
||||
import java.util.Optional
|
||||
|
||||
import scala.language.implicitConversions
|
||||
|
||||
object Implicits {
|
||||
implicit def optionalToOption[T](optional: Optional[T]): Option[T] = {
|
||||
Option(optional.orElse(null.asInstanceOf[T]))
|
||||
}
|
||||
|
||||
implicit def optionToOptional[T](option: Option[T]): Optional[T] = {
|
||||
Optional.of(option.getOrElse(null.asInstanceOf[T]))
|
||||
}
|
||||
}
|
||||
66
common/src/main/scala/com/lbs/common/Logger.scala
Normal file
66
common/src/main/scala/com/lbs/common/Logger.scala
Normal file
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2018 Yevhen Zadyra
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package com.lbs.common
|
||||
|
||||
import org.slf4j
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
trait Logger {
|
||||
private val log: slf4j.Logger = LoggerFactory.getLogger(this.getClass)
|
||||
|
||||
protected val LOG = new LoggerWrapper
|
||||
|
||||
class LoggerWrapper {
|
||||
def debug(msg: => String): Unit = {
|
||||
if (log.isDebugEnabled)
|
||||
log.debug(msg)
|
||||
}
|
||||
|
||||
def warn(msg: => String): Unit = {
|
||||
if (log.isWarnEnabled)
|
||||
log.warn(msg)
|
||||
}
|
||||
|
||||
def warn(msg: => String, throwable: Throwable): Unit = {
|
||||
if (log.isWarnEnabled)
|
||||
log.warn(msg, throwable)
|
||||
}
|
||||
|
||||
def error(msg: => String): Unit = {
|
||||
if (log.isErrorEnabled)
|
||||
log.error(msg)
|
||||
}
|
||||
|
||||
def error(msg: => String, throwable: Throwable): Unit = {
|
||||
if (log.isErrorEnabled)
|
||||
log.error(msg, throwable)
|
||||
}
|
||||
|
||||
def info(msg: => String): Unit = {
|
||||
if (log.isInfoEnabled)
|
||||
log.info(msg)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
47
common/src/main/scala/com/lbs/common/ModelConverters.scala
Normal file
47
common/src/main/scala/com/lbs/common/ModelConverters.scala
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2018 Yevhen Zadyra
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package com.lbs.common
|
||||
|
||||
import scala.collection.generic.CanBuildFrom
|
||||
import scala.language.{higherKinds, implicitConversions}
|
||||
|
||||
trait ModelConverters {
|
||||
|
||||
trait CollectionConverter[-In, Out] {
|
||||
def convert[Z <: In, Col[X] <: Iterable[X]](col: Col[Z])(implicit bf: CanBuildFrom[Col[Z], Out, Col[Out]]): Col[Out]
|
||||
}
|
||||
|
||||
trait ObjectConverter[-In, Out] {
|
||||
def convert[Z <: In](any: Z): Out
|
||||
}
|
||||
|
||||
implicit class CollectionOps[From, Col[X] <: Iterable[X]](col: Col[From]) {
|
||||
def mapTo[To](implicit converter: CollectionConverter[From, To], bf: CanBuildFrom[Col[From], To, Col[To]]): Col[To] = converter.convert(col)
|
||||
}
|
||||
|
||||
implicit class ObjectOps[From](anyRef: From) {
|
||||
def mapTo[To](implicit converter: ObjectConverter[From, To]): To = converter.convert(anyRef)
|
||||
}
|
||||
|
||||
}
|
||||
32
common/src/main/scala/com/lbs/common/ParametrizedLock.scala
Normal file
32
common/src/main/scala/com/lbs/common/ParametrizedLock.scala
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2018 Yevhen Zadyra
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package com.lbs.common
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
class ParametrizedLock[K] {
|
||||
private val locks = new ConcurrentHashMap[K, AnyRef]
|
||||
|
||||
def obtainLock(key: K): AnyRef = locks.computeIfAbsent(key, k => new AnyRef)
|
||||
}
|
||||
41
common/src/main/scala/com/lbs/common/Scheduler.scala
Normal file
41
common/src/main/scala/com/lbs/common/Scheduler.scala
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2018 Yevhen Zadyra
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package com.lbs.common
|
||||
|
||||
import java.util.concurrent.{Executors, ScheduledFuture}
|
||||
|
||||
import scala.concurrent.duration.FiniteDuration
|
||||
|
||||
class Scheduler(poolSize: Int) {
|
||||
private val scheduledThreadPool = Executors.newScheduledThreadPool(poolSize)
|
||||
|
||||
def schedule(fn: => Unit, period: FiniteDuration): ScheduledFuture[_] = {
|
||||
scheduledThreadPool.scheduleAtFixedRate(() => fn, period.length, period.length, period.unit)
|
||||
}
|
||||
|
||||
def schedule(fn: => Unit, delay: FiniteDuration, period: FiniteDuration): ScheduledFuture[_] = {
|
||||
require(delay.unit == period.unit, s"Delay units must be the same as for period ${period.unit}")
|
||||
scheduledThreadPool.scheduleAtFixedRate(() => fn, delay.length, period.length, period.unit)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user