/ app / src / main / java / com / reandroid / common / DiagnosticMessage.java
DiagnosticMessage.java
  1  /*
  2   *  Copyright (C) 2022 github.com/REAndroid
  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  package com.reandroid.common;
 17  
 18  public interface DiagnosticMessage {
 19      Type type();
 20      default String getTag() {
 21          return null;
 22      }
 23      default Origin getSource() {
 24          return null;
 25      }
 26  
 27      class StringMessage implements DiagnosticMessage {
 28  
 29          private final Type type;
 30          private final Origin source;
 31          private final String tag;
 32          private final String message;
 33  
 34          public StringMessage(Type type, Origin source, String tag, String message) {
 35              this.type = type;
 36              this.source = source;
 37              this.tag = tag;
 38              this.message = message;
 39          }
 40          public StringMessage(Type type, String tag, String message) {
 41              this(type, null, tag, message);
 42          }
 43          public StringMessage(Type type, String message) {
 44              this(type, null, null, message);
 45          }
 46          @Override
 47          public Type type() {
 48              return type;
 49          }
 50          @Override
 51          public Origin getSource() {
 52              return source;
 53          }
 54          @Override
 55          public String getTag() {
 56              return tag;
 57          }
 58  
 59          public String getMessage() {
 60              return message;
 61          }
 62  
 63          @Override
 64          public String toString() {
 65              StringBuilder builder = new StringBuilder();
 66              builder.append(type().getName());
 67              builder.append(": ");
 68              String tag = getTag();
 69              if(tag != null) {
 70                  builder.append(tag);
 71                  builder.append(" : ");
 72              }
 73              Origin source = getSource();
 74              if(source != null) {
 75                  builder.append(source);
 76                  builder.append(", ");
 77              }
 78              builder.append(getMessage());
 79              return builder.toString();
 80          }
 81      }
 82  
 83      enum Type {
 84  
 85          INFO("I"),
 86          WARN("W"),
 87          ERROR("E"),
 88          VERBOSE("V"),
 89          DEBUG("D");
 90  
 91          String simpleName;
 92  
 93          Type(String simpleName) {
 94              this.simpleName = simpleName;
 95          }
 96          public String getName() {
 97              return simpleName;
 98          }
 99      }
100  }