Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Cache behaviour as an aspect

Hi,

 Im trying to implement cache behaviour as an aspect. My initial approach
was to implement a class wich performs a calculation in a oblivious manner,
and I expected to develop an Aspect for the class wich would return the
cached value when possible. The trick is that I couldnt find a way to
override the returned value, is this possible? if not, is there some kind
pattern to achieve this?

This is the code for the class:

public class Screen {

	private int width;
	private int height;

	public Screen() {
	}

	public void setSize(int width, int height) {
		this.width = width;
		this.height = height;
	}
	//this is the calculation method
	public int getIndexByCoordenates(int x, int y) {
		return y * width + x;
	}
}

This is the code for the aspect:

public aspect ScreenCacheAspect {

	//cache matrix
	private int [][] screen;

	//initialize cache matrix
	before(Screen scr, int x, int y): call(void Screen.setSize(int,int)) &&
target(scr) && args(x,y){
		this.screen = new int[x][y];
		for(int i = 0; i < x; i++) {
			for (int j = 0; j < y; j++) {
				screen[i][j] = -1;
			}
		}
	}
	//if in cache return value from cache
	before(Screen scr, int x, int y):
				call(int Screen.getIndexByCoordinates(int,int)) && target(scr) &&
args(x,y) {
		if (screen[x][y] != -1) {
			//HOW TO IMPLEMENT THIS?
			return screen[x][y];
		}
	}
	//if not in cache, store the calculated value.
	after(Screen scr, int x, int y) returning (int index):
				call(int Screen.getIndexByCoordinates(int,int)) && target(scr) &&
args(x,y) {
		if (screen[x][y] == -1) {
			screen[x][y] = index;
		}
	}
}

thanks in advance,
Gabriel



Back to the top