I have a system that has a bunch of data that is queryable via some RESTful URL. The query is something like /noun/id/noun2/id/data?param1=23¶m2=45 - the params are optional and have defaults; they are basically passing in a bunch of possible search ranges over different fields in the data set. There is also the ability to set parameters like "startItem", "itemCount" for pagination - a client could ask for startItem = 150, itemCount= 50 to get the 4th page, for example.
The parameters are pushed into some object that does parsing and some default parameter fillin, and then (in this case) handed off to an implementation that builds up a set of matching rules :
FilterAction filter = new FilterAction(parameterObject);
.. in constructor; FilterAction populates a list of FilterRule corresponding to the populated parameters
Then some query runs and gives us a chunk of data and the data layer is going to do something like :
Currently this doesn't support the notion of itemStart and itemCount. I could add some tracking data inside the filter that increments every time the matches function is called, and increments a success counter every time it returns true, and stops returning true once it's hit the limit, but (a) that kind of side effect driven stuff seems awful, and I'm wary of that from a design point of view.
So maybe something like this would be appropriate?
But then it feels like I'm breaking encapsulation - shouldn't this kind of decision be a part of the filter? So maybe this instead :
But that feels a little clumsy too - why should the client have to track this for the filter? If the client forgets to invoke the magic functions then the filter isn't going to work, and that's dumb. So maybe I'm back to tracking things in the side effect after all. Is the answer to just have a better name for the matches function? checkMatchAndIncrementTrackingCounters would at least make it clear that there was state being captured in the filter. Any thoughts?
The parameters are pushed into some object that does parsing and some default parameter fillin, and then (in this case) handed off to an implementation that builds up a set of matching rules :
FilterAction filter = new FilterAction(parameterObject);
.. in constructor; FilterAction populates a list of FilterRule corresponding to the populated parameters
Then some query runs and gives us a chunk of data and the data layer is going to do something like :
Code:
for (Item i : itemResults)
{
if (filter.matches(i))
outputCollector.add(i);
}
Currently this doesn't support the notion of itemStart and itemCount. I could add some tracking data inside the filter that increments every time the matches function is called, and increments a success counter every time it returns true, and stops returning true once it's hit the limit, but (a) that kind of side effect driven stuff seems awful, and I'm wary of that from a design point of view.
So maybe something like this would be appropriate?
Code:
int examined = 0 , succeeded = 0;
for (Item i : itemResults)
{
examined ++;
if (examined < itemStart)
{
if (filter.matches(i)) {
outputCollector.add(i);
succeeded ++;
}
}
if (succeeded >= itemCount)
break;
}
Code:
filter.reset();
for (Item i : itemResults)
{
filter.incrementExamined();
if (filter.matches(i)) {
outputCollector.add(i);
filter.incrementSuccess();
}
if (filter.atItemMax())
break;
}