summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/AndroidManifest.xml23
-rw-r--r--src/main/java/it/alessandroiezzi/commons/page/Page.java28
-rw-r--r--src/main/java/it/alessandroiezzi/commons/page/PageAdapter.java170
-rw-r--r--src/main/java/it/alessandroiezzi/commons/page/PageChangeListener.java26
-rw-r--r--src/main/java/it/alessandroiezzi/commons/page/PageImpl.java51
-rw-r--r--src/main/java/it/alessandroiezzi/commons/page/Pages.java42
6 files changed, 340 insertions, 0 deletions
diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml
new file mode 100644
index 0000000..6e32572
--- /dev/null
+++ b/src/main/AndroidManifest.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ ~ Copyright (C) 2021-2022 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it>
+ ~
+ ~ This file is part of TOP-TAB Android.
+ ~
+ ~ TOP-TAB Android is free software: you can redistribute it and/or modify
+ ~ it under the terms of the GNU General Public License as published by
+ ~ the Free Software Foundation, either version 3 of the License, or
+ ~ (at your option) any later version.
+ ~
+ ~ TOP-TAB Android 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 General Public License for more details.
+ ~
+ ~ You should have received a copy of the GNU General Public License
+ ~ along with TOP-TAB Android. If not, see <https://www.gnu.org/licenses/>.
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android">
+
+</manifest> \ No newline at end of file
diff --git a/src/main/java/it/alessandroiezzi/commons/page/Page.java b/src/main/java/it/alessandroiezzi/commons/page/Page.java
new file mode 100644
index 0000000..333dd4f
--- /dev/null
+++ b/src/main/java/it/alessandroiezzi/commons/page/Page.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2022 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it>
+ *
+ * This file is part of commons-page.
+ *
+ * commons-page is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * commons-page 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with commons-page. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package it.alessandroiezzi.commons.page;
+
+import java.util.*;
+
+public interface Page<T> {
+ List<T> getContent();
+ int getSize();
+ int getNumberOfElements();
+}
diff --git a/src/main/java/it/alessandroiezzi/commons/page/PageAdapter.java b/src/main/java/it/alessandroiezzi/commons/page/PageAdapter.java
new file mode 100644
index 0000000..8774677
--- /dev/null
+++ b/src/main/java/it/alessandroiezzi/commons/page/PageAdapter.java
@@ -0,0 +1,170 @@
+/*
+ * Copyright (C) 2022 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it>
+ *
+ * This file is part of commons-page.
+ *
+ * commons-page is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * commons-page 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with commons-page. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package it.alessandroiezzi.commons.page;
+
+import java.util.*;
+import java.util.function.*;
+
+public class PageAdapter<T> implements Pages<T> {
+ private final List<T> elements;
+ private final int pageSize;
+ private int pageNumber;
+ private PageChangeListener<T> pageChangeListener;
+ boolean pageChanged = false;
+ private Page<T> currentPage;
+
+ public PageAdapter(List<T> elements) {
+ this(elements, 15);
+ }
+
+ public PageAdapter(List<T> elements, int pageSize) {
+ this.elements = elements;
+ this.pageSize = pageSize;
+ this.pageNumber = 0;
+ }
+
+ @Override
+ public Page<T> firstPage() {
+ return toPage(0);
+ }
+
+ @Override
+ public Page<T> previousPage() {
+ return toPage(pageNumber - 1);
+ }
+
+ @Override
+ public Page<T> nextPage() {
+ return toPage(pageNumber + 1);
+ }
+
+ @Override
+ public Page<T> lastPage() {
+ return toPage(getTotalPages() - 1);
+ }
+
+ @Override
+ public Page<T> toPage(int page) {
+ if (page < 0) {
+ page = 0;
+ } else if (page >= getTotalPages()) {
+ page = getTotalPages() - 1;
+ }
+
+ int totalElements = getNumberOfElements();
+ int startIndex = page * pageSize;
+ if (startIndex > totalElements) {
+ startIndex = totalElements;
+ }
+
+ int count = startIndex + pageSize;
+ if (count > totalElements) {
+ count = totalElements;
+ }
+
+ pageChanged = page != pageNumber;
+ pageNumber = page;
+ List<T> subElements = elements.subList(startIndex, count);
+ currentPage = new PageImpl<>(subElements, pageSize);
+
+ if (pageChangeListener != null && pageChanged) {
+ pageChangeListener.pageChanged(new ArrayList<>(subElements), pageSize);
+ }
+
+ return currentPage;
+ }
+
+ @Override
+ public Page<T> getCurrentPage() {
+ return currentPage;
+ }
+
+ @Override
+ public int getCurrentPageNumber() {
+ return pageNumber;
+ }
+
+ @Override
+ public List<T> getContent() {
+ return getCurrentPage().getContent();
+ }
+
+ @Override
+ public int getSize() {
+ return pageSize;
+ }
+
+ @Override
+ public int getNumberOfElements() {
+ return elements.size();
+ }
+
+ @Override
+ public void addElement(T element) {
+ elements.add(element);
+ }
+
+ @Override
+ public void removeElement(T element) {
+ elements.remove(element);
+ }
+
+ @Override
+ public void removeElementIf(Predicate<T> predicate) {
+ elements.removeIf(predicate);
+ }
+
+ @Override
+ public int getTotalPages() {
+ double pages = (double) getNumberOfElements() / (double) getSize();
+ int pagesInt = (int) pages;
+
+ if (pages > pagesInt)
+ pagesInt++;
+
+ return pagesInt;
+ }
+
+ @Override
+ public void addPageChangeListener(PageChangeListener<T> pageChangeListener) {
+ this.pageChangeListener = pageChangeListener;
+ }
+
+ @Override
+ public List<T> getAllElements() {
+ return elements;
+ }
+
+ @Override
+ public boolean isPageChanged() {
+ return pageChanged;
+ }
+
+ @Override
+ public int from() {
+ return (pageNumber * pageSize) + 1;
+ }
+
+ @Override
+ public int to() {
+ int currentSize = getContent().size();
+ return currentSize < pageSize ? getNumberOfElements() : (pageNumber + 1) * currentSize;
+ }
+}
diff --git a/src/main/java/it/alessandroiezzi/commons/page/PageChangeListener.java b/src/main/java/it/alessandroiezzi/commons/page/PageChangeListener.java
new file mode 100644
index 0000000..a489ddc
--- /dev/null
+++ b/src/main/java/it/alessandroiezzi/commons/page/PageChangeListener.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2022 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it>
+ *
+ * This file is part of commons-page.
+ *
+ * commons-page is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * commons-page 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with commons-page. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package it.alessandroiezzi.commons.page;
+
+import java.util.*;
+
+public interface PageChangeListener<T> {
+ void pageChanged(List<T> elements, int pageSize);
+}
diff --git a/src/main/java/it/alessandroiezzi/commons/page/PageImpl.java b/src/main/java/it/alessandroiezzi/commons/page/PageImpl.java
new file mode 100644
index 0000000..1db709f
--- /dev/null
+++ b/src/main/java/it/alessandroiezzi/commons/page/PageImpl.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2022 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it>
+ *
+ * This file is part of commons-page.
+ *
+ * commons-page is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * commons-page 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with commons-page. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package it.alessandroiezzi.commons.page;
+
+import java.util.*;
+
+public class PageImpl<T> implements Page<T> {
+ private final List<T> content = new ArrayList<>();
+ private final int size;
+
+ public PageImpl(List<T> content, int pageSize) {
+ int i = pageSize;
+ for (T element : content) {
+ if (i-- == 0) break;
+ this.content.add(element);
+ }
+ this.size = pageSize;
+ }
+
+ @Override
+ public List<T> getContent() {
+ return content;
+ }
+
+ @Override
+ public int getSize() {
+ return size;
+ }
+
+ @Override
+ public int getNumberOfElements() {
+ return content.size();
+ }
+}
diff --git a/src/main/java/it/alessandroiezzi/commons/page/Pages.java b/src/main/java/it/alessandroiezzi/commons/page/Pages.java
new file mode 100644
index 0000000..2f6e98c
--- /dev/null
+++ b/src/main/java/it/alessandroiezzi/commons/page/Pages.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2022 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it>
+ *
+ * This file is part of commons-page.
+ *
+ * commons-page is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * commons-page 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with commons-page. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package it.alessandroiezzi.commons.page;
+
+import java.util.*;
+import java.util.function.*;
+
+public interface Pages<T> extends Page<T> {
+ Page<T> firstPage();
+ Page<T> lastPage();
+ Page<T> previousPage();
+ Page<T> nextPage();
+ Page<T> toPage(int page);
+ Page<T> getCurrentPage();
+ int getCurrentPageNumber();
+ int getTotalPages();
+ void addElement(T element);
+ void removeElement(T element);
+ void removeElementIf(Predicate<T> predicate);
+ void addPageChangeListener(PageChangeListener<T> pageChangeListener);
+ List<T> getAllElements();
+ boolean isPageChanged();
+ int from();
+ int to();
+}