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 */
017package org.apache.bcel.verifier;
018
019/**
020 * A VerificationResult is what a PassVerifier returns after verifying.
021 */
022public class VerificationResult {
023
024    /**
025     * Constant to indicate verification has not been tried yet. This happens if some earlier verification pass did not
026     * return VERIFIED_OK.
027     */
028    public static final int VERIFIED_NOTYET = 0;
029
030    /** Constant to indicate verification was passed. */
031    public static final int VERIFIED_OK = 1;
032
033    /** Constant to indicate verfication failed. */
034    public static final int VERIFIED_REJECTED = 2;
035
036    /**
037     * This string is the canonical message for verifications that have not been tried yet. This happens if some earlier
038     * verification pass did not return {@link #VERIFIED_OK}.
039     */
040    private static final String VERIFIED_NOTYET_MSG = "Not yet verified.";
041
042    /** This string is the canonical message for passed verification passes. */
043    private static final String VERIFIED_OK_MSG = "Passed verification.";
044
045    /**
046     * Canonical VerificationResult for not-yet-tried verifications. This happens if some earlier verification pass did not
047     * return {@link #VERIFIED_OK}.
048     */
049    public static final VerificationResult VR_NOTYET = new VerificationResult(VERIFIED_NOTYET, VERIFIED_NOTYET_MSG);
050
051    /** Canonical VerificationResult for passed verifications. */
052    public static final VerificationResult VR_OK = new VerificationResult(VERIFIED_OK, VERIFIED_OK_MSG);
053
054    /** The numeric status. */
055    private final int numeric;
056
057    /** The detailed message. */
058    private final String detailMessage;
059
060    /** The usual constructor. */
061    public VerificationResult(final int status, final String message) {
062        numeric = status;
063        detailMessage = message;
064    }
065
066    /**
067     * Returns if two VerificationResult instances are equal.
068     */
069    @Override
070    public boolean equals(final Object o) {
071        if (!(o instanceof VerificationResult)) {
072            return false;
073        }
074        final VerificationResult other = (VerificationResult) o;
075        return other.numeric == this.numeric && other.detailMessage.equals(this.detailMessage);
076    }
077
078    /** Returns a detailed message. */
079    public String getMessage() {
080        return detailMessage;
081    }
082
083    /**
084     * Returns one of the {@link #VERIFIED_OK}, {@link #VERIFIED_NOTYET}, {@link #VERIFIED_REJECTED} constants.
085     */
086    public int getStatus() {
087        return numeric;
088    }
089
090    /**
091     * @return a hash code value for the object.
092     */
093    @Override
094    public int hashCode() {
095        return numeric ^ detailMessage.hashCode();
096    }
097
098    /**
099     * Returns a String representation of the VerificationResult.
100     */
101    @Override
102    public String toString() {
103        String ret = "";
104        if (numeric == VERIFIED_NOTYET) {
105            ret = "VERIFIED_NOTYET";
106        }
107        if (numeric == VERIFIED_OK) {
108            ret = "VERIFIED_OK";
109        }
110        if (numeric == VERIFIED_REJECTED) {
111            ret = "VERIFIED_REJECTED";
112        }
113        ret += "\n" + detailMessage + "\n";
114        return ret;
115    }
116}