summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlessandro Iezzi <aiezzi@alessandroiezzi.it>2022-11-30 16:10:25 +0100
committerAlessandro Iezzi <aiezzi@alessandroiezzi.it>2022-11-30 16:10:25 +0100
commit46bed7c28a5df7add8f52730d87712bde13e29c5 (patch)
tree151383db4c3b4e23e08027c6ea5af1082511e1d0
parent3ceced8cb403d616444afc44717675f251d9c59e (diff)
downloadcommons-page-46bed7c28a5df7add8f52730d87712bde13e29c5.tar.gz
commons-page-46bed7c28a5df7add8f52730d87712bde13e29c5.zip
Add whole directory
-rw-r--r--src/androidTest/java/it/alessandroiezzi/commons/page/ExampleInstrumentedTest.java45
-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
-rw-r--r--src/test/java/it/alessandroiezzi/commons/page/ExampleUnitTest.java36
-rw-r--r--src/test/java/it/alessandroiezzi/commons/page/PageAdapterUnitTest.java61
9 files changed, 482 insertions, 0 deletions
diff --git a/src/androidTest/java/it/alessandroiezzi/commons/page/ExampleInstrumentedTest.java b/src/androidTest/java/it/alessandroiezzi/commons/page/ExampleInstrumentedTest.java
new file mode 100644
index 0000000..b88b024
--- /dev/null
+++ b/src/androidTest/java/it/alessandroiezzi/commons/page/ExampleInstrumentedTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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/>.
+ */
+
+package it.alessandroiezzi.commons.page;
+
+import android.content.Context;
+
+import androidx.test.platform.app.InstrumentationRegistry;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static org.junit.Assert.*;
+
+/**
+ * Instrumented test, which will execute on an Android device.
+ *
+ * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
+ */
+@RunWith(AndroidJUnit4.class)
+public class ExampleInstrumentedTest {
+ @Test
+ public void useAppContext() {
+ // Context of the app under test.
+ Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
+ assertEquals("it.alessandroiezzi.commons.page.test", appContext.getPackageName());
+ }
+} \ No newline at end of file
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();
+}
diff --git a/src/test/java/it/alessandroiezzi/commons/page/ExampleUnitTest.java b/src/test/java/it/alessandroiezzi/commons/page/ExampleUnitTest.java
new file mode 100644
index 0000000..ae6692d
--- /dev/null
+++ b/src/test/java/it/alessandroiezzi/commons/page/ExampleUnitTest.java
@@ -0,0 +1,36 @@
+/*
+ * 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 org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Example local unit test, which will execute on the development machine (host).
+ *
+ * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
+ */
+public class ExampleUnitTest {
+ @Test
+ public void addition_isCorrect() {
+ assertEquals(4, 2 + 2);
+ }
+} \ No newline at end of file
diff --git a/src/test/java/it/alessandroiezzi/commons/page/PageAdapterUnitTest.java b/src/test/java/it/alessandroiezzi/commons/page/PageAdapterUnitTest.java
new file mode 100644
index 0000000..65b880d
--- /dev/null
+++ b/src/test/java/it/alessandroiezzi/commons/page/PageAdapterUnitTest.java
@@ -0,0 +1,61 @@
+/*
+ * 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 org.junit.*;
+
+import java.util.*;
+
+import static org.junit.Assert.*;
+
+public class PageAdapterUnitTest {
+ @Test
+ public void test() {
+ int max = 48;
+ List<Integer> integers = new ArrayList<>();
+ for (int i = 1; i < max; i++) {
+ integers.add(i);
+ }
+ Pages<Integer> pages = new PageAdapter<>(integers);
+ assertEquals(4, pages.getTotalPages());
+
+ int i = 0;
+ for (Integer integer : pages.firstPage().getContent()) {
+ assertEquals(++i, (int) integer);
+ }
+ assertEquals(i, 15);
+
+ for (Integer integer : pages.lastPage().getContent()) {
+// System.out.println(integer);
+ }
+
+ for (int j = max; j < max + 13; j++) {
+ pages.addElement(j);
+ }
+ pages.removeElementIf((e) -> e > 0 && e < 58);
+ for (Integer integer : pages.lastPage().getContent()) {
+ System.out.println(integer);
+ }
+ System.out.println();
+ for (Integer integer : pages.firstPage().getContent()) {
+ System.out.println(integer);
+ }
+ }
+} \ No newline at end of file