I had an idea for a project a while back wherein I was going to write a Java library to allow sorting and selection from arrays using an SQL syntax of sorts.
My idea was that you could select from the array like this:
int[] nums = new int[] { 0, 5, 7, 2, 4, 9, 3, 1, 6, 8 }
int result = select().from(nums).where("value > 2").and("value < 8").sort("ASC");
This would return a new array like this:
{ 3, 4, 5, 6, 7 }
I haven’t really thought of much of an implementation or a reason for doing this, but I think it would be a very interesting endeavor.
My question is, does anything like this exist? Or is there any special way I should go about this?
I was also thinking this could be useful for Object arrays, where you could select multiple fields, like this:
select().from(array).where("Field1 = "+someValue).or("Field1 = "+anotherValue);
And the possibilities go on and on. But I’m unsure exactly where to start, or better put, how to start implementing these methods.
This is more of a “where to go” than a question with an answer, so any help at all will be useful.
You’re right, I think this would be an interesting project.
There are loads of approaches you could take to this. One would be to create a
Queryobject that you build up by calling the methods in turn.So something like this:
So you could then do:
You’d need to keep track of which values had been selected at each point and whether they next condition should add to them (as in the case of an
or) or remove from them (as in the case of anand). There will be lots of edge cases. My advice would be to start off with something very simple like theintsexample and work from there.When you get on to doing the object version you’ll need to use reflection to access the fields of the object.
Once you’ve done that you might want to consider improving the type safety of your code by using generics.
All in all, lots of scope for an interesting project. However, it feels like the sort of thing where there will be lots of edge cases, so worth thinking through the design a fair bit and would also not be a bad idea to do a fair bit of unit testing.