diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..ef22662 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,28 @@ +--- +name: Отчет об ошибке +about: Создайте отчет об ошибке, чтобы улучшить приложение +title: '' +labels: bug +assignees: '' + +--- + +**Опишите ошибку** +Ясное и краткое описание ошибки. + +**Последовательность действий для воспроизведения** +Шаги по воспроизведению ошибки. + +**Ожидаемый результат** +Ясное и краткое описание того, что вы ожидали. + +**Скриншоты** +Если возможно, добавьте скриншоты, чтобы объяснить проблему. + +**Окружение:** + - ОС: [например windows 10] + - Версия Java [например 19] + - Версия приложения[например 2020.1] + +**Дополнительный контекст** +Любой другой контекст проблемы. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..be84300 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Запрос новой функциональности +about: Предложите идею для этого проекта +title: '' +labels: feature +assignees: '' + +--- + +**Связан ли ваш запрос новой функциональности с проблемой? Пожалуйста, опишите.** +Четкое и краткое описание проблемы. Например, я всегда расстраиваюсь, когда [...] + +**Опишите желаемое решение** +Четкое и краткое описание того, что вы хотите сделать. + +**Опишите альтернативы, которые вы рассмотрели** +Четкое и краткое описание любых рассмотренных вами альтернативных решений или функций. + +**Дополнительный контекст** +Любой другой контекст. diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml new file mode 100644 index 0000000..9c61d32 --- /dev/null +++ b/.github/workflows/unit-tests.yml @@ -0,0 +1,45 @@ +name: Unit Tests + +on: + workflow_dispatch: + pull_request: + branches: + - 'master' + - 'develop' + push: + branches: + - 'master' + - 'develop' + +jobs: + tests: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Set up JDK + uses: actions/setup-java@v3 + with: + java-version: '18' + distribution: 'liberica' + cache: maven + + - name: Maven Tests + run: mvn --batch-mode clean test + + - name: Test Coverage + uses: codecov/codecov-action@v3 + + - name: SonarCloud Analyze + run: > + mvn --batch-mode sonar:sonar + -Dsonar.projectKey=spacious-team_table-wrapper-api + -Dsonar.organization=spacious-team + -Dsonar.host.url=https://sonarcloud.io + -Dsonar.login=$SONAR_TOKEN + -Dsonar.coverage.jacoco.xmlReportPaths=./target/site/jacoco/jacoco.xml + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/.gitignore b/.gitignore index 70d1c01..e031059 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ target/ .mvn !.mvn/wrapper/maven-wrapper.properties -!.mvn/wrapper/MavenWrapperDownloader.java !**/src/main/** !**/src/test/** @@ -22,6 +21,7 @@ target/ !.idea/runConfigurations !.idea/codeStyles !.idea/copyright +!.idea/inspectionProfiles ### NetBeans ### /nbproject/private/ diff --git a/.idea/copyright/GNU_AGPLv3.xml b/.idea/copyright/GNU_AGPLv3.xml index d427976..1029333 100644 --- a/.idea/copyright/GNU_AGPLv3.xml +++ b/.idea/copyright/GNU_AGPLv3.xml @@ -1,7 +1,7 @@ \ No newline at end of file diff --git a/.idea/inspectionProfiles/Inspections.xml b/.idea/inspectionProfiles/Inspections.xml new file mode 100644 index 0000000..48b605b --- /dev/null +++ b/.idea/inspectionProfiles/Inspections.xml @@ -0,0 +1,26 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..07fe6c0 --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.mvn/wrapper/MavenWrapperDownloader.java b/.mvn/wrapper/MavenWrapperDownloader.java deleted file mode 100644 index b901097..0000000 --- a/.mvn/wrapper/MavenWrapperDownloader.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright 2007-present the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -import java.net.*; -import java.io.*; -import java.nio.channels.*; -import java.util.Properties; - -public class MavenWrapperDownloader { - - private static final String WRAPPER_VERSION = "0.5.6"; - /** - * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. - */ - private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" - + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; - - /** - * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to - * use instead of the default one. - */ - private static final String MAVEN_WRAPPER_PROPERTIES_PATH = - ".mvn/wrapper/maven-wrapper.properties"; - - /** - * Path where the maven-wrapper.jar will be saved to. - */ - private static final String MAVEN_WRAPPER_JAR_PATH = - ".mvn/wrapper/maven-wrapper.jar"; - - /** - * Name of the property which should be used to override the default download url for the wrapper. - */ - private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; - - public static void main(String args[]) { - System.out.println("- Downloader started"); - File baseDirectory = new File(args[0]); - System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); - - // If the maven-wrapper.properties exists, read it and check if it contains a custom - // wrapperUrl parameter. - File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); - String url = DEFAULT_DOWNLOAD_URL; - if(mavenWrapperPropertyFile.exists()) { - FileInputStream mavenWrapperPropertyFileInputStream = null; - try { - mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); - Properties mavenWrapperProperties = new Properties(); - mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); - url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); - } catch (IOException e) { - System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); - } finally { - try { - if(mavenWrapperPropertyFileInputStream != null) { - mavenWrapperPropertyFileInputStream.close(); - } - } catch (IOException e) { - // Ignore ... - } - } - } - System.out.println("- Downloading from: " + url); - - File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); - if(!outputFile.getParentFile().exists()) { - if(!outputFile.getParentFile().mkdirs()) { - System.out.println( - "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); - } - } - System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); - try { - downloadFileFromURL(url, outputFile); - System.out.println("Done"); - System.exit(0); - } catch (Throwable e) { - System.out.println("- Error downloading"); - e.printStackTrace(); - System.exit(1); - } - } - - private static void downloadFileFromURL(String urlString, File destination) throws Exception { - if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { - String username = System.getenv("MVNW_USERNAME"); - char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); - Authenticator.setDefault(new Authenticator() { - @Override - protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication(username, password); - } - }); - } - URL website = new URL(urlString); - ReadableByteChannel rbc; - rbc = Channels.newChannel(website.openStream()); - FileOutputStream fos = new FileOutputStream(destination); - fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); - fos.close(); - rbc.close(); - } - -} diff --git a/.mvn/wrapper/maven-wrapper.properties b/.mvn/wrapper/maven-wrapper.properties index 642d572..f3283b0 100644 --- a/.mvn/wrapper/maven-wrapper.properties +++ b/.mvn/wrapper/maven-wrapper.properties @@ -1,2 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip -wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar +wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar diff --git a/README.md b/README.md index 2983454..da93eaa 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,10 @@ ![java-version](https://img.shields.io/badge/Java-11-brightgreen?style=flat-square) -![jitpack-last-release](https://jitpack.io/v/spacious-team/table-wrapper-api.svg?style=flat-square) +[![jitpack-last-release](https://jitpack.io/v/spacious-team/table-wrapper-api.svg?style=flat-square)]( +https://jitpack.io/#spacious-team/table-wrapper-api) +[![Unit tests](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fspacious-team%2Ftable-wrapper-api%2Fbadge%3Fref%3Ddevelop&style=flat-square&label=Test&logo=none)]( +https://github.com/spacious-team/table-wrapper-api/actions/workflows/unit-tests.yml) +[![Coverage](https://img.shields.io/codecov/c/github/spacious-team/table-wrapper-api/develop?label=Coverage&style=flat-square&token=SIOIDt0TcY)]( +https://codecov.io/gh/spacious-team/table-wrapper-api) #### Оглавление - [Назначение](#назначение) @@ -7,40 +12,41 @@ - [Зависимости](#зависимости) ### Назначение -Предоставляет удобный API для доступа к табличным данным из файлов в форматах excel, xml и др. -Пусть на листе excel имеется несколько таблиц. +Предоставляет единый удобный API для доступа к табличным данным из файлов в форматах excel, xml, csv и др. + +Разберем доступ к данным на примере. Пусть на листе excel (xml или csv) имеется несколько таблиц. - Таблица с ценой товаров: *Таблица товаров* -Товар | Цена (опт), руб/кг | Цена розничная, руб/кг -------|--------------------|---------------- -Яблоко| 50 | 90.5 -Груша | 120 | 180.0 +| Товар | Цена (опт), руб/кг | Цена розничная, руб/кг | +|--------|--------------------|------------------------| +| Яблоко | 50 | 90.5 | +| Груша | 120 | 180.0 | - Таблица с заголовком из 2-х строк: *Таблица продаж* -Покупатель | | Категория | Объем, ------------|----------------|-------------|-------- -Страна | Компания | покупателя | кг -Россия | "Шестерочка" | опт | 100000 -Беларусь | "Фруктелла" | опт | 50000 -Итого | | | 150000 +| Покупатель | | Категория | Объем, | +|------------|--------------|------------|--------| +| Страна | Компания | покупателя | кг | +| Россия | "Шестерочка" | опт | 100000 | +| Беларусь | "Фруктелла" | опт | 50000 | +| Итого | | | 150000 | - Пусть также иногда встречается следующий вариант заголовка предыдущей таблицы (причем заранее не известно какой вариант встретится в файле): -Покупатель | | Категория | Вес, ------------|----------------|--------------|-------- -Страна | Компания | покупателя | кг +| Покупатель | | Категория | Вес, | +|------------|----------|------------|------| +| Страна | Компания | покупателя | кг | ### Пример использования -Для представленного выше примера объявляются описания столбцов: +Для представленного выше примера объявляются описания столбцов вне зависимости от формата файла (excel, xml, csv и др.): ```java -enum ProductTableHeader implements TableColumnDescription { +enum ProductTableHeader implements TableHeaderColumn { PRODUCT(0), PRICE_TRADE("цена", "опт"), PRICE("цена", "розничная"); @@ -52,7 +58,7 @@ enum ProductTableHeader implements TableColumnDescription { } ProductTableHeader(String... words) { - this.column = TableColumnImpl.of(words); + this.column = PatternTableColumn.of(words); } public TableColumn getColumn() { @@ -60,7 +66,7 @@ enum ProductTableHeader implements TableColumnDescription { } } -enum CellTableHeader implements TableColumnDescription { +enum SalesTableHeader implements TableHeaderColumn { BUYER_COUNTRY(MultiLineTableColumn.of("покупатель", "страна")), BUYER_COMPANY(MultiLineTableColumn.of("покупатель", "компания")), TYPE(MultiLineTableColumn.of("категория", "покупателя")), @@ -79,28 +85,36 @@ enum CellTableHeader implements TableColumnDescription { } } ``` -В зависимости от формата исходных данных подготавливаются объекты. Например для excel файла потребуются +В зависимости от формата исходных данных подготавливаются объекты `ReportPage`. Например, для excel файла потребуются +зависимость [table-wrapper-excel-impl](https://github.com/spacious-team/table-wrapper-excel-impl) или Spring Boot Starter +и код: ```java -// table wrapper excel impl dependency required -Workbook book = new XSSFWorkbook(xlsFileinputStream); // open excel file -ReportPage reportPage = new ExcelSheet(book.getSheetAt(0)); // select first excel sheet +TableFactoryRegistry.add(new ExcelTableFactory()); // регистрируем фабрику +Workbook book = new XSSFWorkbook(xlsFileinputStream); // открываем Excel файл +ReportPage reportPage = new ExcelSheet(book.getSheetAt(0)); // используем 1-ый лист Excel файла для поиска таблиц ``` Используем API для доступа к данным таблиц ```java -// finding row with "таблица товаров" content, parsing next row as header and -// counting next rows as table till empty line +// Регистронезависимо найдет ячейку с текстом "Таблица товаров", +// парсит следующую за ней строку как заголовок таблицы, +// оставшиеся строки парсятся как данные до пустой строки или конца файла Table productTable = reportPage.create("таблица товаров", ProductTableHeader.class); -// finding row with "таблица продаж" content, parsing next 2 rows as header and -// counting next rows as table till row containing "итого" in any cell -Table cellTable = reportPage.create("таблица продаж", "итого", CellTableHeader.class, 2); + +// Регистронезависимо найдет ячейку с текстом "Таблица продаж", +// парсит следующие за ней 2 строки заголовка таблицы, +// оставшиеся строки парсятся как данные таблицы до строки, содержащей ячейку с текстом "Итого" +Table salesTable = reportPage.create("таблица продаж", "итого", SalesTableHeader.class, 2); for (TableRow row : productTable) { - String product = row.getStringCellValueOrDefault(PRICE_TRADE, "Неизвестный товар"); + // Извлечет наименования товаров "Яблоко", "Груша" из "Таблицы товаров" + String product = row.getStringCellValueOrDefault(PRODUCT, "Неизвестный товар"); + // Извлечет оптовые цены 50 и 120 из "Таблицы товаров" BigDecimal price = row.getBigDecimalCellValue(PRICE_TRADE); } -Set countries = cellTable.stream() - .map(row -> row.getStringCellValueOrDefault(BUYER_COUNTRY, "unknown")) +// Список будет содержать ["Россия", "Беларусь"] из "Таблицы продаж" +Set countries = salesTable.stream() + .map(row -> row.getStringCellValue(BUYER_COUNTRY)) .collect(toSet()) ``` API предоставляет и другие удобные интерфейсы для работы с таблицами. @@ -124,7 +138,7 @@ API предоставляет и другие удобные интерфейс ``` -и добавить зависимость `table-wrapper-api` +Далее следует добавить зависимость `table-wrapper-api` ```xml com.github.spacious-team @@ -135,10 +149,10 @@ API предоставляет и другие удобные интерфейс В качестве версии можно использовать: - версию [релиза](https://github.com/spacious-team/table-wrapper-api/releases) на github; - паттерн `-SNAPSHOT` для сборки зависимости с последнего коммита выбранной ветки; -- короткий 10-ти значный номер коммита для сборки зависимости с указанного коммита. +- короткий десяти значный номер коммита для сборки зависимости с указанного коммита. -Вам также потребуется реализация парсера, например -[table-wrapper-excel-impl](https://github.com/spacious-team/table-wrapper-excel-impl) для работы с excel файлами +Для извлечения данных Вам также потребуется одна или несколько реализаций: +1. [table-wrapper-excel-impl](https://github.com/spacious-team/table-wrapper-excel-impl) для работы с excel файлами ```xml com.github.spacious-team @@ -146,3 +160,27 @@ API предоставляет и другие удобные интерфейс master-SNAPSHOT ``` +2. [table-wrapper-xml-impl](https://github.com/spacious-team/table-wrapper-xml-impl) для работы с xml файлами +```xml + + com.github.spacious-team + table-wrapper-xml-impl + master-SNAPSHOT + +``` +3. [table-wrapper-csv-impl](https://github.com/spacious-team/table-wrapper-csv-impl) для работы с csv (tsv) файлами +```xml + + com.github.spacious-team + table-wrapper-csv-impl + master-SNAPSHOT + +``` +Или напишите реализацию для своего формата представления таблицы по +[аналогии](https://github.com/spacious-team/table-wrapper-csv-impl/tree/develop/src/main/java/org/spacious_team/table_wrapper/csv) +с существующими. + +Существует также [Spring Boot Starter](https://github.com/spacious-team/table-wrapper-spring-boot-starter) +для работы со Spring Boot проектами, который поставляет нужные зависимости и создает бины фабрик +`ExcelTableFactory`, `XmlTableFactory`, `CsvTableFactory`. + diff --git a/mvnw b/mvnw index 41c0f0c..8d937f4 100644 --- a/mvnw +++ b/mvnw @@ -19,7 +19,7 @@ # ---------------------------------------------------------------------------- # ---------------------------------------------------------------------------- -# Maven Start Up Batch script +# Apache Maven Wrapper startup batch script, version 3.2.0 # # Required ENV vars: # ------------------ @@ -27,7 +27,6 @@ # # Optional ENV vars # ----------------- -# M2_HOME - location of maven2's installed home dir # MAVEN_OPTS - parameters passed to the Java VM when running Maven # e.g. to debug Maven itself, use # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 @@ -36,6 +35,10 @@ if [ -z "$MAVEN_SKIP_RC" ] ; then + if [ -f /usr/local/etc/mavenrc ] ; then + . /usr/local/etc/mavenrc + fi + if [ -f /etc/mavenrc ] ; then . /etc/mavenrc fi @@ -50,7 +53,7 @@ fi cygwin=false; darwin=false; mingw=false -case "`uname`" in +case "$(uname)" in CYGWIN*) cygwin=true ;; MINGW*) mingw=true;; Darwin*) darwin=true @@ -58,9 +61,9 @@ case "`uname`" in # See https://developer.apple.com/library/mac/qa/qa1170/_index.html if [ -z "$JAVA_HOME" ]; then if [ -x "/usr/libexec/java_home" ]; then - export JAVA_HOME="`/usr/libexec/java_home`" + JAVA_HOME="$(/usr/libexec/java_home)"; export JAVA_HOME else - export JAVA_HOME="/Library/Java/Home" + JAVA_HOME="/Library/Java/Home"; export JAVA_HOME fi fi ;; @@ -68,68 +71,38 @@ esac if [ -z "$JAVA_HOME" ] ; then if [ -r /etc/gentoo-release ] ; then - JAVA_HOME=`java-config --jre-home` + JAVA_HOME=$(java-config --jre-home) fi fi -if [ -z "$M2_HOME" ] ; then - ## resolve links - $0 may be a link to maven's home - PRG="$0" - - # need this for relative symlinks - while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG="`dirname "$PRG"`/$link" - fi - done - - saveddir=`pwd` - - M2_HOME=`dirname "$PRG"`/.. - - # make it fully qualified - M2_HOME=`cd "$M2_HOME" && pwd` - - cd "$saveddir" - # echo Using m2 at $M2_HOME -fi - # For Cygwin, ensure paths are in UNIX format before anything is touched if $cygwin ; then - [ -n "$M2_HOME" ] && - M2_HOME=`cygpath --unix "$M2_HOME"` [ -n "$JAVA_HOME" ] && - JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + JAVA_HOME=$(cygpath --unix "$JAVA_HOME") [ -n "$CLASSPATH" ] && - CLASSPATH=`cygpath --path --unix "$CLASSPATH"` + CLASSPATH=$(cygpath --path --unix "$CLASSPATH") fi # For Mingw, ensure paths are in UNIX format before anything is touched if $mingw ; then - [ -n "$M2_HOME" ] && - M2_HOME="`(cd "$M2_HOME"; pwd)`" - [ -n "$JAVA_HOME" ] && - JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && [ -d "$JAVA_HOME" ] && + JAVA_HOME="$(cd "$JAVA_HOME" || (echo "cannot cd into $JAVA_HOME."; exit 1); pwd)" fi if [ -z "$JAVA_HOME" ]; then - javaExecutable="`which javac`" - if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + javaExecutable="$(which javac)" + if [ -n "$javaExecutable" ] && ! [ "$(expr "\"$javaExecutable\"" : '\([^ ]*\)')" = "no" ]; then # readlink(1) is not available as standard on Solaris 10. - readLink=`which readlink` - if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + readLink=$(which readlink) + if [ ! "$(expr "$readLink" : '\([^ ]*\)')" = "no" ]; then if $darwin ; then - javaHome="`dirname \"$javaExecutable\"`" - javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + javaHome="$(dirname "\"$javaExecutable\"")" + javaExecutable="$(cd "\"$javaHome\"" && pwd -P)/javac" else - javaExecutable="`readlink -f \"$javaExecutable\"`" + javaExecutable="$(readlink -f "\"$javaExecutable\"")" fi - javaHome="`dirname \"$javaExecutable\"`" - javaHome=`expr "$javaHome" : '\(.*\)/bin'` + javaHome="$(dirname "\"$javaExecutable\"")" + javaHome=$(expr "$javaHome" : '\(.*\)/bin') JAVA_HOME="$javaHome" export JAVA_HOME fi @@ -145,7 +118,7 @@ if [ -z "$JAVACMD" ] ; then JAVACMD="$JAVA_HOME/bin/java" fi else - JAVACMD="`which java`" + JAVACMD="$(\unset -f command 2>/dev/null; \command -v java)" fi fi @@ -159,12 +132,9 @@ if [ -z "$JAVA_HOME" ] ; then echo "Warning: JAVA_HOME environment variable is not set." fi -CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher - # traverses directory structure from process work directory to filesystem root # first directory with .mvn subdirectory is considered project base directory find_maven_basedir() { - if [ -z "$1" ] then echo "Path not specified to find_maven_basedir" @@ -180,96 +150,99 @@ find_maven_basedir() { fi # workaround for JBEAP-8937 (on Solaris 10/Sparc) if [ -d "${wdir}" ]; then - wdir=`cd "$wdir/.."; pwd` + wdir=$(cd "$wdir/.." || exit 1; pwd) fi # end of workaround done - echo "${basedir}" + printf '%s' "$(cd "$basedir" || exit 1; pwd)" } # concatenates all lines of a file concat_lines() { if [ -f "$1" ]; then - echo "$(tr -s '\n' ' ' < "$1")" + # Remove \r in case we run on Windows within Git Bash + # and check out the repository with auto CRLF management + # enabled. Otherwise, we may read lines that are delimited with + # \r\n and produce $'-Xarg\r' rather than -Xarg due to word + # splitting rules. + tr -s '\r\n' ' ' < "$1" + fi +} + +log() { + if [ "$MVNW_VERBOSE" = true ]; then + printf '%s\n' "$1" fi } -BASE_DIR=`find_maven_basedir "$(pwd)"` +BASE_DIR=$(find_maven_basedir "$(dirname "$0")") if [ -z "$BASE_DIR" ]; then exit 1; fi +MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}; export MAVEN_PROJECTBASEDIR +log "$MAVEN_PROJECTBASEDIR" + ########################################################################################## # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central # This allows using the maven wrapper in projects that prohibit checking in binary data. ########################################################################################## -if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then - if [ "$MVNW_VERBOSE" = true ]; then - echo "Found .mvn/wrapper/maven-wrapper.jar" - fi +wrapperJarPath="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" +if [ -r "$wrapperJarPath" ]; then + log "Found $wrapperJarPath" else - if [ "$MVNW_VERBOSE" = true ]; then - echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." - fi + log "Couldn't find $wrapperJarPath, downloading it ..." + if [ -n "$MVNW_REPOURL" ]; then - jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + wrapperUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar" else - jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + wrapperUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar" fi - while IFS="=" read key value; do - case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + while IFS="=" read -r key value; do + # Remove '\r' from value to allow usage on windows as IFS does not consider '\r' as a separator ( considers space, tab, new line ('\n'), and custom '=' ) + safeValue=$(echo "$value" | tr -d '\r') + case "$key" in (wrapperUrl) wrapperUrl="$safeValue"; break ;; esac - done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" - if [ "$MVNW_VERBOSE" = true ]; then - echo "Downloading from: $jarUrl" - fi - wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + done < "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.properties" + log "Downloading from: $wrapperUrl" + if $cygwin; then - wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + wrapperJarPath=$(cygpath --path --windows "$wrapperJarPath") fi if command -v wget > /dev/null; then - if [ "$MVNW_VERBOSE" = true ]; then - echo "Found wget ... using wget" - fi + log "Found wget ... using wget" + [ "$MVNW_VERBOSE" = true ] && QUIET="" || QUIET="--quiet" if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then - wget "$jarUrl" -O "$wrapperJarPath" + wget $QUIET "$wrapperUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" else - wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + wget $QUIET --http-user="$MVNW_USERNAME" --http-password="$MVNW_PASSWORD" "$wrapperUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" fi elif command -v curl > /dev/null; then - if [ "$MVNW_VERBOSE" = true ]; then - echo "Found curl ... using curl" - fi + log "Found curl ... using curl" + [ "$MVNW_VERBOSE" = true ] && QUIET="" || QUIET="--silent" if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then - curl -o "$wrapperJarPath" "$jarUrl" -f + curl $QUIET -o "$wrapperJarPath" "$wrapperUrl" -f -L || rm -f "$wrapperJarPath" else - curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + curl $QUIET --user "$MVNW_USERNAME:$MVNW_PASSWORD" -o "$wrapperJarPath" "$wrapperUrl" -f -L || rm -f "$wrapperJarPath" fi - else - if [ "$MVNW_VERBOSE" = true ]; then - echo "Falling back to using Java to download" - fi - javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + log "Falling back to using Java to download" + javaSource="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperDownloader.java" + javaClass="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperDownloader.class" # For Cygwin, switch paths to Windows format before running javac if $cygwin; then - javaClass=`cygpath --path --windows "$javaClass"` + javaSource=$(cygpath --path --windows "$javaSource") + javaClass=$(cygpath --path --windows "$javaClass") fi - if [ -e "$javaClass" ]; then - if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then - if [ "$MVNW_VERBOSE" = true ]; then - echo " - Compiling MavenWrapperDownloader.java ..." - fi - # Compiling the Java class - ("$JAVA_HOME/bin/javac" "$javaClass") + if [ -e "$javaSource" ]; then + if [ ! -e "$javaClass" ]; then + log " - Compiling MavenWrapperDownloader.java ..." + ("$JAVA_HOME/bin/javac" "$javaSource") fi - if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then - # Running the downloader - if [ "$MVNW_VERBOSE" = true ]; then - echo " - Running MavenWrapperDownloader.java ..." - fi - ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + if [ -e "$javaClass" ]; then + log " - Running MavenWrapperDownloader.java ..." + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$wrapperUrl" "$wrapperJarPath") || rm -f "$wrapperJarPath" fi fi fi @@ -278,33 +251,58 @@ fi # End of extension ########################################################################################## -export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} -if [ "$MVNW_VERBOSE" = true ]; then - echo $MAVEN_PROJECTBASEDIR +# If specified, validate the SHA-256 sum of the Maven wrapper jar file +wrapperSha256Sum="" +while IFS="=" read -r key value; do + case "$key" in (wrapperSha256Sum) wrapperSha256Sum=$value; break ;; + esac +done < "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.properties" +if [ -n "$wrapperSha256Sum" ]; then + wrapperSha256Result=false + if command -v sha256sum > /dev/null; then + if echo "$wrapperSha256Sum $wrapperJarPath" | sha256sum -c > /dev/null 2>&1; then + wrapperSha256Result=true + fi + elif command -v shasum > /dev/null; then + if echo "$wrapperSha256Sum $wrapperJarPath" | shasum -a 256 -c > /dev/null 2>&1; then + wrapperSha256Result=true + fi + else + echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." + echo "Please install either command, or disable validation by removing 'wrapperSha256Sum' from your maven-wrapper.properties." + exit 1 + fi + if [ $wrapperSha256Result = false ]; then + echo "Error: Failed to validate Maven wrapper SHA-256, your Maven wrapper might be compromised." >&2 + echo "Investigate or delete $wrapperJarPath to attempt a clean download." >&2 + echo "If you updated your Maven version, you need to update the specified wrapperSha256Sum property." >&2 + exit 1 + fi fi + MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" # For Cygwin, switch paths to Windows format before running java if $cygwin; then - [ -n "$M2_HOME" ] && - M2_HOME=`cygpath --path --windows "$M2_HOME"` [ -n "$JAVA_HOME" ] && - JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + JAVA_HOME=$(cygpath --path --windows "$JAVA_HOME") [ -n "$CLASSPATH" ] && - CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + CLASSPATH=$(cygpath --path --windows "$CLASSPATH") [ -n "$MAVEN_PROJECTBASEDIR" ] && - MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` + MAVEN_PROJECTBASEDIR=$(cygpath --path --windows "$MAVEN_PROJECTBASEDIR") fi # Provide a "standardized" way to retrieve the CLI args that will # work with both Windows and non-Windows executions. -MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $*" export MAVEN_CMD_LINE_ARGS WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain +# shellcheck disable=SC2086 # safe args exec "$JAVACMD" \ $MAVEN_OPTS \ + $MAVEN_DEBUG_OPTS \ -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ - "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/mvnw.cmd b/mvnw.cmd index 8611571..c4586b5 100644 --- a/mvnw.cmd +++ b/mvnw.cmd @@ -18,13 +18,12 @@ @REM ---------------------------------------------------------------------------- @REM ---------------------------------------------------------------------------- -@REM Maven Start Up Batch script +@REM Apache Maven Wrapper startup batch script, version 3.2.0 @REM @REM Required ENV vars: @REM JAVA_HOME - location of a JDK home dir @REM @REM Optional ENV vars -@REM M2_HOME - location of maven2's installed home dir @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven @@ -46,8 +45,8 @@ if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") @REM Execute a user defined script before this one if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre @REM check for pre script, once with legacy .bat ending and once with .cmd ending -if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" -if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %* +if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %* :skipRcPre @setlocal @@ -120,10 +119,10 @@ SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain -set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" +set WRAPPER_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar" -FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( - IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET WRAPPER_URL=%%B ) @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central @@ -134,11 +133,11 @@ if exist %WRAPPER_JAR% ( ) ) else ( if not "%MVNW_REPOURL%" == "" ( - SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + SET WRAPPER_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar" ) if "%MVNW_VERBOSE%" == "true" ( echo Couldn't find %WRAPPER_JAR%, downloading it ... - echo Downloading from: %DOWNLOAD_URL% + echo Downloading from: %WRAPPER_URL% ) powershell -Command "&{"^ @@ -146,7 +145,7 @@ if exist %WRAPPER_JAR% ( "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ "}"^ - "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%WRAPPER_URL%', '%WRAPPER_JAR%')"^ "}" if "%MVNW_VERBOSE%" == "true" ( echo Finished downloading %WRAPPER_JAR% @@ -154,11 +153,35 @@ if exist %WRAPPER_JAR% ( ) @REM End of extension +@REM If specified, validate the SHA-256 sum of the Maven wrapper jar file +SET WRAPPER_SHA_256_SUM="" +FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperSha256Sum" SET WRAPPER_SHA_256_SUM=%%B +) +IF NOT %WRAPPER_SHA_256_SUM%=="" ( + powershell -Command "&{"^ + "$hash = (Get-FileHash \"%WRAPPER_JAR%\" -Algorithm SHA256).Hash.ToLower();"^ + "If('%WRAPPER_SHA_256_SUM%' -ne $hash){"^ + " Write-Output 'Error: Failed to validate Maven wrapper SHA-256, your Maven wrapper might be compromised.';"^ + " Write-Output 'Investigate or delete %WRAPPER_JAR% to attempt a clean download.';"^ + " Write-Output 'If you updated your Maven version, you need to update the specified wrapperSha256Sum property.';"^ + " exit 1;"^ + "}"^ + "}" + if ERRORLEVEL 1 goto error +) + @REM Provide a "standardized" way to retrieve the CLI args that will @REM work with both Windows and non-Windows executions. set MAVEN_CMD_LINE_ARGS=%* -%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +%MAVEN_JAVA_EXE% ^ + %JVM_CONFIG_MAVEN_PROPS% ^ + %MAVEN_OPTS% ^ + %MAVEN_DEBUG_OPTS% ^ + -classpath %WRAPPER_JAR% ^ + "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^ + %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* if ERRORLEVEL 1 goto error goto end @@ -168,15 +191,15 @@ set ERROR_CODE=1 :end @endlocal & set ERROR_CODE=%ERROR_CODE% -if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost @REM check for post script, once with legacy .bat ending and once with .cmd ending -if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" -if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat" +if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd" :skipRcPost @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' -if "%MAVEN_BATCH_PAUSE%" == "on" pause +if "%MAVEN_BATCH_PAUSE%"=="on" pause -if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% +if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE% -exit /B %ERROR_CODE% +cmd /C exit /B %ERROR_CODE% diff --git a/pom.xml b/pom.xml index 5464af8..5cd605b 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,7 @@ + + + org.junit.jupiter + junit-jupiter + 5.9.2 + test + + + org.mockito + mockito-junit-jupiter + 5.2.0 + test + + + nl.jqno.equalsverifier + equalsverifier + 3.14.1 + test + + + + + maven-surefire-plugin + 2.22.2 + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.10.1 + + true + true + true + + + org.projectlombok + lombok + ${lombok.version} + + + org.checkerframework + checker + ${checkerframework.version} + + + + + lombok.launch.AnnotationProcessorHider$AnnotationProcessor + + + org.checkerframework.checker.nullness.NullnessChecker + + + + + -AskipDefs=.*Test + -J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED + -J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED + + + + + org.jacoco + jacoco-maven-plugin + 0.8.8 + + + prepare-agent + + prepare-agent + + + + report + test + + report + + + + org.apache.maven.plugins maven-source-plugin diff --git a/src/main/java/org/spacious_team/table_wrapper/api/AbstractReportPage.java b/src/main/java/org/spacious_team/table_wrapper/api/AbstractReportPage.java index 81fbe09..e881178 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/AbstractReportPage.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/AbstractReportPage.java @@ -1,6 +1,6 @@ /* * Table Wrapper API - * Copyright (C) 2021 Vitalii Ananev + * Copyright (C) 2021 Spacious Team * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -18,12 +18,14 @@ package org.spacious_team.table_wrapper.api; +import org.checkerframework.checker.nullness.qual.Nullable; + /** * {@link ReportPage} with specified {@link ReportPageRow} */ public abstract class AbstractReportPage implements ReportPage { @Override - public abstract T getRow(int i); + public abstract @Nullable T getRow(int i); } diff --git a/src/main/java/org/spacious_team/table_wrapper/api/AbstractReportPageRow.java b/src/main/java/org/spacious_team/table_wrapper/api/AbstractReportPageRow.java index 48ac42c..e432a4b 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/AbstractReportPageRow.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/AbstractReportPageRow.java @@ -1,6 +1,6 @@ /* * Table Wrapper API - * Copyright (C) 2020 Vitalii Ananev + * Copyright (C) 2020 Spacious Team * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -19,6 +19,7 @@ package org.spacious_team.table_wrapper.api; import lombok.RequiredArgsConstructor; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.Iterator; import java.util.function.Function; @@ -26,10 +27,10 @@ public abstract class AbstractReportPageRow implements ReportPageRow { @RequiredArgsConstructor - protected static class ReportPageRowIterator implements Iterator { + protected static class ReportPageRowIterator implements Iterator<@Nullable TableCell> { - private final Iterator innerIterator; - private final Function converter; + private final Iterator<@Nullable T> innerIterator; + private final Function<@Nullable T, @Nullable TableCell> converter; @Override @@ -38,7 +39,7 @@ public boolean hasNext() { } @Override - public TableCell next() { + public @Nullable TableCell next() { return converter.apply(innerIterator.next()); } } diff --git a/src/main/java/org/spacious_team/table_wrapper/api/AbstractTable.java b/src/main/java/org/spacious_team/table_wrapper/api/AbstractTable.java index e6ed686..1c09414 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/AbstractTable.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/AbstractTable.java @@ -1,6 +1,6 @@ /* * Table Wrapper API - * Copyright (C) 2020 Vitalii Ananev + * Copyright (C) 2020 Spacious Team * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -18,9 +18,11 @@ package org.spacious_team.table_wrapper.api; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.ToString; import lombok.extern.slf4j.Slf4j; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.ArrayList; import java.util.Arrays; @@ -30,6 +32,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.NoSuchElementException; import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.BiPredicate; @@ -38,10 +41,15 @@ import java.util.stream.Stream; import java.util.stream.StreamSupport; +import static java.util.Collections.unmodifiableMap; +import static java.util.Objects.requireNonNull; + @Slf4j +@EqualsAndHashCode @ToString(of = {"tableName"}) -public abstract class AbstractTable implements Table { +public abstract class AbstractTable implements Table { + @Getter protected final AbstractReportPage reportPage; protected final String tableName; @Getter @@ -58,11 +66,13 @@ public abstract class AbstractTable implements Table { /** * @param tableRange only first and last row numbers matters */ - protected AbstractTable(AbstractReportPage reportPage, - String tableName, - TableCellRange tableRange, - Class headerDescription, - int headersRowCount) { + @SuppressWarnings("unused") + protected & TableHeaderColumn> + AbstractTable(AbstractReportPage reportPage, + String tableName, + TableCellRange tableRange, + Class headerDescription, + int headersRowCount) { this.reportPage = reportPage; this.tableName = tableName; this.dataRowOffset = 1 + headersRowCount; // table_name + headersRowCount @@ -72,14 +82,15 @@ protected AbstractTable(AbstractReportPage reportPage, getHeaderDescription(reportPage, tableRange, headerDescription, headersRowCount); this.tableRange = empty ? tableRange : - new TableCellRange( + TableCellRange.of( tableRange.getFirstRow(), tableRange.getLastRow(), getColumnIndices(this.headerDescription).min().orElse(tableRange.getFirstColumn()), getColumnIndices(this.headerDescription).max().orElse(tableRange.getLastColumn())); } - protected AbstractTable(AbstractTable table, int appendDataRowsToTop, int appendDataRowsToBottom) { + @SuppressWarnings("unused") + protected AbstractTable(AbstractTable table, int appendDataRowsToTop, int appendDataRowsToBottom) { this.reportPage = table.reportPage; this.tableName = table.tableName; this.tableRange = table.tableRange.addRowsToTop(appendDataRowsToTop).addRowsToBottom(appendDataRowsToBottom); @@ -89,61 +100,69 @@ protected AbstractTable(AbstractTable table, int appendDataRowsToTop, int app } private static boolean isEmpty(TableCellRange tableRange, int dataRowOffset) { - return tableRange.equals(TableCellRange.EMPTY_RANGE) || - (getNumberOfTableRows(tableRange) - dataRowOffset) <= 0; + return getNumberOfTableRows(tableRange) <= dataRowOffset; } private static int getNumberOfTableRows(TableCellRange tableRange) { return tableRange.getLastRow() - tableRange.getFirstRow() + 1; } - private static Map getHeaderDescription(AbstractReportPage reportPage, TableCellRange tableRange, - Class headerDescription, - int headersRowCount) { + private static & TableHeaderColumn> + Map getHeaderDescription(AbstractReportPage reportPage, TableCellRange tableRange, + Class headerDescription, + int headersRowCount) { Map columnIndices = new HashMap<>(); ReportPageRow[] headerRows = new ReportPageRow[headersRowCount]; for (int i = 0; i < headersRowCount; i++) { - headerRows[i] = reportPage.getRow(tableRange.getFirstRow() + 1 + i); + @Nullable ReportPageRow row = reportPage.getRow(tableRange.getFirstRow() + 1 + i); + @SuppressWarnings("nullness") + ReportPageRow notNullRow = requireNonNull(row, "Header row is absent"); + headerRows[i] = notNullRow; } + @SuppressWarnings("nullness") TableColumn[] columns = Arrays.stream(headerDescription.getEnumConstants()) - .map(TableColumnDescription::getColumn) + .map(TableHeaderColumn::getColumn) .toArray(TableColumn[]::new); for (TableColumn column : columns) { - columnIndices.put(column, column.getColumnIndex(headerRows)); + try { + int columnIndex = column.getColumnIndex(headerRows); + columnIndices.put(column, columnIndex); + } catch (OptionalTableColumnNotFound e) { + log.debug("Optional header column is not found: {}", column, e); + } } - return Collections.unmodifiableMap(columnIndices); + return unmodifiableMap(columnIndices); } private static IntStream getColumnIndices(Map headerDescription) { return headerDescription.values() .stream() - .mapToInt(i -> i) - .filter(i -> i != TableColumn.NOCOLUMN_INDEX); + .mapToInt(i -> i); } - public List getData(Object report, Function rowExtractor) { + public List getData(Object report, Function rowExtractor) { return getDataCollection(report, (row, data) -> { - T result = rowExtractor.apply(row); + @Nullable T result = rowExtractor.apply(row); if (result != null) { data.add(result); } }); } - public List getDataCollection(Object report, Function> rowExtractor) { + public List getDataCollection(Object report, Function> rowExtractor) { return getDataCollection(report, (row, data) -> { - Collection result = rowExtractor.apply(row); + @Nullable Collection result = rowExtractor.apply(row); if (result != null) { data.addAll(result); } }); } - public List getDataCollection(Object report, Function> rowExtractor, + public List getDataCollection(Object report, Function> rowExtractor, BiPredicate equalityChecker, - BiFunction> mergeDuplicates) { + BiFunction> mergeDuplicates) { return getDataCollection(report, (row, data) -> { - Collection result = rowExtractor.apply(row); + @Nullable Collection result = rowExtractor.apply(row); if (result != null) { for (T r : result) { addWithEqualityChecker(r, data, equalityChecker, mergeDuplicates); @@ -154,7 +173,7 @@ public List getDataCollection(Object report, Function List getDataCollection(Object report, BiConsumer> rowHandler) { List data = new ArrayList<>(); - for (TableRow row : this) { + for (@Nullable TableRow row : this) { if (row != null) { try { rowHandler.accept(row, data); @@ -170,8 +189,8 @@ private List getDataCollection(Object report, BiConsumer void addWithEqualityChecker(T element, Collection collection, BiPredicate equalityChecker, - BiFunction> duplicatesMerger) { - T equalsObject = null; + BiFunction> duplicatesMerger) { + @Nullable T equalsObject = null; for (T e : collection) { if (equalityChecker.test(e, element)) { equalsObject = e; @@ -180,7 +199,10 @@ public static void addWithEqualityChecker(T element, } if (equalsObject != null) { collection.remove(equalsObject); - collection.addAll(duplicatesMerger.apply(equalsObject, element)); + @Nullable Collection mergedCollection = duplicatesMerger.apply(equalsObject, element); + if (mergedCollection != null) { + collection.addAll(mergedCollection); + } } else { collection.add(element); } @@ -189,17 +211,19 @@ public static void addWithEqualityChecker(T element, /** * {@link TableRow} impl is mutable. * For performance issue same object with changed state is provided in each loop cycle. - * Call {@link TableRow#clone()} if you want use row object outside stream() block. + * Call {@link TableRow#clone()} if you want to use row object outside stream() block. */ @Override - public Stream stream() { + public Stream<@Nullable TableRow> stream() { return StreamSupport.stream(spliterator(), false); } /** - * {@link TableRow} impl is mutable. - * For performance issue same object with changed state is provided in each loop cycle. - * Call {@link TableRow#clone()} if you want use row object outside iterator() block. + * Iterator which returns {@link MutableTableRow} or {@link EmptyTableRow}. + * + * @implSpec This iterator never returns null values. Null rows is wrapped by {@link EmptyTableRow} + * @implNote For performance issue same object with changed state is provided in each loop cycle. + * Call {@link TableRow#clone()} if you want to use row object outside iterator() block. */ @Override public Iterator iterator() { @@ -207,7 +231,7 @@ public Iterator iterator() { } protected class TableIterator implements Iterator { - private final MutableTableRow tableRow = + private final MutableTableRow tableRow = new MutableTableRow<>(AbstractTable.this, getCellDataAccessObject()); private final int numberOfRows = getNumberOfTableRows(tableRange); private int i = dataRowOffset; @@ -217,37 +241,61 @@ public boolean hasNext() { return i < numberOfRows; } + /** + * Returns mutable {@link TableRow} impl. Never returns null value. + */ @Override public TableRow next() { - R row; - do { - row = reportPage.getRow(tableRange.getFirstRow() + (i++)); - } while (row == null && hasNext()); + if (!hasNext()) { + throw new NoSuchElementException(); + } + int rowNum = tableRange.getFirstRow() + (i++); + @Nullable R row = reportPage.getRow(rowNum); + if (row == null) { + return new EmptyTableRow(AbstractTable.this, rowNum); + } tableRow.setRow(row); return tableRow; } } @Override - public TableRow findRow(Object value) { + public @Nullable TableRow getRow(int i) { + return getMutableTableRow(i); + } + + @Override + public @Nullable TableRow findRow(Object value) { TableCellAddress address = reportPage.find(value); return getMutableTableRow(address); } @Override - public TableRow findRowByPrefix(String prefix) { + public @Nullable TableRow findRowByPrefix(String prefix) { TableCellAddress address = reportPage.findByPrefix(prefix); return getMutableTableRow(address); } - private MutableTableRow getMutableTableRow(TableCellAddress address) { + private @Nullable MutableTableRow getMutableTableRow(TableCellAddress address) { if (tableRange.contains(address)) { - MutableTableRow tableRow = new MutableTableRow<>(this, getCellDataAccessObject()); - tableRow.setRow(reportPage.getRow(address.getRow())); + int rowNum = address.getRow(); + @Nullable MutableTableRow row = getMutableTableRow(rowNum); + @SuppressWarnings("nullness") + MutableTableRow tableRow = requireNonNull(row, "Row is empty"); return tableRow; } return null; } - protected abstract CellDataAccessObject getCellDataAccessObject(); + private @Nullable MutableTableRow getMutableTableRow(int i) { + @Nullable R row = reportPage.getRow(i); + if (row == null) { + return null; + } + MutableTableRow tableRow = new MutableTableRow<>(this, getCellDataAccessObject()); + tableRow.setRow(row); + return tableRow; + } + + public abstract CellDataAccessObject getCellDataAccessObject(); } \ No newline at end of file diff --git a/src/main/java/org/spacious_team/table_wrapper/api/AbstractTableCell.java b/src/main/java/org/spacious_team/table_wrapper/api/AbstractTableCell.java index 175152f..b4ad0da 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/AbstractTableCell.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/AbstractTableCell.java @@ -1,6 +1,6 @@ /* * Table Wrapper API - * Copyright (C) 2021 Vitalii Ananev + * Copyright (C) 2021 Spacious Team * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -18,23 +18,31 @@ package org.spacious_team.table_wrapper.api; -import lombok.AccessLevel; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.RequiredArgsConstructor; +import lombok.ToString; +import org.checkerframework.checker.nullness.qual.Nullable; import java.math.BigDecimal; import java.time.Instant; import java.time.LocalDateTime; +import java.time.ZoneId; +import java.util.Objects; -@Getter(AccessLevel.PROTECTED) -@RequiredArgsConstructor(access = AccessLevel.PROTECTED) -public abstract class AbstractTableCell implements TableCell { +import static lombok.AccessLevel.PROTECTED; +@ToString +@EqualsAndHashCode +@RequiredArgsConstructor(access = PROTECTED) +public abstract class AbstractTableCell> implements TableCell { + + @Getter(PROTECTED) private final T cell; - private final CellDataAccessObject dao; + private final D dao; @Override - public Object getValue() { + public @Nullable Object getValue() { return dao.getValue(cell); } @@ -49,7 +57,7 @@ public long getLongValue() { } @Override - public Double getDoubleValue() { + public double getDoubleValue() { return dao.getDoubleValue(cell); } @@ -72,4 +80,23 @@ public Instant getInstantValue() { public LocalDateTime getLocalDateTimeValue() { return dao.getLocalDateTimeValue(cell); } + + @Override + public LocalDateTime getLocalDateTimeValue(ZoneId zoneId) { + return dao.getLocalDateTimeValue(cell, zoneId); + } + + public D getCellDataAccessObject() { + return dao; + } + + /** + * Creates new cell object if provided {@link CellDataAccessObject} + * is different from this class CellDataAccessObject. + */ + public AbstractTableCell withCellDataAccessObject(D dao) { + return Objects.equals(this.dao, dao) ? this : createWithCellDataAccessObject(dao); + } + + protected abstract AbstractTableCell createWithCellDataAccessObject(D dao); } diff --git a/src/main/java/org/spacious_team/table_wrapper/api/AbstractTableFactory.java b/src/main/java/org/spacious_team/table_wrapper/api/AbstractTableFactory.java index 049ef1e..42b1902 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/AbstractTableFactory.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/AbstractTableFactory.java @@ -1,6 +1,6 @@ /* * Table Wrapper API - * Copyright (C) 2021 Vitalii Ananev + * Copyright (C) 2021 Spacious Team * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -19,12 +19,16 @@ package org.spacious_team.table_wrapper.api; import lombok.AccessLevel; +import lombok.EqualsAndHashCode; import lombok.RequiredArgsConstructor; +import lombok.ToString; /** * {@link TableFactory} factory with specified {@link ReportPage} * @param the factory supported {@link ReportPage} type and subtypes */ +@ToString +@EqualsAndHashCode @RequiredArgsConstructor(access = AccessLevel.PROTECTED) public abstract class AbstractTableFactory implements TableFactory { @@ -38,7 +42,7 @@ public boolean canHandle(ReportPage reportPage) { /** * Safe cast operation if {@link #canHandle(ReportPage)} is true */ - @SuppressWarnings("unchecked") + @SuppressWarnings({"unchecked", "unused"}) protected T cast(ReportPage reportPage) { return (T) reportPage; } diff --git a/src/main/java/org/spacious_team/table_wrapper/api/AnyOfTableColumn.java b/src/main/java/org/spacious_team/table_wrapper/api/AnyOfTableColumn.java index 88355e5..f6bf0e3 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/AnyOfTableColumn.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/AnyOfTableColumn.java @@ -1,6 +1,6 @@ /* * Table Wrapper API - * Copyright (C) 2020 Vitalii Ananev + * Copyright (C) 2020 Spacious Team * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -24,14 +24,16 @@ import java.util.Arrays; +import static lombok.AccessLevel.PRIVATE; + @ToString @EqualsAndHashCode -@RequiredArgsConstructor +@RequiredArgsConstructor(access = PRIVATE) public class AnyOfTableColumn implements TableColumn { private final TableColumn[] columns; - public static TableColumn of(TableColumn... columns) { + public static AnyOfTableColumn of(TableColumn... columns) { return new AnyOfTableColumn(columns); } @@ -43,9 +45,10 @@ public int getColumnIndex(int firstColumnForSearch, ReportPageRow... headerRows) } catch (RuntimeException ignore) { } } - throw new RuntimeException("Не обнаружен заголовок таблицы, включающий: " + String.join(", ", + String expected = String.join(", ", Arrays.stream(columns) .map(TableColumn::toString) - .toArray(String[]::new))); + .toArray(String[]::new)); + throw new TableColumnNotFound("Header including '" + expected + "' is not found"); } } diff --git a/src/main/java/org/spacious_team/table_wrapper/api/CellDataAccessObject.java b/src/main/java/org/spacious_team/table_wrapper/api/CellDataAccessObject.java index c2de57a..737d480 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/CellDataAccessObject.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/CellDataAccessObject.java @@ -1,6 +1,6 @@ /* * Table Wrapper API - * Copyright (C) 2021 Vitalii Ananev + * Copyright (C) 2021 Spacious Team * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -18,62 +18,65 @@ package org.spacious_team.table_wrapper.api; +import org.checkerframework.checker.nullness.qual.Nullable; + import java.math.BigDecimal; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; -import java.util.regex.Pattern; +import java.util.Objects; + +import static java.util.Objects.requireNonNull; +import static org.spacious_team.table_wrapper.api.CellDataAccessObjectHelper.*; /** - * Implementation is the stateless singleton cell helper class + * @apiNote Impl may have parameters that affect how the value is parsed, + * for example DataTimeFormat that changes behavior of date time value parser. */ public interface CellDataAccessObject { - ZoneId defaultZoneId = ZoneId.systemDefault(); - Pattern spacePattern = Pattern.compile("\\s"); - String NO_CELL_VALUE_EXCEPTION_MESSAGE = "Cell doesn't contains value"; - + @Nullable C getCell(R row, Integer cellIndex); + @Nullable Object getValue(C cell); /** - * @throws RuntimeException if can't extract int value + * @throws RuntimeException if method can't extract int value */ default int getIntValue(C cell) { return (int) getLongValue(cell); } /** - * @throws RuntimeException if can't extract long value + * @throws RuntimeException if method can't extract long value */ default long getLongValue(C cell) { - Object value = getValue(cell); + @Nullable Object value = getValue(cell); if (value instanceof Number) { return ((Number) value).longValue(); } else if (value != null) { - return Long.parseLong(spacePattern.matcher((CharSequence) value).replaceAll("")); + String str = spacePattern.matcher(value.toString()).replaceAll(""); + return Long.parseLong(str); } else { throw new NullPointerException(NO_CELL_VALUE_EXCEPTION_MESSAGE); } } /** - * @throws RuntimeException if can't extract Double value + * @throws RuntimeException if method can't extract Double value */ default double getDoubleValue(C cell) { - Object value = getValue(cell); + @Nullable Object value = getValue(cell); if (value instanceof Number) { return ((Number) value).doubleValue(); } else if (value != null) { - String str = spacePattern.matcher((CharSequence) value).replaceAll(""); + String str = spacePattern.matcher(value.toString()).replaceAll(""); try { return Double.parseDouble(str); } catch (NumberFormatException e) { if (str.indexOf(',') != -1) { return Double.parseDouble(str.replace(',', '.')); - } else if (str.indexOf('.') != -1) { - return Double.parseDouble(str.replace('.', ',')); } throw e; } @@ -81,93 +84,130 @@ default double getDoubleValue(C cell) { throw new NullPointerException(NO_CELL_VALUE_EXCEPTION_MESSAGE); } } + /** - * @throws RuntimeException if can't extract BigDecimal value + * @throws RuntimeException if method can't extract BigDecimal value + * @see Stack overflow + * for BigDecimal values equality */ default BigDecimal getBigDecimalValue(C cell) { - double number = getDoubleValue(cell); - return (number == 0) ? BigDecimal.ZERO : BigDecimal.valueOf(number); + String number = getStringValue(cell); + number = spacePattern.matcher(number).replaceAll(""); + number = number.replace(',', '.'); + return (Objects.equals(number, "0") || Objects.equals(number, "0.0") || Objects.equals(number, "0.00")) ? + BigDecimal.ZERO : new BigDecimal(number); } /** - * @throws RuntimeException if can't extract string value + * @throws RuntimeException if method can't extract string value */ + default String getStringValue(C cell) { - return getValue(cell).toString(); + @SuppressWarnings({"nullness", "ConstantConditions"}) + Object value = requireNonNull(getValue(cell), "Not a string"); + return value.toString(); } /** - * @throws RuntimeException if can't extract instant value + * @throws RuntimeException if method can't extract instant value */ Instant getInstantValue(C cell); /** - * @throws RuntimeException if can't extract local date time value + * Returns local date time at system default time zone. + * + * @throws RuntimeException if method can't extract local date time value */ default LocalDateTime getLocalDateTimeValue(C cell) { + return getLocalDateTimeValue(cell, defaultZoneId); + } + + /** + * @throws RuntimeException if method can't extract local date time value + */ + default LocalDateTime getLocalDateTimeValue(C cell, ZoneId zoneId) { return getInstantValue(cell) - .atZone(defaultZoneId) + .atZone(zoneId) .toLocalDateTime(); } - default Object getValue(R row, Integer cellIndex) { - C cell = getCell(row, cellIndex); - return getValue(cell); + default @Nullable Object getValue(R row, Integer cellIndex) { + @Nullable C cell = getCell(row, cellIndex); + return (cell == null) ? null : getValue(cell); } /** - * @throws RuntimeException if can't extract int value + * @throws RuntimeException if method can't extract int value */ default int getIntValue(R row, Integer cellIndex) { - C cell = getCell(row, cellIndex); + @SuppressWarnings({"nullness", "ConstantConditions"}) + C cell = requireNonNull(getCell(row, cellIndex), "Cell is not found"); return getIntValue(cell); } /** - * @throws RuntimeException if can't extract long value + * @throws RuntimeException if method can't extract long value */ default long getLongValue(R row, Integer cellIndex) { - C cell = getCell(row, cellIndex); + @SuppressWarnings({"nullness", "ConstantConditions"}) + C cell = requireNonNull(getCell(row, cellIndex), "Cell is not found"); return getLongValue(cell); } /** - * @throws RuntimeException if can't extract Double value + * @throws RuntimeException if method can't extract Double value */ default double getDoubleValue(R row, Integer cellIndex) { - C cell = getCell(row, cellIndex); + @SuppressWarnings({"nullness", "ConstantConditions"}) + C cell = requireNonNull(getCell(row, cellIndex), "Cell is not found"); return getDoubleValue(cell); } + /** - * @throws RuntimeException if can't extract BigDecimal value + * @throws RuntimeException if method can't extract BigDecimal value */ default BigDecimal getBigDecimalValue(R row, Integer cellIndex) { - C cell = getCell(row, cellIndex); + @SuppressWarnings({"nullness", "ConstantConditions"}) + C cell = requireNonNull(getCell(row, cellIndex), "Cell is not found"); return getBigDecimalValue(cell); } /** - * @throws RuntimeException if can't extract string value + * @throws RuntimeException if method can't extract string value */ default String getStringValue(R row, Integer cellIndex) { - C cell = getCell(row, cellIndex); + @SuppressWarnings({"nullness", "ConstantConditions"}) + C cell = requireNonNull(getCell(row, cellIndex), "Cell is not found"); return getStringValue(cell); } /** - * @throws RuntimeException if can't extract instant value + * @throws RuntimeException if method can't extract instant value */ default Instant getInstantValue(R row, Integer cellIndex) { - C cell = getCell(row, cellIndex); + @SuppressWarnings({"nullness", "ConstantConditions"}) + C cell = requireNonNull(getCell(row, cellIndex), "Cell is not found"); return getInstantValue(cell); } /** - * @throws RuntimeException if can't extract local date time value + * Returns local date time at default time zone. + * + * @throws RuntimeException if method can't extract local date time value */ default LocalDateTime getLocalDateTimeValue(R row, Integer cellIndex) { - C cell = getCell(row, cellIndex); + @SuppressWarnings({"nullness", "ConstantConditions"}) + C cell = requireNonNull(getCell(row, cellIndex), "Cell is not found"); return getLocalDateTimeValue(cell); } + /** + * @throws RuntimeException if method can't extract local date time value + */ + @SuppressWarnings("UnusedReturnValue") + default LocalDateTime getLocalDateTimeValue(R row, Integer cellIndex, ZoneId zoneId) { + @SuppressWarnings({"nullness", "ConstantConditions"}) + C cell = requireNonNull(getCell(row, cellIndex), "Cell is not found"); + return getLocalDateTimeValue(cell, zoneId); + } } diff --git a/src/main/java/org/spacious_team/table_wrapper/api/CellDataAccessObjectHelper.java b/src/main/java/org/spacious_team/table_wrapper/api/CellDataAccessObjectHelper.java new file mode 100644 index 0000000..b75a579 --- /dev/null +++ b/src/main/java/org/spacious_team/table_wrapper/api/CellDataAccessObjectHelper.java @@ -0,0 +1,34 @@ +/* + * Table Wrapper API + * Copyright (C) 2022 Spacious Team + * + * This program 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. + * + * This program 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 this program. If not, see . + */ + +package org.spacious_team.table_wrapper.api; + +import lombok.NoArgsConstructor; + +import java.time.ZoneId; +import java.util.regex.Pattern; + +import static lombok.AccessLevel.PRIVATE; + +@NoArgsConstructor(access = PRIVATE) +final class CellDataAccessObjectHelper { + + static final ZoneId defaultZoneId = ZoneId.systemDefault(); + static final Pattern spacePattern = Pattern.compile("\\s"); + static final String NO_CELL_VALUE_EXCEPTION_MESSAGE = "Cell doesn't contains value"; +} diff --git a/src/main/java/org/spacious_team/table_wrapper/api/ConstantPositionTableColumn.java b/src/main/java/org/spacious_team/table_wrapper/api/ConstantPositionTableColumn.java index ccf8fcc..dcf778b 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/ConstantPositionTableColumn.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/ConstantPositionTableColumn.java @@ -1,6 +1,6 @@ /* * Table Wrapper API - * Copyright (C) 2020 Vitalii Ananev + * Copyright (C) 2020 Spacious Team * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -18,10 +18,12 @@ package org.spacious_team.table_wrapper.api; +import lombok.EqualsAndHashCode; import lombok.RequiredArgsConstructor; import lombok.ToString; @ToString +@EqualsAndHashCode @RequiredArgsConstructor(staticName = "of") public class ConstantPositionTableColumn implements TableColumn { private final int columnIndex; diff --git a/src/main/java/org/spacious_team/table_wrapper/api/DateTimeFormatParser.java b/src/main/java/org/spacious_team/table_wrapper/api/DateTimeFormatParser.java new file mode 100644 index 0000000..aef0c86 --- /dev/null +++ b/src/main/java/org/spacious_team/table_wrapper/api/DateTimeFormatParser.java @@ -0,0 +1,221 @@ +/* + * Table Wrapper API + * Copyright (C) 2022 Spacious Team + * + * This program 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. + * + * This program 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 this program. If not, see . + */ + +package org.spacious_team.table_wrapper.api; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.RequiredArgsConstructor; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.time.format.DateTimeFormatter; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import static lombok.AccessLevel.PRIVATE; + +@NoArgsConstructor(access = PRIVATE) +final class DateTimeFormatParser { + + private static final Map dateTimeFormatters = new ConcurrentHashMap<>(); + + static DateTimeFormatter getDateTimeFormatter(String dateTimeOffset) { + Pattern pattern = getPattern(dateTimeOffset); + return getDateTimeFormatter(pattern); + } + + private static Pattern getPattern(String dateTimeOffset) { + @Nullable Pattern pattern = null; + int length = dateTimeOffset.length(); + if (length == 8 || length == 12) { // without and with millis + pattern = getForTime(dateTimeOffset, 0); + } else if (length == 10) { + pattern = getForDate(dateTimeOffset, 0); + } else if (length == 19 || length == 23) { // without and with millis + pattern = getForDateTime(dateTimeOffset); + } else if (length > 19) { + pattern = getForDateTimeZone(dateTimeOffset); + } + if (pattern == null) { + throw new IllegalArgumentException("Unknown date time format for: " + dateTimeOffset); + } + return pattern; + } + + private static TimePattern getForTime(String time, int offset) { + boolean hasMillis = (time.length() > (offset + 8)) && (time.charAt(offset + 8) == '.'); + return TimePattern.of(hasMillis); + } + + private static DatePattern getForDate(String date, int dateOffset) { + boolean isYearAtFirst; + char dateSplitter; + char ch = date.charAt(dateOffset + 2); + if (!Character.isDigit(ch)) { + // date format is DD MM YYYY + isYearAtFirst = false; + dateSplitter = ch; + } else { + // date format is YYYY MM DD + isYearAtFirst = true; + dateSplitter = date.charAt(dateOffset + 4); + } + return DatePattern.of(isYearAtFirst, dateSplitter); + } + + private static DateTimePattern getForDateTime(String dateTime) { + boolean isDateAtFirst; + DatePattern datePattern; + TimePattern timePattern; + char dateTimeSeparator; + if (dateTime.charAt(2) == ':') { + // format is