/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2005 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.cp.xemelios.common;

import java.util.Enumeration;
import java.util.Hashtable;

/**
 * Permet de stocker le nombre d'etats et d'elements contenus dans un document
 * 
 * @author chm
 */
public class FileInfo {

    private Hashtable<String,Long> elementsCount;
    private long globalCount = 0L;
    /**
     * 
     */
    public FileInfo() {
        super();
        elementsCount = new Hashtable<String,Long>();
    }
    public void merge(FileInfo other) {
        if(other==null) return;
        for(Enumeration<String> enumer=other.elementsCount.keys(); enumer.hasMoreElements();) {
            String etat = enumer.nextElement();
            Long otherCount = other.elementsCount.get(etat);
            Long thisCount = this.elementsCount.get(etat);
            if(thisCount!=null) otherCount = new Long(otherCount.longValue() + thisCount.longValue());
            this.elementsCount.put(etat,otherCount);
            other.elementsCount.remove(etat);
        }
        if(!other.elementsCount.isEmpty()) this.elementsCount.putAll(other.elementsCount);
        globalCount+=other.getGlobalCount();
    }
    public void incElement(String etat) {
        Long ret = elementsCount.get(etat);
        if(ret==null) ret = new Long(0);
        elementsCount.put(etat,new Long(ret.longValue()+1));
        globalCount++;
    }
    @Override
    public String toString() {
        StringBuffer ret = new StringBuffer();
        for(Enumeration<String> enumer=elementsCount.keys(); enumer.hasMoreElements();) {
            String etat = enumer.nextElement();
            ret.append("   ").append(etat).append("\t-> ").append(elementsCount.get(etat)).append(" éléments importés\n");
        }
        ret.append("\n");
        return ret.toString();
    }

    public long getGlobalCount() { return globalCount; }
}
