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.

IssuesFinderSort.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 com.google.common.base.Function;
  22. import com.google.common.collect.Ordering;
  23. import java.util.Collection;
  24. import java.util.Date;
  25. import java.util.List;
  26. import javax.annotation.Nonnull;
  27. import org.sonar.api.rule.Severity;
  28. import org.sonar.db.issue.IssueDto;
  29. import org.sonar.server.issue.index.IssueQuery;
  30. /**
  31. * @since 3.6
  32. */
  33. class IssuesFinderSort {
  34. private List<IssueDto> issues;
  35. private IssueQuery query;
  36. public IssuesFinderSort(List<IssueDto> issues, IssueQuery query) {
  37. this.issues = issues;
  38. this.query = query;
  39. }
  40. public List<IssueDto> sort() {
  41. String sort = query.sort();
  42. Boolean asc = query.asc();
  43. if (sort != null && asc != null) {
  44. return getIssueProcessor(sort).sort(issues, asc);
  45. }
  46. return issues;
  47. }
  48. private static IssueProcessor getIssueProcessor(String sort) {
  49. if (IssueQuery.SORT_BY_SEVERITY.equals(sort)) {
  50. return new SeveritySortIssueProcessor();
  51. }
  52. if (IssueQuery.SORT_BY_STATUS.equals(sort)) {
  53. return new StatusSortIssueProcessor();
  54. }
  55. if (IssueQuery.SORT_BY_CREATION_DATE.equals(sort)) {
  56. return new CreationDateSortIssueProcessor();
  57. }
  58. if (IssueQuery.SORT_BY_UPDATE_DATE.equals(sort)) {
  59. return new UpdateDateSortIssueProcessor();
  60. }
  61. if (IssueQuery.SORT_BY_CLOSE_DATE.equals(sort)) {
  62. return new CloseDateSortIssueProcessor();
  63. }
  64. throw new IllegalArgumentException("Cannot sort on field : " + sort);
  65. }
  66. interface IssueProcessor {
  67. Function sortFieldFunction();
  68. Ordering sortFieldOrdering(boolean ascending);
  69. default List<IssueDto> sort(Collection<IssueDto> issueDtos, boolean ascending) {
  70. Ordering<IssueDto> ordering = sortFieldOrdering(ascending).onResultOf(sortFieldFunction());
  71. return ordering.immutableSortedCopy(issueDtos);
  72. }
  73. }
  74. abstract static class TextSortIssueProcessor implements IssueProcessor {
  75. @Override
  76. public Function sortFieldFunction() {
  77. return (Function<IssueDto, String>) this::sortField;
  78. }
  79. abstract String sortField(IssueDto issueDto);
  80. @Override
  81. public Ordering sortFieldOrdering(boolean ascending) {
  82. Ordering<String> ordering = Ordering.from(String.CASE_INSENSITIVE_ORDER).nullsLast();
  83. if (!ascending) {
  84. ordering = ordering.reverse();
  85. }
  86. return ordering;
  87. }
  88. }
  89. static class StatusSortIssueProcessor extends TextSortIssueProcessor {
  90. @Override
  91. String sortField(IssueDto issueDto) {
  92. return issueDto.getStatus();
  93. }
  94. }
  95. static class SeveritySortIssueProcessor implements IssueProcessor {
  96. @Override
  97. public Function sortFieldFunction() {
  98. return IssueDtoToSeverity.INSTANCE;
  99. }
  100. @Override
  101. public Ordering sortFieldOrdering(boolean ascending) {
  102. Ordering<Integer> ordering = Ordering.<Integer>natural().nullsLast();
  103. if (!ascending) {
  104. ordering = ordering.reverse();
  105. }
  106. return ordering;
  107. }
  108. }
  109. abstract static class DateSortRowProcessor implements IssueProcessor {
  110. @Override
  111. public Function sortFieldFunction() {
  112. return (Function<IssueDto, Date>) this::sortField;
  113. }
  114. abstract Date sortField(IssueDto issueDto);
  115. @Override
  116. public Ordering sortFieldOrdering(boolean ascending) {
  117. Ordering<Date> ordering = Ordering.<Date>natural().nullsLast();
  118. if (!ascending) {
  119. ordering = ordering.reverse();
  120. }
  121. return ordering;
  122. }
  123. }
  124. static class CreationDateSortIssueProcessor extends DateSortRowProcessor {
  125. @Override
  126. Date sortField(IssueDto issueDto) {
  127. return issueDto.getIssueCreationDate();
  128. }
  129. }
  130. static class UpdateDateSortIssueProcessor extends DateSortRowProcessor {
  131. @Override
  132. Date sortField(IssueDto issueDto) {
  133. return issueDto.getIssueUpdateDate();
  134. }
  135. }
  136. static class CloseDateSortIssueProcessor extends DateSortRowProcessor {
  137. @Override
  138. Date sortField(IssueDto issueDto) {
  139. return issueDto.getIssueCloseDate();
  140. }
  141. }
  142. private enum IssueDtoToSeverity implements Function<IssueDto, Integer> {
  143. INSTANCE;
  144. @Override
  145. public Integer apply(@Nonnull IssueDto issueDto) {
  146. return Severity.ALL.indexOf(issueDto.getSeverity());
  147. }
  148. }
  149. }