From 46bed7c28a5df7add8f52730d87712bde13e29c5 Mon Sep 17 00:00:00 2001 From: Alessandro Iezzi Date: Wed, 30 Nov 2022 16:10:25 +0100 Subject: Add whole directory --- .../commons/page/ExampleInstrumentedTest.java | 45 ++++++ src/main/AndroidManifest.xml | 23 +++ .../java/it/alessandroiezzi/commons/page/Page.java | 28 ++++ .../alessandroiezzi/commons/page/PageAdapter.java | 170 +++++++++++++++++++++ .../commons/page/PageChangeListener.java | 26 ++++ .../it/alessandroiezzi/commons/page/PageImpl.java | 51 +++++++ .../it/alessandroiezzi/commons/page/Pages.java | 42 +++++ .../commons/page/ExampleUnitTest.java | 36 +++++ .../commons/page/PageAdapterUnitTest.java | 61 ++++++++ 9 files changed, 482 insertions(+) create mode 100644 src/androidTest/java/it/alessandroiezzi/commons/page/ExampleInstrumentedTest.java create mode 100644 src/main/AndroidManifest.xml create mode 100644 src/main/java/it/alessandroiezzi/commons/page/Page.java create mode 100644 src/main/java/it/alessandroiezzi/commons/page/PageAdapter.java create mode 100644 src/main/java/it/alessandroiezzi/commons/page/PageChangeListener.java create mode 100644 src/main/java/it/alessandroiezzi/commons/page/PageImpl.java create mode 100644 src/main/java/it/alessandroiezzi/commons/page/Pages.java create mode 100644 src/test/java/it/alessandroiezzi/commons/page/ExampleUnitTest.java create mode 100644 src/test/java/it/alessandroiezzi/commons/page/PageAdapterUnitTest.java (limited to 'src') 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 + * + * 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 . + */ + +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 Testing documentation + */ +@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 @@ + + + + + + \ 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 + * + * 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 . + */ + +package it.alessandroiezzi.commons.page; + +import java.util.*; + +public interface Page { + List 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 + * + * 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 . + */ + +package it.alessandroiezzi.commons.page; + +import java.util.*; +import java.util.function.*; + +public class PageAdapter implements Pages { + private final List elements; + private final int pageSize; + private int pageNumber; + private PageChangeListener pageChangeListener; + boolean pageChanged = false; + private Page currentPage; + + public PageAdapter(List elements) { + this(elements, 15); + } + + public PageAdapter(List elements, int pageSize) { + this.elements = elements; + this.pageSize = pageSize; + this.pageNumber = 0; + } + + @Override + public Page firstPage() { + return toPage(0); + } + + @Override + public Page previousPage() { + return toPage(pageNumber - 1); + } + + @Override + public Page nextPage() { + return toPage(pageNumber + 1); + } + + @Override + public Page lastPage() { + return toPage(getTotalPages() - 1); + } + + @Override + public Page 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 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 getCurrentPage() { + return currentPage; + } + + @Override + public int getCurrentPageNumber() { + return pageNumber; + } + + @Override + public List 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 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 pageChangeListener) { + this.pageChangeListener = pageChangeListener; + } + + @Override + public List 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 + * + * 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 . + */ + +package it.alessandroiezzi.commons.page; + +import java.util.*; + +public interface PageChangeListener { + void pageChanged(List 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 + * + * 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 . + */ + +package it.alessandroiezzi.commons.page; + +import java.util.*; + +public class PageImpl implements Page { + private final List content = new ArrayList<>(); + private final int size; + + public PageImpl(List content, int pageSize) { + int i = pageSize; + for (T element : content) { + if (i-- == 0) break; + this.content.add(element); + } + this.size = pageSize; + } + + @Override + public List 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 + * + * 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 . + */ + +package it.alessandroiezzi.commons.page; + +import java.util.*; +import java.util.function.*; + +public interface Pages extends Page { + Page firstPage(); + Page lastPage(); + Page previousPage(); + Page nextPage(); + Page toPage(int page); + Page getCurrentPage(); + int getCurrentPageNumber(); + int getTotalPages(); + void addElement(T element); + void removeElement(T element); + void removeElementIf(Predicate predicate); + void addPageChangeListener(PageChangeListener pageChangeListener); + List 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 + * + * 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 . + */ + +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 Testing documentation + */ +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 + * + * 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 . + */ + +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 integers = new ArrayList<>(); + for (int i = 1; i < max; i++) { + integers.add(i); + } + Pages 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 -- cgit v1.2.3