Line data Source code
1 : // Copyright (C) 2014 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.git.validators; 16 : 17 : import java.util.Objects; 18 : 19 : /** 20 : * Message used as result of a validation that run during a git operation (for example {@code git 21 : * push}. Intended to be shown to users. 22 : */ 23 : public class ValidationMessage { 24 91 : public enum Type { 25 91 : FATAL("FATAL: "), 26 91 : ERROR("ERROR: "), 27 91 : WARNING("WARNING: "), 28 91 : HINT("hint: "), 29 91 : OTHER(""); 30 : 31 : private final String prefix; 32 : 33 91 : Type(String prefix) { 34 91 : this.prefix = prefix; 35 91 : } 36 : 37 : public String getPrefix() { 38 90 : return prefix; 39 : } 40 : } 41 : 42 : private final String message; 43 : private final Type type; 44 : 45 : /** @see ValidationMessage */ 46 89 : public ValidationMessage(String message, Type type) { 47 89 : this.message = message; 48 89 : this.type = type; 49 89 : } 50 : 51 : // TODO: Remove and move callers to ValidationMessage(String message, Type type) 52 9 : public ValidationMessage(String message, boolean isError) { 53 9 : this.message = message; 54 9 : this.type = (isError ? Type.ERROR : Type.OTHER); 55 9 : } 56 : 57 : /** Returns the message to be shown to the user. */ 58 : public String getMessage() { 59 91 : return message; 60 : } 61 : 62 : /** 63 : * Returns the {@link Type}. Used to as prefix for the message in the git CLI and to color 64 : * messages. 65 : */ 66 : public Type getType() { 67 90 : return type; 68 : } 69 : 70 : /** 71 : * Returns {@code true} if this message is an error. Used to decide if the operation should be 72 : * aborted. 73 : */ 74 : public boolean isError() { 75 7 : return type == Type.FATAL || type == Type.ERROR; 76 : } 77 : 78 : @Override 79 : public int hashCode() { 80 0 : return Objects.hash(message, type); 81 : } 82 : 83 : @Override 84 : public boolean equals(Object obj) { 85 0 : if (obj instanceof ValidationMessage) { 86 0 : ValidationMessage other = (ValidationMessage) obj; 87 0 : return Objects.equals(message, other.message) && Objects.equals(type, other.type); 88 : } 89 0 : return false; 90 : } 91 : 92 : @Override 93 : public String toString() { 94 0 : return getType() + ": " + getMessage(); 95 : } 96 : }