// --------------------------------------------------------------------------------------------------------------------
// @author Guilliano
//
// Tabela generica para guardar qualquer coisa usando uma chave.
//
// Metodos:
// - bool contem(<object> chave) : Verifica se contem um elemento com a chave informada.
// - void adiciona(<object> chave, <object> objeto) : Adiciona um objeto usando uma chave. O objeto não é inserido se
//													  já existe um outro com a mesma chave.
// - <object> remover(<object> chave) : Remove um objeto que tenha a chave informada.
// - Array<object> getChaves() : Obtém um array com todas as chaves que possuem algum objeto relacionado.
// - Array<object> getObjetos() : Obtém um array com todos os objetos guardados no conjunto.
// - <inteiro> tamanho() : Conta quantos objetos estão contidos no conjunto.
// --------------------------------------------------------------------------------------------------------------------
function ConjuntoGenerico(){
	this.Tabela = new Array();
	this.contem = ConjuntoGenerico_Contem;
	this.adiciona = ConjuntoGenerico_Adiciona;
	this.remove = ConjuntoGenerico_Remover;
	this.getChaves = ConjuntoGenerico_Chaves;
	this.getObjetos = ConjuntoGenerico_Objetos;
	this.tamanho = ConjuntoGenerico_Tamanho;
}
//
// Verifica se ja tem o acessorio na lista.
//
function ConjuntoGenerico_Contem(chave){
	if(chave == null){
		throw "NullPointerException {" + chave + "}";
	}
	if(this.Tabela[chave] != null){
		return true;
	}
	return false;
}
//
// Adiciona um novo acessório a lista.
//
function ConjuntoGenerico_Adiciona(chave, objeto){
	if(chave == null || objeto == null){
		throw "NullPointerException {" + chave + "},{" + objeto + "}";
	}
	if(this.contem(chave)){
		return;
	}
	this.Tabela[chave] = objeto;
}
//
// Remove um objeto do conjunto.
//
function ConjuntoGenerico_Remover(chave){
	if(chave == null){
		throw "NullPointerException {" + chave + "}";
	}
	var obj = this.Tabela[chave];
	this.Tabela[chave] = null;
	return obj;
}
//
// Retorna um array com as chaves identificadoras dos elementos do conjunto.
//
function ConjuntoGenerico_Chaves(){
    var chaves = new Array();
    for (var chave in this.Tabela) {
        if (this.Tabela[chave] != null) {
			chaves.push(chave);
		}
    }
    return chaves;
}
//
// Obtém a lista de todos os objetos guardados no conjunto.
//
function ConjuntoGenerico_Objetos(){
    var objetos = new Array();
    for (var chave in this.Tabela) {
        if (this.Tabela[chave] != null) {
			objetos.push(this.Tabela[chave]);
		}
    }
    return objetos;
}
//
// Retorna a quantidade de objetos existem no conjunto.
//
function ConjuntoGenerico_Tamanho(){
    var quantidade = 0;
    for (var chave in this.Tabela) {
        if (this.Tabela[chave] != null) {
			quantidade = quantidade + 1;
		}
    }
    return quantidade;
}