diff options
author | Julien Lancelot <julien.lancelot@gmail.com> | 2013-04-24 10:19:30 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@gmail.com> | 2013-04-24 10:19:30 +0200 |
commit | d5e8c6cc4cee60aa29f0f403b294f67e9f98d008 (patch) | |
tree | baa57765bb557f91a400d4846af6314f11b1f60c /sonar-plugin-api | |
parent | 25b225b1ed7081bdc11344cd746d376adcbcf9bd (diff) | |
download | sonarqube-d5e8c6cc4cee60aa29f0f403b294f67e9f98d008.tar.gz sonarqube-d5e8c6cc4cee60aa29f0f403b294f67e9f98d008.zip |
SONAR-3755 Improve IssueFinder pagination
Diffstat (limited to 'sonar-plugin-api')
5 files changed, 117 insertions, 11 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueFinder.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueFinder.java index e3c3b5a62ce..d40120f32db 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueFinder.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueFinder.java @@ -47,6 +47,8 @@ public interface IssueFinder extends ServerComponent { Component component(Issue issue); Collection<Component> components(); + + Pagination pagination(); } Results find(IssueQuery query, @Nullable Integer currentUserId, String role); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueQuery.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueQuery.java index 4ad5bc1965b..b481db42961 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueQuery.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueQuery.java @@ -116,16 +116,8 @@ public class IssueQuery { return limit; } -// public int pages(int count) { -// int p = (count / limit); -// if ((count % limit) > 0) { -// p++; -// } -// return p; -// } - - public int offset(){ - return (page - 1) * limit; + public int page() { + return page; } @Override diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/Pagination.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/Pagination.java new file mode 100644 index 00000000000..bacb6d2d637 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/Pagination.java @@ -0,0 +1,62 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 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.issue; + +public class Pagination { + + private int limit; + private int page; + private int count; + + public Pagination(int limit, int page, int count) { + this.limit = limit; + this.page = page; + this.count = count; + } + + public int page() { + return page; + } + + public int limit() { + return limit; + } + + public int count() { + return count; + } + + public int offset(){ + return (page - 1) * limit; + } + + public int pages() { + int p = (count / limit); + if ((count % limit) > 0) { + p++; + } + return p; + } + + public boolean isEmpty() { + return count == 0; + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/issue/IssueQueryTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/issue/IssueQueryTest.java index dd1a2a38990..8f46066a642 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/issue/IssueQueryTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/issue/IssueQueryTest.java @@ -59,6 +59,6 @@ public class IssueQueryTest { assertThat(query.createdAfter()).isNotNull(); assertThat(query.createdBefore()).isNotNull(); assertThat(query.limit()).isEqualTo(10); - assertThat(query.offset()).isEqualTo(10); + assertThat(query.page()).isEqualTo(2); } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/issue/PaginationTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/issue/PaginationTest.java new file mode 100644 index 00000000000..f93ab52ea5b --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/issue/PaginationTest.java @@ -0,0 +1,50 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 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.issue; + +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; + +public class PaginationTest { + + @Test + public void test_pagination(){ + Pagination pagination = new Pagination(5, 1, 20); + + assertThat(pagination.limit()).isEqualTo(5); + assertThat(pagination.page()).isEqualTo(1); + assertThat(pagination.count()).isEqualTo(20); + assertThat(pagination.isEmpty()).isFalse(); + + assertThat(pagination.offset()).isEqualTo(0); + assertThat(pagination.pages()).isEqualTo(4); + } + + @Test + public void test_pagination_on_second_page(){ + Pagination pagination = new Pagination(5, 2, 20); + + assertThat(pagination.offset()).isEqualTo(5); + assertThat(pagination.pages()).isEqualTo(4); + } + +} |