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