You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Result.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.issue;
  21. import java.util.Arrays;
  22. import java.util.List;
  23. import javax.annotation.CheckForNull;
  24. import javax.annotation.Nullable;
  25. import org.apache.commons.lang.builder.ReflectionToStringBuilder;
  26. import static com.google.common.collect.Lists.newArrayList;
  27. /**
  28. * @since 3.6
  29. */
  30. public class Result<T> {
  31. private T object;
  32. private final List<Message> errors = newArrayList();
  33. private Result(@Nullable T object) {
  34. this.object = object;
  35. }
  36. public static <T> Result<T> of() {
  37. return new Result<>(null);
  38. }
  39. public Result<T> set(@Nullable T object) {
  40. this.object = object;
  41. return this;
  42. }
  43. public T get() {
  44. return object;
  45. }
  46. public Result<T> addError(String text) {
  47. return addError(Message.of(text));
  48. }
  49. public Result<T> addError(Message message) {
  50. errors.add(message);
  51. return this;
  52. }
  53. /**
  54. * List of error messages. Empty if there are no errors.
  55. */
  56. public List<Message> errors() {
  57. return errors;
  58. }
  59. /**
  60. * True if there are no errors.
  61. */
  62. public boolean ok() {
  63. return errors.isEmpty();
  64. }
  65. public int httpStatus() {
  66. if (ok()) {
  67. return 200;
  68. }
  69. // TODO support 401, 403 and 500
  70. return 400;
  71. }
  72. public static class Message {
  73. private final String l10nKey;
  74. private final Object[] l10nParams;
  75. private final String text;
  76. private Message(@Nullable String l10nKey, @Nullable Object[] l10nParams, @Nullable String text) {
  77. this.l10nKey = l10nKey;
  78. this.l10nParams = l10nParams;
  79. this.text = text;
  80. }
  81. public static Message of(String text) {
  82. return new Message(null, null, text);
  83. }
  84. public static Message ofL10n(String l10nKey, Object... l10nParams) {
  85. return new Message(l10nKey, l10nParams, null);
  86. }
  87. @CheckForNull
  88. public String text() {
  89. return text;
  90. }
  91. @CheckForNull
  92. public String l10nKey() {
  93. return l10nKey;
  94. }
  95. @CheckForNull
  96. public Object[] l10nParams() {
  97. return l10nParams;
  98. }
  99. @Override
  100. public boolean equals(Object o) {
  101. if (this == o) {
  102. return true;
  103. }
  104. if (o == null || getClass() != o.getClass()) {
  105. return false;
  106. }
  107. Message message = (Message) o;
  108. if ((l10nKey != null) ? !l10nKey.equals(message.l10nKey) : (message.l10nKey != null)) {
  109. return false;
  110. }
  111. // Probably incorrect - comparing Object[] arrays with Arrays.equals
  112. return Arrays.equals(l10nParams, message.l10nParams) && ((text != null) ? text.equals(message.text) : (message.text == null));
  113. }
  114. @Override
  115. public int hashCode() {
  116. int result = l10nKey != null ? l10nKey.hashCode() : 0;
  117. result = 31 * result + (l10nParams != null ? Arrays.hashCode(l10nParams) : 0);
  118. result = 31 * result + (text != null ? text.hashCode() : 0);
  119. return result;
  120. }
  121. @Override
  122. public String toString() {
  123. return new ReflectionToStringBuilder(this).toString();
  124. }
  125. }
  126. }