This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes URLyBird 1.2.2: findByCriteria() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "URLyBird 1.2.2: findByCriteria()" Watch "URLyBird 1.2.2: findByCriteria()" New topic
Author

URLyBird 1.2.2: findByCriteria()

Rajesh Moorthy
Ranch Hand

Joined: Sep 23, 2008
Posts: 30
Hello,

I am working with URLyBird 1.2.2. Could anyone help me out with the algorithm that could be used for findByCriteria() method?

Thanks & regards,
Rajesh.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

i think that's not an appropriate question. no one will give you his/her algorithm. you should ask more detailed questions than this. because no one will give you his/her algorithm.
So just say what you have problem with and we'll be glad to help you

SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Fola Fadairo
Ranch Hand

Joined: Feb 16, 2004
Posts: 35
findByCriteria;
Seems straight forward from the name;
If criteria is true
return found record(s)
else
-Do what your requirements say.

Like Roel wrote, post what you presently have...
Rajesh Moorthy
Ranch Hand

Joined: Sep 23, 2008
Posts: 30
Thanks for your response. I am giving the details below.

Method signature:
public long[] findByCriteria(String[] criteria) {....}

Requirement:
This method should return an array of record numbers that match the specified criteria. Field n in the database file is described by criteria[n]. A null value in criteria[n] matches any field value. A non-null value in criteria[n] matches any field value that begins with criteria[n]. (For example, "Fred" matches "Fred" or "Freddy".)

Problem:
This method should return an array of record numbers (not the records). However, the GUI expects the actual records itself to be displayed. Due to the signature of this method, we are forced to return only the matching record numbers from a database search. Please note that for sending the record numbers, we already have to read the records and compare it with the input criteria. After we send the record numbers, we will have to read the database (or cache) again, when the GUI wants to display the records.

How do we avoid this second read, which is redundant?

Thanks & regards,
Rajesh.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

Hi Rajesh,

There is even more. The gui is expecting exact matches (and not "starting with" ones), so your gui has to do some filtering.

If you really want to avoid the redundant read (like you call it) the only thing you can do is, extend your given interface and add an extra method which returns you a Map of ints and String-arrays. But that's all up to you.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Hey, Rajesh!

Searching for records is one of the points of the certification that are supposed to be ambiguous and put doubts in your head. Please take a look at this thread. If you still have doubts after reading it, please let us know and we'll be glad to help you again!


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2247
    
    7

Roel De Nijs wrote:There is even more. The gui is expecting exact matches (and not "starting with" ones), so your gui has to do some filtering.


Requirement:
This method should return an array of record numbers that match the specified criteria. Field n in the database file is described by criteria[n]. A null value in criteria[n] matches any field value. A non-null value in criteria[n] matches any field value that begins with criteria[n]. (For example, "Fred" matches "Fred" or "Freddy".)


Roel's quote of "expecting exact matches" doesn't quite right because the last sentence of the requirement "(For example, "Fred" matches "Fred" or "Freddy".)"

Exact match uses equals method. The requirement example looks to me startsWith method. Other way will depend on how you present your search GUI. This will affect your implementation a bit.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

K. Tsang,

Roel De Nijs wrote:The gui is expecting exact matches (and not "starting with" ones), so your gui has to do some filtering.


As you can see I said that the gui was expecting "exact matches", so that you have to do in your gui some filtering because (like you said) the requirement of your database-findByCriteria method is to implement a method that returns startsWith-matches

Regards,
Roel
Fola Fadairo
Ranch Hand

Joined: Feb 16, 2004
Posts: 35
What K means by ".... way will depend on how you present your search ..." is: Some people do not allow input text, they display the possible data and the user chooses...
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

Fola Fadairo wrote:What K means by ".... way will depend on how you present your search ..." is: Some people do not allow input text, they display the possible data and the user chooses...


Agreed. But still you have to do the extra filtering

Let say my gui has 2 comboboxes: one containing hotel names, one containing locations. First combo contains: Ritz, Hilton. The second combo contains: Londen, Paris, Brussels

User selects Ritz + Paris and hit search. The search of your Data-class will result in all hotels with name = Ritz or location = Paris (because they are OR'ed). Your GUI should only show the hotels with name = Ritz and location = Paris (because they should be AND'ed)

And if i am wrong on this (which i think i am not), i'm doing too much
Fola Fadairo
Ranch Hand

Joined: Feb 16, 2004
Posts: 35
Sure Roel, you are right. Extra filtering is required.
Rajesh Moorthy
Ranch Hand

Joined: Sep 23, 2008
Posts: 30
Could you please explain why the search of the Data-class should result in all hotels with name = Ritz or location = Paris? Why should they be OR'ed? Why shouldn't they be AND'ed in the Data class itself? Could you please indicate where is such a requirement stated that the search should be OR'ed in the Data class and should be AND'ed in the GUI?

Thanks,
Rajesh.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

If you pay attention to what the find method of your Data class is supposed to do, you'll verify that it must return any record where "a non-null value in criteria[n] matches any field value that begins with criteria[n]", and that the search you're supposed to provide in your GUI "must allow the user to search the data for all records, or for records where the name and/or location fields exactly match values specified by the user".
Rajesh Moorthy
Ranch Hand

Joined: Sep 23, 2008
Posts: 30
The requirement says just that. It means that we should use startsWith() in the Data class and equals() in the GUI. It does not mean that they should be OR'ed in the Data class.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Rajesh Moorthy wrote:It does not mean that they should be OR'ed in the Data class.


Partner, this is kind of implicit. Since it says that "a non-null value in criteria[n] matches any field value that begins with criteria[n]", then if only one value matches this rule, it is enough for that record to be returned.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

Rajesh Moorthy wrote:The requirement says just that. It means that we should use startsWith() in the Data class and equals() in the GUI. It does not mean that they should be OR'ed in the Data class.


That would be just to easy for this assignment.

in db following 3 records:
a) Ritz, Antwerp,...
b) Ritz, Paris,...
c) Hilton, Antwerp,...

user enters hotelName=Ritz and location=Antwerp (expected result in your table would be 1 record, namely record a). Your findByCriteria will (or should) return all 3 records. so you see (like roberto said) the OR'ing is implicit. you loop over your records:
- record a: it's a match because hotelName matches the specified one (no need to check location)
- record b: it's a match because hotelName matches the specified one (no need to check location)
- record c: hotelName doesn't match the specified one, so the location is checked and this is a match too

Kind regards,
Roel
Rajesh Moorthy
Ranch Hand

Joined: Sep 23, 2008
Posts: 30
That clarifies.....Thanks a lot, Dudes !!!
mohamed sulibi
Ranch Hand

Joined: Sep 04, 2005
Posts: 169
Dears;


Your findByCriteria will (or should) return all 3 records.


why should not may be or it my thought about the javadoc ?, actually i interrupt as it will return also the 1 result.

best regards.
Mohamed Sulibi
SCJP, SCJD in progress (From 1/8/2007 till now).
mohamed sulibi
Ranch Hand

Joined: Sep 04, 2005
Posts: 169
dears;

also my understanding of the javadoc for find is:



best regards.
Mohamed Sulibi
SCJP, SCJD in progress (From 1/8/2007 till now).

[Edit to fit on a page]
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

it all depends on interpretation. another thread is going on here

So document your decision in your choices.txt and/or add some explanation in javadoc of the method and you'll be safe (i hope)

[Edit: make URL a URL ]
Liviu Carausu
Ranch Hand

Joined: Oct 07, 2004
Posts: 157
Roel De Nijs wrote:
Rajesh Moorthy wrote:The requirement says just that. It means that we should use startsWith() in the Data class and equals() in the GUI. It does not mean that they should be OR'ed in the Data class.


That would be just to easy for this assignment.

in db following 3 records:
a) Ritz, Antwerp,...
b) Ritz, Paris,...
c) Hilton, Antwerp,...

user enters hotelName=Ritz and location=Antwerp (expected result in your table would be 1 record, namely record a). Your findByCriteria will (or should) return all 3 records. so you see (like roberto said) the OR'ing is implicit. you loop over your records:
- record a: it's a match because hotelName matches the specified one (no need to check location)
- record b: it's a match because hotelName matches the specified one (no need to check location)
- record c: hotelName doesn't match the specified one, so the location is checked and this is a match too

Kind regards,
Roel


Hi Roel,
I have a question : what is the String[] array given as parameter to findByCriteria() method ? Is it {Ritz, Antwerp, null,null,null,null} ?
In this case b) and c ) will not be matched in my opinion by findByCriteria method because Paris does not start with Antwerp and Hilton does not start with Ritz. Please correct me if I'm wrong ...

Greetings,
Liviu


Oracle Certified Master Java SE6 Developer(SCJD),
OCE JEE 6 JSP and Servlets Developer.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

Hi Liviu,

My String[] array will be (like you said): {Ritz, Antwerp, null, null, null, null, null} (i believe urlybird has 7 fields in db-file)

That's your understanding: you will perform an AND between the different criteria (and so record b and c will not match), my understanding is to OR them and then both records are a match.

My approach is more flexible to enhancements in the future: if they want to search in the gui on name, location, name and location, name or location i will keep my code as is (filter the records returning from find-method from database) and for the last one i just return records from find-method (without doing any filtering in the gui).
you will have to do a whole lot more.

Kind regards,
Roel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: URLyBird 1.2.2: findByCriteria()
 
Similar Threads
URLybird
URLyBird Search Options
Doubt in specification of GUI for URLyBird 1.1.2
URLyBird - Data.java
URLyBird 1.2.2: findByCriteria()