Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9137045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:02:30+00:00 2026-06-17T09:02:30+00:00

I have the following code: private def hasRole(role: String): Boolean = { var hasRole

  • 0

I have the following code:

private def hasRole(role: String): Boolean = {
  var hasRole = false;
  if(getUserDetails.isDefined){

    // getAuthorities returns java.util.Collection<GrantedAuthority>
    val authorities: util.Collection[_ <:GrantedAuthority] = getUserDetails.get.getAuthorities
    // Wrap the collection is a Scala class 
    val authoritiesWrapper = JCollectionWrapper.apply(authorities);
    for(authority <- authoritiesWrapper.iterator){
      if(authority.getAuthority == role){
        hasRole = true;
        scala.util.control.Breaks.break  
      }
    }
  }
  hasRole
}

The question is, is scala.util.control.Breaks.break the correct way toreturn when I’ve found the role? Doesn’t look right to me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:02:31+00:00Added an answer on June 17, 2026 at 9:02 am

    If you want to use breakable, you need to do it like so:

    import scala.util.control.Breaks._
    breakable {
      for (i <- 0 to 10000) { if (i>3) break }
    }
    

    But if you find yourself doing that often, you’re probably not using the collections library to its full extent. Try instead

    authoritiesWrapper.iterator.exists(_.getAuthority == role)
    

    Also, in the example you gave, you could also

    if (authority.getAuthority == role) return true
    

    When to choose which? In general, you should use the control-flow options in the collections library if you can. They generally are the fastest and clearest. Wait, fastest–why is that? Both break and return (from within a for or other context that requires a closure–basically anything but if and while and match) actually throw a stackless exception which is caught; in the break case it’s caught by breakable and in the return case it’s caught by the method. Creating a stack trace is really slow, but even stackless exceptions are kind of slow.

    So using the correct collections method–exists in this case–is the best solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: private def formatQuery(q:String = ):String = { val q2
I have the following code: private lazy val keys: List[String] = obj.getKeys().asScala.toList obj.getKeys returns
I have the following code: private String foo; public void setFoo(String bar) { foo
I have the following code: private static final Set<String> allowedParameters; static { Set<String> tmpSet
I have the following code: private static boolean hasTargetStyle(AttributeSet attributes) { final Enumeration<?> attributeNames
I have the following code object DispatchLibrary { private var nodes = Map.empty[java.util.UUID, List[BigInt]]
I have the following code: private DataSet GetDataSet(string tableName) { DbCommand cmd = GetConnection().CreateCommand();
I have the following code: private var xmlC:XMLListCollection = new XMLListCollection(); private var httpS:HTTPService
I have the following code in my user model: before_save :create_remember_token private def create_remember_token
I have two questions: If I have the following code: private boolean handleCollision(Rectangle_Double test)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.