001/*
002 * #%L
003 * Netarchivesuite - harvester
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023package dk.netarkivet.harvester.harvesting.frontier;
024
025import java.io.Serializable;
026import java.util.TreeMap;
027import java.util.TreeSet;
028
029/**
030 * Implements a frontier report wrapper that is stored in memory. This implementation is intended for small reports that
031 * are the result of the filtering of a full frontier report obtained from Heritrix 1. This implementation is
032 * serializable, so it can be transmitted in a JMS message.
033 * <p>
034 * The report lines are sorted according to the natural order defined by {@link FrontierReportLine}, e.g. descending
035 * size of the queue.
036 */
037@SuppressWarnings({"serial"})
038public class InMemoryFrontierReport extends AbstractFrontierReport implements Serializable {
039
040    /**
041     * The lines of the report, sorted by natural order.
042     */
043    private TreeSet<FrontierReportLine> lines = new TreeSet<FrontierReportLine>();
044
045    /**
046     * The lines of the report, mapped by domain name.
047     */
048    private TreeMap<String, FrontierReportLine> linesByDomain = new TreeMap<String, FrontierReportLine>();
049
050    /**
051     * Default empty contructor.
052     */
053    InMemoryFrontierReport() {
054
055    }
056
057    /**
058     * Builds an empty report.
059     *
060     * @param jobName the Heritrix job name
061     */
062    public InMemoryFrontierReport(String jobName) {
063        super(jobName);
064    }
065
066    /**
067     * Returns the lines of the report.
068     *
069     * @return the lines of the report.
070     */
071    public FrontierReportLine[] getLines() {
072        return (FrontierReportLine[]) lines.toArray(new FrontierReportLine[lines.size()]);
073    }
074
075    @Override
076    public void addLine(FrontierReportLine line) {
077        lines.add(line);
078        linesByDomain.put(line.getDomainName(), line);
079    }
080
081    @Override
082    public FrontierReportLine getLineForDomain(String domainName) {
083        return linesByDomain.get(domainName);
084    }
085
086    /**
087     * Returns the report size, e.g. the count of report lines.
088     *
089     * @return the report size
090     */
091    public int getSize() {
092        return lines.size();
093    }
094
095}