aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/main/java/org/sonar/api/utils/ValidationMessages.java
blob: 967bcd67dbb10b72d1f8b14de6021641d66d9c8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * SonarQube, open source software quality management tool.
 * Copyright (C) 2008-2014 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * SonarQube is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * SonarQube is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.api.utils;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.List;

public final class ValidationMessages {

  private List<String> errors = new ArrayList<String>();
  private List<String> warnings = new ArrayList<String>();
  private List<String> infos = new ArrayList<String>();

  /**
   * Use the factory method <code>create()</code>
   */
  ValidationMessages() {
  }

  public static ValidationMessages create() {
    return new ValidationMessages();
  }

  public boolean hasErrors() {
    return !errors.isEmpty();
  }

  public List<String> getErrors() {
    return errors;
  }

  public ValidationMessages addErrorText(String text) {
    errors.add(text);
    return this;
  }

  public List<String> getWarnings() {
    return warnings;
  }

  public boolean hasWarnings() {
    return !warnings.isEmpty();
  }

  public ValidationMessages addWarningText(String text) {
    warnings.add(text);
    return this;
  }

  public List<String> getInfos() {
    return infos;
  }

  public boolean hasInfos() {
    return !infos.isEmpty();
  }

  public ValidationMessages addInfoText(String text) {
    infos.add(text);
    return this;
  }

  /**
   * @since 5.1
   */
  public void log(org.sonar.api.utils.log.Logger logger) {
    for (String error : getErrors()) {
      logger.error(error);
    }
    for (String warning : getWarnings()) {
      logger.warn(warning);
    }
    for (String info : getInfos()) {
      logger.info(info);
    }
  }

  /**
   * @deprecated replaced by {@link #log(org.sonar.api.utils.log.Logger)} since deprecation of slf4j in 5.1
   */
  @Deprecated
  public void log(Logger logger) {
    for (String error : getErrors()) {
      logger.error(error);
    }
    for (String warning : getWarnings()) {
      logger.warn(warning);
    }
    for (String info : getInfos()) {
      logger.info(info);
    }
  }

  @Override
  public String toString() {
    return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
        .append("errors", errors)
        .append("warnings", warnings)
        .append("infos", infos)
        .toString();
  }
}