File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes FBN criteriaFind( String criteria ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "FBN criteriaFind( String criteria )" Watch "FBN criteriaFind( String criteria )" New topic
Author

FBN criteriaFind( String criteria )

Travis Zimmerman
Greenhorn

Joined: Mar 06, 2002
Posts: 27
I have read quite a few posts concerning this part of the application. I came up with an algorithm and would like some feedback if possible.
Parse criteria using StringTokenizer for ",".
Parse tokens for "=".
Add the value pair to a TreeMap using the field name as the key value.
Read a record.
Place the record into a TreeMap using the field name as the key.
Iterate through criteria map keys checking the record map containskey().
If it is, get() this value and compare against the criteria value.
If all match then add the DataInfo object to an array.
Read Next Record.

I am not quite positive how this lies in the Big O scheme of things, but it seems like it should search rather quickly.
Any comments or suggestions are appreciated.
Thanks
Travis Zimmerman
Faye
Greenhorn

Joined: Jan 26, 2002
Posts: 5
Hi Travis,
My approach is kind of different from yours.
1. Parse criteria using StringTokenizer for ",". I also watch out for the scenario where there is only one search pair, such as "Carrie=ANY" (I try to make it as generic as possible).
2. Parse tokens for "=".
3. Find out the field position for each value pair. For example, Carrie is in 3rd position (description[3] is Carrie).
4. I populate a HashMap with key value pairs (key as the field position, and value as the field value to match).
All the above steps are handled in a separate utility class.
5. Read a record.
6. Loop through the previous populated HashMap. Access only those record fields, which are key value in the map. Compare value and add valid records to a list.
7. Turn the list into an Array and return it.
In this case, I don't need to go through every single field in each record. After all, I just need to compare values for the fields specified in the criteria.
Hope this will help.
-Faye
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Faye's approach is closer to what I did. The only difference is that if the user selects any in any field, I just don't add that as part of the criteria string.
example>
carrier = any
origin = SFO
arrival = any
criteria string = "origin='SFO'"
This way in my code in the CriteriaBuilder class, or in the criteriaFind method does not need any code to check for "ANY"
Mark
[ March 07, 2002: Message edited by: Mark Spritzler ]

Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Rajesh Matti
Ranch Hand

Joined: Jan 07, 2002
Posts: 121
Mark - I had asked you this question before, never could get throgh it. How are you handling carrier = any
origin = any
arrival = any
scenario ?. In this case, You will send "" to the criteriaFind() and that method always returns all records, upon the reception of an empty criteria ?.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

In this case, You will send "" to the criteriaFind() and that method always returns all records, upon the reception of an empty criteria ?.

Yes, exactly
Mark
Faye
Greenhorn

Joined: Jan 26, 2002
Posts: 5
Mark,
I like your approach of handling "ANY" keyword. In this way, the keyword is checked at the client side and it is easier for maintenance and future changes. I will incorporate it in my design. Thanks.
-Faye
Travis Zimmerman
Greenhorn

Joined: Mar 06, 2002
Posts: 27
Faye,
I thought about doing something similar at first and ened up doing it the way I did. Your approach make more sense, now in thinking about it. I will look it over again. Thanks for the input.
I am not sure why you stated you were watching for "Any" as an example. The UI shouldn't pass on this value to the Data class at all. As long as the user enters at least on legitimate value, "any" other values are returned.
Mark,
I assumed this as well. The mention of the "Any" criteria value is in the UI requirement and is not needed in criteriaFind(). The algorithm I used and, it seems as though, the one Fay used will both return the same results as yours does.
In the case where the user is requesting all records, I also send criteriaFind an empty string and return all records.

Thanks for the input,
Travis Zimmerman
[ March 08, 2002: Message edited by: Travis Zimmerman ]
Andrew Collins
Ranch Hand

Joined: Mar 28, 2002
Posts: 42
Hi there,
I'm about to implement the searchCriteria method in a similar way Faye and Mark did, but there's one question :
What if this method receives something like f.e.:
"Carrier='DHL,Inc'"
I mean, what to do if a value contains "," or "=" ??
Thanx
Adam Caldwell
Greenhorn

Joined: Mar 27, 2002
Posts: 17
My method was pretty complicated since I handled the case where they sent in Key=value,Key2='value2 ',Key3=Value3
The spec wasn't clear on if the format was key='value' or key=value, although the example showed key='value'.
My algorithm was similar to:
LOOP:
Look for equals sign (key=st.nextToken("="))
Make sure key is valid. If not immediately return null (per spec).
Skip the equals and get next token (st.nextToken("=',"))
value = st.nextToken();
if (value is ')
handle as quoted string

tuck key id and value into an ArrayList
Skip the comma
ENDLOOP
-Adam
Bhuvan mehra
Ranch Hand

Joined: Mar 29, 2002
Posts: 69
Mark Spritzler
---------------------------------
example>
carrier = any
origin = SFO
arrival = any
------------------------------------
hi!
i hope we have to provide option to choose only origin and destination, not carrier in ClientGUI. but our criteriaFind() must be able to handle all criterias.
thanks and regards,
bhuvan.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

I just added carrier. It is confusing in the specs because one statement says
The user should be able to select the origin and destination of flights, and the display should update to show only flights that satisfy those criteria. The user must be able to describe enter the string value "any" for the origin, destination, or both, so as to implement a wildcard-like feature.

then in their example of a criteria string it says
For example, the following argument string would select all records describing flights by the SpeedyAir carrier that originate in San Francisco.
"Carrier='SpeedyAir',Origin='SFO'"


The first quote is from the "Creating the User Interface" section.
Hmmm. So with that, that is why I included all three, just to be safe.
Mark
Andrew Collins
Ranch Hand

Joined: Mar 28, 2002
Posts: 42
Hi
Like Adam said, things get pretty complicated when you assume - I think you allways should - that the client could send whatever it wants - f.e. "Car,rier ='bla=blabla=x,y'"
Did any of you just assumed that the client sticks to a layout "a='b',c='d',e='f'" ?
I've looked at StreamTokenizer but I think that's too complicated for a Junior to easily understand..

Andrew
Horst Dehmer
Greenhorn

Joined: Mar 29, 2002
Posts: 10
Hi all,
think I did mention it a few days ago...
ANY is a valid three letter code for an airport:
ANY - Anthony, KS [Anthony Municipal Airport], USA
Don't quite like the idea of dropping this issue.
So what's it supposed to mean if the user types 'any' for origin/destination airport? I decided to provide some sort of lookup (9200+ airports) with a 'don't care' entry. ANY-criterions won't be included in the criteria and thus won't come thru to my criteriaFind() method.
Whatya think? Horst!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

I think all you need for this assignment is to make a JComboBox with all the uniques values for a field, and add a "All blah" for whatever field it is.
For example,
If you are doing Origin Airport, I had a JComboBox where the first and default value was "All Origins", then the JComboBox had all the Origin Airports that are in the db.db file listed.
Don't worry that there are 7000 or so airports in the world, or that there is an ANY airport, that will mess up the any criteria. That's beyond the scope of this assignment.
Make your life easier.
Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FBN criteriaFind( String criteria )