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 objects.
026 *
027 * @param <T> diagnosis class stored in this diagnostics instance.
028 *
029 * @author nicl
030 */
031public class DiagnosticsGeneric<T> {
032
033    /** List of error diagnoses. */
034    protected List<T> errors = new LinkedList<T>();
035
036    /** List of warning diagnoses. */
037    protected List<T> warnings = new LinkedList<T>();
038
039    /**
040     * Resets the collection of errors and warnings. Use with care.
041     */
042    public void reset() {
043        errors.clear();
044        warnings.clear();
045    }
046
047    /**
048     * Does collection have an error diagnosis.
049     * @return boolean indicating the presence of an error diagnosis
050     */
051    public boolean hasErrors() {
052        return !errors.isEmpty();
053    }
054
055    /**
056     * Does collection have a warning diagnosis.
057     * @return boolean indicating the presence of a warning diagnosis
058     */
059    public boolean hasWarnings() {
060        return !warnings.isEmpty();
061    }
062
063    /**
064     * Add all errors/warnings from supplied object to this one.
065     * @param diagnostics other diagnostics with errors/warnings
066     */
067    public void addAll(DiagnosticsGeneric<T> diagnostics) {
068        if (diagnostics != null && diagnostics != this) {
069            errors.addAll(diagnostics.errors);
070            warnings.addAll(diagnostics.warnings);
071        }
072    }
073
074    /**
075     * Add errors diagnosis to the collection.
076     * @param d error diagnosis
077     */
078    public void addError(T d) {
079        errors.add(d);
080    }
081
082    /**
083     * Add warning diagnosis to the collection.
084     * @param d warning diagnosis
085     */
086    public void addWarning(T d) {
087        warnings.add(d);
088    }
089
090    /**
091     * Returns unmodifiable list of error diagnoses.
092     * @return unmodifiable list of error diagnoses
093     */
094    public List<T> getErrors() {
095        return Collections.unmodifiableList(errors);
096    }
097
098    /**
099     * Returns unmodifiable list of warning diagnoses.
100     * @return unmodifiable list of warning diagnoses
101     */
102    public List<T> getWarnings() {
103        return Collections.unmodifiableList(warnings);
104    }
105
106}