001/* 002 * SonarQube 003 * Copyright (C) 2009-2017 SonarSource SA 004 * mailto:info AT sonarsource DOT com 005 * 006 * This program is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public License 017 * along with this program; if not, write to the Free Software Foundation, 018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 */ 020package org.sonar.api.utils; 021 022import org.apache.commons.lang.builder.ToStringBuilder; 023import org.apache.commons.lang.builder.ToStringStyle; 024import org.slf4j.Logger; 025 026import java.util.ArrayList; 027import java.util.List; 028 029public final class ValidationMessages { 030 031 private List<String> errors = new ArrayList<>(); 032 private List<String> warnings = new ArrayList<>(); 033 private List<String> infos = new ArrayList<>(); 034 035 /** 036 * Use the factory method <code>create()</code> 037 */ 038 ValidationMessages() { 039 } 040 041 public static ValidationMessages create() { 042 return new ValidationMessages(); 043 } 044 045 public boolean hasErrors() { 046 return !errors.isEmpty(); 047 } 048 049 public List<String> getErrors() { 050 return errors; 051 } 052 053 public ValidationMessages addErrorText(String text) { 054 errors.add(text); 055 return this; 056 } 057 058 public List<String> getWarnings() { 059 return warnings; 060 } 061 062 public boolean hasWarnings() { 063 return !warnings.isEmpty(); 064 } 065 066 public ValidationMessages addWarningText(String text) { 067 warnings.add(text); 068 return this; 069 } 070 071 public List<String> getInfos() { 072 return infos; 073 } 074 075 public boolean hasInfos() { 076 return !infos.isEmpty(); 077 } 078 079 public ValidationMessages addInfoText(String text) { 080 infos.add(text); 081 return this; 082 } 083 084 /** 085 * @since 5.1 086 */ 087 public void log(org.sonar.api.utils.log.Logger logger) { 088 for (String error : getErrors()) { 089 logger.error(error); 090 } 091 for (String warning : getWarnings()) { 092 logger.warn(warning); 093 } 094 for (String info : getInfos()) { 095 logger.info(info); 096 } 097 } 098 099 /** 100 * @deprecated replaced by {@link #log(org.sonar.api.utils.log.Logger)} since deprecation of slf4j in 5.1 101 */ 102 @Deprecated 103 public void log(Logger logger) { 104 for (String error : getErrors()) { 105 logger.error(error); 106 } 107 for (String warning : getWarnings()) { 108 logger.warn(warning); 109 } 110 for (String info : getInfos()) { 111 logger.info(info); 112 } 113 } 114 115 @Override 116 public String toString() { 117 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) 118 .append("errors", errors) 119 .append("warnings", warnings) 120 .append("infos", infos) 121 .toString(); 122 } 123}