001/**
002 * Java Web Archive Toolkit - Software to read and validate ARC, WARC
003 * and GZip files. (http://jwat.org/)
004 * Copyright 2011-2012 Netarkivet.dk (http://netarkivet.dk/)
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.jwat.common;
019
020import java.util.Collections;
021import java.util.LinkedList;
022import java.util.List;
023
024/**
025 * Class used to keep track of a collection of error and warning diagnosis
026 * objects.
027 *
028 * @param <T> diagnosis class stored in this diagnostics instance.
029 *
030 * @author nicl
031 */
032public class Diagnostics<T> {
033
034    /** List of error diagnoses. */
035    protected List<T> errors = new LinkedList<T>();
036
037    /** List of warning diagnoses. */
038    protected List<T> warnings = new LinkedList<T>();
039
040    /**
041     * Resets the collection of errors and warnings. Use with care.
042     */
043    public void reset() {
044        errors.clear();
045        warnings.clear();
046    }
047
048    /**
049     * Does collection have an error diagnosis.
050     * @return boolean indicating the presence of an error diagnosis
051     */
052    public boolean hasErrors() {
053        return !errors.isEmpty();
054    }
055
056    /**
057     * Does collection have a warning diagnosis.
058     * @return boolean indicating the presence of a warning diagnosis
059     */
060    public boolean hasWarnings() {
061        return !warnings.isEmpty();
062    }
063
064    /**
065     * Add all errors/warnings from supplied object to this one.
066     * @param diagnostics other diagnostics with errors/warnings
067     */
068    public void addAll(Diagnostics<T> diagnostics) {
069        if (diagnostics != null && diagnostics != this) {
070            errors.addAll(diagnostics.errors);
071            warnings.addAll(diagnostics.warnings);
072        }
073    }
074
075    /**
076     * Add errors diagnosis to the collection.
077     * @param d error diagnosis
078     */
079    public void addError(T d) {
080        errors.add(d);
081    }
082
083    /**
084     * Add warning diagnosis to the collection.
085     * @param d warning diagnosis
086     */
087    public void addWarning(T d) {
088        warnings.add(d);
089    }
090
091    /**
092     * Returns unmodifiable list of error diagnoses.
093     * @return unmodifiable list of error diagnoses
094     */
095    public List<T> getErrors() {
096        return Collections.unmodifiableList(errors);
097    }
098
099    /**
100     * Returns unmodifiable list of warning diagnoses.
101     * @return unmodifiable list of warning diagnoses
102     */
103    public List<T> getWarnings() {
104        return Collections.unmodifiableList(warnings);
105    }
106
107}