chiappone
chiappone

Reputation: 2908

Convert Java method to Scala method

I need to convert the following java method to scala and am having difficulty because scala doesn't allow you to return values in the middle of methods. Could someone give me a hand or at least a start on how to convert this:

public boolean isAllowed(String method, String path, Map<String,String> apiUrlMap) {

    if (apiUrlMap != null) {
        Set<Entry<String, String>> apiSet = apiUrlMap.entrySet();

        for (Entry<String, String> apiUrl : apiSet) {
            String aUrl = apiUrl.getKey();
            String aMeth = apiUrl.getValue();
            if (aUrl.equals("#")) {
                if (aMeth.contains(method)) {
                    return true;
                }
            }

            if (aUrl.endsWith("#")) {
                String testUrl = aUrl.replaceFirst("/#", "");
                if (path.startsWith(testUrl)) {
                    if (aMeth.contains(method)) {
                        return true;
                    }
                }
            }

            if (aUrl.equals(path) || path.equals(aUrl +"/")) {
                if (aMeth.contains(method)) {
                    return true;
                }
            }

        }
    }

    return false;
}

Upvotes: 0

Views: 242

Answers (3)

Nicolas
Nicolas

Reputation: 24769

The idea in scala is to use function call instead of control structure. Here is a (quickly written) example:

def isAllowed(
    method: String,
    path: String,
    apiUrlMap: Option[Map[String, String]]) =

  apiUrlMap flatMap {m =>
    m collectFirst {
      case ("#", aMeth) 
          if aMeth contains `method` =>
        true 
      case (aUrl, aMeth)
          if (aUrl endsWith "#") && 
             (path startsWith aUrl.replaceFirst("/#", "")) &&
             (aMeth contains `method`) =>
        true 
      case (`path`, aMeth)
          if (aMeth contains `method`) =>
        true 
      case (aUrl, aMeth)
          if (path == aUrl + "/") && 
             (aMeth contains `method`) =>
        true 
    }
  } getOrElse false

Upvotes: 1

hbatista
hbatista

Reputation: 1237

Having a quick go at it (not tested)

def isAllowed(method:String, path:String, apiUrlMap:Map[String,String]) = 
  apiUrlMap exists { case (aUrl, aMeth) => {
        aUrl.equals("#") ||  
        aUrl.endsWith("#") && path.startsWith(aUrl.replaceFirst("/#", "")) || 
        (aUrl.equals(path) || path.equals(aUrl +"/"))
      } && aMeth.contains(method)
    }

Upvotes: 4

dhg
dhg

Reputation: 52691

Scala does let you return, it's just not recommended style.

For example, the following works fine:

def f(x: Int): String = {
  if(x > 5)
    return "greater"
  else
    return "less"
}

One catch is that Scala requires you to have an explicit return type specified if you use the return keyword.

The better style would be to simply have the if-expression be the last expression of the method:

def f(x: Int): String = {
  if(x > 5)
    "greater"
  else
    "less"
}

Upvotes: 7

Related Questions