Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

criteriaFind().. implementation

 
Suresh Babu Seeram
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
I am using search algorithm in Separate class Called SearchCriteria Builder.
Please see the following code and comment on
clarity and efficiency of this alogrithm..


criteriaFind() method will look like this..

[ June 24, 2002: Message edited by: Suresh Babu Seeram ]
[ June 24, 2002: Message edited by: Suresh Babu Seeram ]
[ June 24, 2002: Message edited by: Suresh Babu Seeram ]
[ June 24, 2002: Message edited by: Suresh Babu Seeram ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getCriteriaMap. My code to create a HashMap of the criteria string is only 10 lines of code. I am confused as to what you are actually doing in this method?
In one aspect it looks like it is trying to not only make the HashMap, but also check to see if it is a match to a record, then putting it somewhere.
In another it might just be to get your String into the HashMap.
I would suggest really looking at this method use more of the given classes that is in Java's API and also suggest splitting this apart into seperate methods. One method for one purpose. It will be easier to read, more maintanable, more reusable and a Junior Programmer would be able to use it.
I like you idea of having a class handle all the criteria string stuff.
Hope that helps.
Mark
 
Suresh Babu Seeram
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,
Thank you very much for your reply..

getCriteriaMap. My code to create a HashMap of the criteria string is only 10 lines of code. I am confused as to what you are actually doing in this method?
In one aspect it looks like it is trying to not only make the HashMap, but also check to see if it is a match to a record, then putting it somewhere.

Thanks for your patience of reading my post and your reply.....
In method getCriterMap(String criteria,Field fieldInfo)
StringTokenzier splits the criteria twice to get fieldName[i] and dataValue[i]..
eg:
criteria = "Carrier='PromptAir',Origin airport='DAL ',Destination airport='FRA'"
fieldName[0]=Carrier dataValue[0]='PromptAir'
fieldName[1]=Origin airport dataValue[1]='DAL'
fieldName[2]=Destination airport dataValue[2]='FRA'
My HasMap is not storing fieldName[i] & dataValue[i].. Instead of that it is storing
fieldIndex[i] & dataValue[i]..


Above code gets fieldIndex by using private method getKeyIndex()..
In fieldInfo... Header contains.
fieldInfo[0]=FlightNumber
fieldInfo[1]=Origin airport
fieldInfo[2]=Destination airport
fieldInfo[3]=Carrier
fieldINfo[4]=Price..
....
...
fieldInfo[8]=Available seats...
MEthod getKeyIndex(FieldName[i],FieldInfo)..
returns corresponding index..
i.e If fieldName is "Carrier" it returns "3"
If fieldName is "Origin airport" it returns "1"
If fieldName is "Destination airport" it returns "2"
So I am storing {(3,'PromptAir'),(1,'DAL'),(2,'FRA')} in to the HashMap...

public boolean isRecordMatch(DataInfo record,Map map){
}
This method Takes Datainfo record and HashMap(fieldIndex,dataValues).. It can search fastly by using fieldIndex..
eg;
In record it search whether 3 index Value is 'PromptAir' or not. If it matches then search for origin airport is 'DAL or not.It is matches then search for Destination airport is 'FRA' or not. If all these three matches then it return true otherwise false..
Please give your feedback on this..
[ June 24, 2002: Message edited by: Suresh Babu Seeram
[ June 24, 2002: Message edited by: Suresh Babu Seeram ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, that is one way to get the search to work. However, is there still ways you can break apart that method to be smaller multiple methods. One to break apart the string, another to get the information that you want to put into the Map, then another that puts those values into the Map.
Something like that, that will make it more readable and clear to a Junior Level programmer.
Thanks
Mark
 
Gosling Gong
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About criteriaFind implementation, I have some points not clear.
1)implement use methods in Data class Vs. create seperate helper class.
what's the benefit to create a new helper class? from my opion, in every search, we must instantiate that helper class, which will bring some more overhead.
2)to hold criteria, use HashMap
yes, it's convenient to use HashMap and can make code clean, but is it more efficient to just use array, for ext., String[][]
3)how did you handle the exceptions in criteriaFind?
in the case that field not exist, and criteria syntax not correct, did you just ignore it or throws DatabaseException?
thanks!
 
Eduard Jodas
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1)implement use methods in Data class Vs. create seperate helper class.
what's the benefit to create a new helper class? from my opion, in every search, we must instantiate that helper class, which will bring some more overhead.

The overhead of creating one object per call has nothing to do with the overhead of reading a file. Furthermore, inside the criteriaFind method you are going to create lots of objects, no matter then one more. Finally, you can create the helper once and reuse it.
Advantadges: the criteriaFind algorism becomes cleaner and clearer, as you divide it in small methods. All these methods are put together in the same helper class so that it becomes more cohesive and readable. If you put the methods inside Data class they will be mixed up with the other Data methods.

2)to hold criteria, use HashMap
yes, it's convenient to use HashMap and can make code clean, but is it more efficient to just use array, for ext., String[][]

In my opinion, you can use whatever you need to make it work. For instance, DataInfo is not a HashMap.
Furthermore, I don't find using a HashMap so clean: the average number of fields in the database may be 10, 20 at most. Using a HashMap for such amount of values is overkill. And as the fields in the db are all in the same position, using a HashMap of <field, value> pairs is not so convenient compared to using an array of just values.

3)how did you handle the exceptions in criteriaFind?
in the case that field not exist, and criteria syntax not correct, did you just ignore it or throws DatabaseException?

if criteria syntax not correct, I throw a MalformedCriteriaException, which is a DatabaseException.
 
Gosling Gong
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
very clear explanation, thanks!
then, how about create a seperate helper class, but with its methods static? I think it should be nicer!
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thoguth that Static methods would work, but thinking about it, what happens when multiple clients are all searching at the same time, would you need Synchronized on all the methods.
Or not worry about that at all and have then be instance methods instead.
Mark
 
Gosling Gong
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, Mark,
I am wondering on what you said. when multiple clients are all searching at the same time, do you think there is something will wrong?
sychronized is used to protect data(instance variable) to be written sychronizely. but here I have no data to protect, I only give parameters to the static methods, and they return value to the Data class. and the variable is local in method, I think every call it will not affect the other call even it is not synchronized. not sure if I explained clearly?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess the best thing to do is to try it. You are probably right about it being ok to be static.
I just always think of Static classes as classes for Constants, or so all clients get the same data, like a counter to keep track of current users, etc. But that could just be my shallow thinking.
Mark
 
Eduard Jodas
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one reusable criteriaFind Helper class for each Data instance. It is a private inner class.
My Helper class contains data of its own, and this data is different depending on the Data instance the Helper class is bound to, so its methods can't be static.
My criteriaFind method is synchronized, so I have no need to synchronize the methods in the Helper class, as they are only called from within the criteriaFind method.
I would be very wary about using non-synchronized static methods which access a file. Making them synchronized will not harm you, and the performance overhead is nothing compared to the cost of reading a file.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic