• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

criteriaFInd critiques

 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I have implemented my criteriaFind() method using a brute force technique in the following manner.
I create a StringTokenizer object on the criteria string passed in. I then create a String[] array that contains all the (trimmed) Field names. I also create a HashMap to hold the individual values of the criteria String.
I then loop thru the Tokenizer and create another StringTokenizer on the indvidual tokens. I then check if there are exactly two tokens on the new Tokenizer and if not I imediately return an empty DataInfo[] array. I then check the first token against all the Field names in a loop and if I find a match I create an Integer using the loop index, set a boolean to true to indicate that I have a valid Field name and break. Once out of the loop if my boolean is false I once again return an empty array. If not I get the next token, strip off the quotes then add it to my HashMap using the Integer as key.
Once I have the HashMap loaded, I loop thru all records in the database (calling getRecord) and check each against the values in the Hash and if a record matches everything in the Hash, I add the record to an ArrayList.
Finally I return a DataInfo[] using the toArray method of the ArrayList (of course typecasting it back to DataInfo[]).
Also I am trimming the tokens so that a criteria String could look somethin like this and still work:
" Carrier = ' SpeedyAir ', Origin = ' DFW '"
I appreciate any feedback.
Michael Morris
 
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
It's tough to comment on it. I mean if it works than keep it just like that. It is close to what I had.
Mark
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark,
What about allowing the whitespace? I've always done this in my real apps. Also I accept 'any' or '*' as a wildcard. In a real app I would probably just use '*'.
Thanks again for all your help
Michael Morris
 
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 never really worry about whitespace, especially since your code is going to create the criteriaString, so therefore you are completely in control as to whether a space can ever get in there. As far as Any or *, I didn't even include them. So if they selected origin as any, arrival as any, and carrier as "My Carrier". My criteria String would be just "carrier="My Carrier"". SOmething like that.
Mark
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again Mark,
Yea because all I'm doing if I get a wildcard is just ignore it (ie not put it in the Hash) anyway.
Michael Morris
 
reply
    Bookmark Topic Watch Topic
  • New Topic