本文介绍了最好的groovy闭包成语取代java内部类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为新的groovy ...



我试图替换事件监听器,过滤器等的java成语。



我在Groovy中的工作代码如下:

  def find(){
ODB odb = ODBFactory.open(files.nodupes); //数据核对象数据库
对象< Prospect> src = odb.getObjects(new QProspect());

src.each {println it};

odb.close();

}

class QProspect extends SimpleNativeQuery {
public boolean match(Prospect p){
if(p.url){
return p.url.endsWith(。biz);
}
return false;
}
}

现在,这远不是我使用的在java中,其中Query接口的实现是在odb.getObjects()方法内部完成的。如果我在哪里编码java我可能会做一些类似下面的,但它不工作:

  Objects< Prospect> ; src = odb.getObjects({
boolean match(p){
if(p.url){
return p.url.endsWith(。biz);
}
return false;
}
} as SimpleNativeQuery);

或更好,我想这样:

 对象< Prospect> src = odb.getObjects(
{it.url.endsWith(。biz)} as SimpleNativeQuery
);

然而,什么groovy它关联匹配方法与外部脚本上下文, 。



我发现groovy ... groovy反正我会坚持学习更多。感谢。






我应该问的是我们如何在groovy中做匿名类。这里是java的成语:

  void defReadAFile(){
File [] files = new File(。 .listFiles(new FileFilter(){
public boolean accept(File file){
return file.getPath()。endsWith(。biz);
}
} ;
}

可以没有额外的类声明吗?

解决方案

我认为如果你抽象出问题所以它不会依赖Neodatis DB接口, - 这让我一个循环,因为我从来没有使用它。下面我写的是一个非常粗略的分析。



对于这个问题,我从来没有使用过Groovy,虽然我喜欢我看到它。但是看到没有其他人已经回答了,你被困住了: - )



我认为问题(或至少一部分)可能是你'期望过多的来自Neodatis的SimpleNativeQuery类。它看起来不像它甚至试图过滤对象,然后将它们添加到返回的集合。我想,而是你想使用 org.neodatis.odb.impl.core.query.criteria.CriteriaQuery 。 (注意包路径中的impl。这让我有些紧张,因为我不知道这个类是否被调用者使用,但我没有看到在Neodatis中允许指定查询条件的任何其他类)。



但是,与其直接使用CriteriaQuery,我认为你宁愿将它包装在Groovy类中所以你可以使用它与闭包。所以,我认为一个Groovy版本的代码与闭包可能看起来像这样:

  //创建一个包装CriteriaQuery并允许你
//传递闭包。这也是字面意思,但至少它的
//可重用。

import org.neodatis.odb.impl.core.query.criteria;

类GroovyCriteriaQuery扩展CriteriaQuery {
private final c;

QProspect(theClosure){
//我更喜欢在这里检查null,而不是在match()
if(theClosure == null){
throw new InvalidArgumentException(theClosure不能为null!);
}
c = theClosure;
}

public boolean match(AbstractObjectInfo aoi){
// !!我假设这里'aoi'可以用作实际的
//!对象实例(或至少作为它的代理)。
//! (你可能需要在调用c之前从aoi中提取实际的对象。)
return c(aoi);
}
}

//现在在一些随机代码中使用查询类。

Objects< Prospect> src = odb.getObjects(
new GroovyCriteriaQuery(
{it.url.endsWith(。biz)}




我希望这有助于!


As new to groovy...

I'm trying to replace the java idiom for event listeners, filters, etc.

My working code in groovy is the following:

def find() {
    ODB odb = ODBFactory.open(files.nodupes); // data nucleus object database
    Objects<Prospect> src = odb.getObjects(new QProspect());

    src.each { println it };

    odb.close();

}

class QProspect extends SimpleNativeQuery {
    public boolean match(Prospect p) {
        if (p.url) {
            return p.url.endsWith(".biz");
        }
        return false;
    }
}

Now, this is far from what I'm used to in java, where the implementation of the Query interface is done right inside the odb.getObjects() method. If I where to code "java" I'd probably do something like the following, yet it's not working:

Objects<Prospect> src = odb.getObjects( {
        boolean match(p) { 
            if (p.url) {
            return p.url.endsWith(".biz");
        }
            return false; 
        }
    } as SimpleNativeQuery);

Or better, I'd like it to be like this:

 Objects<Prospect> src = odb.getObjects( 
      { it.url.endsWith(".biz") } as SimpleNativeQuery
 );

However, what groovy does it to associate the "match" method with the outer script context and fail me.

I find groovy... groovy anyways so I'll stick to learning more about it. Thanks.


What I should've asked was how do we do the "anonymous" class in groovy. Here's the java idiom:

void defReadAFile() {
    File[] files = new File(".").listFiles(new FileFilter() {
        public boolean accept(File file) {
            return file.getPath().endsWith(".biz");
        }
    });
}

Can groovy be as concise with no additional class declaration?

解决方案

I think it would have helped you to get answers if you'd abstracted the problem so that it didn't rely on the Neodatis DB interface -- that threw me for a loop, as I've never used it. What I've written below about it is based on a very cursory analysis.

For that matter, I've never used Groovy either, though I like what I've seen of it. But seeing as no one else has answered yet, you're stuck with me :-)

I think the problem (or at least part of it) may be that you're expecting too much of the SimpleNativeQuery class from Neodatis. It doesn't look like it even tries to filter the objects before it adds them to the returned collection. I think instead you want to use org.neodatis.odb.impl.core.query.criteria.CriteriaQuery. (Note the "impl" in the package path. This has me a bit nervous, as I don't know for sure if this class is meant to be used by callers. But I don't see any other classes in Neodatis that allow for query criteria to be specified.)

But instead of using CriteriaQuery directly, I think you'd rather wrap it inside of a Groovy class so that you can use it with closures. So, I think a Groovy version of your code with closures might look something like this:

// Create a class that wraps CriteriaQuery and allows you 
// to pass closures.  This is wordy too, but at least it's
// reusable.

import org.neodatis.odb.impl.core.query.criteria;

class GroovyCriteriaQuery extends CriteriaQuery {
    private final c;

    QProspect(theClosure) {
         // I prefer to check for null here, instead of in match()
         if (theClosure == null) {
             throw new InvalidArgumentException("theClosure can't be null!");
         }
         c = theClosure;
    }

    public boolean match(AbstractObjectInfo aoi){
        //!! I'm assuming here that 'aoi' can be used as the actual
        //!! object instance (or at least as proxy for it.)
        //!! (You may have to extract the actual object from aoi before calling c.)
        return c(aoi);
    }
}

// Now use the query class in some random code.

 Objects<Prospect> src = odb.getObjects( 
      new GroovyCriteriaQuery(
          { it.url.endsWith(".biz") } 
      )
 )

I hope this helps!

这篇关于最好的groovy闭包成语取代java内部类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 00:15