I have a point object:
class Point {
final int x,y;
...
}
Because these points will be used/created all over the place in my code, I want to start using guavas cache. Unfortuinally the CacheLoader only accept one parameter. Another question here on stackoverflow use a pair object for a similar problem. But I don't like the idea to create a dummy object for every cache request. So I come up with my own workaround:
Because the object is specified by the x and y, I think I can merge(shift) both values into a long, which will be my key.
void test(int x, int y) {
Long key = (long) ((long) (x) << Integer.SIZE | y);
Point point = cache.get(key);
}
CacheLoader<Long, Point> loader = new CacheLoader<Long, Point>() {
public Point load(Long key) throws Exception {
final int x,y;
// shift magic
x = (int) (key >> Integer.SIZE);
y = key.intValue();
return new Point(x, y);
}
};
I'm actually a shift noob. Will this work? Did I miss something? Is this "faster" than the pair class? That's my question!
Yes, I test the code and it works so far I can tell.