Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 182865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:00:54+00:00 2026-05-11T15:00:54+00:00

As new to groovy… I’m trying to replace the java idiom for event listeners,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T15:00:55+00:00Added an answer on May 11, 2026 at 3:00 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I just got programmatic expanding and collapsing of NSSplitView to… May 11, 2026 at 11:42 pm
  • Editorial Team
    Editorial Team added an answer It seems to me that the method you outlined in… May 11, 2026 at 11:42 pm
  • Editorial Team
    Editorial Team added an answer That's a reasonable way to implement plugins. May 11, 2026 at 11:42 pm

Related Questions

As new to groovy... I'm trying to replace the java idiom for event listeners,
The 1st example below illustrates the working code. I want to take the working
My application is logically divided into server, which contain my Grails domain objects and
I'm trying to call a methond on a Java class from a Groovy class.
I have a groovy class that has the ability to write its output to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.