I am in the process of learning how to code in Scala by translating from older Java code. The following code is an attempt in this learning process:
I created a class as below. Now, can the if statements in this code be replaced by case match block ? I am specifically looking to do a case match with a fall through default case that handles an exceptional case. How can I accomplish this?
class MyTest {
//ResEmail is a case class
//MultipartEnityBuilder is from the httpmime-4.4.jar
def makeResponse(resEmail: ResEmail): HttpEntity = {
val maker = MultipartEntityBuilder.create()
maker.addTextBody("api_user", this.apikey)
maker.addTextBody("api_key", this.apivalue)
val tos = resEmail.toList
val tonames = resEmail.toNameList
val ccs = resEmail.ccS
if (tos.length == 0) {
builder.addTextBody(stringToFormattedString(ARG_TO), respEmail.fromEmail, ContentType.create("text/plain", "UTF-8"))
}
else if(tos.length > 0) {
for (i <- 0 until tos.length)
maker.addTextBody(PARAM_TO.format(PARAM_TO, i), tos(i), ContentType.create("text/plain", "UTF-8"))
}
if (resEmail.fromEmail != null && !resEmail.fromEmail.isEmpty) maker.addTextBody(PARAM_FROM, resEmail.fromEmail,
ContentType.create("text/plain", "UTF-8"))
if (resEmail.fromName != null && !resEmail.fromName.isEmpty) maker.addTextBody(PARAM_FROMNAME,
respEmail.fromName, ContentType.create("text/plain", "UTF-8"))
if (respEmail.replyToEmail != null && !resEmail.replyToEmail.isEmpty) maker.addTextBody(PARAM_REPLYTO,
respEmail.replyToEmail, ContentType.create("text/plain", "UTF-8"))
if (resEmail.subject != null && !resEmail.subject.isEmpty) maker.addTextBody(PARAM_SUBJECT,
resEmail.subject, ContentType.create("text/plain", "UTF-8"))
val tmpString = new MyExpApi().jsonString()
if (tmpString != "{}") maker.addTextBody(PARAM_MYSMTPAPI, tmpString, ContentType.create("text/plain",
"UTF-8"))
maker.build()
}
}// end of class MyTest