简体   繁体   中英

Converting Java code into Groovy

I am trying to convert a Java function into equivalent Groovy code, but I am not able to find anything which does && operation in loop. Can anyone guide me through..

So far this is what I got

public List getAlert(def searchParameters, def numOfResult) throws UnsupportedEncodingException
{
    List respList=null
    respList = new ArrayList()
    String[] searchStrings = searchParameters.split(",")
    try
    {
        for(strIndex in searchStrings)
        {
            IQueryResult result = search(searchStrings[strIndex])
            if(result!=null)
            {
                def count = 0

                /*The below line gives me error*/
                for(it in result.document && count < numOfResult)
                {

                }
            }
        }
    }   
    catch(Exception e)
    {
        e.printStackTrace()
    }
}

My Java code

public List getAlert(String searchParameters, int numOfResult) throws UnsupportedEncodingException
{
     List respList = null
     respList = new ArrayList()
     String[] searchStrings = searchParameters.split(",")
     try {
       for (int strIndex = 0; strIndex < searchStrings.length; strIndex++) {
         IQueryResult result = search(searchStrings[strIndex])
         if (result != null) {
           ListIterator it = result.documents()
           int count = 0
           while ((it.hasNext()) && (count < numOfResult)) {
             IDocumentSummary summary = (IDocumentSummary)it.next()

             if (summary != null) {

               String docid = summary.getSummaryField("infadocid").getStringValue()
               int index = docid.indexOf("#")
               docid = docid.substring(index + 1)


               String url = summary.getSummaryField("url").getStringValue()
               int i = url.indexOf("/", 8)
               String endURL = url.substring(i + 1, url.length())
               String body = summary.getSummaryField("infadocumenttitle").getStringValue()

               String frontURL = produrl + endURL
               String strURL
               strURL = frontURL
               strURL = body
               String strDocId 
               strDocId = frontURL
               strDocId = docid

               count++
             }
           }
         }
         result = null
       }
     } catch (Exception e) {
       e.printStackTrace()
       return respList
     }
     return respList
   }

It seems to me like

def summary = result.documents.first()
if (summary) {
           String docid = summary.getSummaryField("infadocid").getStringValue()
           ...
           strDocId = docid        
}

is all you really need, because the for loop actually doesn't make much sense when all you want is to process the first record.

If there is a possibility that result.documents contains null s, then replace first() with find()

Edit: To process more than one result:

def summaries = result.documents.take(numOfResult)

// above code assumes result.documents contains no nulls; otherwise:
//    def count=0
//    def summaries = result.documents.findAll { it && count++<numOfResult }

summaries.each { summary ->
           String docid = summary.getSummaryField("infadocid").getStringValue()
           ...
           strDocId = docid        
}

In idiomatic Groovy code, many loops are replace by iterating methods like each()

You know the while statement also exists in Groovy ?

As a consequence, there is no reason to transform it into a for loop.

/*The below line gives me error*/
for(it in result.document && count < 1)
{

}

This line is giving you an error, because result.document will try to call result.getDocument() which doesn't exist.

Also, you should avoid using it as a variable name in Groovy, because within the scope of a closure it is the default name of the first closure parameter.

I haven't looked at the code thoroughly (or as the kids say, "tl;dr"), but I suspect if you just rename the file from .java to .groovy, it will probably work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM