[英]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. 我正在尝试将Java函数转换为等效的Groovy代码,但无法找到循环执行&&
操作的任何内容。 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 我的Java代码
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. 这就是您真正需要的,因为for循环实际上只需要处理第一条记录就没有多大意义。
If there is a possibility that result.documents contains null
s, then replace first()
with find()
如果有可能result.documents包含null
,则将first()
替换为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()
在惯用的Groovy代码中,许多循环被迭代方法替换,例如each()
You know the while statement also exists in Groovy ? 您知道Groovy中还存在while语句吗?
As a consequence, there is no reason to transform it into a for loop. 因此,没有理由将其转换为for循环。
/*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. 这行给您一个错误,因为result.document
将尝试调用不存在的result.getDocument()
。
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. 同样,您应该避免在Groovy中将it
用作变量名,因为在闭包范围内, it
是第一个闭包参数的默认名称。
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. 我没有仔细研究代码(或者像孩子们说的那样,“ tl; dr”),但是我怀疑如果只是将文件从.java重命名为.groovy,它可能会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.