Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

criteriaFInd critiques

 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • 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: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Just the other day, I was thinking ... about this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic