This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.mipt; | |
import java.util.ArrayList; | |
// Такой комментарий надо либо удалять, либо изменять на осмысленный. | |
/** | |
* Created by IntelliJ IDEA. | |
* User: asus | |
* Date: 22.09.12 | |
* Time: 11:38 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}) | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def validateRequest(request: Request): Unit = { | |
require(request != null) | |
} | |
def regularRpcEndpoint(request: Request): Response = { | |
validateRequest(request) | |
if (!isPermittedViaRpc) { | |
throw new PermissionDeniedException("...") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
OlderNewer