Skip to content

Instantly share code, notes, and snippets.

Avatar

Dmitry Komanov dkomanov

View GitHub Profile
@dkomanov
dkomanov / gist:3796414
Created Sep 27, 2012 — forked from AlexanderVNikitin/gist:3796396
Java calculator 2.0.
View gist:3796414
package ru.mipt;
import java.util.ArrayList;
// Такой комментарий надо либо удалять, либо изменять на осмысленный.
/**
* Created by IntelliJ IDEA.
* User: asus
* Date: 22.09.12
* Time: 11:38
@dkomanov
dkomanov / processRequestOld.scala
Created Jan 9, 2016
[scala] result code: old java style
View processRequestOld.scala
def processRequestOld(userId: UUID, requestId: UUID): BusinessResult = {
val userOpt = getUser(userId)
if (userOpt.isEmpty) {
return BusinessResult.UserNotFound
}
val requestOpt = getRequestById(requestId)
if (requestOpt.isEmpty) {
return BusinessResult.RequestNotFound
}
@dkomanov
dkomanov / processRequestWithException.scala
Created Jan 9, 2016
[scala] result code: use exceptions internally
View processRequestWithException.scala
def processRequestWithException(userId: UUID, requestId: UUID): BusinessResult = {
case class BusinessException(result: BusinessResult) extends RuntimeException
try {
val user = getUser(userId).getOrElse(throw new BusinessException(BusinessResult.UserNotFound))
val request = getRequestById(requestId).getOrElse(throw new BusinessException(BusinessResult.RequestNotFound))
checkAccess(request, user).toOption.getOrElse(throw new BusinessException(BusinessResult.NotOwner))
BusinessResult.Ok
} catch {
case be: BusinessException => be.result
View processRequestCollections.scala
def processRequestCollections(userId: UUID, requestId: UUID): BusinessResult = {
getUser(userId).fold(BusinessResult.UserNotFound)(user => {
getRequestById(requestId).fold(BusinessResult.RequestNotFound)(request => {
checkAccess(request, user).toOption.fold(BusinessResult.NotOwner)(_ => BusinessResult.Ok)
})
})
}
@dkomanov
dkomanov / processRequestEither.scala
Created Jan 9, 2016
[scala] result code: either
View processRequestEither.scala
def processRequestEither(userId: UUID, requestId: UUID): BusinessResult = {
val result: Either[BusinessResult, BusinessResult] = for {
user <- either(getUser(userId), BusinessResult.UserNotFound)
request <- either(getRequestById(requestId), BusinessResult.RequestNotFound)
_ <- either(checkAccess(request, user).toOption, BusinessResult.NotOwner)
} yield BusinessResult.Ok
result.fold(identity, identity)
}
@dkomanov
dkomanov / processRequestNew.scala
Created Jan 9, 2016
[scala] result code: either with some implicits
View processRequestNew.scala
def processRequestNew(userId: UUID, requestId: UUID): BusinessResult = {
for {
user <- getUser(userId) orResult BusinessResult.UserNotFound
request <- getRequestById(requestId) orResult BusinessResult.RequestNotFound
_ <- checkAccess(request, user) orResult BusinessResult.NotOwner
} yield BusinessResult.Ok
}
@dkomanov
dkomanov / package.scala
Created Jan 9, 2016
[scala] result code: either with some implicits 2
View package.scala
import scala.language.implicitConversions
import scala.util.Try
package object business {
implicit final class ModelOrResultFromOption[M](private val opt: Option[M])
extends AnyVal {
// converts Option to Either.RightProjection
def orResult[R](result: => R) =
opt.fold[Either[R, M]](Left(result))(Right(_)).right
View BlockingQueueBenchmark.java
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.util.ArrayList;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@dkomanov
dkomanov / RegularSyncWebApplication.scala
Last active May 22, 2020
[writing-async-app-in-scala-part-1] regular sync web application example
View RegularSyncWebApplication.scala
def validateRequest(request: Request): Unit = {
require(request != null)
}
def regularRpcEndpoint(request: Request): Response = {
validateRequest(request)
if (!isPermittedViaRpc) {
throw new PermissionDeniedException("...")
}
@dkomanov
dkomanov / NaiveRewriteToAsync.scala
Created May 22, 2020
[writing-async-app-in-scala-part-1] naive rewrite to async
View NaiveRewriteToAsync.scala
def regularRpcEndpoint(request: Request): Future[Response] = {
validateRequest(request)
isPermittedViaRpc.flatMap { isPermitted =>
if (!isPermitted) {
Future.failed(new PermissionDeniedException("..."))
} else {
retrieveFromDatabase(request.id)
.map(convertPayloadFromDb)
.map(Response)