
/*
 * Sistema de cache. 
 * Permite almacenar objetos y recuperarlos posteriormente.
 * Ofrece una interfaz para moverse hacia detras y hacia delante.
 * El tamaņo de la cache se establece en el constructor.
 */
function cache(limit) {

    this.limit = limit;
    this.index = -1;
    this.stack = new Array();
    var self = this;

    this.set = function(record) {
        if (self.index < (self.stack.length - 1)) {
            while (self.index < (self.stack.length - 1))
                self.stack.pop();
        }
        self.stack.push(record);
        self.index++;
        if (self.stack.length > self.limit) {
            self.stack.shift();
            self.index--;
        }
    }

    this.hasPrevious = function() {
        return (self.index > 0);
    }

    this.getPrevious = function() {
        if (self.hasPrevious()) {
            self.index--;
            return self.stack[self.index];
        } else return null;
    }

    this.hasNext = function() {
        return (self.index < (self.stack.length - 1));
    }

    this.getNext = function() {
        if (self.hasNext()) {
            self.index++;
            return self.stack[self.index];
        } else return null;
    }
}