Initial commit

This commit is contained in:
Eugene Zadyra
2018-05-31 00:28:58 +02:00
commit 68557d960a
116 changed files with 8590 additions and 0 deletions

5
common/build.gradle Normal file
View 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"
}

View 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]))
}
}

View 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)
}
}
}

View 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)
}
}

View 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)
}

View 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)
}
}