/*
 * Copyright (c) 1997-2001 Centro de Informática - UFPE
 */

/**
 * Esta classe implementa um o Repositório de Registros.
 */
public class RepositorioRegistros {

    // O Registro, que pode ser um Arquivo ou Diretorio.
    private Registro registro;

    /* A referencia para o proximo elemento da lista que é do tipo
     * RepositorioRegistros. */
    private RepositorioRegistros proximo;


    /**
     * Insere um dado Registro no Repositório.
     *
     * @param registro a ser inserido.
     */
    public void inserir(Registro registro) {

        if (this.registro == null) {
            this.registro = registro;
            proximo = new RepositorioRegistros();
        }
        else
            proximo.inserir(registro);
    }


    /**
     * Exclui um dado Registro do Repositório.
     *
     * @param registro a ser excluído.
     */
    public void excluir(Registro registro) {

        if (this.registro == null) {
            System.out.println("Arquivo ou diretorio inexistente\n");
        }
        else {
            if (this.registro.equals(registro)) {
                this.registro = proximo.registro;
                this.proximo = proximo.proximo;
            }
            else
                proximo.excluir(registro);
        }
    }


    /**
     * Retorna o nome dos registros pertencentes ao repositório.
     *
     * @return Nome dos registros pertencentes ao repositório.
     */
    public String listar() {

        String retorno = "";

        if (this.registro != null) {
            retorno = registro.getNome() + "\n" + proximo.listar();
        }
        return retorno;
    }


    /**
     * Verifica se um dado Registro está no repositório.
     *
     * @param true se o registro pertence ao repositório, false caso contrário.
     */
    public boolean existe(Registro registro) {

        boolean retorno = false;

        if (this.registro != null) {
            if (this.registro.equals(registro))
                retorno = true;
            else
                retorno = proximo.existe(registro);
        }
        return retorno;
    }


    /**
     * Compara dois repositórios de registros.
     *
     * @return true se são iguais, false se são diferentes.
     */
    public boolean equals(RepositorioRegistros registros) {

        boolean retorno = false;

        if (this == registros) {
            retorno = true;
        }
        else if (this.registro != null && registros.registro != null
                && this.registro.equals(registros.registro)
                && this.proximo != null && registros.proximo != null
                && this.proximo.equals(registros.proximo)) {

            retorno = true;
        }
        return retorno;
    }

}