int increment = 0;
if (StringUtils.isNotBlank(request.getParameter(NEXT_SCREEN_PARAMETER_NAME))) {
increment = 1;
} else if (StringUtils.isNotBlank(request.getParameter(PREV_SCREEN_PARAMETER_NAME))) {
increment = -1;
} else if (StringUtils.isNotBlank(request.getParameter(LAST_SCREEN_PARAMETER_NAME))) {
increment = Integer.MAX_VALUE;
}
I think you'd try to avoid setting up the problem this way to begin with, but if that's what you had to deal with, I think the clearest would be something like
def testParam(s: String) = StringUtils.isNotBlank(request.getParameter(s))
val increment = (
if (testParam(NEXT_SCREEN_PARAMETER_NAME)) 1
else if (testParam(PREV_SCREEN_PARAMETER_NAME)) -1
else if (testParam(LAST_SCREEN_PARAMETER_NAME)) Int.MaxValue
else 0
)
val ps = Seq(1 -> NEXT_SCREEN_PARAMETER_NAME,
-1 -> PREV_SCREEN_PARAMETER_NAME,
Int.MaxValue -> LAST_SCREEN_PARAMETER_NAME)
val test = StringUtils.isNotBlank(request.getParameter(_ : String))
(ps.view map { case (i,n) => i -> test(n) }) collect { case (i, true) => i } headOption getOrElse 0
Using scalaz, you can use the map map (∘∘) function:
ps.∘∘[PartialApply1Of2[Tuple2, Int]#Apply, String, Boolean](test)
collect { case (i, true) => i } headOption orZero
As always with Scalaz, it's a real shame that scala's type inference cannot infer partially-applied type constructors. Then you'd have:
(ps ∘∘ test) collect { case (i, true) => i } headOption orZero
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With