Line data Source code
1 : // Copyright (C) 2015 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.gpg; 16 : 17 : import com.google.gerrit.extensions.common.GpgKeyInfo.Status; 18 : import java.util.ArrayList; 19 : import java.util.Arrays; 20 : import java.util.Collections; 21 : import java.util.List; 22 : 23 : /** Result of checking an object like a key or signature. */ 24 : public class CheckResult { 25 : static CheckResult ok(String... problems) { 26 1 : return create(Status.OK, problems); 27 : } 28 : 29 : static CheckResult bad(String... problems) { 30 1 : return create(Status.BAD, problems); 31 : } 32 : 33 : static CheckResult trusted() { 34 3 : return new CheckResult(Status.TRUSTED, Collections.emptyList()); 35 : } 36 : 37 : static CheckResult create(Status status, String... problems) { 38 : List<String> problemList = 39 1 : problems.length > 0 40 1 : ? Collections.unmodifiableList(Arrays.asList(problems)) 41 1 : : Collections.emptyList(); 42 1 : return new CheckResult(status, problemList); 43 : } 44 : 45 : static CheckResult create(Status status, List<String> problems) { 46 3 : return new CheckResult(status, Collections.unmodifiableList(new ArrayList<>(problems))); 47 : } 48 : 49 : static CheckResult create(List<String> problems) { 50 3 : return new CheckResult( 51 3 : problems.isEmpty() ? Status.OK : Status.BAD, Collections.unmodifiableList(problems)); 52 : } 53 : 54 : private final Status status; 55 : private final List<String> problems; 56 : 57 3 : private CheckResult(Status status, List<String> problems) { 58 3 : if (status == null) { 59 0 : throw new IllegalArgumentException("status must not be null"); 60 : } 61 3 : this.status = status; 62 3 : this.problems = problems; 63 3 : } 64 : 65 : /** Returns whether the result has status {@link Status#OK} or better. */ 66 : public boolean isOk() { 67 3 : return status.compareTo(Status.OK) >= 0; 68 : } 69 : 70 : /** Returns whether the result has status {@link Status#TRUSTED} or better. */ 71 : public boolean isTrusted() { 72 1 : return status.compareTo(Status.TRUSTED) >= 0; 73 : } 74 : 75 : /** Returns the status enum value associated with the object. */ 76 : public Status getStatus() { 77 3 : return status; 78 : } 79 : 80 : /** Returns any problems encountered during checking. */ 81 : public List<String> getProblems() { 82 3 : return problems; 83 : } 84 : 85 : @Override 86 : public String toString() { 87 0 : StringBuilder sb = new StringBuilder(getClass().getSimpleName()).append('[').append(status); 88 0 : for (int i = 0; i < problems.size(); i++) { 89 0 : sb.append(i == 0 ? ": " : ", ").append(problems.get(i)); 90 : } 91 0 : return sb.append(']').toString(); 92 : } 93 : }