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()

}

}

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 nulls, 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++

summaries.each { summary ->

String docid = summary.getSummaryField("infadocid").getStringValue()

...

strDocId = docid

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值