• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DataInfo[] criteriaFind(String criteria);

 
Miguel Roque
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
This afternoon i've had a brainstorm and 10 minutes after i've build my criteriaFind. Here is how i've done it.
It has only been tested with the 2 combos that i've put in the main screen of FBN so that we can chose the origin and destination airport but for shure it will work also when i implement the search dialog.
Well, it's like this. First i've created a CriteriaDecompiler class that has a method with the following signature: public DataInfo decompileCriteria(description: FieldInfo[], String criteria) { .. } this method decompiles the passed criteria and returns a DataInfo created with record number 0 that has all the fields either with "ANY" (default if no criteria is passed) or with the value of the criteria.
This method is used in the very first begining of criteriaFind method.
In criteriaFind I create a ArrayList and read the database into it. Inside the Data class, I've created a private method named contentsEqual that accepts 2 arrays of Strings that loks like the following:

In the criteriaFind, i check all the contents of the ArrayList against the DataInfo created by the CriteriaDecompiler class and remove the ones that are false.
Voila, here it is my criteriaFind implementation.
Comments are welcome.
Tanks,
Miguel
(jl: added code tags)
[ December 20, 2002: Message edited by: Junilu Lacar ]
 
Junilu Lacar
Bartender
Pie
Posts: 8770
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comments added to code:


I'm not sure I would use this approach though. I think there's a simpler and more straightforward way to code this. Have to go now though. Be back later...
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a design problem with your criteriaFind implementation. Before you added this method, Data was an entirely generic, fully reusable class. Your criteriaFind implementation on the other hand is directly tied to the Fly By Night database structure.
This introduces an inconsistency in Data and reduces its reusability to, well, zero. IMHO, you should do one of two things: either you move criteriaFind into an application-specific subclass of Data (eg FlightData), or you modify your implementation to be fully generic.
- Peter
 
Miguel Roque
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Peter.
Well, the code of criteria find is tied to the DataInfo and FieldInfo class! As far as I can see, if we change these classes, the criteriaFind will also work. Also, Data class uses DataInfo for retriving records and is DataInfo that i'm using to see if they are equal!
The equals method only cares to see if the 2 arrays passed have the same lenght, so if we change our database so that instead of 9 fields we put 12 fields, it will allways compare all of them!
Maybe I must change the place where i've defined some constants with the field position and the number of fields in the database (defined in DataInfo).
The code of criteriaFind is as follows:

As you can see, I've only used DataInfo and FieldInfo in the search, so I don't see wy can't we reuse.
Waiting for your comments.
Thanks,
Miguel
 
Miguel Roque
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again.
I've changed the criteriaFind method. Now, I have it as follows:

Well. Now I think that this implementation is independent and can be used in any database file.
Everything is inside the Data class.
Comments please ...
Thanks,
Miguel
 
Junilu Lacar
Bartender
Pie
Posts: 8770
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm back
Peter hit it right on the head. You were making the code too application-specific. My first cut at it would have been to add the following methods:

I like to start with methods as private then increase their visibility as the need arises.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Miguel Roque:
Well, the code of criteria find is tied to the DataInfo and FieldInfo class! As far as I can see, if we change these classes, the criteriaFind will also work. [...]
Cut, paste and modify is not exactly the re-use I had in mind, and certainly less than ideal, but I think you realised that already
The code, by and large, looks OK. You might use a StringTokenizer in decompileCriteria(). The two loops in contentsEqual() can be combined; in fact, I would break out of the comparison loop as soon as an inequality was found. The original.length != toCompare.length check should either be omitted or throw a RuntimeException, because it would signify a serious software bug. There is no need for the result[] array.
There isn't any need to read the database contents into a List either. Just process records as you read them.
- Peter
 
Junilu Lacar
Bartender
Pie
Posts: 8770
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or you could refactor a bit (including the contract for matches) so that you could write
if (next.matches(criteria)) {
// add next to collection
}
Yeah, I think I like that better...
 
Miguel Roque
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again.
I've changed the decompiler method in order to use a StringTokenizer. It has add a few more lines to the code but i didn't reinvented the weel
This method continues to return a DataInfo that is null if something happens when decompiling. Like this, i've killed 2 rabbits in one go has i've solved one of the requirements that is;
In the event of an invalid field name being
provided as part of the criteria the behavior of this method is the same as if no
records matched correctly specified criteria.

Thanks for all the help.
Miguel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic