• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

FBN criteriaFind( String criteria )

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic