将Java代码转换为Groovy

将Java代码转换为Groovy

本文介绍了将Java代码转换为Groovy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将Java函数转换为等效的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()
    }
}

我的Java代码

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
}

是您真正需要的,因为当您想要处理第一条记录时,for循环实际上没有多大意义。

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.

如果有可能result.documents包含 null s,然后将 first()替换为 find() code>

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

编辑:处理多个结果:

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
}

在惯用的Groovy代码中,很多循环都会被迭代方法取代,如 each()

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

这篇关于将Java代码转换为Groovy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 05:47