So I have a problem that has been holding me up for quite some time, I think my brain is just fried and it may just need a different perspective. I figured gamers often tend to be programmers, plus I don't have any other active forum accounts, so...
I'm trying to create a class that deals extensively with a large collection of objects of type T, ie T is a generic type. So the class might look like:
class Processor < T > {
Processor(List< T > vals) {
DoStuffWith(vals);
}
}
This Processor class frequently needs to pass an object from the generic collection to various worker classes that are built to expect objects of that type (these classes are provided in the constructor, so are also generic, and are activated through an abstract parameter passing method).
so now the class might look like
class Processor < T > {
Processor(List< T > vals, Worker< T >[] workers) {
DoStuffWith(vals, workers);
}
}
These worker classes often need to check if other objects exist in the set (ie if vals contains). One example might be if the processor class is dealing with 2D points. If the worker class is passed x:4 y:5, it may want to check if x:4 y:6 exists. However the only way I understand to do this, is to create an x=4 y=6 instance and check if it exists in the set, by ensuring the generic object's .equals and .hashcode methods would return true on such a case and such a case only. (working in Java ftr, but compiling to JavaScript with GWT, though I think this is more a general Object Oriented question)
This seems dodgy to me, as I'm instancing a class only to compare (and this could happen many many many times). I could create an abstract key object that the generic class must provide to at least reduce the bloat of instancing a compare key, but this still seems wrong.
What I'd prefer is to compare on a primitive so that no new class needs to be initialised when checking if one exists in the set. (so all elements in val might now exist in a hashmap of primitive->instance, where primitive might be an Integer hash)
Or, alternatively, to know if there is some better & faster way of checking if a multi-featured object exists in a set of objects that are abstract.
If I was only working with one Val class type, I could just use a static Integer key() method, that takes the few parameters that are necessary (eg x & y). But since I'm using abstracts, I can't declare such a key generator method. (ie no abstract static methods)
Does this even make sense to any body else? And is any body else pro enough to know what solutions might exist? I tried reaching the ideal blood-alcohol concentration level to achieve programming godhood, but it just induced outsourcing my question to the internets.