Line data Source code
1 : // Copyright (C) 2012 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.entities; 16 : 17 : import com.google.gerrit.extensions.client.SubmitType; 18 : 19 : /** Describes the submit type for a change. */ 20 : public class SubmitTypeRecord { 21 103 : public enum Status { 22 : /** The type was computed successfully */ 23 103 : OK, 24 : 25 : /** 26 : * An internal server error occurred preventing computation. 27 : * 28 : * <p>Additional detail may be available in {@link SubmitTypeRecord#errorMessage} 29 : */ 30 103 : RULE_ERROR 31 : } 32 : 33 : public static SubmitTypeRecord OK(SubmitType type) { 34 103 : return new SubmitTypeRecord(Status.OK, type, null); 35 : } 36 : 37 : public static SubmitTypeRecord error(String err) { 38 1 : return new SubmitTypeRecord(SubmitTypeRecord.Status.RULE_ERROR, null, err); 39 : } 40 : 41 : /** Status enum value of the record. */ 42 : public final Status status; 43 : 44 : /** Submit type of the record; never null if {@link #status} is {@code OK}. */ 45 : public final SubmitType type; 46 : 47 : /** Submit type of the record; always null if {@link #status} is {@code OK}. */ 48 : public final String errorMessage; 49 : 50 103 : private SubmitTypeRecord(Status status, SubmitType type, String errorMessage) { 51 103 : if (type == SubmitType.INHERIT) { 52 0 : throw new IllegalArgumentException("Cannot output submit type " + type); 53 : } 54 103 : this.status = status; 55 103 : this.type = type; 56 103 : this.errorMessage = errorMessage; 57 103 : } 58 : 59 : public boolean isOk() { 60 103 : return status == Status.OK; 61 : } 62 : 63 : @Override 64 : public String toString() { 65 0 : StringBuilder sb = new StringBuilder(); 66 0 : sb.append(status); 67 0 : if (status == Status.RULE_ERROR && errorMessage != null) { 68 0 : sb.append(" (").append(errorMessage).append(")"); 69 : } 70 0 : if (type != null) { 71 0 : sb.append('['); 72 0 : sb.append(type.name()); 73 0 : sb.append(']'); 74 : } 75 0 : return sb.toString(); 76 : } 77 : }