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
020
021/**
022 * Class representing a diagnosis which was found while validating an entity.
023 *
024 * @author nicl
025 */
026public class Diagnosis {
027
028    /** Diagnosis type. */
029    public final DiagnosisType type;
030
031    /** Source entity on which the diagnosis is relevant. */
032    public final String entity;
033
034    /** Any optional information which may be relevant for the specific
035     * diagnosis. */
036    public final String[] information;
037
038    /**
039     * Construct a diagnosis instance given the type, entity and optional
040     * information.
041     * @param type diagnosis type
042     * @param entity entity relevant for this diagnosis
043     * @param information optional information relevant for this diagnosis
044     */
045    public Diagnosis(DiagnosisType type, String entity, String... information) {
046        if (type == null) {
047            throw new IllegalArgumentException("'type' is null!");
048        }
049        if (entity == null) {
050            throw new IllegalArgumentException("'entity' is null!");
051        }
052        this.type = type;
053        this.entity = entity;
054        this.information = information;
055        if (type.expected_information > 0 && (information == null
056                || information.length < type.expected_information)) {
057            throw new IllegalArgumentException("Missing information!");
058        }
059    }
060
061    /**
062     * Returns an array comprising of the entity followed by the information
063     * array elements.
064     * @return array with the entity followed by the information array elements
065     */
066    public Object[] getMessageArgs() {
067        Object[] messageArgs = new Object[information.length + 1];
068        messageArgs[0] = entity;
069        if (information.length > 0) {
070            System.arraycopy(information, 0, messageArgs, 1, information.length);
071        }
072        return messageArgs;
073    }
074
075    @Override
076    public boolean equals(Object obj) {
077        if (obj == null || !(obj instanceof Diagnosis)) {
078            return false;
079        }
080        Diagnosis diagnosisObj = (Diagnosis)obj;
081        if (!type.equals(diagnosisObj.type)) {
082            return false;
083        }
084        if (!entity.equals(diagnosisObj.entity)) {
085            return false;
086        }
087        if (information != null && diagnosisObj.information != null) {
088            if (information.length != diagnosisObj.information.length) {
089                return false;
090            }
091            for (int i=0; i<information.length; ++i) {
092                if (information[i] != null) {
093                    if (!information[i].equals(diagnosisObj.information[i])) {
094                        return false;
095                    }
096                } else if (diagnosisObj.information[i] != null) {
097                    return false;
098                }
099            }
100        } else if (information != null || diagnosisObj.information != null) {
101            return false;
102        }
103        return true;
104    }
105
106    @Override
107    public int hashCode() {
108        int hashCode = type.hashCode();
109        hashCode ^= entity.hashCode();
110        if (information != null) {
111            hashCode ^= 31331;
112            for (int i=0; i<information.length; ++i) {
113                if (information[ i ] != null) {
114                    hashCode ^= information[ i ].hashCode();
115                }
116            }
117        }
118        return hashCode;
119    }
120
121}