1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.libcore.timezone.tzlookup; 18 19 import java.util.ArrayList; 20 import java.util.LinkedList; 21 import java.util.List; 22 23 /** 24 * Stores context, errors and error severity for logging and flow control. 25 */ 26 final class Errors { 27 28 private final static int LEVEL_WARNING = 1; 29 private final static int LEVEL_ERROR = 2; 30 private final static int LEVEL_FATAL = 3; 31 32 private int level = 0; 33 34 private final LinkedList<String> scopes = new LinkedList<>(); 35 private final List<String> messages = new ArrayList<>(); 36 Errors()37 Errors() { 38 } 39 pushScope(String name)40 void pushScope(String name) { 41 scopes.add(name); 42 } 43 popScope()44 String popScope() { 45 return scopes.removeLast(); 46 } 47 addFatal(String msg)48 void addFatal(String msg) { 49 level = Math.max(level, LEVEL_FATAL); 50 add(msg); 51 } 52 addError(String msg)53 void addError(String msg) { 54 level = Math.max(level, LEVEL_ERROR); 55 add(msg); 56 } 57 addWarning(String msg)58 void addWarning(String msg) { 59 level = Math.max(level, LEVEL_WARNING); 60 add(msg); 61 } 62 asString()63 String asString() { 64 StringBuilder sb = new StringBuilder(); 65 for (String message : messages) { 66 sb.append(message); 67 sb.append("\n"); 68 } 69 return sb.toString(); 70 } 71 isEmpty()72 boolean isEmpty() { 73 return messages.isEmpty(); 74 } 75 hasError()76 boolean hasError() { 77 return level >= LEVEL_ERROR; 78 } 79 hasFatal()80 boolean hasFatal() { 81 return level >= LEVEL_FATAL; 82 } 83 add(String msg)84 private void add(String msg) { 85 messages.add(scopes.toString() + ": " + msg); 86 } 87 } 88