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 after verifying.
022 *
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    /** The usual constructor. */
063    public VerificationResult(final int status, final String message) {
064        numeric = status;
065        detailMessage = message;
066    }
067
068    /**
069     * Returns if two VerificationResult instances are equal.
070     */
071    @Override
072    public boolean equals(final Object o) {
073        if (!(o instanceof VerificationResult)) {
074            return false;
075        }
076        final VerificationResult other = (VerificationResult) o;
077        return other.numeric == this.numeric && other.detailMessage.equals(this.detailMessage);
078    }
079
080    /** Returns a detailed message. */
081    public String getMessage() {
082        return detailMessage;
083    }
084
085    /**
086     * Returns one of the {@link #VERIFIED_OK}, {@link #VERIFIED_NOTYET}, {@link #VERIFIED_REJECTED} constants.
087     */
088    public int getStatus() {
089        return numeric;
090    }
091
092    /**
093     * @return a hash code value for the object.
094     */
095    @Override
096    public int hashCode() {
097        return numeric ^ detailMessage.hashCode();
098    }
099
100    /**
101     * Returns a String representation of the VerificationResult.
102     */
103    @Override
104    public String toString() {
105        String ret = "";
106        if (numeric == VERIFIED_NOTYET) {
107            ret = "VERIFIED_NOTYET";
108        }
109        if (numeric == VERIFIED_OK) {
110            ret = "VERIFIED_OK";
111        }
112        if (numeric == VERIFIED_REJECTED) {
113            ret = "VERIFIED_REJECTED";
114        }
115        ret += "\n" + detailMessage + "\n";
116        return ret;
117    }
118}