001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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<String>();
032  private List<String> warnings = new ArrayList<String>();
033  private List<String> infos = new ArrayList<String>();
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  public void log(Logger logger) {
085    for (String error : getErrors()) {
086      logger.error(error);
087    }
088    for (String warning : getWarnings()) {
089      logger.warn(warning);
090    }
091    for (String info : getInfos()) {
092      logger.info(info);
093    }
094  }
095
096  @Override
097  public String toString() {
098    return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
099        .append("errors", errors)
100        .append("warnings", warnings)
101        .append("infos", infos)
102        .toString();
103  }
104}