    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 *
    017 */
    018package org.apache.bcel.verifier;
    019
    020/**
    021 * A VerificationResult is what a PassVerifier returns
    022 * after verifying.
    023 *
    024 * @version $Id: VerificationResult.java 1811486 2017-10-08 12:49:43Z dbrosius $
    025 */
    026public class VerificationResult {
    027
    028    /**
    029     * Constant to indicate verification has not been tried yet.
    030     * This happens if some earlier verification pass did not return VERIFIED_OK.
    031     */
    032    public static final int VERIFIED_NOTYET = 0;
    033
    034    /** Constant to indicate verification was passed. */
    035    public static final int VERIFIED_OK = 1;
    036
    037    /** Constant to indicate verfication failed. */
    038    public static final int VERIFIED_REJECTED = 2;
    039
    040    /**
    041     * This string is the canonical message for verifications that have not been tried yet.
    042     * This happens if some earlier verification pass did not return {@link #VERIFIED_OK}.
    043     */
    044    private static final String VERIFIED_NOTYET_MSG = "Not yet verified.";
    045
    046    /** This string is the canonical message for passed verification passes. */
    047    private static final String VERIFIED_OK_MSG = "Passed verification.";
    048
    049    /**
    050     * Canonical VerificationResult for not-yet-tried verifications.
    051     * This happens if some earlier verification pass did not return {@link #VERIFIED_OK}.
    052     */
    053    public static final VerificationResult VR_NOTYET = new VerificationResult(VERIFIED_NOTYET, VERIFIED_NOTYET_MSG);
    054
    055    /** Canonical VerificationResult for passed verifications. */
    056    public static final VerificationResult VR_OK = new VerificationResult(VERIFIED_OK, VERIFIED_OK_MSG);
    057
    058    /** The numeric status. */
    059    private final int numeric;
    060
    061    /** The detailed message. */
    062    private final String detailMessage;
    063
    064
    065    /** The usual constructor. */
    066    public VerificationResult(final int status, final String message) {
    067        numeric = status;
    068        detailMessage = message;
    069    }
    070
    071
    072    /**
    073     * Returns one of the {@link #VERIFIED_OK}, {@link #VERIFIED_NOTYET},
    074     * {@link #VERIFIED_REJECTED} constants.
    075     */
    076    public int getStatus() {
    077        return numeric;
    078    }
    079
    080
    081    /** Returns a detailed message. */
    082    public String getMessage() {
    083        return detailMessage;
    084    }
    085
    086
    087    /**
    088     * @return a hash code value for the object.
    089     */
    090    @Override
    091    public int hashCode() {
    092        return numeric ^ detailMessage.hashCode();
    093    }
    094
    095
    096    /**
    097     * Returns if two VerificationResult instances are equal.
    098     */
    099    @Override
    100    public boolean equals( final Object o ) {
    101        if (!(o instanceof VerificationResult)) {
    102            return false;
    103        }
    104        final VerificationResult other = (VerificationResult) o;
    105        return (other.numeric == this.numeric) && other.detailMessage.equals(this.detailMessage);
    106    }
    107
    108
    109    /**
    110     * Returns a String representation of the VerificationResult.
    111     */
    112    @Override
    113    public String toString() {
    114        String ret = "";
    115        if (numeric == VERIFIED_NOTYET) {
    116            ret = "VERIFIED_NOTYET";
    117        }
    118        if (numeric == VERIFIED_OK) {
    119            ret = "VERIFIED_OK";
    120        }
    121        if (numeric == VERIFIED_REJECTED) {
    122            ret = "VERIFIED_REJECTED";
    123        }
    124        ret += "\n" + detailMessage + "\n";
    125        return ret;
    126    }
    127}