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