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 --- 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 +++++ 6 files changed, 340 insertions(+) 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 (limited to 'src/main') 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(); +} -- cgit v1.2.3