diff --git a/.github/workflows/deploy-action.yml b/.github/workflows/deploy-action.yml new file mode 100644 index 0000000..d2d4056 --- /dev/null +++ b/.github/workflows/deploy-action.yml @@ -0,0 +1,39 @@ +name: Deploy + +on: + push: + branches: + - master + +jobs: + deploy: + runs-on: ubuntu-18.04 + steps: + - name: Check out Git repository + uses: actions/checkout@v2 + + - name: Install Java and Maven + uses: actions/setup-java@v1 + with: + java-version: 1.8 + + - name: Cache Maven packages + uses: actions/cache@v2 + with: + path: ~/.m2 + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-m2 + + - name: Test Maven package + run: mvn -B test --file pom.xml + + - name: Build Maven package + run: mvn -B package --file pom.xml -DskipTests + + - name: Deploy Sonatype + uses: samuelmeuli/action-maven-publish@v1 + with: + gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }} + gpg_passphrase: ${{ secrets.GPG_PASSPHRASE }} + nexus_username: ${{ secrets.OSSRH_USERNAME }} + nexus_password: ${{ secrets.OSSRH_PASSWORD }} diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..af7025f --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Jonathan de Almeida Pereira + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index 7ce1302..e4019d3 100644 --- a/README.md +++ b/README.md @@ -1,127 +1,46 @@ # JFile Reader -This project helps you to build functionalities that needs to read and validate files. +This project provides an easy use and extensible API for read, validate, and parse files into java objects. ## Installation ```xml - com.github.jonpereiradev - jfile-reader - 0.7.0 + com.github.jonpereiradev + jfile-reader + ${jfile-reader.version} ``` -## JFileReaderFactory - -The JFileReaderFactory provides an API to read a file and instanciate the right class to handle the -type of file: - -**Example** - -```java -public class Main { - - public static void main(String[] args) { - String regexColumnSeparator = "\\|"; - Path path = Paths.get("path", "to", "file"); - ReaderConfiguration readerConfiguration = ReaderConfiguration.utf8Reader(regexColumnSeparator); - JFileReader jFileReader = JFileReaderFactory.newInstance(path, readerConfiguration); - } -} -``` - -## Annotations - -The annotations provided by the jfile reader are: - -- __FileColumn:__ to map a column to a wrapper field; -- __DecimalFormatter:__ to map the pattern of a number object; -- __DateTimeFormatter:__ to map the pattern of a date object; - -**Example** - -```java -public class User { - - @FileColumn(1) - private Integer type; - - @FileColumn(2) - private Boolean active; - - @FileColumn(3) - private String name; - - @FileColumn(4) - @DecimalFormatter("#,##0.0#") - private BigDecimal currency; - - @FileColumn(5) - @DateTimeFormatter("ddMMyyyy") - private LocalDate dateInactivation; - -} -``` - -```java -public class Main { - - public static void main(String[] args) { - String regexColumnSeparator = "\\|"; - Path path = Paths.get("path", "to", "file"); - ReaderConfiguration readerConfiguration = ReaderConfiguration.utf8Reader(regexColumnSeparator); - - try (JFileReader jFileReader = JFileReaderFactory.newInstance(path, readerConfiguration)) { - jFileReader.forEach(User.class, user -> { - System.out.println(user.getType()); - System.out.println(user.getActive()); - System.out.println(user.getName()); - System.out.println(user.getBigDecimal()); - System.out.println(user.getDateInactivation()); - }); - } - } -} -``` - -## Validations - -You can applly validations to the file using the class RuleConfigurator. - -**Example** - -```java -public class Main { - - public static void main(String[] args) { - String regexColumnSeparator = "\\|"; - Path path = Paths.get("path", "to", "file"); - ReaderConfiguration readerConfiguration = ReaderConfiguration.utf8Reader(regexColumnSeparator); - - RuleConfigurator - .defaultConfigurator(readerConfiguration) - .column(1) - .localDateType("dd/MM/yyyy") - .pastOrPresent() - .column(2) - .stringType() - .regex(Pattern.compile("\\d+")); - - try (JFileReader jFileReader = JFileReaderFactory.newInstance(path, readerConfiguration)) { - List violations = jFileReader.validate(); - - if (!violations.isEmpty()) { - // handle violations - } - - // it's ok? process lines - jFileReader.forEach(User.class, user -> System.out.println(user)); - } - } -} -``` - -# License +## Release Note + +**Version:** 0.8.0 + +- [x] Removing commons-lang dependency +- [x] Refactory JFileLine.getRow() method to JFileLine.getLineNumber() method +- [x] Refactory JFileColumn.getPosition() method to JFileColumn.getColumnNumber() method +- [x] Refactory JFileLine class to LineValue class +- [x] Refactory JFileColumn class to ColumnValue class +- [x] Moving JFileReader validate() method to JFileValidator class +- [X] Refactory Report class to ValidationReport class +- [X] Refactory Report isInvalid method to isNotValid method +- [X] Refactory ReaderConfiguration class to JFileReaderConfig class +- [X] Add throws IOException in JFileReaderFactory methods +- [x] Removing exception suppress from ColumnValue getter methods +- [X] Removing internal complexities +- [X] Removing JFileValidator.validate(fileReader) method +- [X] Adding JFileReader.stream() non-parallel support +- [X] String Rule for exactLength +- [X] Rename rule min and max to minLength and maxLength for string types +- [x] Enrich the API documentation +- [x] MIT License + +**Version:** 0.7.0 + +- [x] Inclusion of method canValidate for Rules +- [x] LocalDate and LocalDateTime validation Rules +- [x] Benchmark implementation with JMH + +## License JFile-Reader is available under the [MIT license](https://tldrlegal.com/license/mit-license). diff --git a/checkstyle.xml b/checkstyle.xml deleted file mode 100644 index 688a29a..0000000 --- a/checkstyle.xml +++ /dev/null @@ -1,122 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/pmd.xml b/pmd.xml deleted file mode 100644 index c20da28..0000000 --- a/pmd.xml +++ /dev/null @@ -1,250 +0,0 @@ - - Application ruleset - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/pom.xml b/pom.xml index 5eb194e..3e4fe53 100644 --- a/pom.xml +++ b/pom.xml @@ -1,14 +1,15 @@ - + 4.0.0 com.github.jonpereiradev jfile-reader - 0.7.0 + 0.8.0-SNAPSHOT jar ${project.groupId}:${project.artifactId} - API for read and parse a file into an object + API for read, validate, and parse files to objects https://github.com/jonpereiradev/jfile-reader @@ -18,7 +19,7 @@ - Jonathan Pereira + Jonathan de Almeida Pereira jonpereiradev@gmail.com https://github.com/jonpereiradev @@ -35,11 +36,6 @@ UTF-8 - - org.apache.commons - commons-lang3 - 3.8.1 - junit junit @@ -61,104 +57,55 @@ - - org.apache.maven.plugins - maven-source-plugin - 3.0.1 - - - attach-sources - - jar-no-fork - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 3.0.1 - - - attach-javadocs - - jar - - - - org.apache.maven.plugins maven-compiler-plugin - 3.7.0 - - 8 - 8 - - - - org.apache.maven.plugins - maven-site-plugin - 3.7.1 + 3.8.1 - org.apache.maven.plugins - maven-project-info-reports-plugin - 3.0.0 - - - org.apache.maven.plugins - maven-surefire-plugin - 3.0.0-M3 + external.atlassian.jgitflow + jgitflow-maven-plugin + 1.0-m5.1 - 5 + true + true + true + true + true + + master + develop + feature/ + release/ + hotfix/ + org.apache.maven.plugins - maven-pmd-plugin - 3.11.0 + maven-source-plugin + 3.2.1 - validate - validate + attach-sources - check - cpd-check + jar-no-fork - - false - false - - pmd.xml - - org.apache.maven.plugins - maven-checkstyle-plugin - 3.0.0 + maven-javadoc-plugin + 3.2.0 - validate - validate - - checkstyle.xml - UTF-8 - true - false - false - + attach-javadocs - check + jar - - false - @@ -168,45 +115,6 @@ - - true - - - org.apache.maven.plugins - maven-project-info-reports-plugin - 3.0.0 - - - - index - summary - plugins - - - - - - org.apache.maven.plugins - maven-pmd-plugin - 3.11.0 - - false - - pmd.xml - - - - - org.apache.maven.plugins - maven-checkstyle-plugin - 3.0.0 - - false - checkstyle.xml - - - - ossrh @@ -222,10 +130,21 @@ + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.8 + true + + ossrh + https://oss.sonatype.org/ + false + + org.apache.maven.plugins maven-gpg-plugin - 1.5 + 1.6 sign-artifacts @@ -233,20 +152,15 @@ sign + + + --pinentry-mode + loopback + + - - org.sonatype.plugins - nexus-staging-maven-plugin - 1.6.7 - true - - ossrh - https://oss.sonatype.org/ - true - - diff --git a/src/main/java/com/jonpereiradev/jfile/reader/JFilePatternConfig.java b/src/main/java/com/jonpereiradev/jfile/reader/JFilePatternConfig.java new file mode 100644 index 0000000..eee8a52 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/JFilePatternConfig.java @@ -0,0 +1,100 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader; + + +import java.text.DateFormat; +import java.text.DecimalFormat; +import java.time.format.DateTimeFormatter; + + +/** + *

Class to configure everything related to data patterns.

+ * + * @param the type of class returned by the method to allow fluent builder. + * + * @author jonpereiradev + * @see JFileReaderConfig + * @see com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig + * @since 0.1.0 + */ +public interface JFilePatternConfig> { + + /** + * Configure the default {@link java.util.Date} string formatter. + * + * @param dateFormat the formatter that applies to all {@link java.util.Date} type columns. + * + * @return the type of class returned by the method to allow fluent builder. + */ + T dateFormatter(DateFormat dateFormat); + + /** + * @return the date formatter for {@link java.util.Date}. + */ + DateFormat getDateFormat(); + + /** + * Configure the default {@link java.time.LocalDate} string formatter. + * + * @param localDateFormatter the formatter that applies to all {@link java.time.LocalDate} type columns. + * + * @return the type of class returned by the method to allow fluent builder. + */ + T localDateFormatter(DateTimeFormatter localDateFormatter); + + /** + * @return the local date formatter for {@link java.time.LocalDate} + */ + DateTimeFormatter getLocalDateFormatter(); + + /** + * Configure the default {@link java.time.LocalDateTime} string formatter. + * + * @param localDateTimeFormatter the formatter that applies to all {@link java.time.LocalDateTime} type columns. + * + * @return the type of class returned by the method to allow fluent builder. + */ + T localDateTimeFormatter(DateTimeFormatter localDateTimeFormatter); + + /** + * @return the local date time formatter for {@link java.time.LocalDateTime} + */ + DateTimeFormatter getLocalDateTimeFormatter(); + + /** + * Configure the default {@link java.math.BigDecimal} string formatter. + * + * @param decimalFormat the formatter that applies to all {@link java.math.BigDecimal} type columns. + * + * @return the type of class returned by the method to allow fluent builder. + */ + T bigDecimalFormat(DecimalFormat decimalFormat); + + /** + * @return the big decimal formatter for {@link java.math.BigDecimal} + */ + DecimalFormat getBigDecimalFormatter(); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/JFileReader.java b/src/main/java/com/jonpereiradev/jfile/reader/JFileReader.java index c0df218..452e666 100644 --- a/src/main/java/com/jonpereiradev/jfile/reader/JFileReader.java +++ b/src/main/java/com/jonpereiradev/jfile/reader/JFileReader.java @@ -1,49 +1,82 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package com.jonpereiradev.jfile.reader; -import com.jonpereiradev.jfile.reader.file.JFileLine; -import com.jonpereiradev.jfile.reader.validation.Report; + +import com.jonpereiradev.jfile.reader.file.LineValue; +import com.jonpereiradev.jfile.reader.validator.JFileValidator; +import com.jonpereiradev.jfile.reader.validator.ValidationReport; import java.io.Closeable; +import java.util.Iterator; import java.util.Objects; +import java.util.function.BiConsumer; import java.util.function.Consumer; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + /** * Point of access for file reading and validation. + * + * @author jonpereiradev + * @see JFileReaderConfig + * @see JFileReaderFactory + * @since 0.1.0 */ -public interface JFileReader extends Iterable, Closeable { +public interface JFileReader extends Iterable, Closeable { /** - * Creates an iterator for read the file line by line. + * Creates an iterator to read the file. * - * @return iterator for read the file. + * @return returns a new iterator to read the file. */ @Override - JFileReaderIterator iterator(); + Iterator iterator(); /** - * Validates all lines of the file with the configured rule. + * Parses the current line to the type of the class. * - * @return all violations of the file. - */ - Report validate(); - - /** - * Validates all lines of the file with the configured rule. + * @param toClass the type of object to parse the line. + * @param the object type. + * + * @return the line parsed into the object. * - * @return all violations of the file. + * @throws java.util.NoSuchElementException when no current line is present in the iterator */ - Report validate(JFileLine line); + T converted(Class toClass); /** * Parses a line to the type of the class. * - * @param line the line that will be transformed in object. + * @param lineValue the line that will be transformed in object. * @param toClass the type of object to parse the line. * @param the object type. * - * @return the line parsed in object. + * @return the line parsed into the object. */ - T parse(JFileLine line, Class toClass); + T convert(LineValue lineValue, Class toClass); /** * Iterates over the lines converting the line to the class type. @@ -54,29 +87,42 @@ public interface JFileReader extends Iterable, Closeable { */ default void forEach(Class clazz, Consumer consumer) { Objects.requireNonNull(consumer); - JFileReaderIterator iterator = iterator(); - while (iterator.hasNext()) { - JFileLine line = iterator.next(); - T object = parse(line, clazz); + for (LineValue lineValue : this) { + T object = convert(lineValue, clazz); consumer.accept(object); } } + /** + * Iterates over the lines converting the line to the class type. + * + * @param fileValidator the validator that applies validation to the line. + * @param consumer the execution for each iteration. + */ + default void forEach(JFileValidator fileValidator, BiConsumer consumer) { + Objects.requireNonNull(consumer); + + for (LineValue lineValue : this) { + ValidationReport validationReport = fileValidator.validate(lineValue); + consumer.accept(lineValue, validationReport); + } + } + /** * Iterates over the valid lines. * + * @param fileValidator the validator that applies validation to the line. * @param consumer the execution for each iteration. */ - default void forEachValid(Consumer consumer) { + default void forEachValid(JFileValidator fileValidator, Consumer consumer) { Objects.requireNonNull(consumer); - JFileReaderIterator iterator = iterator(); - while (iterator.hasNext()) { - JFileLine line = iterator.next(); + for (LineValue lineValue : this) { + ValidationReport validationReport = fileValidator.validate(lineValue); - if (validate(line).isValid()) { - consumer.accept(line); + if (validationReport.isValid()) { + consumer.accept(lineValue); } } } @@ -85,20 +131,43 @@ default void forEachValid(Consumer consumer) { * Iterates over the valid lines converting the line to the class type. * * @param clazz the class type of the object. + * @param fileValidator the validator that applies validation to the line. * @param consumer the execution for each iteration. * @param the type of the object. */ - default void forEachValid(Class clazz, Consumer consumer) { + default void forEachValid(JFileValidator fileValidator, Class clazz, Consumer consumer) { Objects.requireNonNull(consumer); - JFileReaderIterator iterator = iterator(); - while (iterator.hasNext()) { - JFileLine line = iterator.next(); + for (LineValue lineValue : this) { + ValidationReport validationReport = fileValidator.validate(lineValue); - if (validate(line).isValid()) { - T object = parse(line, clazz); + if (validationReport.isValid()) { + T object = convert(lineValue, clazz); consumer.accept(object); } } } + + /** + * Iterates over the valid lines. + * + * @param fileValidator the validator that applies validation to the line. + * @param consumer the execution for each iteration. + */ + default void forEachNotValid(JFileValidator fileValidator, BiConsumer consumer) { + Objects.requireNonNull(consumer); + + for (LineValue lineValue : this) { + ValidationReport validationReport = fileValidator.validate(lineValue); + + if (validationReport.isNotValid()) { + consumer.accept(lineValue, validationReport); + } + } + } + + default Stream stream() { + return StreamSupport.stream(spliterator(), false); + } + } diff --git a/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderConfig.java b/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderConfig.java new file mode 100644 index 0000000..9f3e23b --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderConfig.java @@ -0,0 +1,58 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader; + + +import com.jonpereiradev.jfile.reader.converter.LineValueConverter; + +import java.nio.charset.Charset; +import java.util.regex.Pattern; + + +/** + *

Class to configure and customize the execution of a {@link JFileReader}.

+ * + * @author jonpereiradev + * @see JFileReader + * @see JFileReaderFactory + * @since 0.1.0 + */ +public interface JFileReaderConfig extends JFilePatternConfig { + + /** + * @return the pattern to split the line into columns. + */ + Pattern getPattern(); + + /** + * @return the charset of the file content. + */ + Charset getCharset(); + + /** + * @return the converter responsible to transform a line into an object. + */ + LineValueConverter getLineValueConverter(); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderConfigImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderConfigImpl.java new file mode 100644 index 0000000..82cfb14 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderConfigImpl.java @@ -0,0 +1,118 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader; + + +import com.jonpereiradev.jfile.reader.converter.LineValueConverter; +import com.jonpereiradev.jfile.reader.converter.ReflectionLineValueConverter; + +import java.nio.charset.Charset; +import java.text.DateFormat; +import java.text.DecimalFormat; +import java.time.format.DateTimeFormatter; +import java.util.regex.Pattern; + + +final class JFileReaderConfigImpl implements JFileReaderConfig { + + private final Pattern pattern; + private final Charset charset; + private final LineValueConverter lineValueConverter; + + private DateFormat dateFormat; + private DateTimeFormatter localDateFormatter; + private DateTimeFormatter localDateTimeFormatter; + private DecimalFormat bigDecimalFormatter; + + JFileReaderConfigImpl(Pattern pattern, Charset charset) { + this.pattern = pattern; + this.charset = charset; + this.lineValueConverter = new ReflectionLineValueConverter(this); + this.dateFormat = DateFormat.getInstance(); + this.localDateFormatter = DateTimeFormatter.ISO_LOCAL_DATE; + this.localDateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; + this.bigDecimalFormatter = new DecimalFormat(); + this.bigDecimalFormatter.setParseBigDecimal(true); + } + + @Override + public JFileReaderConfig dateFormatter(DateFormat dateFormat) { + this.dateFormat = dateFormat; + return this; + } + + @Override + public DateFormat getDateFormat() { + return dateFormat; + } + + @Override + public JFileReaderConfig localDateFormatter(DateTimeFormatter localDateFormatter) { + this.localDateFormatter = localDateFormatter; + return this; + } + + @Override + public DateTimeFormatter getLocalDateFormatter() { + return localDateFormatter; + } + + @Override + public JFileReaderConfig localDateTimeFormatter(DateTimeFormatter localDateTimeFormatter) { + this.localDateTimeFormatter = localDateTimeFormatter; + return this; + } + + @Override + public DateTimeFormatter getLocalDateTimeFormatter() { + return localDateTimeFormatter; + } + + @Override + public JFileReaderConfig bigDecimalFormat(DecimalFormat decimalFormat) { + this.bigDecimalFormatter = decimalFormat; + return this; + } + + @Override + public DecimalFormat getBigDecimalFormatter() { + return bigDecimalFormatter; + } + + @Override + public Pattern getPattern() { + return pattern; + } + + @Override + public Charset getCharset() { + return charset; + } + + @Override + public LineValueConverter getLineValueConverter() { + return lineValueConverter; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderContext.java b/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderContext.java deleted file mode 100644 index 529e40a..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderContext.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.jonpereiradev.jfile.reader; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.parser.FileLineParser; -import com.jonpereiradev.jfile.reader.rule.RuleConfiguration; -import com.jonpereiradev.jfile.reader.stream.StreamReader; - -import java.io.InputStream; -import java.nio.charset.Charset; -import java.text.DateFormat; -import java.text.DecimalFormat; -import java.time.format.DateTimeFormatter; -import java.util.regex.Pattern; - -/** - * Context to access all necessary information of the current reading operation. - */ -public class JFileReaderContext { - - private final InputStream inputStream; - private final ReaderConfiguration readerConfiguration; - - JFileReaderContext(InputStream inputStream, ReaderConfiguration readerConfiguration) { - this.inputStream = inputStream; - this.readerConfiguration = readerConfiguration; - } - - public InputStream getInputStream() { - return inputStream; - } - - public ReaderConfiguration getReaderConfiguration() { - return readerConfiguration; - } - - public Pattern getPattern() { - return readerConfiguration.getPattern(); - } - - public Charset getCharset() { - return readerConfiguration.getCharset(); - } - - public DateFormat getDateFormat() { - return readerConfiguration.getDateFormat(); - } - - public DateTimeFormatter getLocalDateFormatter() { - return readerConfiguration.getLocalDateFormatter(); - } - - public DateTimeFormatter getLocalDateTimeFormatter() { - return readerConfiguration.getLocalDateTimeFormatter(); - } - - public DecimalFormat getBigDecimalFormatter() { - return readerConfiguration.getBigDecimalFormatter(); - } - - public RuleConfiguration getRuleConfiguration() { - return readerConfiguration.getRuleConfiguration(); - } - - public StreamReader getStreamReader() { - return readerConfiguration.getStreamReader().apply(inputStream); - } - - public FileLineParser getFileLineParser() { - return readerConfiguration.getFileLineParser(); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderEngine.java b/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderEngine.java new file mode 100644 index 0000000..ecd4f44 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderEngine.java @@ -0,0 +1,154 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader; + + +import com.jonpereiradev.jfile.reader.converter.LineValueConverter; +import com.jonpereiradev.jfile.reader.file.ColumnValue; +import com.jonpereiradev.jfile.reader.file.LineValue; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.SortedSet; +import java.util.TreeSet; +import java.util.regex.Pattern; + +import static com.jonpereiradev.jfile.reader.file.ColumnValue.newColumnValue; +import static com.jonpereiradev.jfile.reader.file.LineValue.newLineValue; + + +final class JFileReaderEngine implements JFileReader { + + private final JFileReaderConfig readerConfig; + private final LineValueConverter lineValueConverter; + private final BufferedReader bufferedReader; + private final Iterator iterator; + + private JFileReaderEngine(InputStream inputStream, JFileReaderConfig readerConfig) { + this.readerConfig = readerConfig; + this.lineValueConverter = readerConfig.getLineValueConverter(); + this.bufferedReader = new BufferedReader(new InputStreamReader(inputStream, readerConfig.getCharset())); + this.iterator = new JFileReaderIterator(); + } + + static JFileReaderEngine newInstance(InputStream inputStream, JFileReaderConfig readerConfig) throws IOException { + validateInputStream(inputStream); + return new JFileReaderEngine(inputStream, readerConfig); + } + + private static void validateInputStream(InputStream inputStream) throws IOException { + if (inputStream.available() == 0) { + throw new IOException("InputStream doesn't have bytes to read"); + } + } + + @Override + public Iterator iterator() { + return iterator; + } + + @Override + public T converted(Class toClass) { + JFileReaderIterator iterator = (JFileReaderIterator) iterator(); + LineValue lastLineValue = iterator.lastLineValue; + + if (lastLineValue == null) { + throw new NoSuchElementException("The iterator has no line value present"); + } + + return convert(lastLineValue, toClass); + } + + @Override + public T convert(LineValue lineValue, Class toClass) { + return lineValueConverter.convert(lineValue, toClass); + } + + @Override + public void close() throws IOException { + bufferedReader.close(); + } + + final class JFileReaderIterator implements Iterator { + + private int lineNumber; + private String lastLineFromReader; + private LineValue lastLineValue; + + @Override + public boolean hasNext() { + setCurrentLine(); + return lastLineFromReader != null; + } + + @Override + public LineValue next() { + setCurrentLine(); + String contentCurrentLine = getContentCurrentLine(); + lastLineValue = getCurrentLineValue(contentCurrentLine); + return lastLineValue; + } + + private LineValue getCurrentLineValue(String contentCurrentLine) { + Pattern pattern = readerConfig.getPattern(); + String[] columns = pattern.split(contentCurrentLine); + SortedSet columnValues = new TreeSet<>(); + + for (int i = 0; i < columns.length; i++) { + int columnNumber = i + 1; + ColumnValue columnValue = newColumnValue(readerConfig, columnNumber, columns[i]); + columnValues.add(columnValue); + } + + return newLineValue(++lineNumber, contentCurrentLine, columnValues); + } + + private void setCurrentLine() { + if (lastLineFromReader == null) { + try { + lastLineFromReader = bufferedReader.readLine(); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + } + + private String getContentCurrentLine() { + String currentLine = lastLineFromReader; + lastLineFromReader = null; + + if (currentLine == null) { + throw new NoSuchElementException("No more lines to read."); + } + + return currentLine; + } + + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderFactory.java b/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderFactory.java index fa80297..4c37883 100644 --- a/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderFactory.java +++ b/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderFactory.java @@ -1,39 +1,163 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package com.jonpereiradev.jfile.reader; -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.Objects; +import java.util.regex.Pattern; + /** - * Factory to create the {@link JFileReader} implementation. + *

Factory to produces {@link JFileReader} and {@link JFileReaderConfig} objects to configure and read files.

+ * + * @author jonpereiradev + * @see JFileReader + * @see JFileReaderConfig + * @since 0.1.0 */ public final class JFileReaderFactory { private JFileReaderFactory() { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException("Instantiation not supported"); + } + + /** + * Creates a {@link JFileReader} object configured with file and regex split pattern. + * + * @param file the file instance used to create the {@link InputStream}. + * @param regex the pattern used to split a line into columns. + * + * @return a {@link JFileReader} object configured for file reading. + * + * @throws IOException if a problem occurs when creating {@link InputStream} for the file. + */ + public static JFileReader newUtf8JFileReader(File file, String regex) throws IOException { + return newUtf8JFileReader(file.toPath(), regex); + } + + /** + * Creates a {@link JFileReader} object configured with file and configuration. + * + * @param file the file instance used to create the {@link InputStream}. + * @param readerConfig the configuration for reading the file. + * + * @return a {@link JFileReader} object configured for file reading. + * + * @throws IOException if a problem occurs when creating {@link InputStream} for the file. + */ + public static JFileReader newJFileReader(File file, JFileReaderConfig readerConfig) throws IOException { + return newJFileReader(file.toPath(), readerConfig); + } + + /** + * Creates a {@link JFileReader} object configured with file and configuration. + * + * @param path the path used to create the {@link InputStream}. + * @param regex the pattern used to split a line into columns. + * + * @return a {@link JFileReader} object configured for file reading. + * + * @throws IOException if a problem occurs when creating {@link InputStream} for the path. + */ + public static JFileReader newUtf8JFileReader(Path path, String regex) throws IOException { + return newUtf8JFileReader(Files.newInputStream(path), regex); + } + + /** + * Creates a {@link JFileReader} object configured with file and configuration. + * + * @param path the path used to create the {@link InputStream}. + * @param readerConfig the configuration for reading the file. + * + * @return a {@link JFileReader} object configured for file reading. + * + * @throws IOException if a problem occurs when creating {@link InputStream} for the path. + */ + public static JFileReader newJFileReader(Path path, JFileReaderConfig readerConfig) throws IOException { + return newJFileReader(Files.newInputStream(path), readerConfig); + } + + /** + * Creates a {@link JFileReader} object configured with file and configuration. + * + * @param inputStream the stream to read the file content. + * @param regex the pattern used to split a line into columns. + * + * @return a {@link JFileReader} object configured for file reading. + * + * @throws IOException if a problem occurs when creating {@link InputStream} for the path. + */ + public static JFileReader newUtf8JFileReader(InputStream inputStream, String regex) throws IOException { + return newJFileReader(inputStream, newUtf8ReaderConfig(regex)); } - public static JFileReader newInstance(File file, ReaderConfiguration configuration) { - return newInstance(file.toPath(), configuration); + /** + * Creates a {@link JFileReader} object configured with file and configuration. + * + * @param inputStream the file instance to creates the stream. + * @param readerConfig the configuration for reading the file. + * + * @return a {@link JFileReader} object configured for file reading. + * + * @throws IOException if a problem occurs when validating the {@link InputStream}. + */ + public static JFileReader newJFileReader( + InputStream inputStream, + JFileReaderConfig readerConfig) throws IOException { + Objects.requireNonNull(inputStream, "InputStream is required"); + Objects.requireNonNull(readerConfig, "JFileReaderConfig is required"); + return JFileReaderEngine.newInstance(inputStream, readerConfig); } - public static JFileReader newInstance(Path path, ReaderConfiguration configuration) { - try { - return newInstance(Files.newInputStream(path), configuration); - } catch (IOException e) { - throw new IllegalArgumentException(e); - } + /** + * Creates a {@link JFileReaderConfig} object configured with UTF-8 encoding. + * + * @param regex the pattern used to split a line into columns. + * + * @return a {@link JFileReaderConfig} object configured with UTF-8 encoding. + */ + public static JFileReaderConfig newUtf8ReaderConfig(String regex) { + return newReaderConfig(regex, StandardCharsets.UTF_8); } - public static JFileReader newInstance(InputStream inputStream, ReaderConfiguration configuration) { - Objects.requireNonNull(inputStream, "InputStream is required."); - JFileReaderContext context = new JFileReaderContext(inputStream, configuration); - return new JFileReaderImpl(context); + /** + * Creates a {@link JFileReaderConfig} object configured with regex and charset. + * + * @param regex the pattern used to split a line into columns. + * @param charset the encode of the file content. + * + * @return a {@link JFileReaderConfig} object. + */ + public static JFileReaderConfig newReaderConfig(String regex, Charset charset) { + return new JFileReaderConfigImpl(Pattern.compile(regex), charset); } } diff --git a/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderImpl.java deleted file mode 100644 index a81fe05..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderImpl.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.jonpereiradev.jfile.reader; - -import com.jonpereiradev.jfile.reader.file.JFileLine; -import com.jonpereiradev.jfile.reader.rule.RuleViolation; -import com.jonpereiradev.jfile.reader.stream.StreamReader; -import com.jonpereiradev.jfile.reader.validation.JFileValidatorEngine; -import com.jonpereiradev.jfile.reader.validation.Report; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; - -final class JFileReaderImpl implements JFileReader { - - private final JFileReaderContext context; - private final StreamReader streamReader; - - private JFileReaderIterator iterator; - - JFileReaderImpl(JFileReaderContext context) { - this.context = context; - this.streamReader = context.getStreamReader(); - } - - @Override - public JFileReaderIterator iterator() { - if (iterator == null) { - iterator = new JFileReaderIteratorImpl(context, streamReader); - } - - return iterator; - } - - @Override - public Report validate() { - Objects.requireNonNull(context.getRuleConfiguration(), "No rule configuration provided."); - - if (iterator != null) { - throw new IllegalStateException("Iterator already initialized. You can't use this method after called iterator()."); - } - - List lines = new ArrayList<>(); - Report report = Report.defaultReport(); - int maxViolationSize = context.getReaderConfiguration().getMaxViolationSize(); - JFileReaderIterator iterator = iterator(); - - while (iterator.hasNext()) { - JFileLine line = iterator.next(); - List violations = validateLine(line); - - report.put(line.getRow(), violations); - lines.add(line); - - if (maxViolationSize > 0 && report.getViolations().size() > maxViolationSize) { - break; - } - } - - this.iterator = new InMemoryJFileReaderIterator(lines); - - return report; - } - - @Override - public Report validate(JFileLine line) { - Report report = Report.defaultReport(); - List violations = validateLine(line); - - report.put(line.getRow(), violations); - - return report; - } - - private List validateLine(JFileLine line) { - Objects.requireNonNull(context.getRuleConfiguration(), "No rule configuration provided."); - - if (line == null) { - throw new IllegalStateException("No line selected in the iterator."); - } - - return JFileValidatorEngine.defaultEngine(context).validate(line); - } - - @Override - public T parse(JFileLine line, Class toClass) { - return context.getFileLineParser().parse(line, toClass); - } - - @Override - public void close() throws IOException { - streamReader.close(); - } - - private class InMemoryJFileReaderIterator implements JFileReaderIterator { - - private final List lines; - - private int index = 0; - - private InMemoryJFileReaderIterator(List lines) { - this.lines = lines; - } - - @Override - public boolean hasNext() { - return index < lines.size(); - } - - @Override - public JFileLine next() { - return lines.get(index++); - } - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderIterator.java b/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderIterator.java deleted file mode 100644 index d57e3b3..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderIterator.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.jonpereiradev.jfile.reader; - -import com.jonpereiradev.jfile.reader.file.JFileLine; - -import java.util.Iterator; - -/** - * Iterator for read and validate the line of a file. - */ -public interface JFileReaderIterator extends Iterator { - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderIteratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderIteratorImpl.java deleted file mode 100644 index 2b301ae..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/JFileReaderIteratorImpl.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.jonpereiradev.jfile.reader; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; -import com.jonpereiradev.jfile.reader.file.JFileLine; -import com.jonpereiradev.jfile.reader.stream.StreamReader; - -import java.util.Iterator; -import java.util.SortedSet; -import java.util.TreeSet; - -final class JFileReaderIteratorImpl implements JFileReaderIterator { - - private final JFileReaderContext context; - private final Iterator iterator; - - private int row; - - JFileReaderIteratorImpl(JFileReaderContext context, StreamReader streamReader) { - this.context = context; - this.iterator = streamReader.iterator(); - } - - @Override - public boolean hasNext() { - return iterator.hasNext(); - } - - @Override - public JFileLine next() { - String content = iterator.next(); - String[] columns = context.getPattern().split(content); - - SortedSet fileColumns = new TreeSet<>(); - - for (int i = 0; i < columns.length; i++) { - fileColumns.add(new JFileColumn(context, i + 1, columns[i])); - } - - return new JFileLine(++row, content, fileColumns); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/configuration/ReaderConfiguration.java b/src/main/java/com/jonpereiradev/jfile/reader/configuration/ReaderConfiguration.java deleted file mode 100644 index 471fa47..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/configuration/ReaderConfiguration.java +++ /dev/null @@ -1,110 +0,0 @@ -package com.jonpereiradev.jfile.reader.configuration; - -import com.jonpereiradev.jfile.reader.parser.FileLineParser; -import com.jonpereiradev.jfile.reader.rule.RuleConfiguration; -import com.jonpereiradev.jfile.reader.stream.StreamReader; - -import java.io.InputStream; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; -import java.text.DateFormat; -import java.text.DecimalFormat; -import java.time.format.DateTimeFormatter; -import java.util.function.Function; -import java.util.regex.Pattern; - -public interface ReaderConfiguration { - - static ReaderConfiguration utf8Reader(String regex) { - return new ReaderConfigurationImpl(Pattern.compile(regex), StandardCharsets.UTF_8); - } - - static ReaderConfiguration reader(String regex, Charset charset) { - return new ReaderConfigurationImpl(Pattern.compile(regex), charset); - } - - /** - * Configure the default {@link java.util.Date} string formatter. - */ - ReaderConfiguration withDateFormatter(DateFormat dateFormatter); - - /** - * Configure the default {@link java.time.LocalDate} string formatter. - */ - ReaderConfiguration withLocalDateFormatter(DateTimeFormatter localDateFormatter); - - /** - * Configure the default {@link java.time.LocalDateTime} string formatter. - */ - ReaderConfiguration withLocalDateTimeFormatter(DateTimeFormatter localDateTimeFormatter); - - /** - * Configure the default {@link java.math.BigDecimal} string formatter. - */ - ReaderConfiguration withBigDecimalFormat(DecimalFormat decimalFormat); - - /** - * Configure the rules of validation. - */ - ReaderConfiguration withRuleConfiguration(RuleConfiguration ruleConfiguration); - - /** - * Configure the {@link StreamReader} to parse the file. - */ - ReaderConfiguration withStreamReader(Function streamReader); - - /** - * Configure the max violation size for lines. - */ - ReaderConfiguration withMaxViolationSize(int maxViolationSize); - - /** - * @return the pattern to split the line in columns. - */ - Pattern getPattern(); - - /** - * @return the charset to read the bytes as string. - */ - Charset getCharset(); - - /** - * @return the date formatter for {@link java.util.Date} - */ - DateFormat getDateFormat(); - - /** - * @return the local date formatter for {@link java.time.LocalDate} - */ - DateTimeFormatter getLocalDateFormatter(); - - /** - * @return the local date time formatter for {@link java.time.LocalDateTime} - */ - DateTimeFormatter getLocalDateTimeFormatter(); - - /** - * @return the big decimal formatter for {@link java.math.BigDecimal} - */ - DecimalFormat getBigDecimalFormatter(); - - /** - * @return the rules configuration for this reader. - */ - RuleConfiguration getRuleConfiguration(); - - /** - * @return the responsible stream reader to read the file content. - */ - Function getStreamReader(); - - /** - * @return the parser responsible to convert a line into an object. - */ - FileLineParser getFileLineParser(); - - /** - * @return the max lines violation size. - */ - int getMaxViolationSize(); -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/configuration/ReaderConfigurationImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/configuration/ReaderConfigurationImpl.java deleted file mode 100644 index c9f191b..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/configuration/ReaderConfigurationImpl.java +++ /dev/null @@ -1,132 +0,0 @@ -package com.jonpereiradev.jfile.reader.configuration; - -import com.jonpereiradev.jfile.reader.parser.FileLineParser; -import com.jonpereiradev.jfile.reader.rule.RuleConfiguration; -import com.jonpereiradev.jfile.reader.stream.StreamReader; - -import java.io.InputStream; -import java.nio.charset.Charset; -import java.text.DateFormat; -import java.text.DecimalFormat; -import java.time.format.DateTimeFormatter; -import java.util.function.Function; -import java.util.regex.Pattern; - -final class ReaderConfigurationImpl implements ReaderConfiguration { - - private final Pattern pattern; - private final Charset charset; - private final FileLineParser fileLineParser; - - private DateFormat dateFormat; - private DateTimeFormatter localDateFormatter; - private DateTimeFormatter localDateTimeFormatter; - private DecimalFormat bigDecimalFormatter; - private RuleConfiguration ruleConfiguration; - private Function streamReader; - private int maxViolationSize = -1; - - ReaderConfigurationImpl(Pattern pattern, Charset charset) { - this.pattern = pattern; - this.charset = charset; - this.fileLineParser = FileLineParser.defaultParser(this); - this.dateFormat = DateFormat.getInstance(); - this.localDateFormatter = DateTimeFormatter.ISO_LOCAL_DATE; - this.localDateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; - this.bigDecimalFormatter = new DecimalFormat(); - this.bigDecimalFormatter.setParseBigDecimal(true); - this.streamReader = inputStream -> StreamReader.defaultStreamReader(inputStream, charset); - } - - @Override - public ReaderConfiguration withDateFormatter(DateFormat dateFormat) { - this.dateFormat = dateFormat; - return this; - } - - @Override - public ReaderConfiguration withLocalDateFormatter(DateTimeFormatter localDateFormatter) { - this.localDateFormatter = localDateFormatter; - return this; - } - - @Override - public ReaderConfiguration withLocalDateTimeFormatter(DateTimeFormatter localDateTimeFormatter) { - this.localDateTimeFormatter = localDateTimeFormatter; - return this; - } - - @Override - public ReaderConfiguration withBigDecimalFormat(DecimalFormat decimalFormat) { - this.bigDecimalFormatter = decimalFormat; - return this; - } - - @Override - public ReaderConfiguration withRuleConfiguration(RuleConfiguration ruleConfiguration) { - this.ruleConfiguration = ruleConfiguration; - return this; - } - - @Override - public ReaderConfiguration withStreamReader(Function streamReader) { - this.streamReader = streamReader; - return this; - } - - @Override - public ReaderConfiguration withMaxViolationSize(int maxViolationSize) { - this.maxViolationSize = maxViolationSize; - return this; - } - - @Override - public Pattern getPattern() { - return pattern; - } - - @Override - public Charset getCharset() { - return charset; - } - - @Override - public DateFormat getDateFormat() { - return dateFormat; - } - - @Override - public DateTimeFormatter getLocalDateFormatter() { - return localDateFormatter; - } - - @Override - public DateTimeFormatter getLocalDateTimeFormatter() { - return localDateTimeFormatter; - } - - @Override - public DecimalFormat getBigDecimalFormatter() { - return bigDecimalFormatter; - } - - @Override - public RuleConfiguration getRuleConfiguration() { - return ruleConfiguration; - } - - @Override - public Function getStreamReader() { - return streamReader; - } - - @Override - public FileLineParser getFileLineParser() { - return fileLineParser; - } - - @Override - public int getMaxViolationSize() { - return maxViolationSize; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/converter/DateTimeFormatter.java b/src/main/java/com/jonpereiradev/jfile/reader/converter/DateTimeFormatter.java new file mode 100644 index 0000000..6c5b7fe --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/converter/DateTimeFormatter.java @@ -0,0 +1,43 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.converter; + + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface DateTimeFormatter { + + String value(); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/converter/DecimalFormatter.java b/src/main/java/com/jonpereiradev/jfile/reader/converter/DecimalFormatter.java new file mode 100644 index 0000000..b894c39 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/converter/DecimalFormatter.java @@ -0,0 +1,47 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.converter; + + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface DecimalFormatter { + + String value(); + + char groupingSeparator() default '.'; + + char decimalSeparator() default ','; + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/converter/FileColumn.java b/src/main/java/com/jonpereiradev/jfile/reader/converter/FileColumn.java new file mode 100644 index 0000000..1f132fb --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/converter/FileColumn.java @@ -0,0 +1,43 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.converter; + + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface FileColumn { + + int value(); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/converter/GetterSetterPair.java b/src/main/java/com/jonpereiradev/jfile/reader/converter/GetterSetterPair.java new file mode 100644 index 0000000..5f47c89 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/converter/GetterSetterPair.java @@ -0,0 +1,78 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.converter; + + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +final class GetterSetterPair { + + private Field field; + private Method getter; + private Method setter; + private FileColumn annotation; + + Field getField() { + return field; + } + + void setField(Field field) { + this.field = field; + } + + Method getGetter() { + return getter; + } + + void setGetter(Method getter) { + this.getter = getter; + } + + Method getSetter() { + return setter; + } + + void setSetter(Method setter) { + this.setter = setter; + } + + FileColumn getAnnotation() { + return annotation; + } + + void setAnnotation(FileColumn annotation) { + this.annotation = annotation; + } + + boolean hasGetterAndSetter() { + return getter != null && setter != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/converter/LineValueConverter.java b/src/main/java/com/jonpereiradev/jfile/reader/converter/LineValueConverter.java new file mode 100644 index 0000000..dc50d6f --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/converter/LineValueConverter.java @@ -0,0 +1,38 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.converter; + + +import com.jonpereiradev.jfile.reader.file.LineValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface LineValueConverter { + + T convert(LineValue lineValue, Class classType); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/converter/ReflectionLineValueConverter.java b/src/main/java/com/jonpereiradev/jfile/reader/converter/ReflectionLineValueConverter.java new file mode 100644 index 0000000..937fcf2 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/converter/ReflectionLineValueConverter.java @@ -0,0 +1,89 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.converter; + + +import com.jonpereiradev.jfile.reader.JFileReaderConfig; +import com.jonpereiradev.jfile.reader.file.LineValue; + +import java.util.HashMap; +import java.util.Map; + + +public final class ReflectionLineValueConverter implements LineValueConverter { + + private static final Map, Map> CACHE_CLASS_REFLECTIONS = new HashMap<>(); + + private final JFileReaderConfig readerConfig; + + public ReflectionLineValueConverter(JFileReaderConfig readerConfig) { + this.readerConfig = readerConfig; + } + + @Override + @SuppressWarnings("unchecked") + public T convert(LineValue lineValue, Class classType) { + if (classType.isAssignableFrom(String.class)) { + return (T) lineValue.getContent(); + } + + if (classType.equals(LineValue.class)) { + return (T) lineValue; + } + + return convertLineValueToObject(lineValue, classType); + } + + private T convertLineValueToObject(LineValue lineValue, Class classType) { + T object = newInstance(classType); + Map pair = getGetterSetterPair(classType); + ReflectionObjectWriter reflectionObjectWriter = new ReflectionObjectWriter(readerConfig); + + lineValue.getColumnValues().forEach(o -> { + if (pair.containsKey(o.getColumnNumber())) { + reflectionObjectWriter.write(object, o, pair.get(o.getColumnNumber())); + } + }); + + return object; + } + + private Map getGetterSetterPair(Class clazz) { + if (!CACHE_CLASS_REFLECTIONS.containsKey(clazz)) { + ReflectionObjectReader reflectionObjectReader = new ReflectionObjectReader(); + CACHE_CLASS_REFLECTIONS.put(clazz, reflectionObjectReader.read(clazz)); + } + + return CACHE_CLASS_REFLECTIONS.get(clazz); + } + + private T newInstance(Class clazz) { + try { + return clazz.newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + throw new IllegalArgumentException(e); + } + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/parser/ObjectReader.java b/src/main/java/com/jonpereiradev/jfile/reader/converter/ReflectionObjectReader.java similarity index 71% rename from src/main/java/com/jonpereiradev/jfile/reader/parser/ObjectReader.java rename to src/main/java/com/jonpereiradev/jfile/reader/converter/ReflectionObjectReader.java index 5c83895..ace6c26 100644 --- a/src/main/java/com/jonpereiradev/jfile/reader/parser/ObjectReader.java +++ b/src/main/java/com/jonpereiradev/jfile/reader/converter/ReflectionObjectReader.java @@ -1,4 +1,27 @@ -package com.jonpereiradev.jfile.reader.parser; +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.converter; import java.lang.reflect.Field; @@ -10,7 +33,7 @@ import java.util.function.Consumer; -final class ObjectReader { +final class ReflectionObjectReader { private static final String PREFIX_GETTER_METHOD = "get"; private static final String PREFIX_SETTER_METHOD = "set"; @@ -18,7 +41,7 @@ final class ObjectReader { private Map getterSetterMapping; - public Map read(Class clazz) { + Map read(Class clazz) { getterSetterMapping = new TreeMap<>(); for (final Method method : clazz.getMethods()) { @@ -82,4 +105,5 @@ private boolean isJavaBeanSetterMethod(Method method) { private boolean isJavaBeanBooleanGetterMethod(Method method) { return method.getName().startsWith(PREFIX_IS_METHOD) && method.getParameters().length == 0; } + } diff --git a/src/main/java/com/jonpereiradev/jfile/reader/converter/ReflectionObjectWriter.java b/src/main/java/com/jonpereiradev/jfile/reader/converter/ReflectionObjectWriter.java new file mode 100644 index 0000000..72d22ff --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/converter/ReflectionObjectWriter.java @@ -0,0 +1,131 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.converter; + + +import com.jonpereiradev.jfile.reader.JFileReaderConfig; +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.lang.reflect.AccessibleObject; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.math.BigDecimal; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.time.LocalDate; +import java.time.LocalDateTime; + + +final class ReflectionObjectWriter { + + private final JFileReaderConfig readerConfig; + + ReflectionObjectWriter(JFileReaderConfig readerConfig) { + this.readerConfig = readerConfig; + } + + void write(Object instance, ColumnValue columnValue, GetterSetterPair getterSetterPair) { + try { + writeOnInstance(instance, columnValue, getterSetterPair); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + } + + private void writeOnInstance(Object instance, ColumnValue columnValue, GetterSetterPair getterSetterPair) + throws IllegalAccessException, InvocationTargetException { + if (getterSetterPair.hasGetterAndSetter()) { + Class parameterType = getterSetterPair.getSetter().getParameterTypes()[0]; + Object newObject = createObject(columnValue, getterSetterPair.getField(), parameterType); + + getterSetterPair.getSetter().invoke(instance, newObject); + } else if (getterSetterPair.getGetter() != null) { + Field field = getterSetterPair.getField(); + Object newObject = createObject(columnValue, field, getterSetterPair.getGetter().getReturnType()); + + AccessibleObject.setAccessible(new Field[]{field}, true); + field.set(instance, newObject); + AccessibleObject.setAccessible(new Field[]{field}, false); + } + } + + private Object createObject(ColumnValue columnValue, Field field, Class classType) { + if (classType == BigDecimal.class) { + return createBigDecimalObject(columnValue, field); + } + + if (classType == LocalDate.class) { + return createLocalDateObject(columnValue, field); + } + + if (classType == LocalDateTime.class) { + return createLocalDateTimeObject(columnValue, field); + } + + return columnValue.getContent(classType); + } + + private BigDecimal createBigDecimalObject(ColumnValue columnValue, Field field) { + DecimalFormat numberFormatter = readerConfig.getBigDecimalFormatter(); + + if (field.isAnnotationPresent(DecimalFormatter.class)) { + DecimalFormatter annotation = field.getAnnotation(DecimalFormatter.class); + DecimalFormat format = new DecimalFormat(annotation.value()); + DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(); + + symbols.setDecimalSeparator(annotation.decimalSeparator()); + symbols.setGroupingSeparator(annotation.groupingSeparator()); + + format.setDecimalFormatSymbols(symbols); + format.setParseBigDecimal(true); + + numberFormatter = format; + } + + return columnValue.getBigDecimal(numberFormatter); + } + + private LocalDate createLocalDateObject(ColumnValue columnValue, Field field) { + java.time.format.DateTimeFormatter formatter = readerConfig.getLocalDateFormatter(); + + if (field.isAnnotationPresent(DateTimeFormatter.class)) { + DateTimeFormatter annotation = field.getAnnotation(DateTimeFormatter.class); + formatter = java.time.format.DateTimeFormatter.ofPattern(annotation.value()); + } + + return columnValue.getLocalDate(formatter); + } + + private LocalDateTime createLocalDateTimeObject(ColumnValue columnValue, Field field) { + java.time.format.DateTimeFormatter formatter = readerConfig.getLocalDateTimeFormatter(); + + if (field.isAnnotationPresent(DateTimeFormatter.class)) { + DateTimeFormatter annotation = field.getAnnotation(DateTimeFormatter.class); + formatter = java.time.format.DateTimeFormatter.ofPattern(annotation.value()); + } + + return columnValue.getLocalDateTime(formatter); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/file/ColumnValue.java b/src/main/java/com/jonpereiradev/jfile/reader/file/ColumnValue.java new file mode 100644 index 0000000..3e0da7a --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/file/ColumnValue.java @@ -0,0 +1,150 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.file; + + +import com.jonpereiradev.jfile.reader.JFilePatternConfig; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.text.DateFormat; +import java.text.DecimalFormat; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Date; +import java.util.regex.Pattern; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface ColumnValue extends Comparable { + + static ColumnValue newColumnValue(JFilePatternConfig patternConfig, int columnNumber, String content) { + return new ColumnValueImpl(patternConfig, columnNumber, content); + } + + int getColumnNumber(); + + T getContent(Class clazz); + + String getText(); + + String[] getTextArray(); + + String[] getTextArray(Pattern splitPattern); + + Character getCharacter(); + + Character[] getCharacterArray(); + + Character[] getCharacterArray(Pattern splitPattern); + + Short getShort(); + + Short[] getShortArray(); + + Short[] getShortArray(Pattern splitPattern); + + Integer getInt(); + + Integer[] getIntArray(); + + Integer[] getIntArray(Pattern splitPattern); + + Long getLong(); + + Long[] getLongArray(); + + Long[] getLongArray(Pattern splitPattern); + + Float getFloat(); + + Float[] getFloatArray(); + + Float[] getFloatArray(Pattern splitPattern); + + Double getDouble(); + + Double[] getDoubleArray(); + + Double[] getDoubleArray(Pattern splitPattern); + + Boolean getBoolean(); + + Boolean[] getBooleanArray(); + + Boolean[] getBooleanArray(Pattern splitPattern); + + BigInteger getBigInteger(); + + BigInteger[] getBigIntegerArray(); + + BigInteger[] getBigIntegerArray(Pattern splitPattern); + + BigDecimal getBigDecimal(); + + BigDecimal getBigDecimal(DecimalFormat bigDecimalFormatter); + + BigDecimal[] getBigDecimalArray(); + + BigDecimal[] getBigDecimalArray(Pattern splitPattern); + + BigDecimal[] getBigDecimalArray(Pattern splitPattern, DecimalFormat bigDecimalFormatter); + + Date getDate(); + + Date getDate(DateFormat dateFormat); + + Date[] getDateArray(); + + Date[] getDateArray(Pattern splitPattern); + + Date[] getDateArray(Pattern splitPattern, DateFormat dateFormat); + + LocalDate getLocalDate(); + + LocalDate getLocalDate(DateTimeFormatter dateTimeFormatter); + + LocalDate[] getLocalDateArray(); + + LocalDate[] getLocalDateArray(Pattern splitPattern); + + LocalDate[] getLocalDateArray(Pattern splitPattern, DateTimeFormatter dateTimeFormatter); + + LocalDateTime getLocalDateTime(); + + LocalDateTime getLocalDateTime(DateTimeFormatter dateTimeFormatter); + + LocalDateTime[] getLocalDateTimeArray(); + + LocalDateTime[] getLocalDateTimeArray(Pattern splitPattern); + + LocalDateTime[] getLocalDateTimeArray(Pattern splitPattern, DateTimeFormatter dateTimeFormatter); + + JFilePatternConfig getPatternConfig(); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/file/ColumnValueImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/file/ColumnValueImpl.java new file mode 100644 index 0000000..03538ca --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/file/ColumnValueImpl.java @@ -0,0 +1,476 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.file; + + +import com.jonpereiradev.jfile.reader.JFilePatternConfig; +import com.jonpereiradev.jfile.reader.validator.rule.RuleUtils; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.text.DateFormat; +import java.text.DecimalFormat; +import java.text.ParseException; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Arrays; +import java.util.Date; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.function.Function; +import java.util.regex.Pattern; + + +final class ColumnValueImpl implements ColumnValue { + + private static final Pattern DEFAULT_ARRAY_SEPARATOR = Pattern.compile(",\\s*"); + private static final ConcurrentMap, Function> MAPPER = new ConcurrentHashMap<>(); + + static { + MAPPER.putIfAbsent(char.class, ColumnValueImpl::getCharacter); + MAPPER.putIfAbsent(Character.class, ColumnValueImpl::getCharacter); + MAPPER.putIfAbsent(String.class, ColumnValueImpl::getText); + MAPPER.putIfAbsent(short.class, ColumnValueImpl::getShort); + MAPPER.putIfAbsent(Short.class, ColumnValueImpl::getShort); + MAPPER.putIfAbsent(int.class, ColumnValueImpl::getInt); + MAPPER.putIfAbsent(Integer.class, ColumnValueImpl::getInt); + MAPPER.putIfAbsent(long.class, ColumnValueImpl::getLong); + MAPPER.putIfAbsent(Long.class, ColumnValueImpl::getLong); + MAPPER.putIfAbsent(float.class, ColumnValueImpl::getFloat); + MAPPER.putIfAbsent(Float.class, ColumnValueImpl::getFloat); + MAPPER.putIfAbsent(double.class, ColumnValueImpl::getDouble); + MAPPER.putIfAbsent(Double.class, ColumnValueImpl::getDouble); + MAPPER.putIfAbsent(boolean.class, ColumnValueImpl::getBoolean); + MAPPER.putIfAbsent(Boolean.class, ColumnValueImpl::getBoolean); + MAPPER.putIfAbsent(BigInteger.class, ColumnValueImpl::getBigInteger); + MAPPER.putIfAbsent(BigDecimal.class, ColumnValueImpl::getBigDecimal); + MAPPER.putIfAbsent(Date.class, ColumnValueImpl::getDate); + MAPPER.putIfAbsent(LocalDate.class, ColumnValueImpl::getLocalDate); + MAPPER.putIfAbsent(LocalDateTime.class, ColumnValueImpl::getLocalDateTime); + } + + private final JFilePatternConfig patternConfig; + + private final int columnNumber; + private final String content; + + ColumnValueImpl(JFilePatternConfig patternConfig, int columnNumber, String content) { + this.patternConfig = patternConfig; + this.columnNumber = columnNumber; + this.content = RuleUtils.trimToEmpty(content); + } + + @Override + public int getColumnNumber() { + return columnNumber; + } + + @SuppressWarnings("unchecked") + public T getContent(Class clazz) { + return (T) MAPPER.get(clazz).apply(this); + } + + @Override + public String getText() { + return content; + } + + @Override + public String[] getTextArray() { + return getTextArray(DEFAULT_ARRAY_SEPARATOR); + } + + @Override + public String[] getTextArray(Pattern splitPattern) { + return getArrayOf( + splitPattern, + array -> Arrays.stream(array).map(ColumnValueImpl::getText).toArray(String[]::new) + ); + } + + @Override + public Character getCharacter() { + if (RuleUtils.isBlank(content)) { + return null; + } + + if (content.length() > 1) { + throw new IllegalStateException("The value '" + content + "' is not parsable to Character"); + } + + return content.charAt(0); + } + + @Override + public Character[] getCharacterArray() { + return getCharacterArray(DEFAULT_ARRAY_SEPARATOR); + } + + @Override + public Character[] getCharacterArray(Pattern splitPattern) { + return getArrayOf( + splitPattern, + array -> Arrays.stream(array).map(ColumnValueImpl::getCharacter).toArray(Character[]::new) + ); + } + + @Override + public Short getShort() { + if (RuleUtils.isBlank(content)) { + return null; + } + + return Short.valueOf(content); + } + + @Override + public Short[] getShortArray() { + return getShortArray(DEFAULT_ARRAY_SEPARATOR); + } + + @Override + public Short[] getShortArray(Pattern splitPattern) { + return getArrayOf( + splitPattern, + array -> Arrays.stream(array).map(ColumnValueImpl::getShort).toArray(Short[]::new) + ); + } + + @Override + public Integer getInt() { + if (RuleUtils.isBlank(content)) { + return null; + } + + return Integer.valueOf(content); + } + + @Override + public Integer[] getIntArray() { + return getIntArray(DEFAULT_ARRAY_SEPARATOR); + } + + @Override + public Integer[] getIntArray(Pattern splitPattern) { + return getArrayOf( + splitPattern, + array -> Arrays.stream(array).map(ColumnValueImpl::getInt).toArray(Integer[]::new) + ); + } + + @Override + public Long getLong() { + if (RuleUtils.isBlank(content)) { + return null; + } + + return Long.valueOf(content); + } + + @Override + public Long[] getLongArray() { + return getLongArray(DEFAULT_ARRAY_SEPARATOR); + } + + @Override + public Long[] getLongArray(Pattern splitPattern) { + return getArrayOf( + splitPattern, + array -> Arrays.stream(array).map(ColumnValueImpl::getLong).toArray(Long[]::new) + ); + } + + @Override + public Float getFloat() { + if (RuleUtils.isBlank(content)) { + return null; + } + + return Float.valueOf(content); + } + + @Override + public Float[] getFloatArray() { + return getFloatArray(DEFAULT_ARRAY_SEPARATOR); + } + + @Override + public Float[] getFloatArray(Pattern splitPattern) { + return getArrayOf( + splitPattern, + array -> Arrays.stream(array).map(ColumnValueImpl::getFloat).toArray(Float[]::new) + ); + } + + @Override + public Double getDouble() { + if (RuleUtils.isBlank(content)) { + return null; + } + + return Double.valueOf(content); + } + + @Override + public Double[] getDoubleArray() { + return getDoubleArray(DEFAULT_ARRAY_SEPARATOR); + } + + @Override + public Double[] getDoubleArray(Pattern splitPattern) { + return getArrayOf( + splitPattern, + array -> Arrays.stream(array).map(ColumnValueImpl::getDouble).toArray(Double[]::new) + ); + } + + @Override + public Boolean getBoolean() { + if (RuleUtils.isBlank(content)) { + return null; + } + + String booleanString = content; + + if (booleanString.equals("0")) { + booleanString = "false"; + } else if (booleanString.equals("1")) { + booleanString = "true"; + } + + return RuleUtils.toBooleanObject(booleanString); + } + + @Override + public Boolean[] getBooleanArray() { + return getBooleanArray(DEFAULT_ARRAY_SEPARATOR); + } + + @Override + public Boolean[] getBooleanArray(Pattern splitPattern) { + return getArrayOf( + splitPattern, + array -> Arrays.stream(array).map(ColumnValueImpl::getBoolean).toArray(Boolean[]::new) + ); + } + + @Override + public BigInteger getBigInteger() { + if (RuleUtils.isBlank(content)) { + return null; + } + + return new BigInteger(content); + } + + @Override + public BigInteger[] getBigIntegerArray() { + return getBigIntegerArray(DEFAULT_ARRAY_SEPARATOR); + } + + @Override + public BigInteger[] getBigIntegerArray(Pattern splitPattern) { + return getArrayOf( + splitPattern, + array -> Arrays.stream(array).map(ColumnValueImpl::getBigInteger).toArray(BigInteger[]::new) + ); + } + + @Override + public BigDecimal getBigDecimal() { + return getBigDecimal(patternConfig.getBigDecimalFormatter()); + } + + @Override + public BigDecimal getBigDecimal(DecimalFormat bigDecimalFormatter) { + if (RuleUtils.isBlank(content)) { + return null; + } + + try { + return (BigDecimal) bigDecimalFormatter.parse(content); + } catch (ParseException e) { + throw new NumberFormatException( + "The value '" + content + "' is not parsable to BigDecimal with current DecimalFormat" + ); + } + } + + @Override + public BigDecimal[] getBigDecimalArray() { + return getBigDecimalArray(DEFAULT_ARRAY_SEPARATOR); + } + + @Override + public BigDecimal[] getBigDecimalArray(Pattern splitPattern) { + return getBigDecimalArray(splitPattern, patternConfig.getBigDecimalFormatter()); + } + + @Override + public BigDecimal[] getBigDecimalArray(Pattern splitPattern, DecimalFormat bigDecimalFormatter) { + return getArrayOf( + splitPattern, + array -> Arrays.stream(array).map(o -> o.getBigDecimal(bigDecimalFormatter)).toArray(BigDecimal[]::new) + ); + } + + @Override + public Date getDate() { + return getDate(patternConfig.getDateFormat()); + } + + @Override + public Date getDate(DateFormat dateFormat) { + if (RuleUtils.isBlank(content)) { + return null; + } + + try { + return dateFormat.parse(content); + } catch (ParseException e) { + throw new IllegalStateException( + "The value '" + content + "' is not parsable to Date with current DateFormat" + ); + } + } + + @Override + public Date[] getDateArray() { + return getDateArray(DEFAULT_ARRAY_SEPARATOR); + } + + @Override + public Date[] getDateArray(Pattern splitPattern) { + return getDateArray(splitPattern, patternConfig.getDateFormat()); + } + + @Override + public Date[] getDateArray(Pattern splitPattern, DateFormat dateFormat) { + return getArrayOf( + splitPattern, + array -> Arrays.stream(array).map(o -> o.getDate(dateFormat)).toArray(Date[]::new) + ); + } + + @Override + public LocalDate getLocalDate() { + return getLocalDate(patternConfig.getLocalDateFormatter()); + } + + @Override + public LocalDate getLocalDate(DateTimeFormatter dateTimeFormatter) { + if (RuleUtils.isBlank(content)) { + return null; + } + + return LocalDate.parse(content, dateTimeFormatter); + } + + @Override + public LocalDate[] getLocalDateArray() { + return getLocalDateArray(DEFAULT_ARRAY_SEPARATOR); + } + + @Override + public LocalDate[] getLocalDateArray(Pattern splitPattern) { + return getLocalDateArray(splitPattern, patternConfig.getLocalDateFormatter()); + } + + @Override + public LocalDate[] getLocalDateArray(Pattern splitPattern, DateTimeFormatter dateTimeFormatter) { + return getArrayOf( + splitPattern, + array -> Arrays.stream(array).map(o -> o.getLocalDate(dateTimeFormatter)).toArray(LocalDate[]::new) + ); + } + + @Override + public LocalDateTime getLocalDateTime() { + return getLocalDateTime(patternConfig.getLocalDateTimeFormatter()); + } + + @Override + public LocalDateTime getLocalDateTime(DateTimeFormatter dateTimeFormatter) { + if (RuleUtils.isBlank(content)) { + return null; + } + + return LocalDateTime.parse(content, dateTimeFormatter); + } + + @Override + public LocalDateTime[] getLocalDateTimeArray() { + return getLocalDateTimeArray(DEFAULT_ARRAY_SEPARATOR); + } + + @Override + public LocalDateTime[] getLocalDateTimeArray(Pattern splitPattern) { + return getLocalDateTimeArray(splitPattern, patternConfig.getLocalDateTimeFormatter()); + } + + @Override + public LocalDateTime[] getLocalDateTimeArray(Pattern splitPattern, DateTimeFormatter dateTimeFormatter) { + return getArrayOf( + splitPattern, + array -> Arrays.stream(array).map(o -> o.getLocalDateTime(dateTimeFormatter)).toArray(LocalDateTime[]::new) + ); + } + + @Override + public JFilePatternConfig getPatternConfig() { + return patternConfig; + } + + private T[] getArrayOf(Pattern splitPattern, Function function) { + String[] split = splitPattern.split(content); + + ColumnValueImpl[] columnValueStream = Arrays + .stream(split) + .map(content -> new ColumnValueImpl(patternConfig, columnNumber, content)) + .toArray(ColumnValueImpl[]::new); + + return function.apply(columnValueStream); + } + + @Override + public int compareTo(ColumnValue o) { + return Integer.compare(columnNumber, o.getColumnNumber()); + } + + @Override + public int hashCode() { + return Objects.hash(columnNumber); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ColumnValueImpl that = (ColumnValueImpl) o; + return columnNumber == that.columnNumber; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/file/JFileColumn.java b/src/main/java/com/jonpereiradev/jfile/reader/file/JFileColumn.java deleted file mode 100644 index fdd33a2..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/file/JFileColumn.java +++ /dev/null @@ -1,382 +0,0 @@ -package com.jonpereiradev.jfile.reader.file; - -import com.jonpereiradev.jfile.reader.JFileReaderContext; -import org.apache.commons.lang3.BooleanUtils; -import org.apache.commons.lang3.StringUtils; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.text.DateFormat; -import java.text.DecimalFormat; -import java.text.ParseException; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.time.format.DateTimeParseException; -import java.util.Arrays; -import java.util.Date; -import java.util.Objects; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.function.Function; -import java.util.regex.Pattern; - -public class JFileColumn implements Comparable { - - private static final Pattern DEFAULT_ARRAY_SEPARATOR = Pattern.compile(",\\s*"); - private static final ConcurrentMap, Function> MAPPER = new ConcurrentHashMap<>(); - - static { - MAPPER.putIfAbsent(char.class, JFileColumn::getCharacter); - MAPPER.putIfAbsent(Character.class, JFileColumn::getCharacter); - MAPPER.putIfAbsent(String.class, JFileColumn::getText); - MAPPER.putIfAbsent(short.class, JFileColumn::getShort); - MAPPER.putIfAbsent(Short.class, JFileColumn::getShort); - MAPPER.putIfAbsent(int.class, JFileColumn::getInt); - MAPPER.putIfAbsent(Integer.class, JFileColumn::getInt); - MAPPER.putIfAbsent(long.class, JFileColumn::getLong); - MAPPER.putIfAbsent(Long.class, JFileColumn::getLong); - MAPPER.putIfAbsent(float.class, JFileColumn::getFloat); - MAPPER.putIfAbsent(Float.class, JFileColumn::getFloat); - MAPPER.putIfAbsent(double.class, JFileColumn::getDouble); - MAPPER.putIfAbsent(Double.class, JFileColumn::getDouble); - MAPPER.putIfAbsent(boolean.class, JFileColumn::getBoolean); - MAPPER.putIfAbsent(Boolean.class, JFileColumn::getBoolean); - MAPPER.putIfAbsent(BigInteger.class, JFileColumn::getBigInteger); - MAPPER.putIfAbsent(BigDecimal.class, JFileColumn::getBigDecimal); - MAPPER.putIfAbsent(Date.class, JFileColumn::getDate); - MAPPER.putIfAbsent(LocalDate.class, JFileColumn::getLocalDate); - MAPPER.putIfAbsent(LocalDateTime.class, JFileColumn::getLocalDateTime); - } - - private final JFileReaderContext context; - - private final int position; - private final String content; - - public JFileColumn(JFileReaderContext context, int position, String content) { - this.context = context; - this.position = position; - this.content = StringUtils.trimToEmpty(content); - } - - public int getPosition() { - return position; - } - - @SuppressWarnings("unchecked") - public T getContent(Class clazz) { - return (T) MAPPER.get(clazz).apply(this); - } - - public String getText() { - return content; - } - - public String[] getTextArray() { - return getTextArray(DEFAULT_ARRAY_SEPARATOR); - } - - public String[] getTextArray(Pattern pattern) { - return getArrayOf(pattern, array -> Arrays.stream(array).map(JFileColumn::getText).toArray(String[]::new)); - } - - public Character getCharacter() { - if (StringUtils.isBlank(content) || content.length() > 1) { - return null; - } - - return content.charAt(0); - } - - public Character[] getCharacterArray() { - return getCharacterArray(DEFAULT_ARRAY_SEPARATOR); - } - - public Character[] getCharacterArray(Pattern pattern) { - return getArrayOf(pattern, array -> Arrays.stream(array).map(JFileColumn::getCharacter).toArray(Character[]::new)); - } - - public Short getShort() { - if (StringUtils.isBlank(content)) { - return null; - } - - try { - return Short.valueOf(content); - } catch (NumberFormatException e) { - return null; - } - } - - public Short[] getShortArray() { - return getShortArray(DEFAULT_ARRAY_SEPARATOR); - } - - public Short[] getShortArray(Pattern pattern) { - return getArrayOf(pattern, array -> Arrays.stream(array).map(JFileColumn::getShort).toArray(Short[]::new)); - } - - public Integer getInt() { - if (StringUtils.isBlank(content)) { - return null; - } - - try { - return Integer.valueOf(content); - } catch (NumberFormatException e) { - return null; - } - } - - public Integer[] getIntArray() { - return getIntArray(DEFAULT_ARRAY_SEPARATOR); - } - - public Integer[] getIntArray(Pattern pattern) { - return getArrayOf(pattern, array -> Arrays.stream(array).map(JFileColumn::getInt).toArray(Integer[]::new)); - } - - public Long getLong() { - if (StringUtils.isBlank(content)) { - return null; - } - - try { - return Long.valueOf(content); - } catch (NumberFormatException e) { - return null; - } - } - - public Long[] getLongArray() { - return getLongArray(DEFAULT_ARRAY_SEPARATOR); - } - - public Long[] getLongArray(Pattern pattern) { - return getArrayOf(pattern, array -> Arrays.stream(array).map(JFileColumn::getLong).toArray(Long[]::new)); - } - - public Float getFloat() { - if (StringUtils.isBlank(content)) { - return null; - } - - try { - return Float.valueOf(content); - } catch (NumberFormatException e) { - return null; - } - } - - public Float[] getFloatArray() { - return getFloatArray(DEFAULT_ARRAY_SEPARATOR); - } - - public Float[] getFloatArray(Pattern pattern) { - return getArrayOf(pattern, array -> Arrays.stream(array).map(JFileColumn::getFloat).toArray(Float[]::new)); - } - - public Double getDouble() { - if (StringUtils.isBlank(content)) { - return null; - } - - try { - return Double.valueOf(content); - } catch (NumberFormatException e) { - return null; - } - } - - public Double[] getDoubleArray() { - return getDoubleArray(DEFAULT_ARRAY_SEPARATOR); - } - - public Double[] getDoubleArray(Pattern pattern) { - return getArrayOf(pattern, array -> Arrays.stream(array).map(JFileColumn::getDouble).toArray(Double[]::new)); - } - - public Boolean getBoolean() { - if (StringUtils.isBlank(content)) { - return null; - } - - String booleanString = content; - - if (booleanString.equals("0")) { - booleanString = "false"; - } else if (booleanString.equals("1")) { - booleanString = "true"; - } - - return BooleanUtils.toBooleanObject(booleanString); - } - - public Boolean[] getBooleanArray() { - return getBooleanArray(DEFAULT_ARRAY_SEPARATOR); - } - - public Boolean[] getBooleanArray(Pattern pattern) { - return getArrayOf(pattern, array -> Arrays.stream(array).map(JFileColumn::getBoolean).toArray(Boolean[]::new)); - } - - public BigInteger getBigInteger() { - if (StringUtils.isBlank(content)) { - return null; - } - - try { - return new BigInteger(content); - } catch (NumberFormatException e) { - return null; - } - } - - public BigInteger[] getBigIntegerArray() { - return getBigIntegerArray(DEFAULT_ARRAY_SEPARATOR); - } - - public BigInteger[] getBigIntegerArray(Pattern pattern) { - return getArrayOf(pattern, array -> Arrays.stream(array).map(JFileColumn::getBigInteger).toArray(BigInteger[]::new)); - } - - public BigDecimal getBigDecimal() { - return getBigDecimal(context.getBigDecimalFormatter()); - } - - public BigDecimal getBigDecimal(DecimalFormat bigDecimalFormatter) { - if (StringUtils.isBlank(content)) { - return null; - } - - try { - return (BigDecimal) bigDecimalFormatter.parse(content); - } catch (ParseException e) { - return null; - } - } - - public BigDecimal[] getBigDecimalArray() { - return getBigDecimalArray(DEFAULT_ARRAY_SEPARATOR); - } - - public BigDecimal[] getBigDecimalArray(Pattern pattern) { - return getBigDecimalArray(pattern, context.getBigDecimalFormatter()); - } - - public BigDecimal[] getBigDecimalArray(Pattern pattern, DecimalFormat bigDecimalFormatter) { - return getArrayOf(pattern, array -> Arrays.stream(array).map(o -> o.getBigDecimal(bigDecimalFormatter)).toArray(BigDecimal[]::new)); - } - - public Date getDate() { - return getDate(context.getDateFormat()); - } - - public Date getDate(DateFormat pattern) { - if (StringUtils.isBlank(content)) { - return null; - } - - try { - return pattern.parse(content); - } catch (ParseException e) { - return null; - } - } - - public Date[] getDateArray() { - return getDateArray(DEFAULT_ARRAY_SEPARATOR); - } - - public Date[] getDateArray(Pattern pattern) { - return getDateArray(pattern, context.getDateFormat()); - } - - public Date[] getDateArray(Pattern pattern, DateFormat dateFormat) { - return getArrayOf(pattern, array -> Arrays.stream(array).map(o -> o.getDate(dateFormat)).toArray(Date[]::new)); - } - - public LocalDate getLocalDate() { - return getLocalDate(context.getLocalDateFormatter()); - } - - public LocalDate getLocalDate(DateTimeFormatter dateTimeFormatter) { - if (StringUtils.isBlank(content)) { - return null; - } - - try { - return LocalDate.parse(content, dateTimeFormatter); - } catch (DateTimeParseException e) { - return null; - } - } - - public LocalDate[] getLocalDateArray() { - return getLocalDateArray(DEFAULT_ARRAY_SEPARATOR); - } - - public LocalDate[] getLocalDateArray(Pattern pattern) { - return getLocalDateArray(pattern, context.getLocalDateFormatter()); - } - - public LocalDate[] getLocalDateArray(Pattern pattern, DateTimeFormatter dateTimeFormatter) { - return getArrayOf(pattern, array -> Arrays.stream(array).map(o -> o.getLocalDate(dateTimeFormatter)).toArray(LocalDate[]::new)); - } - - public LocalDateTime getLocalDateTime() { - return getLocalDateTime(context.getLocalDateTimeFormatter()); - } - - public LocalDateTime getLocalDateTime(DateTimeFormatter dateTimeFormatter) { - if (StringUtils.isBlank(content)) { - return null; - } - - try { - return LocalDateTime.parse(content, dateTimeFormatter); - } catch (DateTimeParseException e) { - return null; - } - } - - public LocalDateTime[] getLocalDateTimeArray() { - return getLocalDateTimeArray(DEFAULT_ARRAY_SEPARATOR); - } - - public LocalDateTime[] getLocalDateTimeArray(Pattern pattern) { - return getLocalDateTimeArray(pattern, context.getLocalDateTimeFormatter()); - } - - public LocalDateTime[] getLocalDateTimeArray(Pattern pattern, DateTimeFormatter dateTimeFormatter) { - return getArrayOf(pattern, array -> Arrays.stream(array).map(o -> o.getLocalDateTime(dateTimeFormatter)).toArray(LocalDateTime[]::new)); - } - - private T[] getArrayOf(Pattern pattern, Function function) { - String[] split = pattern.split(content); - return function.apply(Arrays.stream(split).map(s -> new JFileColumn(context, position, s)).toArray(JFileColumn[]::new)); - } - - public JFileReaderContext getContext() { - return context; - } - - @Override - public int compareTo(JFileColumn o) { - return Integer.compare(position, o.position); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - JFileColumn that = (JFileColumn) o; - return position == that.position; - } - - @Override - public int hashCode() { - return Objects.hash(position); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/file/JFileLine.java b/src/main/java/com/jonpereiradev/jfile/reader/file/JFileLine.java deleted file mode 100644 index 7ab81bc..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/file/JFileLine.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.jonpereiradev.jfile.reader.file; - -import java.util.Collections; -import java.util.Objects; -import java.util.SortedSet; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.stream.Stream; - -public class JFileLine implements Comparable { - - private final ConcurrentMap columnsByPosition = new ConcurrentHashMap<>(); - - private final int row; - private final String content; - private final SortedSet columns; - - public JFileLine(int row, String content, SortedSet columns) { - this.row = row; - this.content = content; - this.columns = Collections.unmodifiableSortedSet(columns); - } - - @Override - public int compareTo(JFileLine o) { - return Integer.compare(row, o.row); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - JFileLine jFileLine = (JFileLine) o; - return row == jFileLine.row; - } - - @Override - public int hashCode() { - return Objects.hash(row); - } - - public JFileColumn getColumn(int position) { - if (!columnsByPosition.containsKey(position)) { - Stream stream = getColumns().stream().filter(o -> o.getPosition() == position); - JFileColumn column = stream.findFirst().orElseThrow(() -> new IllegalArgumentException("Position doesn't exists in line.")); - columnsByPosition.put(position, column); - } - - return columnsByPosition.get(position); - } - - public int getRow() { - return row; - } - - public String getContent() { - return content; - } - - public SortedSet getColumns() { - return columns; - } - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/file/LineValue.java b/src/main/java/com/jonpereiradev/jfile/reader/file/LineValue.java new file mode 100644 index 0000000..c115caa --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/file/LineValue.java @@ -0,0 +1,48 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.file; + + +import java.util.SortedSet; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface LineValue extends Comparable { + + static LineValueImpl newLineValue(int lineNumber, String content, SortedSet columnValues) { + return new LineValueImpl(lineNumber, content, columnValues); + } + + int getLineNumber(); + + String getContent(); + + ColumnValue getColumnValue(int columnNumber); + + SortedSet getColumnValues(); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/file/LineValueImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/file/LineValueImpl.java new file mode 100644 index 0000000..181c52b --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/file/LineValueImpl.java @@ -0,0 +1,100 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.file; + + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.SortedSet; +import java.util.stream.Stream; + + +final class LineValueImpl implements LineValue { + + private static final String POSITION_ERROR = "Position doesn't exists in line."; + + private final Map columnsByPosition = new HashMap<>(); + + private final int lineNumber; + private final String content; + private final SortedSet columnValues; + + LineValueImpl(int lineNumber, String content, SortedSet columnValues) { + this.lineNumber = lineNumber; + this.content = content; + this.columnValues = Collections.unmodifiableSortedSet(columnValues); + } + + @Override + public int compareTo(LineValue o) { + return Integer.compare(lineNumber, o.getLineNumber()); + } + + @Override + public int hashCode() { + return Objects.hash(lineNumber); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + LineValueImpl lineValue = (LineValueImpl) o; + return lineNumber == lineValue.lineNumber; + } + + @Override + public String toString() { + return "[lineNumber=" + lineNumber + ", content=" + content + "]"; + } + + @Override + public int getLineNumber() { + return lineNumber; + } + + @Override + public String getContent() { + return content; + } + + @Override + public ColumnValue getColumnValue(int columnNumber) { + if (!columnsByPosition.containsKey(columnNumber)) { + Stream stream = getColumnValues().stream().filter(o -> o.getColumnNumber() == columnNumber); + ColumnValue column = stream.findFirst().orElseThrow(() -> new IllegalArgumentException(POSITION_ERROR)); + columnsByPosition.put(columnNumber, column); + } + + return columnsByPosition.get(columnNumber); + } + + @Override + public SortedSet getColumnValues() { + return columnValues; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/parser/DateTimeFormatter.java b/src/main/java/com/jonpereiradev/jfile/reader/parser/DateTimeFormatter.java deleted file mode 100644 index dc08a30..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/parser/DateTimeFormatter.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.jonpereiradev.jfile.reader.parser; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target(ElementType.FIELD) -@Retention(RetentionPolicy.RUNTIME) -public @interface DateTimeFormatter { - - String value(); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/parser/DecimalFormatter.java b/src/main/java/com/jonpereiradev/jfile/reader/parser/DecimalFormatter.java deleted file mode 100644 index d6c1017..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/parser/DecimalFormatter.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.jonpereiradev.jfile.reader.parser; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target(ElementType.FIELD) -@Retention(RetentionPolicy.RUNTIME) -public @interface DecimalFormatter { - - String value(); - - char groupingSeparator() default '.'; - - char decimalSeparator() default ','; - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/parser/DefaultFileLineParser.java b/src/main/java/com/jonpereiradev/jfile/reader/parser/DefaultFileLineParser.java deleted file mode 100644 index 6124671..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/parser/DefaultFileLineParser.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.jonpereiradev.jfile.reader.parser; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.file.JFileLine; - -import java.util.HashMap; -import java.util.Map; - -final class DefaultFileLineParser implements FileLineParser { - - private static final Map, Map> CACHE_CLASS_REFLECTIONS = new HashMap<>(); - - private final ReaderConfiguration configuration; - - DefaultFileLineParser(ReaderConfiguration configuration) { - this.configuration = configuration; - } - - @Override - public T parse(JFileLine fileLine, Class clazz) { - T object = newInstance(clazz); - Map pair = getGetterSetterPair(clazz); - - ObjectWriter objectWriter = new ObjectWriter(configuration); - - fileLine.getColumns().forEach(o -> { - if (pair.containsKey(o.getPosition())) { - objectWriter.write(object, o, pair.get(o.getPosition())); - } - }); - - return object; - } - - private Map getGetterSetterPair(Class clazz) { - if (!CACHE_CLASS_REFLECTIONS.containsKey(clazz)) { - ObjectReader objectReader = new ObjectReader(); - CACHE_CLASS_REFLECTIONS.put(clazz, objectReader.read(clazz)); - } - - return CACHE_CLASS_REFLECTIONS.get(clazz); - } - - private T newInstance(Class clazz) { - try { - return clazz.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { - throw new IllegalArgumentException(e); - } - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/parser/FileColumn.java b/src/main/java/com/jonpereiradev/jfile/reader/parser/FileColumn.java deleted file mode 100644 index d55441f..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/parser/FileColumn.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.jonpereiradev.jfile.reader.parser; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target(ElementType.FIELD) -@Retention(RetentionPolicy.RUNTIME) -public @interface FileColumn { - - int value(); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/parser/FileLineParser.java b/src/main/java/com/jonpereiradev/jfile/reader/parser/FileLineParser.java deleted file mode 100644 index 0f3e57d..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/parser/FileLineParser.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.jonpereiradev.jfile.reader.parser; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.file.JFileLine; - -public interface FileLineParser { - - static FileLineParser defaultParser(ReaderConfiguration configuration) { - return new DefaultFileLineParser(configuration); - } - - T parse(JFileLine fileLine, Class clazz); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/parser/GetterSetterPair.java b/src/main/java/com/jonpereiradev/jfile/reader/parser/GetterSetterPair.java deleted file mode 100644 index 5179328..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/parser/GetterSetterPair.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.jonpereiradev.jfile.reader.parser; - - -import java.lang.reflect.Field; -import java.lang.reflect.Method; - - -final class GetterSetterPair { - - private Field field; - private Method getter; - private Method setter; - private FileColumn annotation; - - Field getField() { - return field; - } - - void setField(Field field) { - this.field = field; - } - - Method getGetter() { - return getter; - } - - void setGetter(Method getter) { - this.getter = getter; - } - - Method getSetter() { - return setter; - } - - void setSetter(Method setter) { - this.setter = setter; - } - - FileColumn getAnnotation() { - return annotation; - } - - void setAnnotation(FileColumn annotation) { - this.annotation = annotation; - } - - boolean hasGetterAndSetter() { - return getter != null && setter != null; - } - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/parser/ObjectWriter.java b/src/main/java/com/jonpereiradev/jfile/reader/parser/ObjectWriter.java deleted file mode 100644 index 46750c0..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/parser/ObjectWriter.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.jonpereiradev.jfile.reader.parser; - - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.math.BigDecimal; -import java.text.DecimalFormat; -import java.text.DecimalFormatSymbols; -import java.time.LocalDate; -import java.time.LocalDateTime; - - -final class ObjectWriter { - - private final ReaderConfiguration configuration; - - ObjectWriter(ReaderConfiguration configuration) { - this.configuration = configuration; - } - - public void write(Object instance, JFileColumn fileColumn, GetterSetterPair getterSetterPair) { - try { - writeOnInstance(instance, fileColumn, getterSetterPair); - } catch (IllegalAccessException | InvocationTargetException e) { - throw new RuntimeException(e); - } - } - - private void writeOnInstance(Object instance, JFileColumn fileColumn, GetterSetterPair getterSetterPair) - throws IllegalAccessException, InvocationTargetException { - if (getterSetterPair.hasGetterAndSetter()) { - Class parameterType = getterSetterPair.getSetter().getParameterTypes()[0]; - Object newObject = createObject(fileColumn, getterSetterPair.getField(), parameterType); - - getterSetterPair.getSetter().invoke(instance, newObject); - } else if (getterSetterPair.getGetter() != null) { - Field field = getterSetterPair.getField(); - Object newObject = createObject(fileColumn, field, getterSetterPair.getGetter().getReturnType()); - - field.setAccessible(true); - field.set(instance, newObject); - field.setAccessible(false); - } - } - - private Object createObject(JFileColumn fileColumn, Field field, Class clazz) { - if (clazz == BigDecimal.class) { - return createBigDecimalObject(fileColumn, field); - } - - if (clazz == LocalDate.class) { - return createLocalDateObject(fileColumn, field); - } - - if (clazz == LocalDateTime.class) { - return createLocalDateTimeObject(fileColumn, field); - } - - return fileColumn.getContent(clazz); - } - - private BigDecimal createBigDecimalObject(JFileColumn fileColumn, Field field) { - DecimalFormat numberFormatter = configuration.getBigDecimalFormatter(); - - if (field.isAnnotationPresent(DecimalFormatter.class)) { - DecimalFormatter annotation = field.getAnnotation(DecimalFormatter.class); - DecimalFormat format = new DecimalFormat(annotation.value()); - DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(); - - symbols.setDecimalSeparator(annotation.decimalSeparator()); - symbols.setGroupingSeparator(annotation.groupingSeparator()); - - format.setDecimalFormatSymbols(symbols); - format.setParseBigDecimal(true); - - numberFormatter = format; - } - - return fileColumn.getBigDecimal(numberFormatter); - } - - private LocalDate createLocalDateObject(JFileColumn fileColumn, Field field) { - java.time.format.DateTimeFormatter formatter = configuration.getLocalDateFormatter(); - - if (field.isAnnotationPresent(DateTimeFormatter.class)) { - DateTimeFormatter annotation = field.getAnnotation(DateTimeFormatter.class); - formatter = java.time.format.DateTimeFormatter.ofPattern(annotation.value()); - } - - return fileColumn.getLocalDate(formatter); - } - - private LocalDateTime createLocalDateTimeObject(JFileColumn fileColumn, Field field) { - java.time.format.DateTimeFormatter formatter = configuration.getLocalDateTimeFormatter(); - - if (field.isAnnotationPresent(DateTimeFormatter.class)) { - DateTimeFormatter annotation = field.getAnnotation(DateTimeFormatter.class); - formatter = java.time.format.DateTimeFormatter.ofPattern(annotation.value()); - } - - return fileColumn.getLocalDateTime(formatter); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/Rule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/Rule.java deleted file mode 100644 index 9d10e38..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/Rule.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule; - -public interface Rule { - - boolean isValid(T object); - - boolean canValidate(T object); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleConfiguration.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleConfiguration.java deleted file mode 100644 index 3670155..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleConfiguration.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule; - -import com.jonpereiradev.jfile.reader.rule.column.ColumnRule; -import com.jonpereiradev.jfile.reader.rule.file.FileRule; -import com.jonpereiradev.jfile.reader.rule.line.LineRule; - -public interface RuleConfiguration { - - RuleNode getFileRootNode(); - - RuleNode getLineRootNode(); - - RuleNode getColumnRootNode(); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleConfigurationImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleConfigurationImpl.java deleted file mode 100644 index 87d78e0..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleConfigurationImpl.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule; - -import com.jonpereiradev.jfile.reader.rule.column.ColumnRule; -import com.jonpereiradev.jfile.reader.rule.file.FileRule; -import com.jonpereiradev.jfile.reader.rule.line.LineRule; - -final class RuleConfigurationImpl implements RuleConfiguration { - - private final RuleNode fileRuleNode = new RuleNodeImpl<>(null, null); - private final RuleNode lineRuleNode = new RuleNodeImpl<>(null, null); - private final RuleNode columnRuleNode = new RuleNodeImpl<>(null, null); - - @Override - public RuleNode getFileRootNode() { - return fileRuleNode; - } - - @Override - public RuleNode getLineRootNode() { - return lineRuleNode; - } - - @Override - public RuleNode getColumnRootNode() { - return columnRuleNode; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleConfigurator.java deleted file mode 100644 index aff6cc1..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleConfigurator.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.rule.configurator.FileRuleConfigurator; - -public interface RuleConfigurator { - - static RuleConfigurator defaultConfigurator(ReaderConfiguration readerConfiguration) { - readerConfiguration.withRuleConfiguration(new RuleConfigurationImpl()); - return new RuleConfiguratorImpl(readerConfiguration); - } - - FileRuleConfigurator files(); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleConfiguratorImpl.java deleted file mode 100644 index e58e5fc..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleConfiguratorImpl.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.rule.configurator.FileRuleConfigurator; - -final class RuleConfiguratorImpl implements RuleConfigurator { - - private final ReaderConfiguration configuration; - - RuleConfiguratorImpl(ReaderConfiguration configuration) { - this.configuration = configuration; - } - - @Override - public FileRuleConfigurator files() { - return FileRuleConfigurator.defaultConfigurator(configuration); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleNode.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleNode.java deleted file mode 100644 index e15e468..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleNode.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule; - -import java.util.List; - -public interface RuleNode> extends Iterable { - - Class getType(); - - void add(T rule); - - RuleNode getParentNode(); - - List getChildrens(); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleNodeImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleNodeImpl.java deleted file mode 100644 index c3c987d..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleNodeImpl.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; - -public class RuleNodeImpl> implements RuleNode { - - private final Class type; - private final RuleNode parentNode; - private final List rules = new ArrayList<>(); - - public RuleNodeImpl(Class type, RuleNode parentNode) { - this.type = type; - this.parentNode = parentNode; - } - - @Override - public void add(T rule) { - this.rules.add(rule); - } - - @Override - public Class getType() { - return type; - } - - @Override - public Iterator iterator() { - return rules.iterator(); - } - - @Override - public RuleNode getParentNode() { - return parentNode; - } - - @Override - public List getChildrens() { - return Collections.unmodifiableList(rules); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleViolation.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleViolation.java deleted file mode 100644 index e18d24b..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/RuleViolation.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule; - -import java.util.Objects; - -public final class RuleViolation { - - private int row; - private int column; - private String content; - private String rule; - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - RuleViolation that = (RuleViolation) o; - return row == that.row && - column == that.column && - Objects.equals(rule, that.rule); - } - - @Override - public int hashCode() { - return Objects.hash(row, column, rule); - } - - public int getRow() { - return row; - } - - public void setRow(int row) { - this.row = row; - } - - public int getColumn() { - return column; - } - - public void setColumn(int column) { - this.column = column; - } - - public String getContent() { - return content; - } - - public void setContent(String content) { - this.content = content; - } - - public String getRule() { - return rule; - } - - public void setRule(String rule) { - this.rule = rule; - } - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/AbstractColumnRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/AbstractColumnRule.java deleted file mode 100644 index 7d98f5b..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/AbstractColumnRule.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; -import com.jonpereiradev.jfile.reader.file.JFileLine; -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import org.apache.commons.lang3.StringUtils; - -public abstract class AbstractColumnRule implements ColumnRule { - - private final int position; - - private JFileLine fileLine; - private RuleNode ruleNode; - - public AbstractColumnRule(int position) { - this.position = position; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return StringUtils.isNotBlank(fileColumn.getText()); - } - - @Override - public int getPosition() { - return position; - } - - @Override - public JFileLine getFileLine() { - return fileLine; - } - - @Override - public void setFileLine(JFileLine fileLine) { - this.fileLine = fileLine; - } - - @Override - public RuleNode getRuleNode() { - return ruleNode; - } - - @Override - public void setRuleNode(RuleNode ruleNode) { - this.ruleNode = ruleNode; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/AbstractRefRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/AbstractRefRule.java deleted file mode 100644 index 53ad468..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/AbstractRefRule.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; -import com.jonpereiradev.jfile.reader.file.JFileLine; -import com.jonpereiradev.jfile.reader.rule.RuleNode; - -public abstract class AbstractRefRule implements RefRule { - - private final int refPosition; - private final int position; - - private JFileLine fileLine; - private RuleNode ruleNode; - - public AbstractRefRule(int refPosition, int position) { - this.refPosition = refPosition; - this.position = position; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return true; - } - - @Override - public int getRefPosition() { - return refPosition; - } - - @Override - public int getPosition() { - return position; - } - - @Override - public JFileLine getFileLine() { - return fileLine; - } - - @Override - public void setFileLine(JFileLine fileLine) { - this.fileLine = fileLine; - } - - @Override - public RuleNode getRuleNode() { - return ruleNode; - } - - @Override - public void setRuleNode(RuleNode ruleNode) { - this.ruleNode = ruleNode; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/ArrayOfTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/ArrayOfTypeRule.java deleted file mode 100644 index b4f74ad..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/ArrayOfTypeRule.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.util.Arrays; -import java.util.List; -import java.util.regex.Pattern; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -public class ArrayOfTypeRule extends AbstractColumnRule { - - private final Pattern pattern; - - public ArrayOfTypeRule(int position, Pattern pattern) { - super(position); - this.pattern = pattern; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return true; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return true; - } - - public List split(JFileColumn fileColumn) { - String[] values = pattern.split(fileColumn.getText()); - Stream stream = Arrays.stream(values).map(o -> new JFileColumn(fileColumn.getContext(), getPosition(), o)); - return stream.collect(Collectors.toList()); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/BigDecimalTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/BigDecimalTypeRule.java deleted file mode 100644 index 8baf06d..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/BigDecimalTypeRule.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.text.DecimalFormat; - -public class BigDecimalTypeRule extends AbstractColumnRule { - - private final DecimalFormat decimalFormat; - - public BigDecimalTypeRule(int position, DecimalFormat decimalFormat) { - super(position); - this.decimalFormat = decimalFormat; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getText().isEmpty() || fileColumn.getBigDecimal(decimalFormat) != null; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return true; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/BigIntegerTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/BigIntegerTypeRule.java deleted file mode 100644 index aa760db..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/BigIntegerTypeRule.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class BigIntegerTypeRule extends AbstractColumnRule { - - public BigIntegerTypeRule(int position) { - super(position); - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getText().isEmpty() || fileColumn.getBigInteger() != null; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return true; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/BooleanTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/BooleanTypeRule.java deleted file mode 100644 index 0b4592a..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/BooleanTypeRule.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class BooleanTypeRule extends AbstractColumnRule { - - public BooleanTypeRule(int position) { - super(position); - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getText().isEmpty() || fileColumn.getBoolean() != null; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return true; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/CharacterTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/CharacterTypeRule.java deleted file mode 100644 index 3a784f5..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/CharacterTypeRule.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class CharacterTypeRule extends AbstractColumnRule { - - public CharacterTypeRule(int position) { - super(position); - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getText().isEmpty() || fileColumn.getCharacter() != null; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return true; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/CnpjRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/CnpjRule.java deleted file mode 100644 index 9fce975..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/CnpjRule.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.util.Arrays; -import java.util.List; - -public class CnpjRule extends AbstractColumnRule { - - private static final List INVALID_CNPJS = Arrays.asList( - "00000000000000", - "11111111111111", - "22222222222222", - "33333333333333", - "44444444444444", - "55555555555555", - "66666666666666", - "77777777777777", - "88888888888888", - "99999999999999" - ); - - public CnpjRule(int position) { - super(position); - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - String cnpj = fileColumn.getText(); - - if (cnpj.length() != 14 || INVALID_CNPJS.contains(cnpj)) { - return false; - } - - int i; - int j; - int digit; - int coeficient; - int sum; - int[] foundDv = {0, 0}; - - int dv1 = Integer.parseInt(String.valueOf(cnpj.charAt(cnpj.length() - 2))); - int dv2 = Integer.parseInt(String.valueOf(cnpj.charAt(cnpj.length() - 1))); - - for (j = 0; j < 2; j++) { - sum = 0; - coeficient = 2; - - for (i = cnpj.length() - 3 + j; i >= 0; i--) { - digit = Integer.parseInt(String.valueOf(cnpj.charAt(i))); - sum += digit * coeficient; - coeficient++; - - if (coeficient > 9) { - coeficient = 2; - } - } - - foundDv[j] = 11 - sum % 11; - - if (foundDv[j] >= 10) { - foundDv[j] = 0; - } - } - - return dv1 == foundDv[0] && dv2 == foundDv[1]; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/ColumnRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/ColumnRule.java deleted file mode 100644 index ccb6b8d..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/ColumnRule.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; -import com.jonpereiradev.jfile.reader.file.JFileLine; -import com.jonpereiradev.jfile.reader.rule.Rule; -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import org.apache.commons.lang3.StringUtils; - -public interface ColumnRule extends Rule { - - @Override - boolean isValid(JFileColumn fileColumn); - - @Override - default boolean canValidate(JFileColumn fileColumn) { - return StringUtils.isNotBlank(fileColumn.getText()); - } - - int getPosition(); - - JFileLine getFileLine(); - - void setFileLine(JFileLine fileLine); - - RuleNode getRuleNode(); - - void setRuleNode(RuleNode ruleNode); -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/CpfRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/CpfRule.java deleted file mode 100644 index 987a882..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/CpfRule.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.util.Arrays; -import java.util.List; - -public class CpfRule extends AbstractColumnRule { - - private static final List INVALID_CPFS = Arrays.asList( - "00000000000", - "11111111111", - "22222222222", - "33333333333", - "44444444444", - "55555555555", - "66666666666", - "77777777777", - "88888888888", - "99999999999" - ); - - public CpfRule(int position) { - super(position); - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - String cpf = fileColumn.getText(); - - if (cpf.length() != 11 || INVALID_CPFS.contains(cpf)) { - return false; - } - - int i; - int j; - int digit; - int coeficient; - int sum; - int[] foundDv = {0, 0}; - - int dv1 = Integer.parseInt(String.valueOf(cpf.charAt(cpf.length() - 2))); - int dv2 = Integer.parseInt(String.valueOf(cpf.charAt(cpf.length() - 1))); - - for (j = 0; j < 2; j++) { - sum = 0; - coeficient = 2; - - for (i = cpf.length() - 3 + j; i >= 0; i--) { - digit = Integer.parseInt(String.valueOf(cpf.charAt(i))); - sum += digit * coeficient; - coeficient++; - } - - foundDv[j] = 11 - sum % 11; - - if (foundDv[j] >= 10) { - foundDv[j] = 0; - } - } - - return dv1 == foundDv[0] && dv2 == foundDv[1]; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DateAfterRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DateAfterRule.java deleted file mode 100644 index b2d6654..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DateAfterRule.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.text.DateFormat; -import java.util.Date; - -public class DateAfterRule extends AbstractColumnRule { - - private final DateFormat dateFormat; - private final Date min; - private final int columnPosition; - - public DateAfterRule(int position, DateFormat dateFormat, Date min) { - super(position); - this.dateFormat = dateFormat; - this.min = min; - this.columnPosition = -1; - } - - public DateAfterRule(int position, DateFormat dateFormat, int columnPosition) { - super(position); - this.dateFormat = dateFormat; - this.min = null; - this.columnPosition = columnPosition; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - Date date = fileColumn.getDate(dateFormat); - return date.compareTo(getComparingDate()) > 0; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getDate(dateFormat) != null && getComparingDate() != null; - } - - private Date getComparingDate() { - if (columnPosition == -1) { - return min; - } - - return getFileLine().getColumn(columnPosition).getDate(dateFormat); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DateBeforeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DateBeforeRule.java deleted file mode 100644 index eae1840..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DateBeforeRule.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.text.DateFormat; -import java.util.Date; - -public class DateBeforeRule extends AbstractColumnRule { - - private final DateFormat dateFormat; - private final Date max; - private final int columnPosition; - - public DateBeforeRule(int position, DateFormat dateFormat, Date max) { - super(position); - this.dateFormat = dateFormat; - this.max = max; - this.columnPosition = -1; - } - - public DateBeforeRule(Integer position, DateFormat dateFormat, int columnPosition) { - super(position); - this.dateFormat = dateFormat; - this.max = null; - this.columnPosition = columnPosition; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - Date date = fileColumn.getDate(dateFormat); - return date.compareTo(getComparingDate()) < 0; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getDate(dateFormat) != null && getComparingDate() != null; - } - - private Date getComparingDate() { - if (columnPosition == -1) { - return max; - } - - return getFileLine().getColumn(columnPosition).getDate(dateFormat); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DateFutureOrPresentRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DateFutureOrPresentRule.java deleted file mode 100644 index a4043b9..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DateFutureOrPresentRule.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.text.DateFormat; -import java.util.Calendar; -import java.util.Date; - -public class DateFutureOrPresentRule extends AbstractColumnRule { - - private final DateFormat dateFormat; - - public DateFutureOrPresentRule(int position, DateFormat dateFormat) { - super(position); - this.dateFormat = dateFormat; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - Date date = fileColumn.getDate(dateFormat); - Date current = Calendar.getInstance().getTime(); - - return current.compareTo(date) <= 0; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getDate(dateFormat) != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DateFutureRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DateFutureRule.java deleted file mode 100644 index 81e3147..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DateFutureRule.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.text.DateFormat; -import java.util.Calendar; -import java.util.Date; - -public class DateFutureRule extends AbstractColumnRule { - - private final DateFormat dateFormat; - - public DateFutureRule(int position, DateFormat dateFormat) { - super(position); - this.dateFormat = dateFormat; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - Date date = fileColumn.getDate(dateFormat); - Date current = Calendar.getInstance().getTime(); - - return current.before(date); - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getDate(dateFormat) != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DatePastOrPresentRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DatePastOrPresentRule.java deleted file mode 100644 index 5309d04..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DatePastOrPresentRule.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.text.DateFormat; -import java.util.Calendar; -import java.util.Date; - -public class DatePastOrPresentRule extends AbstractColumnRule { - - private final DateFormat dateFormat; - - public DatePastOrPresentRule(int position, DateFormat dateFormat) { - super(position); - this.dateFormat = dateFormat; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - Date date = fileColumn.getDate(dateFormat); - Date current = Calendar.getInstance().getTime(); - - return current.compareTo(date) >= 0; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getDate(dateFormat) != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DatePastRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DatePastRule.java deleted file mode 100644 index 21afb45..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DatePastRule.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.text.DateFormat; -import java.util.Calendar; -import java.util.Date; - -public class DatePastRule extends AbstractColumnRule { - - private final DateFormat dateFormat; - - public DatePastRule(int position, DateFormat dateFormat) { - super(position); - this.dateFormat = dateFormat; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - Date date = fileColumn.getDate(dateFormat); - Date current = Calendar.getInstance().getTime(); - - return current.after(date); - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getDate(dateFormat) != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DateTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DateTypeRule.java deleted file mode 100644 index 4255067..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DateTypeRule.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.text.DateFormat; - -public class DateTypeRule extends AbstractColumnRule { - - private final DateFormat pattern; - - public DateTypeRule(int position, DateFormat pattern) { - super(position); - this.pattern = pattern; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getText().isEmpty() || fileColumn.getDate(pattern) != null; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return true; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DomainCharacterRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DomainCharacterRule.java deleted file mode 100644 index ef5264f..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DomainCharacterRule.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.util.List; - -public class DomainCharacterRule extends AbstractColumnRule { - - private final List domains; - - public DomainCharacterRule(int position, List domains) { - super(position); - this.domains = domains; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return domains.contains(fileColumn.getCharacter()); - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getCharacter() != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DomainIntegerRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DomainIntegerRule.java deleted file mode 100644 index 2193ce4..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DomainIntegerRule.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.util.List; - -public class DomainIntegerRule extends AbstractColumnRule { - - private final List domains; - - public DomainIntegerRule(int position, List domains) { - super(position); - this.domains = domains; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return domains.contains(fileColumn.getInt()); - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getInt() != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DomainLongRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DomainLongRule.java deleted file mode 100644 index 4385815..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DomainLongRule.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.util.List; - -public class DomainLongRule extends AbstractColumnRule { - - private final List domains; - - public DomainLongRule(int position, List domains) { - super(position); - this.domains = domains; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return domains.contains(fileColumn.getLong()); - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getLong() != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DomainRefRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DomainRefRule.java deleted file mode 100644 index bd67a3c..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DomainRefRule.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.util.List; - -public class DomainRefRule extends AbstractRefRule { - - private final List domains; - private final Class clazz; - - @SuppressWarnings("unchecked") - public DomainRefRule(int refPosition, int position, List domains) { - super(refPosition, position); - this.domains = domains; - this.clazz = (Class) domains.get(0).getClass(); - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return domains.contains(fileColumn.getContent(clazz)); - } - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DomainShortRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DomainShortRule.java deleted file mode 100644 index d843fcb..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DomainShortRule.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.util.List; - -public class DomainShortRule extends AbstractColumnRule { - - private final List domains; - - public DomainShortRule(int position, List domains) { - super(position); - this.domains = domains; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return domains.contains(fileColumn.getShort()); - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getShort() != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DomainStringRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DomainStringRule.java deleted file mode 100644 index ca2177a..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DomainStringRule.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.util.List; - -public class DomainStringRule extends AbstractColumnRule { - - private final List domains; - - public DomainStringRule(int position, List domains) { - super(position); - this.domains = domains; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return domains.contains(fileColumn.getText()); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DoubleTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DoubleTypeRule.java deleted file mode 100644 index 5d90dc2..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/DoubleTypeRule.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class DoubleTypeRule extends AbstractColumnRule { - - public DoubleTypeRule(int position) { - super(position); - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getText().isEmpty() || fileColumn.getDouble() != null; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return true; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/EmailRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/EmailRule.java deleted file mode 100644 index 162838b..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/EmailRule.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import java.util.regex.Pattern; - -public class EmailRule extends RegexRule { - - public EmailRule(int position) { - super(position, Pattern.compile("^[_A-Za-z0-9-+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$")); - } - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/EmptyRefRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/EmptyRefRule.java deleted file mode 100644 index 3d1b9d5..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/EmptyRefRule.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class EmptyRefRule extends AbstractRefRule { - - public EmptyRefRule(int refPosition, int position) { - super(refPosition, position); - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return new OnlyNullRule(fileColumn.getPosition()).isValid(fileColumn); - } - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/FilledRefRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/FilledRefRule.java deleted file mode 100644 index e6f112b..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/FilledRefRule.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class FilledRefRule extends AbstractRefRule { - - public FilledRefRule(int refPosition, int position) { - super(refPosition, position); - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return new NotEmptyRule(fileColumn.getPosition()).isValid(fileColumn); - } - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/FloatTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/FloatTypeRule.java deleted file mode 100644 index 25a1b52..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/FloatTypeRule.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class FloatTypeRule extends AbstractColumnRule { - - public FloatTypeRule(int position) { - super(position); - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getText().isEmpty() || fileColumn.getFloat() != null; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return true; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/IntegerTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/IntegerTypeRule.java deleted file mode 100644 index abf2869..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/IntegerTypeRule.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class IntegerTypeRule extends AbstractColumnRule { - - public IntegerTypeRule(int position) { - super(position); - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getText().isEmpty() || fileColumn.getInt() != null; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return true; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateAfterRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateAfterRule.java deleted file mode 100644 index 0c8b101..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateAfterRule.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.time.LocalDate; -import java.time.format.DateTimeFormatter; - -public class LocalDateAfterRule extends AbstractColumnRule { - - private final DateTimeFormatter dateTimeFormatter; - private final LocalDate min; - private final int refColumn; - - public LocalDateAfterRule(int position, DateTimeFormatter dateTimeFormatter, LocalDate min) { - super(position); - this.dateTimeFormatter = dateTimeFormatter; - this.min = min; - this.refColumn = -1; - } - - public LocalDateAfterRule(int position, DateTimeFormatter dateTimeFormatter, int refColumn) { - super(position); - this.dateTimeFormatter = dateTimeFormatter; - this.min = null; - this.refColumn = refColumn; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - LocalDate date = fileColumn.getLocalDate(dateTimeFormatter); - return date.compareTo(getComparingDate()) > 0; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getLocalDate(dateTimeFormatter) != null && getComparingDate() != null; - } - - private LocalDate getComparingDate() { - if (refColumn == -1) { - return min; - } - - return getFileLine().getColumn(refColumn).getLocalDate(dateTimeFormatter); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateBeforeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateBeforeRule.java deleted file mode 100644 index af18aa8..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateBeforeRule.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.time.LocalDate; -import java.time.format.DateTimeFormatter; - -public class LocalDateBeforeRule extends AbstractColumnRule { - - private final DateTimeFormatter dateTimeFormatter; - private final LocalDate max; - private final int refColumn; - - public LocalDateBeforeRule(int position, DateTimeFormatter dateTimeFormatter, LocalDate max) { - super(position); - this.dateTimeFormatter = dateTimeFormatter; - this.max = max; - this.refColumn = -1; - } - - public LocalDateBeforeRule(int position, DateTimeFormatter dateTimeFormatter, int refColumn) { - super(position); - this.dateTimeFormatter = dateTimeFormatter; - this.max = null; - this.refColumn = refColumn; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - LocalDate date = fileColumn.getLocalDate(dateTimeFormatter); - return date.compareTo(getComparingDate()) < 0; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getLocalDate(dateTimeFormatter) != null && getComparingDate() != null; - } - - private LocalDate getComparingDate() { - if (refColumn == -1) { - return max; - } - - return getFileLine().getColumn(refColumn).getLocalDate(dateTimeFormatter); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateFutureOrPresentRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateFutureOrPresentRule.java deleted file mode 100644 index 50ee2d8..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateFutureOrPresentRule.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.time.LocalDate; -import java.time.format.DateTimeFormatter; - -public class LocalDateFutureOrPresentRule extends AbstractColumnRule { - - private final DateTimeFormatter dateTimeFormatter; - - public LocalDateFutureOrPresentRule(int position, DateTimeFormatter dateTimeFormatter) { - super(position); - this.dateTimeFormatter = dateTimeFormatter; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - LocalDate date = fileColumn.getLocalDate(dateTimeFormatter); - LocalDate current = LocalDate.now(); - - return current.compareTo(date) <= 0; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getLocalDate(dateTimeFormatter) != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateFutureRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateFutureRule.java deleted file mode 100644 index 2e81c94..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateFutureRule.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.time.LocalDate; -import java.time.format.DateTimeFormatter; - -public class LocalDateFutureRule extends AbstractColumnRule { - - private final DateTimeFormatter dateTimeFormatter; - - public LocalDateFutureRule(int position, DateTimeFormatter dateTimeFormatter) { - super(position); - this.dateTimeFormatter = dateTimeFormatter; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - LocalDate date = fileColumn.getLocalDate(dateTimeFormatter); - LocalDate current = LocalDate.now(); - - return current.compareTo(date) < 0; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getLocalDate(dateTimeFormatter) != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDatePastOrPresentRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDatePastOrPresentRule.java deleted file mode 100644 index e4515b7..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDatePastOrPresentRule.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.time.LocalDate; -import java.time.format.DateTimeFormatter; - -public class LocalDatePastOrPresentRule extends AbstractColumnRule { - - private final DateTimeFormatter dateTimeFormatter; - - public LocalDatePastOrPresentRule(int position, DateTimeFormatter dateTimeFormatter) { - super(position); - this.dateTimeFormatter = dateTimeFormatter; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - LocalDate date = fileColumn.getLocalDate(dateTimeFormatter); - LocalDate current = LocalDate.now(); - - return current.compareTo(date) >= 0; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getLocalDate(dateTimeFormatter) != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDatePastRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDatePastRule.java deleted file mode 100644 index baf77a3..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDatePastRule.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.time.LocalDate; -import java.time.format.DateTimeFormatter; - -public class LocalDatePastRule extends AbstractColumnRule { - - private final DateTimeFormatter dateTimeFormatter; - - public LocalDatePastRule(int position, DateTimeFormatter dateTimeFormatter) { - super(position); - this.dateTimeFormatter = dateTimeFormatter; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - LocalDate date = fileColumn.getLocalDate(dateTimeFormatter); - LocalDate current = LocalDate.now(); - - return current.compareTo(date) > 0; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getLocalDate(dateTimeFormatter) != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimeAfterRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimeAfterRule.java deleted file mode 100644 index c50f1fc..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimeAfterRule.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; - -public class LocalDateTimeAfterRule extends AbstractColumnRule { - - private final DateTimeFormatter dateTimeFormatter; - private final LocalDateTime min; - private final int refColumn; - - public LocalDateTimeAfterRule(int position, DateTimeFormatter dateTimeFormatter, LocalDateTime min) { - super(position); - this.dateTimeFormatter = dateTimeFormatter; - this.min = min; - this.refColumn = -1; - } - - public LocalDateTimeAfterRule(int position, DateTimeFormatter dateTimeFormatter, int refColumn) { - super(position); - this.dateTimeFormatter = dateTimeFormatter; - this.min = null; - this.refColumn = refColumn; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - LocalDateTime date = fileColumn.getLocalDateTime(dateTimeFormatter); - return date.compareTo(getComparingDate()) > 0; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getLocalDateTime(dateTimeFormatter) != null && getComparingDate() != null; - } - - private LocalDateTime getComparingDate() { - if (refColumn == -1) { - return min; - } - - return getFileLine().getColumn(refColumn).getLocalDateTime(dateTimeFormatter); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimeBeforeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimeBeforeRule.java deleted file mode 100644 index 471ff6e..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimeBeforeRule.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; - -public class LocalDateTimeBeforeRule extends AbstractColumnRule { - - private final DateTimeFormatter dateTimeFormatter; - private final LocalDateTime max; - private final int refColumn; - - public LocalDateTimeBeforeRule(int position, DateTimeFormatter dateTimeFormatter, LocalDateTime max) { - super(position); - this.dateTimeFormatter = dateTimeFormatter; - this.max = max; - this.refColumn = -1; - } - - public LocalDateTimeBeforeRule(int position, DateTimeFormatter dateTimeFormatter, int refColumn) { - super(position); - this.dateTimeFormatter = dateTimeFormatter; - this.max = null; - this.refColumn = refColumn; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - LocalDateTime date = fileColumn.getLocalDateTime(dateTimeFormatter); - return date.compareTo(getComparingDate()) < 0; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getLocalDateTime(dateTimeFormatter) != null && getComparingDate() != null; - } - - private LocalDateTime getComparingDate() { - if (refColumn == -1) { - return max; - } - - return getFileLine().getColumn(refColumn).getLocalDateTime(dateTimeFormatter); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimeFutureOrPresentRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimeFutureOrPresentRule.java deleted file mode 100644 index e24b7be..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimeFutureOrPresentRule.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; - -public class LocalDateTimeFutureOrPresentRule extends AbstractColumnRule { - - private final DateTimeFormatter dateTimeFormatter; - - public LocalDateTimeFutureOrPresentRule(int position, DateTimeFormatter dateTimeFormatter) { - super(position); - this.dateTimeFormatter = dateTimeFormatter; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - LocalDateTime date = fileColumn.getLocalDateTime(dateTimeFormatter); - LocalDateTime current = LocalDateTime.now(); - - return current.compareTo(date) <= 0; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getLocalDateTime(dateTimeFormatter) != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimeFutureRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimeFutureRule.java deleted file mode 100644 index 1f14fb2..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimeFutureRule.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; - -public class LocalDateTimeFutureRule extends AbstractColumnRule { - - private final DateTimeFormatter dateTimeFormatter; - - public LocalDateTimeFutureRule(int position, DateTimeFormatter dateTimeFormatter) { - super(position); - this.dateTimeFormatter = dateTimeFormatter; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - LocalDateTime date = fileColumn.getLocalDateTime(dateTimeFormatter); - LocalDateTime current = LocalDateTime.now(); - - return current.compareTo(date) < 0; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getLocalDateTime(dateTimeFormatter) != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimePastOrPresentRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimePastOrPresentRule.java deleted file mode 100644 index 767b867..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimePastOrPresentRule.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; - -public class LocalDateTimePastOrPresentRule extends AbstractColumnRule { - - private final DateTimeFormatter dateTimeFormatter; - - public LocalDateTimePastOrPresentRule(int position, DateTimeFormatter dateTimeFormatter) { - super(position); - this.dateTimeFormatter = dateTimeFormatter; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - LocalDateTime date = fileColumn.getLocalDateTime(dateTimeFormatter); - LocalDateTime current = LocalDateTime.now(); - - return current.compareTo(date) >= 0; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getLocalDateTime(dateTimeFormatter) != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimePastRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimePastRule.java deleted file mode 100644 index 5392d4a..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimePastRule.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; - -public class LocalDateTimePastRule extends AbstractColumnRule { - - private final DateTimeFormatter dateTimeFormatter; - - public LocalDateTimePastRule(int position, DateTimeFormatter dateTimeFormatter) { - super(position); - this.dateTimeFormatter = dateTimeFormatter; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - LocalDateTime date = fileColumn.getLocalDateTime(dateTimeFormatter); - LocalDateTime current = LocalDateTime.now(); - - return current.compareTo(date) > 0; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getLocalDateTime(dateTimeFormatter) != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimeTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimeTypeRule.java deleted file mode 100644 index 2fb5ddd..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimeTypeRule.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.time.format.DateTimeFormatter; -import java.time.format.DateTimeParseException; - -public class LocalDateTimeTypeRule extends AbstractColumnRule { - - private final DateTimeFormatter dateTimeFormatter; - - public LocalDateTimeTypeRule(int position, DateTimeFormatter dateTimeFormatter) { - super(position); - this.dateTimeFormatter = dateTimeFormatter; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - try { - return fileColumn.getText().isEmpty() || fileColumn.getLocalDateTime(dateTimeFormatter) != null; - } catch (DateTimeParseException e) { - return false; - } - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return true; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTypeRule.java deleted file mode 100644 index 08ff756..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTypeRule.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.time.format.DateTimeFormatter; -import java.time.format.DateTimeParseException; - -public class LocalDateTypeRule extends AbstractColumnRule { - - private final DateTimeFormatter dateTimeFormatter; - - public LocalDateTypeRule(int position, DateTimeFormatter dateTimeFormatter) { - super(position); - this.dateTimeFormatter = dateTimeFormatter; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - try { - return fileColumn.getText().isEmpty() || fileColumn.getLocalDate(dateTimeFormatter) != null; - } catch (DateTimeParseException e) { - return false; - } - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return true; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LongTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LongTypeRule.java deleted file mode 100644 index ddc66a6..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/LongTypeRule.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class LongTypeRule extends AbstractColumnRule { - - public LongTypeRule(int position) { - super(position); - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - try { - return fileColumn.getText().isEmpty() || fileColumn.getLong() != null; - } catch (NumberFormatException e) { - return false; - } - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return true; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxBigDecimalRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxBigDecimalRule.java deleted file mode 100644 index dd30dab..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxBigDecimalRule.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.math.BigDecimal; -import java.text.DecimalFormat; - -public class MaxBigDecimalRule extends AbstractColumnRule { - - private final BigDecimal max; - private final DecimalFormat decimalFormat; - - public MaxBigDecimalRule(int position, BigDecimal max, DecimalFormat decimalFormat) { - super(position); - this.max = max; - this.decimalFormat = decimalFormat; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getBigDecimal(decimalFormat).compareTo(max) <= 0; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getBigDecimal(decimalFormat) != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxBigIntegerRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxBigIntegerRule.java deleted file mode 100644 index aa43c2f..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxBigIntegerRule.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.math.BigInteger; - -public class MaxBigIntegerRule extends AbstractColumnRule { - - private final BigInteger max; - - public MaxBigIntegerRule(int position, BigInteger max) { - super(position); - this.max = max; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getBigInteger().compareTo(max) <= 0; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getBigInteger() != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxDoubleRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxDoubleRule.java deleted file mode 100644 index 3b9e506..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxDoubleRule.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class MaxDoubleRule extends AbstractColumnRule { - - private final double max; - - public MaxDoubleRule(int position, double max) { - super(position); - this.max = max; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getDouble() <= max; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getDouble() != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxFloatRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxFloatRule.java deleted file mode 100644 index 5d3e739..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxFloatRule.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class MaxFloatRule extends AbstractColumnRule { - - private final float max; - - public MaxFloatRule(int position, float max) { - super(position); - this.max = max; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getFloat() <= max; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getFloat() != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxIntegerRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxIntegerRule.java deleted file mode 100644 index c600d15..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxIntegerRule.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class MaxIntegerRule extends AbstractColumnRule { - - private final int max; - - public MaxIntegerRule(int position, int max) { - super(position); - this.max = max; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getInt() <= max; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getInt() != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxLongRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxLongRule.java deleted file mode 100644 index a2a29e9..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxLongRule.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class MaxLongRule extends AbstractColumnRule { - - private final long max; - - public MaxLongRule(int position, long max) { - super(position); - this.max = max; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getLong() <= max; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getLong() != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxShortRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxShortRule.java deleted file mode 100644 index f1ef28d..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxShortRule.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class MaxShortRule extends AbstractColumnRule { - - private final short max; - - public MaxShortRule(int position, short max) { - super(position); - this.max = max; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getShort() <= max; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getShort() != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxStringRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxStringRule.java deleted file mode 100644 index e0cbf56..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MaxStringRule.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class MaxStringRule extends AbstractColumnRule { - - private final int max; - - public MaxStringRule(int position, int max) { - super(position); - this.max = max; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getText().trim().length() <= max; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinBigDecimalRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinBigDecimalRule.java deleted file mode 100644 index 286d86d..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinBigDecimalRule.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.math.BigDecimal; -import java.text.DecimalFormat; - -public class MinBigDecimalRule extends AbstractColumnRule { - - private final BigDecimal min; - private final DecimalFormat decimalFormat; - - public MinBigDecimalRule(int position, BigDecimal min, DecimalFormat decimalFormat) { - super(position); - this.min = min; - this.decimalFormat = decimalFormat; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getBigDecimal(decimalFormat).compareTo(min) >= 0; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getBigDecimal(decimalFormat) != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinBigIntegerRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinBigIntegerRule.java deleted file mode 100644 index f7daab2..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinBigIntegerRule.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.math.BigInteger; - -public class MinBigIntegerRule extends AbstractColumnRule { - - private final BigInteger min; - - public MinBigIntegerRule(int position, BigInteger min) { - super(position); - this.min = min; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getBigInteger().compareTo(min) >= 0; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getBigInteger() != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinDoubleRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinDoubleRule.java deleted file mode 100644 index 9db24ac..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinDoubleRule.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class MinDoubleRule extends AbstractColumnRule { - - private final double min; - - public MinDoubleRule(int position, double min) { - super(position); - this.min = min; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getDouble() >= min; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getDouble() != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinFloatRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinFloatRule.java deleted file mode 100644 index d07a079..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinFloatRule.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class MinFloatRule extends AbstractColumnRule { - - private final float min; - - public MinFloatRule(int position, float min) { - super(position); - this.min = min; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getFloat() >= min; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getFloat() != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinIntegerRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinIntegerRule.java deleted file mode 100644 index b83cab9..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinIntegerRule.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class MinIntegerRule extends AbstractColumnRule { - - private final int min; - - public MinIntegerRule(int position, int min) { - super(position); - this.min = min; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getInt() >= min; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getInt() != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinLongRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinLongRule.java deleted file mode 100644 index 4aa6228..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinLongRule.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class MinLongRule extends AbstractColumnRule { - - private final long min; - - public MinLongRule(int position, long min) { - super(position); - this.min = min; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getLong() >= min; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getLong() != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinShortRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinShortRule.java deleted file mode 100644 index 9fc37f9..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinShortRule.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class MinShortRule extends AbstractColumnRule { - - private final short min; - - public MinShortRule(int position, short min) { - super(position); - this.min = min; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getShort() >= min; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return fileColumn.getShort() != null; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinStringRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinStringRule.java deleted file mode 100644 index c57ac6c..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/MinStringRule.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class MinStringRule extends AbstractColumnRule { - - private final int min; - - public MinStringRule(int position, int min) { - super(position); - this.min = min; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return fileColumn.getText().trim().length() >= min; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/NotEmptyRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/NotEmptyRule.java deleted file mode 100644 index 61ed17c..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/NotEmptyRule.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; -import org.apache.commons.lang3.StringUtils; - -public class NotEmptyRule extends AbstractColumnRule { - - public NotEmptyRule(int position) { - super(position); - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return StringUtils.isNotBlank(fileColumn.getText()); - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return true; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/NotNullRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/NotNullRule.java deleted file mode 100644 index c2e566f..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/NotNullRule.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; -import org.apache.commons.lang3.StringUtils; - -public class NotNullRule extends AbstractColumnRule { - - public NotNullRule(int position) { - super(position); - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return StringUtils.isNotEmpty(fileColumn.getText()); - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return true; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/OnlyNullRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/OnlyNullRule.java deleted file mode 100644 index 6adc7a8..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/OnlyNullRule.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; -import org.apache.commons.lang3.StringUtils; - -public class OnlyNullRule extends AbstractColumnRule { - - public OnlyNullRule(int position) { - super(position); - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return StringUtils.isBlank(fileColumn.getText()); - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return true; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/RefRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/RefRule.java deleted file mode 100644 index 2d74a63..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/RefRule.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -public interface RefRule extends ColumnRule { - - int getRefPosition(); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/RegexRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/RegexRule.java deleted file mode 100644 index 44fb6f1..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/RegexRule.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -import java.util.regex.Pattern; - -public class RegexRule extends AbstractColumnRule { - - private final Pattern regex; - - public RegexRule(int position, Pattern pattern) { - super(position); - this.regex = pattern; - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return regex.matcher(fileColumn.getText()).matches(); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/ShortTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/ShortTypeRule.java deleted file mode 100644 index b0163aa..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/ShortTypeRule.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class ShortTypeRule extends AbstractColumnRule { - - public ShortTypeRule(int position) { - super(position); - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - try { - return fileColumn.getText().isEmpty() || fileColumn.getShort() != null; - } catch (NumberFormatException e) { - return false; - } - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return true; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/StringTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/column/StringTypeRule.java deleted file mode 100644 index 60c10a7..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/column/StringTypeRule.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.file.JFileColumn; - -public class StringTypeRule extends AbstractColumnRule { - - public StringTypeRule(int position) { - super(position); - } - - @Override - public boolean isValid(JFileColumn fileColumn) { - return true; - } - - @Override - public boolean canValidate(JFileColumn fileColumn) { - return true; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/AbstractRuleConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/AbstractRuleConfigurator.java deleted file mode 100644 index 65749e1..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/AbstractRuleConfigurator.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import com.jonpereiradev.jfile.reader.rule.RuleNodeImpl; -import com.jonpereiradev.jfile.reader.rule.column.ColumnRule; -import com.jonpereiradev.jfile.reader.rule.column.NotNullRule; -import com.jonpereiradev.jfile.reader.rule.column.OnlyNullRule; -import com.jonpereiradev.jfile.reader.rule.column.RefRule; - -import java.util.function.Function; - -abstract class AbstractRuleConfigurator> implements TypedRuleConfigurator { - - private final int position; - private final ReaderConfiguration configuration; - - private RuleNode ruleNode; - - AbstractRuleConfigurator(int position, ReaderConfiguration configuration, RuleNode ruleNode) { - this.position = position; - this.configuration = configuration; - this.ruleNode = ruleNode; - } - - @Override - public T notNull() { - return rule(NotNullRule::new); - } - - @Override - public T onlyNull() { - return rule(OnlyNullRule::new); - } - - @Override - @SuppressWarnings("unchecked") - public T rule(Function rule) { - ColumnRule columnRule = rule.apply(position); - columnRule.setRuleNode(new RuleNodeImpl<>(columnRule.getClass(), ruleNode)); - ruleNode.add(columnRule); - return (T) this; - } - - @Override - public GenericTypeConfigurator column(int position) { - return new GenericTypeConfiguratorImpl(position, configuration, getParentNode()); - } - - private RuleNode getParentNode() { - RuleNode node = ruleNode; - - while (node.getParentNode() != null) { - node = node.getParentNode(); - } - - return node; - } - - @Override - @SuppressWarnings("unchecked") - public RefRuleConfigurator depends(int column) { - RuleNode parentNode = ruleNode; - - if (ruleNode.getParentNode() != null && RefRule.class.isAssignableFrom(ruleNode.getType())) { - parentNode = ruleNode.getParentNode(); - } - - return new RefRuleConfiguratorImpl<>(column, position, parentNode, (T) this); - } - - @Override - @SuppressWarnings("unchecked") - public T apply() { - if (ruleNode.getParentNode() != null) { - setRuleNode(ruleNode.getParentNode()); - } - - return (T) this; - } - - @Override - public void setRuleNode(RuleNode ruleNode) { - this.ruleNode = ruleNode; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/ArrayTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/ArrayTypeConfigurator.java deleted file mode 100644 index 517e943..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/ArrayTypeConfigurator.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import java.text.DateFormat; -import java.text.DecimalFormat; -import java.time.format.DateTimeFormatter; - -public interface ArrayTypeConfigurator { - - /** - * defines a rule for a column of short type. - */ - ShortTypeConfigurator shortType(); - - /** - * defines a rule for a column of integer type. - */ - IntegerTypeConfigurator integerType(); - - /** - * defines a rule for a column of long type. - */ - LongTypeConfigurator longType(); - - /** - * defines a rule for a column of float type. - */ - FloatTypeConfigurator floatType(); - - /** - * defines a rule for a column of double type. - */ - DoubleTypeConfigurator doubleType(); - - /** - * defines a rule for a column of boolean type. - */ - BooleanTypeConfigurator booleanType(); - - /** - * defines a rule for a column of character type. - */ - CharacterTypeConfigurator characterType(); - - /** - * defines a rule for a column of string type. - */ - StringTypeConfigurator stringType(); - - /** - * defines a rule for a column of big integer type. - */ - BigIntegerTypeConfigurator bigIntegerType(); - - /** - * defines a rule for a column of big decimal type. - */ - BigDecimalTypeConfigurator bigDecimalType(); - - /** - * defines a rule for a column of big decimal type. - */ - BigDecimalTypeConfigurator bigDecimalType(DecimalFormat decimalFormat); - - /** - * defines a rule for a column of date type. - */ - DateTypeConfigurator dateType(); - - /** - * defines a rule for a column of date type. - */ - DateTypeConfigurator dateType(DateFormat pattern); - - /** - * defines a rule for a column of local date type. - */ - LocalDateTypeConfigurator localDateType(); - - /** - * defines a rule for a column of local date type. - */ - LocalDateTypeConfigurator localDateType(DateTimeFormatter dateTimeFormatter); - - /** - * defines a rule for a column of local date time type. - */ - LocalDateTimeTypeConfigurator localDateTimeType(); - - /** - * defines a rule for a column of local date time type. - */ - LocalDateTimeTypeConfigurator localDateTimeType(DateTimeFormatter dateTimeFormatter); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/BigDecimalTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/BigDecimalTypeConfigurator.java deleted file mode 100644 index e4aa3b2..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/BigDecimalTypeConfigurator.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import java.math.BigDecimal; - -public interface BigDecimalTypeConfigurator extends TypedRuleConfigurator { - - /** - * defines a min value rule validation. - */ - BigDecimalTypeConfigurator min(BigDecimal min); - - /** - * defines a max value rule validation. - */ - BigDecimalTypeConfigurator max(BigDecimal max); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/BigDecimalTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/BigDecimalTypeConfiguratorImpl.java deleted file mode 100644 index fdc1429..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/BigDecimalTypeConfiguratorImpl.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import com.jonpereiradev.jfile.reader.rule.column.ColumnRule; -import com.jonpereiradev.jfile.reader.rule.column.MaxBigDecimalRule; -import com.jonpereiradev.jfile.reader.rule.column.MinBigDecimalRule; - -import java.math.BigDecimal; -import java.text.DecimalFormat; - -final class BigDecimalTypeConfiguratorImpl - extends AbstractRuleConfigurator implements BigDecimalTypeConfigurator { - - private final DecimalFormat decimalFormat; - - BigDecimalTypeConfiguratorImpl( - int position, - DecimalFormat decimalFormat, - ReaderConfiguration configuration, - RuleNode ruleNode - ) { - super(position, configuration, ruleNode); - this.decimalFormat = decimalFormat; - } - - @Override - public BigDecimalTypeConfigurator min(BigDecimal min) { - return rule(position -> new MinBigDecimalRule(position, min, decimalFormat)); - } - - @Override - public BigDecimalTypeConfigurator max(BigDecimal max) { - return rule(position -> new MaxBigDecimalRule(position, max, decimalFormat)); - } - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/BigIntegerTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/BigIntegerTypeConfigurator.java deleted file mode 100644 index f21f462..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/BigIntegerTypeConfigurator.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import java.math.BigInteger; - -public interface BigIntegerTypeConfigurator extends TypedRuleConfigurator { - - /** - * defines a min value rule validation. - */ - BigIntegerTypeConfigurator min(BigInteger min); - - /** - * defines a max value rule validation. - */ - BigIntegerTypeConfigurator max(BigInteger max); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/BigIntegerTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/BigIntegerTypeConfiguratorImpl.java deleted file mode 100644 index 5de8878..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/BigIntegerTypeConfiguratorImpl.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import com.jonpereiradev.jfile.reader.rule.column.ColumnRule; -import com.jonpereiradev.jfile.reader.rule.column.MaxBigIntegerRule; -import com.jonpereiradev.jfile.reader.rule.column.MinBigIntegerRule; - -import java.math.BigInteger; - -final class BigIntegerTypeConfiguratorImpl - extends AbstractRuleConfigurator implements BigIntegerTypeConfigurator { - - BigIntegerTypeConfiguratorImpl(int position, ReaderConfiguration configuration, RuleNode ruleNode) { - super(position, configuration, ruleNode); - } - - @Override - public BigIntegerTypeConfigurator min(BigInteger min) { - return rule(position -> new MinBigIntegerRule(position, min)); - } - - @Override - public BigIntegerTypeConfigurator max(BigInteger max) { - return rule(position -> new MaxBigIntegerRule(position, max)); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/BooleanTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/BooleanTypeConfigurator.java deleted file mode 100644 index 463c5dd..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/BooleanTypeConfigurator.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -public interface BooleanTypeConfigurator extends TypedRuleConfigurator { - - /** - * defines a domain rule validation with possible values options. - */ - BooleanTypeConfigurator domain(Character... values); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/BooleanTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/BooleanTypeConfiguratorImpl.java deleted file mode 100644 index a59747a..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/BooleanTypeConfiguratorImpl.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import com.jonpereiradev.jfile.reader.rule.column.ColumnRule; -import com.jonpereiradev.jfile.reader.rule.column.DomainCharacterRule; - -import java.util.Arrays; - -final class BooleanTypeConfiguratorImpl extends AbstractRuleConfigurator implements BooleanTypeConfigurator { - - BooleanTypeConfiguratorImpl(int position, ReaderConfiguration configuration, RuleNode ruleNode) { - super(position, configuration, ruleNode); - } - - @Override - public BooleanTypeConfigurator domain(Character... values) { - return rule(position -> new DomainCharacterRule(position, Arrays.asList(values))); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/CharacterTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/CharacterTypeConfigurator.java deleted file mode 100644 index a39a3d4..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/CharacterTypeConfigurator.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -public interface CharacterTypeConfigurator extends TypedRuleConfigurator { - - /** - * defines a domain rule validation with possible values options. - */ - CharacterTypeConfigurator domain(Character... values); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/CharacterTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/CharacterTypeConfiguratorImpl.java deleted file mode 100644 index 89fac78..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/CharacterTypeConfiguratorImpl.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import com.jonpereiradev.jfile.reader.rule.column.ColumnRule; -import com.jonpereiradev.jfile.reader.rule.column.DomainCharacterRule; - -import java.util.Arrays; - -final class CharacterTypeConfiguratorImpl - extends AbstractRuleConfigurator implements CharacterTypeConfigurator { - - CharacterTypeConfiguratorImpl(int position, ReaderConfiguration configuration, RuleNode ruleNode) { - super(position, configuration, ruleNode); - } - - @Override - public CharacterTypeConfigurator domain(Character... values) { - return rule(position -> new DomainCharacterRule(position, Arrays.asList(values))); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/DateTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/DateTypeConfigurator.java deleted file mode 100644 index c1b93f1..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/DateTypeConfigurator.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import java.util.Date; - -public interface DateTypeConfigurator extends TypedRuleConfigurator { - - /** - * defines a future rule validation. - */ - DateTypeConfigurator future(); - - /** - * defines a future or present rule validation. - */ - DateTypeConfigurator futureOrPresent(); - - /** - * defines a past rule validation. - */ - DateTypeConfigurator past(); - - /** - * defines a past or present rule validation. - */ - DateTypeConfigurator pastOrPresent(); - - /** - * defines a min date rule validation. - */ - DateTypeConfigurator after(Date min); - - /** - * defines a min date rule validation comparing to another column. - */ - DateTypeConfigurator after(int columnPosition); - - /** - * defines a max date rule validation. - */ - DateTypeConfigurator before(Date max); - - /** - * defines a max date rule validation comparing to another column. - */ - DateTypeConfigurator before(int columnPosition); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/DateTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/DateTypeConfiguratorImpl.java deleted file mode 100644 index 8ce1a0e..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/DateTypeConfiguratorImpl.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import com.jonpereiradev.jfile.reader.rule.column.*; - -import java.text.DateFormat; -import java.util.Date; - -final class DateTypeConfiguratorImpl extends AbstractRuleConfigurator implements DateTypeConfigurator { - - private final DateFormat dateFormat; - - DateTypeConfiguratorImpl(int position, DateFormat dateFormat, ReaderConfiguration configuration, RuleNode ruleNode) { - super(position, configuration, ruleNode); - this.dateFormat = dateFormat; - } - - @Override - public DateTypeConfigurator future() { - return rule(position -> new DateFutureRule(position, dateFormat)); - } - - @Override - public DateTypeConfigurator futureOrPresent() { - return rule(position -> new DateFutureOrPresentRule(position, dateFormat)); - } - - @Override - public DateTypeConfigurator past() { - return rule(position -> new DatePastRule(position, dateFormat)); - } - - @Override - public DateTypeConfigurator pastOrPresent() { - return rule(position -> new DatePastOrPresentRule(position, dateFormat)); - } - - @Override - public DateTypeConfigurator after(Date min) { - return rule(position -> new DateAfterRule(position, dateFormat, min)); - } - - @Override - public DateTypeConfigurator after(int columnPosition) { - return rule(position -> new DateAfterRule(position, dateFormat, columnPosition)); - } - - @Override - public DateTypeConfigurator before(Date max) { - return rule(position -> new DateBeforeRule(position, dateFormat, max)); - } - - @Override - public DateTypeConfigurator before(int columnPosition) { - return rule(position -> new DateBeforeRule(position, dateFormat, columnPosition)); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/DoubleTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/DoubleTypeConfigurator.java deleted file mode 100644 index 9985017..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/DoubleTypeConfigurator.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -public interface DoubleTypeConfigurator extends TypedRuleConfigurator { - - /** - * defines a min value rule validation. - */ - DoubleTypeConfigurator min(double min); - - /** - * defines a max value rule validation. - */ - DoubleTypeConfigurator max(double max); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/DoubleTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/DoubleTypeConfiguratorImpl.java deleted file mode 100644 index 8eca7bc..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/DoubleTypeConfiguratorImpl.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import com.jonpereiradev.jfile.reader.rule.column.ColumnRule; -import com.jonpereiradev.jfile.reader.rule.column.MaxDoubleRule; -import com.jonpereiradev.jfile.reader.rule.column.MinDoubleRule; - -final class DoubleTypeConfiguratorImpl extends AbstractRuleConfigurator implements DoubleTypeConfigurator { - - DoubleTypeConfiguratorImpl(int position, ReaderConfiguration configuration, RuleNode ruleNode) { - super(position, configuration, ruleNode); - } - - @Override - public DoubleTypeConfigurator min(double min) { - return rule(position -> new MinDoubleRule(position, min)); - } - - @Override - public DoubleTypeConfigurator max(double max) { - return rule(position -> new MaxDoubleRule(position, max)); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/FileRuleConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/FileRuleConfigurator.java deleted file mode 100644 index 4b281aa..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/FileRuleConfigurator.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; - -public interface FileRuleConfigurator { - - static FileRuleConfigurator defaultConfigurator(ReaderConfiguration configuration) { - return new FileRuleConfiguratorImpl(configuration); - } - - /** - * creates the rule configurator for lines. - */ - LineRuleConfigurator lines(); -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/FileRuleConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/FileRuleConfiguratorImpl.java deleted file mode 100644 index c3b8476..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/FileRuleConfiguratorImpl.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; - -final class FileRuleConfiguratorImpl implements FileRuleConfigurator { - - private final ReaderConfiguration configuration; - - FileRuleConfiguratorImpl(ReaderConfiguration configuration) { - this.configuration = configuration; - } - - @Override - public LineRuleConfigurator lines() { - return new LineRuleConfiguratorImpl(configuration); - } - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/FloatTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/FloatTypeConfigurator.java deleted file mode 100644 index ae99c11..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/FloatTypeConfigurator.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -public interface FloatTypeConfigurator extends TypedRuleConfigurator { - - /** - * defines a min value rule validation. - */ - FloatTypeConfigurator min(float min); - - /** - * defines a max value rule validation. - */ - FloatTypeConfigurator max(float max); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/FloatTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/FloatTypeConfiguratorImpl.java deleted file mode 100644 index 0af865a..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/FloatTypeConfiguratorImpl.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import com.jonpereiradev.jfile.reader.rule.column.ColumnRule; -import com.jonpereiradev.jfile.reader.rule.column.MaxFloatRule; -import com.jonpereiradev.jfile.reader.rule.column.MinFloatRule; - -final class FloatTypeConfiguratorImpl extends AbstractRuleConfigurator implements FloatTypeConfigurator { - - FloatTypeConfiguratorImpl(int position, ReaderConfiguration configuration, RuleNode ruleNode) { - super(position, configuration, ruleNode); - } - - @Override - public FloatTypeConfigurator min(float min) { - return rule(position -> new MinFloatRule(position, min)); - } - - @Override - public FloatTypeConfigurator max(float max) { - return rule(position -> new MaxFloatRule(position, max)); - } - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/GenericTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/GenericTypeConfigurator.java deleted file mode 100644 index 291c24e..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/GenericTypeConfigurator.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import java.text.DateFormat; -import java.text.DecimalFormat; -import java.time.format.DateTimeFormatter; -import java.util.regex.Pattern; - -public interface GenericTypeConfigurator { - - /** - * defines a rule for a column of short type. - */ - ShortTypeConfigurator shortType(); - - /** - * defines a rule for a column of integer type. - */ - IntegerTypeConfigurator integerType(); - - /** - * defines a rule for a column of long type. - */ - LongTypeConfigurator longType(); - - /** - * defines a rule for a column of float type. - */ - FloatTypeConfigurator floatType(); - - /** - * defines a rule for a column of double type. - */ - DoubleTypeConfigurator doubleType(); - - /** - * defines a rule for a column of boolean type. - */ - BooleanTypeConfigurator booleanType(); - - /** - * defines a rule for a column of character type. - */ - CharacterTypeConfigurator characterType(); - - /** - * defines a rule for a column of string type. - */ - StringTypeConfigurator stringType(); - - /** - * defines a rule for a column of big integer type. - */ - BigIntegerTypeConfigurator bigIntegerType(); - - /** - * defines a rule for a column of big decimal type. - */ - BigDecimalTypeConfigurator bigDecimalType(); - - /** - * defines a rule for a column of big decimal type. - */ - BigDecimalTypeConfigurator bigDecimalType(DecimalFormat decimalFormat); - - /** - * defines a rule for a column of date type. - */ - DateTypeConfigurator dateType(); - - /** - * defines a rule for a column of date type. - */ - DateTypeConfigurator dateType(DateFormat pattern); - - /** - * defines a rule for a column of local date type. - */ - LocalDateTypeConfigurator localDateType(); - - /** - * defines a rule for a column of local date type. - */ - LocalDateTypeConfigurator localDateType(DateTimeFormatter dateTimeFormatter); - - /** - * defines a rule for a column of local date time type. - */ - LocalDateTimeTypeConfigurator localDateTimeType(); - - /** - * defines a rule for a column of local date time type. - */ - LocalDateTimeTypeConfigurator localDateTimeType(DateTimeFormatter dateTimeFormatter); - - /** - * defines a rule for a column of array type. - */ - ArrayTypeConfigurator arrayOf(); - - /** - * defines a rule for a column of array type. - */ - ArrayTypeConfigurator arrayOf(Pattern pattern); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/GenericTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/GenericTypeConfiguratorImpl.java deleted file mode 100644 index f088f72..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/GenericTypeConfiguratorImpl.java +++ /dev/null @@ -1,164 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import com.jonpereiradev.jfile.reader.rule.RuleNodeImpl; -import com.jonpereiradev.jfile.reader.rule.column.*; - -import java.text.DateFormat; -import java.text.DecimalFormat; -import java.time.format.DateTimeFormatter; -import java.util.regex.Pattern; - -final class GenericTypeConfiguratorImpl implements GenericTypeConfigurator { - - private static final Pattern DEFAULT_ARRAY_SEPARATOR = Pattern.compile(",\\s*"); - - private final int position; - private final ReaderConfiguration configuration; - private final RuleNode ruleNode; - - GenericTypeConfiguratorImpl(int position, ReaderConfiguration configuration, RuleNode ruleNode) { - this.position = position; - this.configuration = configuration; - this.ruleNode = ruleNode; - } - - @Override - public ShortTypeConfigurator shortType() { - ShortTypeRule rule = new ShortTypeRule(position); - rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); - ruleNode.add(rule); - return new ShortTypeConfiguratorImpl(position, configuration, rule.getRuleNode()); - } - - @Override - public IntegerTypeConfigurator integerType() { - IntegerTypeRule rule = new IntegerTypeRule(position); - rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); - ruleNode.add(rule); - return new IntegerTypeConfiguratorImpl(position, configuration, rule.getRuleNode()); - } - - @Override - public LongTypeConfigurator longType() { - LongTypeRule rule = new LongTypeRule(position); - rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); - ruleNode.add(rule); - return new LongTypeConfiguratorImpl(position, configuration, rule.getRuleNode()); - } - - @Override - public FloatTypeConfigurator floatType() { - FloatTypeRule rule = new FloatTypeRule(position); - rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); - ruleNode.add(rule); - return new FloatTypeConfiguratorImpl(position, configuration, rule.getRuleNode()); - } - - @Override - public DoubleTypeConfigurator doubleType() { - DoubleTypeRule rule = new DoubleTypeRule(position); - rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); - ruleNode.add(rule); - return new DoubleTypeConfiguratorImpl(position, configuration, rule.getRuleNode()); - } - - @Override - public BooleanTypeConfigurator booleanType() { - BooleanTypeRule rule = new BooleanTypeRule(position); - rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); - ruleNode.add(rule); - return new BooleanTypeConfiguratorImpl(position, configuration, rule.getRuleNode()); - } - - @Override - public CharacterTypeConfigurator characterType() { - CharacterTypeRule rule = new CharacterTypeRule(position); - rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); - ruleNode.add(rule); - return new CharacterTypeConfiguratorImpl(position, configuration, rule.getRuleNode()); - } - - @Override - public StringTypeConfigurator stringType() { - StringTypeRule rule = new StringTypeRule(position); - rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); - ruleNode.add(rule); - return new StringTypeConfiguratorImpl(position, configuration, rule.getRuleNode()); - } - - @Override - public BigIntegerTypeConfigurator bigIntegerType() { - BigIntegerTypeRule rule = new BigIntegerTypeRule(position); - rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); - ruleNode.add(rule); - return new BigIntegerTypeConfiguratorImpl(position, configuration, rule.getRuleNode()); - } - - @Override - public BigDecimalTypeConfigurator bigDecimalType() { - return bigDecimalType(configuration.getBigDecimalFormatter()); - } - - @Override - public BigDecimalTypeConfigurator bigDecimalType(DecimalFormat decimalFormat) { - BigDecimalTypeRule rule = new BigDecimalTypeRule(position, decimalFormat); - rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); - ruleNode.add(rule); - return new BigDecimalTypeConfiguratorImpl(position, decimalFormat, configuration, rule.getRuleNode()); - } - - @Override - public DateTypeConfigurator dateType() { - return dateType(configuration.getDateFormat()); - } - - @Override - public DateTypeConfigurator dateType(DateFormat dateFormat) { - DateTypeRule rule = new DateTypeRule(position, dateFormat); - rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); - ruleNode.add(rule); - return new DateTypeConfiguratorImpl(position, dateFormat, configuration, rule.getRuleNode()); - } - - @Override - public LocalDateTypeConfigurator localDateType() { - return localDateType(configuration.getLocalDateFormatter()); - } - - @Override - public LocalDateTypeConfigurator localDateType(DateTimeFormatter dateTimeFormatter) { - LocalDateTypeRule rule = new LocalDateTypeRule(position, dateTimeFormatter); - rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); - ruleNode.add(rule); - return new LocalDateTypeConfiguratorImpl(position, dateTimeFormatter, configuration, rule.getRuleNode()); - } - - @Override - public LocalDateTimeTypeConfigurator localDateTimeType() { - return localDateTimeType(configuration.getLocalDateTimeFormatter()); - } - - @Override - public LocalDateTimeTypeConfigurator localDateTimeType(DateTimeFormatter dateTimeFormatter) { - LocalDateTimeTypeRule rule = new LocalDateTimeTypeRule(position, dateTimeFormatter); - rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); - ruleNode.add(rule); - return new LocalDateTimeTypeConfiguratorImpl(position, dateTimeFormatter, configuration, rule.getRuleNode()); - } - - @Override - public ArrayTypeConfigurator arrayOf() { - return arrayOf(DEFAULT_ARRAY_SEPARATOR); - } - - @Override - public ArrayTypeConfigurator arrayOf(Pattern pattern) { - ArrayOfTypeRule rule = new ArrayOfTypeRule(position, pattern); - rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); - ruleNode.add(rule); - return new ArrayTypeConfiguratorImpl(position, configuration, rule.getRuleNode()); - } - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/IntegerTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/IntegerTypeConfigurator.java deleted file mode 100644 index 3e335fa..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/IntegerTypeConfigurator.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -public interface IntegerTypeConfigurator extends TypedRuleConfigurator { - - /** - * defines a min value rule validation. - */ - IntegerTypeConfigurator min(int min); - - /** - * defines a max value rule validation. - */ - IntegerTypeConfigurator max(int max); - - /** - * defines a domain rule validation with possible values options. - */ - IntegerTypeConfigurator domain(Integer... values); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/IntegerTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/IntegerTypeConfiguratorImpl.java deleted file mode 100644 index a4124c3..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/IntegerTypeConfiguratorImpl.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import com.jonpereiradev.jfile.reader.rule.column.ColumnRule; -import com.jonpereiradev.jfile.reader.rule.column.DomainIntegerRule; -import com.jonpereiradev.jfile.reader.rule.column.MaxIntegerRule; -import com.jonpereiradev.jfile.reader.rule.column.MinIntegerRule; - -import java.util.Arrays; - -final class IntegerTypeConfiguratorImpl extends AbstractRuleConfigurator implements IntegerTypeConfigurator { - - IntegerTypeConfiguratorImpl(int position, ReaderConfiguration configuration, RuleNode ruleNode) { - super(position, configuration, ruleNode); - } - - @Override - public IntegerTypeConfigurator min(int min) { - return rule(position -> new MinIntegerRule(position, min)); - } - - @Override - public IntegerTypeConfigurator max(int max) { - return rule(position -> new MaxIntegerRule(position, max)); - } - - @Override - public IntegerTypeConfigurator domain(Integer... values) { - return rule(position -> new DomainIntegerRule(position, Arrays.asList(values))); - } - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LineRuleConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LineRuleConfigurator.java deleted file mode 100644 index 603b921..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LineRuleConfigurator.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -public interface LineRuleConfigurator { - - /** - * define the number of columns for one line. - */ - LineRuleConfigurator columns(int size); - - /** - * creates the rule configuration for the column at position. - */ - GenericTypeConfigurator column(int position); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LineRuleConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LineRuleConfiguratorImpl.java deleted file mode 100644 index 3c704df..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LineRuleConfiguratorImpl.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.rule.line.LineColumnSizeRule; - -final class LineRuleConfiguratorImpl implements LineRuleConfigurator { - - private final ReaderConfiguration configuration; - - LineRuleConfiguratorImpl(ReaderConfiguration configuration) { - this.configuration = configuration; - } - - @Override - public LineRuleConfigurator columns(int size) { - configuration.getRuleConfiguration().getLineRootNode().add(new LineColumnSizeRule(size)); - return this; - } - - @Override - public GenericTypeConfigurator column(int position) { - return new GenericTypeConfiguratorImpl(position, configuration, configuration.getRuleConfiguration().getColumnRootNode()); - } - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LocalDateTimeTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LocalDateTimeTypeConfigurator.java deleted file mode 100644 index ea21206..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LocalDateTimeTypeConfigurator.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import java.time.LocalDateTime; - -public interface LocalDateTimeTypeConfigurator extends TypedRuleConfigurator { - - /** - * defines a future rule validation. - */ - LocalDateTimeTypeConfigurator future(); - - /** - * defines a future or present rule validation. - */ - LocalDateTimeTypeConfigurator futureOrPresent(); - - /** - * defines a past rule validation. - */ - LocalDateTimeTypeConfigurator past(); - - /** - * defines a past or present rule validation. - */ - LocalDateTimeTypeConfigurator pastOrPresent(); - - /** - * defines a min date rule validation. - */ - LocalDateTimeTypeConfigurator after(LocalDateTime min); - - /** - * defines a min date rule validation comparing to another column. - */ - LocalDateTimeTypeConfigurator after(int columnPosition); - - /** - * defines a max date rule validation. - */ - LocalDateTimeTypeConfigurator before(LocalDateTime max); - - /** - * defines a max date rule validation comparing to another column. - */ - LocalDateTimeTypeConfigurator before(int columnPosition); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LocalDateTimeTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LocalDateTimeTypeConfiguratorImpl.java deleted file mode 100644 index 2c167e1..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LocalDateTimeTypeConfiguratorImpl.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import com.jonpereiradev.jfile.reader.rule.column.*; - -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; - -final class LocalDateTimeTypeConfiguratorImpl - extends AbstractRuleConfigurator implements LocalDateTimeTypeConfigurator { - - private final DateTimeFormatter dateTimeFormatter; - - LocalDateTimeTypeConfiguratorImpl( - int position, - DateTimeFormatter dateTimeFormatter, - ReaderConfiguration configuration, - RuleNode ruleNode - ) { - super(position, configuration, ruleNode); - this.dateTimeFormatter = dateTimeFormatter; - } - - @Override - public LocalDateTimeTypeConfigurator future() { - return rule(position -> new LocalDateTimeFutureRule(position, dateTimeFormatter)); - } - - @Override - public LocalDateTimeTypeConfigurator futureOrPresent() { - return rule(position -> new LocalDateTimeFutureOrPresentRule(position, dateTimeFormatter)); - } - - @Override - public LocalDateTimeTypeConfigurator past() { - return rule(position -> new LocalDateTimePastRule(position, dateTimeFormatter)); - } - - @Override - public LocalDateTimeTypeConfigurator pastOrPresent() { - return rule(position -> new LocalDateTimePastOrPresentRule(position, dateTimeFormatter)); - } - - @Override - public LocalDateTimeTypeConfigurator after(LocalDateTime min) { - return rule(position -> new LocalDateTimeAfterRule(position, dateTimeFormatter, min)); - } - - @Override - public LocalDateTimeTypeConfigurator after(int column) { - return rule(position -> new LocalDateTimeAfterRule(position, dateTimeFormatter, column)); - } - - @Override - public LocalDateTimeTypeConfigurator before(LocalDateTime max) { - return rule(position -> new LocalDateTimeBeforeRule(position, dateTimeFormatter, max)); - } - - @Override - public LocalDateTimeTypeConfigurator before(int column) { - return rule(position -> new LocalDateTimeBeforeRule(position, dateTimeFormatter, column)); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LocalDateTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LocalDateTypeConfigurator.java deleted file mode 100644 index ea6188c..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LocalDateTypeConfigurator.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import java.time.LocalDate; - -public interface LocalDateTypeConfigurator extends TypedRuleConfigurator { - - /** - * defines a future rule validation. - */ - LocalDateTypeConfigurator future(); - - /** - * defines a future or present rule validation. - */ - LocalDateTypeConfigurator futureOrPresent(); - - /** - * defines a past rule validation. - */ - LocalDateTypeConfigurator past(); - - /** - * defines a past or present rule validation. - */ - LocalDateTypeConfigurator pastOrPresent(); - - /** - * defines a min date rule validation. - */ - LocalDateTypeConfigurator after(LocalDate min); - - /** - * defines a min date rule validation comparing to another column. - */ - LocalDateTypeConfigurator after(int columnPosition); - - /** - * defines a max date rule validation. - */ - LocalDateTypeConfigurator before(LocalDate max); - - /** - * defines a max date rule validation comparing to another column. - */ - LocalDateTypeConfigurator before(int columnPosition); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LocalDateTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LocalDateTypeConfiguratorImpl.java deleted file mode 100644 index a8b090f..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LocalDateTypeConfiguratorImpl.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import com.jonpereiradev.jfile.reader.rule.column.*; - -import java.time.LocalDate; -import java.time.format.DateTimeFormatter; - -final class LocalDateTypeConfiguratorImpl - extends AbstractRuleConfigurator implements LocalDateTypeConfigurator { - - private final DateTimeFormatter dateTimeFormatter; - - LocalDateTypeConfiguratorImpl( - int position, - DateTimeFormatter dateTimeFormatter, - ReaderConfiguration configuration, - RuleNode ruleNode - ) { - super(position, configuration, ruleNode); - this.dateTimeFormatter = dateTimeFormatter; - } - - @Override - public LocalDateTypeConfigurator future() { - return rule(position -> new LocalDateFutureRule(position, dateTimeFormatter)); - } - - @Override - public LocalDateTypeConfigurator futureOrPresent() { - return rule(position -> new LocalDateFutureOrPresentRule(position, dateTimeFormatter)); - } - - @Override - public LocalDateTypeConfigurator past() { - return rule(position -> new LocalDatePastRule(position, dateTimeFormatter)); - } - - @Override - public LocalDateTypeConfigurator pastOrPresent() { - return rule(position -> new LocalDatePastOrPresentRule(position, dateTimeFormatter)); - } - - @Override - public LocalDateTypeConfigurator after(LocalDate min) { - return rule(position -> new LocalDateAfterRule(position, dateTimeFormatter, min)); - } - - @Override - public LocalDateTypeConfigurator after(int columnPosition) { - return rule(position -> new LocalDateAfterRule(position, dateTimeFormatter, columnPosition)); - } - - @Override - public LocalDateTypeConfigurator before(LocalDate max) { - return rule(position -> new LocalDateBeforeRule(position, dateTimeFormatter, max)); - } - - @Override - public LocalDateTypeConfigurator before(int columnPosition) { - return rule(position -> new LocalDateBeforeRule(position, dateTimeFormatter, columnPosition)); - } - - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LongTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LongTypeConfigurator.java deleted file mode 100644 index 470d683..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LongTypeConfigurator.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -public interface LongTypeConfigurator extends TypedRuleConfigurator { - - /** - * defines a min value rule validation. - */ - LongTypeConfigurator min(long min); - - /** - * defines a max value rule validation. - */ - LongTypeConfigurator max(long max); - - /** - * defines a domain rule validation with possible values options. - */ - LongTypeConfigurator domain(Long... values); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LongTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LongTypeConfiguratorImpl.java deleted file mode 100644 index b1160d8..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/LongTypeConfiguratorImpl.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import com.jonpereiradev.jfile.reader.rule.column.ColumnRule; -import com.jonpereiradev.jfile.reader.rule.column.DomainLongRule; -import com.jonpereiradev.jfile.reader.rule.column.MaxLongRule; -import com.jonpereiradev.jfile.reader.rule.column.MinLongRule; - -import java.util.Arrays; - -final class LongTypeConfiguratorImpl extends AbstractRuleConfigurator implements LongTypeConfigurator { - - LongTypeConfiguratorImpl(int position, ReaderConfiguration configuration, RuleNode ruleNode) { - super(position, configuration, ruleNode); - } - - @Override - public LongTypeConfigurator min(long min) { - return rule(position -> new MinLongRule(position, min)); - } - - @Override - public LongTypeConfigurator max(long max) { - return rule(position -> new MaxLongRule(position, max)); - } - - @Override - public LongTypeConfigurator domain(Long... values) { - return rule(position -> new DomainLongRule(position, Arrays.asList(values))); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/RefRuleConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/RefRuleConfigurator.java deleted file mode 100644 index e836843..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/RefRuleConfigurator.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -/** - * Configurate the reference column rule. - * - * @param the type of rule configurator returned. - */ -public interface RefRuleConfigurator> { - - /** - * creates a rule for not empty column. - */ - T filled(); - - /** - * creates a rule for column with specific values. - */ - T filled(Object... values); - - /** - * creates a rule for column with empty value. - */ - T empty(); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/RefRuleConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/RefRuleConfiguratorImpl.java deleted file mode 100644 index 4b31e08..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/RefRuleConfiguratorImpl.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import com.jonpereiradev.jfile.reader.rule.RuleNodeImpl; -import com.jonpereiradev.jfile.reader.rule.column.*; - -import java.util.Arrays; - -final class RefRuleConfiguratorImpl> implements RefRuleConfigurator { - - private final int refPosition; - private final int position; - private final T currentConfigurator; - private final RuleNode rule; - - RefRuleConfiguratorImpl(int refPosition, int position, RuleNode ruleNode, T currentConfigurator) { - this.refPosition = refPosition; - this.position = position; - this.currentConfigurator = currentConfigurator; - this.rule = ruleNode; - } - - @Override - public T filled() { - RefRule ref = new FilledRefRule(refPosition, position); - ref.setRuleNode(new RuleNodeImpl<>(ref.getClass(), rule)); - rule.add(ref); - currentConfigurator.setRuleNode(ref.getRuleNode()); - return currentConfigurator; - } - - @Override - @SuppressWarnings("unchecked") - public T filled(Object... values) { - RefRule ref = new DomainRefRule(refPosition, position, Arrays.asList(values)); - ref.setRuleNode(new RuleNodeImpl<>(ref.getClass(), rule)); - rule.add(ref); - currentConfigurator.setRuleNode(ref.getRuleNode()); - return currentConfigurator; - } - - @Override - public T empty() { - RefRule ref = new EmptyRefRule(refPosition, position); - ref.setRuleNode(new RuleNodeImpl<>(ref.getClass(), rule)); - rule.add(ref); - currentConfigurator.setRuleNode(ref.getRuleNode()); - return currentConfigurator; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/ShortTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/ShortTypeConfigurator.java deleted file mode 100644 index 08f1883..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/ShortTypeConfigurator.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -public interface ShortTypeConfigurator extends TypedRuleConfigurator { - - /** - * defines a min value rule validation. - */ - ShortTypeConfigurator min(short min); - - /** - * defines a max value rule validation. - */ - ShortTypeConfigurator max(short max); - - /** - * defines a domain rule validation with possible values options. - */ - ShortTypeConfigurator domain(Short... values); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/ShortTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/ShortTypeConfiguratorImpl.java deleted file mode 100644 index a7c0e4a..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/ShortTypeConfiguratorImpl.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import com.jonpereiradev.jfile.reader.rule.column.ColumnRule; -import com.jonpereiradev.jfile.reader.rule.column.DomainShortRule; -import com.jonpereiradev.jfile.reader.rule.column.MaxShortRule; -import com.jonpereiradev.jfile.reader.rule.column.MinShortRule; - -import java.util.Arrays; - -final class ShortTypeConfiguratorImpl extends AbstractRuleConfigurator implements ShortTypeConfigurator { - - ShortTypeConfiguratorImpl(int position, ReaderConfiguration configuration, RuleNode ruleNode) { - super(position, configuration, ruleNode); - } - - @Override - public ShortTypeConfigurator min(short min) { - return rule(position -> new MinShortRule(position, min)); - } - - @Override - public ShortTypeConfigurator max(short max) { - return rule(position -> new MaxShortRule(position, max)); - } - - @Override - public ShortTypeConfigurator domain(Short... values) { - return rule(position -> new DomainShortRule(position, Arrays.asList(values))); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/StringTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/StringTypeConfigurator.java deleted file mode 100644 index 88a8823..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/StringTypeConfigurator.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import java.util.regex.Pattern; - -public interface StringTypeConfigurator extends TypedRuleConfigurator { - - /** - * defines a not empty rule validation. - */ - StringTypeConfigurator notEmpty(); - - /** - * defines a min length rule validation. - */ - StringTypeConfigurator min(int min); - - /** - * defines a max length rule validation. - */ - StringTypeConfigurator max(int max); - - /** - * defines a domain rule validation with possible values options. - */ - StringTypeConfigurator domain(String... values); - - /** - * defines an email rule validation. - */ - StringTypeConfigurator email(); - - /** - * defines a brazilian document CPF rule validation. - */ - StringTypeConfigurator cpf(); - - /** - * defines a brazilian document CNPJ rule validation. - */ - StringTypeConfigurator cnpj(); - - /** - * defines a pattern rule validation. - */ - StringTypeConfigurator regex(Pattern pattern); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/StringTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/StringTypeConfiguratorImpl.java deleted file mode 100644 index a7e6419..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/StringTypeConfiguratorImpl.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import com.jonpereiradev.jfile.reader.rule.column.*; - -import java.util.Arrays; -import java.util.regex.Pattern; - -final class StringTypeConfiguratorImpl extends AbstractRuleConfigurator implements StringTypeConfigurator { - - StringTypeConfiguratorImpl(int position, ReaderConfiguration configuration, RuleNode ruleNode) { - super(position, configuration, ruleNode); - } - - @Override - public StringTypeConfigurator notEmpty() { - return rule(NotEmptyRule::new); - } - - @Override - public StringTypeConfigurator min(int min) { - return rule(position -> new MinStringRule(position, min)); - } - - @Override - public StringTypeConfigurator max(int max) { - return rule(position -> new MaxStringRule(position, max)); - } - - @Override - public StringTypeConfigurator domain(String... values) { - return rule(position -> new DomainStringRule(position, Arrays.asList(values))); - } - - @Override - public StringTypeConfigurator email() { - return rule(EmailRule::new); - } - - @Override - public StringTypeConfigurator cpf() { - return rule(CpfRule::new); - } - - @Override - public StringTypeConfigurator cnpj() { - return rule(CnpjRule::new); - } - - @Override - public StringTypeConfigurator regex(Pattern pattern) { - return rule(position -> new RegexRule(position, pattern)); - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/TypedRuleConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/TypedRuleConfigurator.java deleted file mode 100644 index e556849..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/TypedRuleConfigurator.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import com.jonpereiradev.jfile.reader.rule.column.ColumnRule; - -import java.util.function.Function; - -public interface TypedRuleConfigurator> { - - /** - * apply the rule of not null validation. - */ - T notNull(); - - /** - * apply the rule of only null validation. - */ - T onlyNull(); - - /** - * apply the current depends configuration and return to the root configuration node. - */ - T apply(); - - /** - * define a custom rule validation. - */ - T rule(Function rule); - - /** - * creates the rule configuration for the column at position. - */ - GenericTypeConfigurator column(int position); - - /** - * creates the dependency rule validation between columns. - */ - RefRuleConfigurator depends(int column); - - /** - * changes the current node rule validation. - */ - void setRuleNode(RuleNode ruleNode); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/file/FileRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/file/FileRule.java deleted file mode 100644 index 48a7bca..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/file/FileRule.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.file; - -import com.jonpereiradev.jfile.reader.rule.Rule; - -import java.io.File; - -public interface FileRule extends Rule { - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/line/LineColumnSizeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/line/LineColumnSizeRule.java deleted file mode 100644 index 7d71307..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/line/LineColumnSizeRule.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.line; - -import com.jonpereiradev.jfile.reader.file.JFileLine; - -public class LineColumnSizeRule implements LineRule { - - private final int size; - - public LineColumnSizeRule(int size) { - this.size = size; - } - - public boolean isValid(JFileLine fileLine) { - return fileLine.getColumns().size() == size; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/line/LineRule.java b/src/main/java/com/jonpereiradev/jfile/reader/rule/line/LineRule.java deleted file mode 100644 index c3c83d5..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/line/LineRule.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.line; - -import com.jonpereiradev.jfile.reader.file.JFileLine; -import com.jonpereiradev.jfile.reader.rule.Rule; -import org.apache.commons.lang3.StringUtils; - -public interface LineRule extends Rule { - - @Override - default boolean canValidate(JFileLine fileLine) { - return StringUtils.isNotBlank(fileLine.getContent()); - } - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/stream/AbstractStreamReader.java b/src/main/java/com/jonpereiradev/jfile/reader/stream/AbstractStreamReader.java deleted file mode 100644 index 6c974dd..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/stream/AbstractStreamReader.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.jonpereiradev.jfile.reader.stream; - -import java.io.InputStream; -import java.nio.charset.Charset; - -public abstract class AbstractStreamReader implements StreamReader { - - private final InputStream inputStream; - private final Charset charset; - - protected AbstractStreamReader(InputStream inputStream, Charset charset) { - this.inputStream = inputStream; - this.charset = charset; - } - - public InputStream getInputStream() { - return inputStream; - } - - public Charset getCharset() { - return charset; - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/stream/DefaultStreamReader.java b/src/main/java/com/jonpereiradev/jfile/reader/stream/DefaultStreamReader.java deleted file mode 100644 index d66edd2..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/stream/DefaultStreamReader.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.jonpereiradev.jfile.reader.stream; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.nio.charset.Charset; -import java.util.Iterator; -import java.util.NoSuchElementException; - -final class DefaultStreamReader extends AbstractStreamReader { - - private final BufferedReader reader; - - DefaultStreamReader(InputStream inputStream, Charset charset) { - super(inputStream, charset); - this.reader = new BufferedReader(new InputStreamReader(inputStream, charset)); - } - - @Override - public Iterator iterator() { - return new DefaultStreamReaderIterator(); - } - - @Override - public void close() throws IOException { - reader.close(); - } - - private class DefaultStreamReaderIterator implements Iterator { - - private String currentLine; - - @Override - public boolean hasNext() { - if (currentLine == null) { - try { - currentLine = reader.readLine(); - } catch (IOException e) { - throw new IllegalStateException(e); - } - } - - return currentLine != null; - } - - @Override - public String next() { - if (currentLine == null) { - try { - currentLine = reader.readLine(); - } catch (IOException e) { - throw new IllegalStateException(e); - } - } - - String line = currentLine; - currentLine = null; - - if (line == null) { - throw new NoSuchElementException("No more lines to read."); - } - - return line; - } - } -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/stream/StreamReader.java b/src/main/java/com/jonpereiradev/jfile/reader/stream/StreamReader.java deleted file mode 100644 index b01c3ff..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/stream/StreamReader.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.jonpereiradev.jfile.reader.stream; - -import java.io.Closeable; -import java.io.InputStream; -import java.nio.charset.Charset; - -public interface StreamReader extends Iterable, Closeable { - - static StreamReader defaultStreamReader(InputStream inputStream, Charset charset) { - return new DefaultStreamReader(inputStream, charset); - } - - /** - * @return the input stream with the file content. - */ - InputStream getInputStream(); - - /** - * @return the charset of the file content. - */ - Charset getCharset(); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validation/JFileValidatorEngine.java b/src/main/java/com/jonpereiradev/jfile/reader/validation/JFileValidatorEngine.java deleted file mode 100644 index c9b3720..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/validation/JFileValidatorEngine.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.jonpereiradev.jfile.reader.validation; - -import com.jonpereiradev.jfile.reader.JFileReaderContext; -import com.jonpereiradev.jfile.reader.file.JFileLine; -import com.jonpereiradev.jfile.reader.rule.RuleViolation; - -import java.util.List; - -public interface JFileValidatorEngine { - - static JFileValidatorEngine defaultEngine(JFileReaderContext context) { - return new JFileValidatorEngineImpl(context); - } - - List validate(JFileLine line); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validation/JFileValidatorEngineImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validation/JFileValidatorEngineImpl.java deleted file mode 100644 index f303692..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/validation/JFileValidatorEngineImpl.java +++ /dev/null @@ -1,122 +0,0 @@ -package com.jonpereiradev.jfile.reader.validation; - -import com.jonpereiradev.jfile.reader.JFileReaderContext; -import com.jonpereiradev.jfile.reader.file.JFileColumn; -import com.jonpereiradev.jfile.reader.file.JFileLine; -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import com.jonpereiradev.jfile.reader.rule.RuleViolation; -import com.jonpereiradev.jfile.reader.rule.column.ArrayOfTypeRule; -import com.jonpereiradev.jfile.reader.rule.column.ColumnRule; -import com.jonpereiradev.jfile.reader.rule.column.RefRule; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.SortedSet; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -final class JFileValidatorEngineImpl implements JFileValidatorEngine { - - private final JFileReaderContext context; - - JFileValidatorEngineImpl(JFileReaderContext context) { - this.context = context; - } - - @Override - public List validate(JFileLine line) { - List violations = new ArrayList<>(); - - checkLineRuleViolation(line, violations); - - if (violations.isEmpty()) { - checkColumnRuleViolation(line, violations); - } - - return Collections.unmodifiableList(violations); - } - - private void checkLineRuleViolation(JFileLine line, List violations) { - context.getRuleConfiguration().getLineRootNode().forEach(rule -> { - if (rule.canValidate(line) && !rule.isValid(line)) { - RuleViolation violation = new RuleViolation(); - - violation.setRow(line.getRow()); - violation.setColumn(-1); - violation.setContent(line.getContent()); - violation.setRule(rule.getClass().getSimpleName()); - - violations.add(violation); - } - }); - } - - private void checkColumnRuleViolation(JFileLine line, List violations) { - SortedSet columns = line.getColumns(); - - columns.forEach(column -> { - RuleNode rules = context.getRuleConfiguration().getColumnRootNode(); - violations.addAll(validateColumnRules(line, column, rules)); - }); - } - - private List validateColumnRules(JFileLine fileLine, JFileColumn column, RuleNode node) { - Stream stream = node.getChildrens().stream(); - List filtered = stream.filter(o -> o.getPosition() == column.getPosition()).collect(Collectors.toList()); - List violations = new ArrayList<>(); - - for (ColumnRule rule : filtered) { - JFileColumn fileColumn = isRefRule(rule) ? getDependsColumn(fileLine, (RefRule) rule) : column; - - rule.setFileLine(fileLine); - - if (rule instanceof ArrayOfTypeRule) { - ArrayOfTypeRule arrayOf = (ArrayOfTypeRule) rule; - arrayOf.split(column).forEach(o -> violations.addAll(validateColumnRules(fileLine, o, rule.getRuleNode()))); - } else if (rule.canValidate(fileColumn)) { - recursivelyValidate(fileLine, column, rule, violations); - } - - if (!violations.isEmpty()) { - break; - } - } - - return violations; - } - - private void recursivelyValidate(JFileLine line, JFileColumn column, ColumnRule rule, List violations) { - if (!rule.isValid(column)) { - createViolation(line, column, rule, violations); - } else { - violations.addAll(validateColumnRules(line, column, rule.getRuleNode())); - } - } - - private void createViolation(JFileLine line, JFileColumn column, ColumnRule rule, List violations) { - RuleViolation violation = new RuleViolation(); - - violation.setRow(line.getRow()); - violation.setColumn(column.getPosition()); - violation.setContent(column.getText()); - violation.setRule(rule.getClass().getName()); - - violations.add(violation); - } - - private boolean isRefRule(ColumnRule rule) { - return rule instanceof RefRule && ((RefRule) rule).getRefPosition() != -1; - } - - private JFileColumn getDependsColumn(JFileLine line, RefRule refRule) { - JFileColumn refColumn = line.getColumn(refRule.getRefPosition()); - - if (refColumn == null) { - throw new NullPointerException("Column doesn't exists at " + refRule.getRefPosition() + "position"); - } - - return refColumn; - } - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validation/Report.java b/src/main/java/com/jonpereiradev/jfile/reader/validation/Report.java deleted file mode 100644 index 162013f..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/validation/Report.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.jonpereiradev.jfile.reader.validation; - -import com.jonpereiradev.jfile.reader.rule.RuleViolation; - -import java.util.List; - -public interface Report { - - static Report defaultReport() { - return new ReportImpl(); - } - - void put(int row, RuleViolation violation); - - void put(int row, List violations); - - boolean isValid(); - - boolean isInvalid(); - - List getViolations(int row); - - List getViolations(int row, int position); - - List getViolations(); - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validation/ReportImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validation/ReportImpl.java deleted file mode 100644 index 8dcd1db..0000000 --- a/src/main/java/com/jonpereiradev/jfile/reader/validation/ReportImpl.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.jonpereiradev.jfile.reader.validation; - -import com.jonpereiradev.jfile.reader.rule.RuleViolation; - -import java.util.*; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -final class ReportImpl implements Report { - - private final Map violationsPerRow = new TreeMap<>(); - - public void put(int row, RuleViolation violation) { - if (!violationsPerRow.containsKey(row)) { - violationsPerRow.put(row, new ReportLineValidation()); - } - - violationsPerRow.get(row).add(violation); - } - - public void put(int row, List violations) { - if (!violationsPerRow.containsKey(row)) { - violationsPerRow.put(row, new ReportLineValidation()); - } - - violationsPerRow.get(row).addAll(violations); - } - - @Override - public boolean isValid() { - return getViolations().isEmpty(); - } - - @Override - public boolean isInvalid() { - return !getViolations().isEmpty(); - } - - @Override - public List getViolations(int row) { - if (violationsPerRow.containsKey(row)) { - return Collections.unmodifiableList(violationsPerRow.get(row).violations); - } - - return Collections.emptyList(); - } - - @Override - public List getViolations(int row, int position) { - if (violationsPerRow.containsKey(row)) { - Stream stream = violationsPerRow.get(row).violations.stream().filter(o -> o.getColumn() == position); - return Collections.unmodifiableList(stream.collect(Collectors.toList())); - } - - return Collections.emptyList(); - } - - @Override - public List getViolations() { - return violationsPerRow.entrySet().stream().flatMap(o -> o.getValue().violations.stream()).collect(Collectors.toList()); - } - - private class ReportLineValidation { - - private final List violations; - - private ReportLineValidation() { - this.violations = new ArrayList<>(); - } - - void add(RuleViolation violation) { - this.violations.add(violation); - } - - void addAll(List violations) { - this.violations.addAll(violations); - } - } - -} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileRuleConfig.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileRuleConfig.java new file mode 100644 index 0000000..5dccd22 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileRuleConfig.java @@ -0,0 +1,45 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator; + + +import com.jonpereiradev.jfile.reader.validator.rule.configurator.ColumnRuleConfigurator; +import com.jonpereiradev.jfile.reader.validator.rule.configurator.FileRuleConfigurator; +import com.jonpereiradev.jfile.reader.validator.rule.configurator.LineRuleConfigurator; + + +/** + * @author jonpereiradev + * @see JFileValidator + * @since 0.1.0 + */ +public interface JFileRuleConfig { + + FileRuleConfigurator files(); + + LineRuleConfigurator lines(); + + ColumnRuleConfigurator columns(); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileRuleConfigImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileRuleConfigImpl.java new file mode 100644 index 0000000..e575691 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileRuleConfigImpl.java @@ -0,0 +1,55 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator; + + +import com.jonpereiradev.jfile.reader.validator.rule.configurator.ColumnRuleConfigurator; +import com.jonpereiradev.jfile.reader.validator.rule.configurator.FileRuleConfigurator; +import com.jonpereiradev.jfile.reader.validator.rule.configurator.LineRuleConfigurator; + + +final class JFileRuleConfigImpl implements JFileRuleConfig { + + private final JFileValidatorConfig configuration; + + JFileRuleConfigImpl(JFileValidatorConfig configuration) { + this.configuration = configuration; + } + + @Override + public FileRuleConfigurator files() { + return FileRuleConfigurator.defaultConfigurator(configuration); + } + + @Override + public LineRuleConfigurator lines() { + return LineRuleConfigurator.defaultConfigurator(configuration); + } + + @Override + public ColumnRuleConfigurator columns() { + return ColumnRuleConfigurator.defaultConfigurator(configuration); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileValidator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileValidator.java new file mode 100644 index 0000000..03ca022 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileValidator.java @@ -0,0 +1,49 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator; + + +import com.jonpereiradev.jfile.reader.file.LineValue; + + +/** + * Point of access for file validation. + * + * @author jonpereiradev + * @see JFileValidatorFactory + * @see JFileValidatorConfig + * @since 0.1.0 + */ +public interface JFileValidator { + + /** + * Validates all lines of the file with the configured rule. + * + * @param lineValue the object that will be validated. + * + * @return all violations of the file. + */ + ValidationReport validate(LineValue lineValue); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileValidatorConfig.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileValidatorConfig.java new file mode 100644 index 0000000..9c4d7e1 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileValidatorConfig.java @@ -0,0 +1,59 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator; + + +import com.jonpereiradev.jfile.reader.JFilePatternConfig; +import com.jonpereiradev.jfile.reader.validator.rule.RuleRoot; + + +/** + * @author jonpereiradev + * @see JFileValidator + * @see JFileValidatorFactory + * @since 0.1.0 + */ +public interface JFileValidatorConfig extends JFilePatternConfig, JFileRuleConfig { + + /** + * Configure the max violation size to limit the number of errors that a file can have before stops the validation. + * The default is to not have a limit. + * + * @param maxViolationSize an int number for the max violation size. + * + * @return the object with the maxViolationSize configured. + */ + JFileValidatorConfig maxViolationSize(int maxViolationSize); + + /** + * @return the max violation size configured. + */ + int getMaxViolationSize(); + + /** + * @return the root of all rules configured for the validator. + */ + RuleRoot getRuleRoot(); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileValidatorConfigImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileValidatorConfigImpl.java new file mode 100644 index 0000000..5cd6d5b --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileValidatorConfigImpl.java @@ -0,0 +1,144 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator; + + +import com.jonpereiradev.jfile.reader.JFilePatternConfig; +import com.jonpereiradev.jfile.reader.validator.rule.RuleRoot; +import com.jonpereiradev.jfile.reader.validator.rule.RuleRootImpl; +import com.jonpereiradev.jfile.reader.validator.rule.configurator.ColumnRuleConfigurator; +import com.jonpereiradev.jfile.reader.validator.rule.configurator.FileRuleConfigurator; +import com.jonpereiradev.jfile.reader.validator.rule.configurator.LineRuleConfigurator; + +import java.text.DateFormat; +import java.text.DecimalFormat; +import java.time.format.DateTimeFormatter; + + +final class JFileValidatorConfigImpl implements JFileValidatorConfig { + + private final RuleRoot ruleRoot; + private final JFileRuleConfig ruleConfig; + + private DateFormat dateFormat; + private DateTimeFormatter localDateFormatter; + private DateTimeFormatter localDateTimeFormatter; + private DecimalFormat bigDecimalFormatter; + private int maxViolationSize = -1; + + JFileValidatorConfigImpl() { + this.dateFormat = DateFormat.getInstance(); + this.localDateFormatter = DateTimeFormatter.ISO_LOCAL_DATE; + this.localDateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; + this.bigDecimalFormatter = new DecimalFormat(); + this.bigDecimalFormatter.setParseBigDecimal(true); + this.ruleRoot = new RuleRootImpl(); + this.ruleConfig = new JFileRuleConfigImpl(this); + } + + JFileValidatorConfigImpl(JFilePatternConfig filePatternConfig) { + this.dateFormat = filePatternConfig.getDateFormat(); + this.localDateFormatter = filePatternConfig.getLocalDateFormatter(); + this.localDateTimeFormatter = filePatternConfig.getLocalDateTimeFormatter(); + this.bigDecimalFormatter = filePatternConfig.getBigDecimalFormatter(); + this.ruleRoot = new RuleRootImpl(); + this.ruleConfig = new JFileRuleConfigImpl(this); + } + + @Override + public JFileValidatorConfig maxViolationSize(int maxViolationSize) { + this.maxViolationSize = maxViolationSize; + return this; + } + + @Override + public int getMaxViolationSize() { + return maxViolationSize; + } + + @Override + public RuleRoot getRuleRoot() { + return ruleRoot; + } + + @Override + public FileRuleConfigurator files() { + return ruleConfig.files(); + } + + @Override + public LineRuleConfigurator lines() { + return ruleConfig.lines(); + } + + @Override + public ColumnRuleConfigurator columns() { + return ruleConfig.columns(); + } + + @Override + public JFileValidatorConfig dateFormatter(DateFormat dateFormat) { + this.dateFormat = dateFormat; + return this; + } + + @Override + public DateFormat getDateFormat() { + return dateFormat; + } + + @Override + public JFileValidatorConfig localDateFormatter(DateTimeFormatter localDateFormatter) { + this.localDateFormatter = localDateFormatter; + return this; + } + + @Override + public DateTimeFormatter getLocalDateFormatter() { + return localDateFormatter; + } + + @Override + public JFileValidatorConfig localDateTimeFormatter(DateTimeFormatter localDateTimeFormatter) { + this.localDateTimeFormatter = localDateTimeFormatter; + return this; + } + + @Override + public DateTimeFormatter getLocalDateTimeFormatter() { + return localDateTimeFormatter; + } + + @Override + public JFileValidatorConfig bigDecimalFormat(DecimalFormat decimalFormat) { + this.bigDecimalFormatter = decimalFormat; + return this; + } + + @Override + public DecimalFormat getBigDecimalFormatter() { + return bigDecimalFormatter; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileValidatorEngine.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileValidatorEngine.java new file mode 100644 index 0000000..927e40e --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileValidatorEngine.java @@ -0,0 +1,178 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; +import com.jonpereiradev.jfile.reader.file.LineValue; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolation; +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolationImpl; +import com.jonpereiradev.jfile.reader.validator.rule.column.ArrayOfTypeRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.RefRule; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.SortedSet; +import java.util.stream.Collectors; + + +final class JFileValidatorEngine implements JFileValidator { + + private final JFileValidatorConfig validatorConfig; + + JFileValidatorEngine(JFileValidatorConfig validatorConfig) { + this.validatorConfig = validatorConfig; + } + + @Override + public ValidationReport validate(LineValue lineValue) { + List ruleViolations = validateLine(lineValue); + ValidationReportImpl validationReport = new ValidationReportImpl(); + + validationReport.put(lineValue.getLineNumber(), ruleViolations); + + return validationReport; + } + + private List validateLine(LineValue lineValue) { + List violations = new ArrayList<>(); + + checkLineRuleViolation(lineValue, violations); + + if (violations.isEmpty()) { + checkColumnRuleViolation(lineValue, violations); + } + + return Collections.unmodifiableList(violations); + } + + private void checkLineRuleViolation(LineValue lineValue, List violations) { + validatorConfig.getRuleRoot().getLineRootNode().forEach(rule -> { + if (rule.canValidate(lineValue) && !rule.isValid(lineValue)) { + RuleViolationImpl violation = new RuleViolationImpl(); + + violation.setLineNumber(lineValue.getLineNumber()); + violation.setColumnNumber(-1); + violation.setContent(lineValue.getContent()); + violation.setRule(rule.getClass().getSimpleName()); + + violations.add(violation); + } + }); + } + + private void checkColumnRuleViolation(LineValue lineValue, List violations) { + SortedSet columnValues = lineValue.getColumnValues(); + + columnValues.forEach(columnValue -> { + RuleNode columnRootNode = validatorConfig.getRuleRoot().getColumnRootNode(); + violations.addAll(validateColumnRules(lineValue, columnValue, columnRootNode)); + }); + } + + private List validateColumnRules( + LineValue lineValue, + ColumnValue columnValue, + RuleNode ruleNode) { + List filtered = ruleNode + .getChildren() + .stream() + .filter(o -> o.getColumnNumber() == columnValue.getColumnNumber()) + .collect(Collectors.toList()); + + List violations = new ArrayList<>(); + + for (ColumnRule columnRule : filtered) { + ColumnValue fileColumn = columnValue; + + if (isRefRule(columnRule)) { + fileColumn = getDependsColumn(lineValue, (RefRule) columnRule); + } + + columnRule.setLineValue(lineValue); + + if (columnRule instanceof ArrayOfTypeRule) { + ArrayOfTypeRule arrayOf = (ArrayOfTypeRule) columnRule; + arrayOf.split(columnValue).forEach(o -> violations.addAll(validateColumnRules( + lineValue, + o, + columnRule.getRuleNode() + ))); + } else if (columnRule.canValidate(fileColumn)) { + recursivelyValidate(lineValue, columnValue, columnRule, violations); + } + + if (!violations.isEmpty()) { + break; + } + } + + return violations; + } + + private void recursivelyValidate( + LineValue lineValue, + ColumnValue columnValue, + ColumnRule columnRule, + List ruleViolations) { + if (!columnRule.isValid(columnValue)) { + createViolation(lineValue, columnValue, columnRule, ruleViolations); + } else { + ruleViolations.addAll(validateColumnRules(lineValue, columnValue, columnRule.getRuleNode())); + } + } + + private void createViolation( + LineValue lineValue, + ColumnValue columnValue, + ColumnRule columnRule, + List ruleViolations) { + RuleViolationImpl violation = new RuleViolationImpl(); + + violation.setLineNumber(lineValue.getLineNumber()); + violation.setColumnNumber(columnValue.getColumnNumber()); + violation.setContent(columnValue.getText()); + violation.setRule(columnRule.getClass().getName()); + + ruleViolations.add(violation); + } + + private boolean isRefRule(ColumnRule columnRule) { + return columnRule instanceof RefRule && ((RefRule) columnRule).getRefColumnNumber() != -1; + } + + private ColumnValue getDependsColumn(LineValue lineValue, RefRule refRule) { + ColumnValue refColumn = lineValue.getColumnValue(refRule.getRefColumnNumber()); + + if (refColumn == null) { + throw new NullPointerException("Column doesn't exists at " + refRule.getRefColumnNumber() + "position"); + } + + return refColumn; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileValidatorFactory.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileValidatorFactory.java new file mode 100644 index 0000000..170b14e --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/JFileValidatorFactory.java @@ -0,0 +1,70 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator; + + +import com.jonpereiradev.jfile.reader.JFilePatternConfig; + + +/** + * Factory to create the {@link JFileValidator} implementation. + */ +public final class JFileValidatorFactory { + + private JFileValidatorFactory() { + throw new UnsupportedOperationException(); + } + + /** + * Create a File Validator with the desired parameters. + * + * @param validatorConfig the config that tells how to validate the file. + * + * @return an instance of JFileReader for the file and config specified. + */ + public static JFileValidator newJFileValidator(JFileValidatorConfig validatorConfig) { + return new JFileValidatorEngine(validatorConfig); + } + + /** + * Create a File Validator Config to configure the validator engine. + * + * @return a new instance of the validator config. + */ + public static JFileValidatorConfig newValidatorConfig() { + return new JFileValidatorConfigImpl(); + } + + /** + * Create a File Validator Config from a Pattern Config to configure the validator engine. + * + * @param filePatternConfig the config that will be copy to the new validator config. + * + * @return a new instance of the validator config. + */ + public static JFileValidatorConfig newValidatorConfig(JFilePatternConfig filePatternConfig) { + return new JFileValidatorConfigImpl(filePatternConfig); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/ValidationReport.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/ValidationReport.java new file mode 100644 index 0000000..559193b --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/ValidationReport.java @@ -0,0 +1,51 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator; + + +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolation; + +import java.util.List; + + +/** + * Class with all information about the violations generated by {@link JFileValidator}. + * + * @author jonpereiradev + * @see JFileValidator + * @since 0.1.0 + */ +public interface ValidationReport { + + boolean isValid(); + + boolean isNotValid(); + + List getViolations(int lineNumber); + + List getViolations(int lineNumber, int columnNumber); + + List getViolations(); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/ValidationReportImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/ValidationReportImpl.java new file mode 100644 index 0000000..07439f1 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/ValidationReportImpl.java @@ -0,0 +1,119 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator; + + +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolation; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import java.util.stream.Collectors; +import java.util.stream.Stream; + + +final class ValidationReportImpl implements ValidationReport { + + private final Map violationsPerRow = new TreeMap<>(); + + void put(int lineNumber, RuleViolation ruleViolation) { + if (!violationsPerRow.containsKey(lineNumber)) { + violationsPerRow.put(lineNumber, new ReportLineValidation()); + } + + violationsPerRow.get(lineNumber).add(ruleViolation); + } + + void put(int lineNumber, List ruleViolations) { + if (!violationsPerRow.containsKey(lineNumber)) { + violationsPerRow.put(lineNumber, new ReportLineValidation()); + } + + violationsPerRow.get(lineNumber).addAll(ruleViolations); + } + + @Override + public boolean isValid() { + return getViolations().isEmpty(); + } + + @Override + public boolean isNotValid() { + return !isValid(); + } + + @Override + public List getViolations(int lineNumber) { + if (violationsPerRow.containsKey(lineNumber)) { + return Collections.unmodifiableList(violationsPerRow.get(lineNumber).ruleViolations); + } + + return Collections.emptyList(); + } + + @Override + public List getViolations(int lineNumber, int columnNumber) { + if (violationsPerRow.containsKey(lineNumber)) { + Stream stream = violationsPerRow + .get(lineNumber) + .ruleViolations + .stream() + .filter(o -> o.getColumnNumber() == columnNumber); + + return Collections.unmodifiableList(stream.collect(Collectors.toList())); + } + + return Collections.emptyList(); + } + + @Override + public List getViolations() { + return violationsPerRow + .entrySet() + .stream() + .flatMap(o -> o.getValue().ruleViolations.stream()) + .collect(Collectors.toList()); + } + + private class ReportLineValidation { + + private final List ruleViolations; + + private ReportLineValidation() { + this.ruleViolations = new ArrayList<>(); + } + + void add(RuleViolation ruleViolation) { + this.ruleViolations.add(ruleViolation); + } + + void addAll(List ruleViolations) { + this.ruleViolations.addAll(ruleViolations); + } + + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/Rule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/Rule.java new file mode 100644 index 0000000..84d5afd --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/Rule.java @@ -0,0 +1,37 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface Rule { + + boolean isValid(T object); + + boolean canValidate(T object); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleNode.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleNode.java new file mode 100644 index 0000000..5baefb2 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleNode.java @@ -0,0 +1,44 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule; + + +import java.util.List; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface RuleNode> extends Iterable { + + Class getType(); + + void add(T rule); + + RuleNode getParentNode(); + + List getChildren(); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleNodeImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleNodeImpl.java new file mode 100644 index 0000000..fb1eea3 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleNodeImpl.java @@ -0,0 +1,69 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule; + + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + + +public class RuleNodeImpl> implements RuleNode { + + private final Class type; + private final RuleNode parentNode; + private final List rules = new ArrayList<>(); + + public RuleNodeImpl(Class type, RuleNode parentNode) { + this.type = type; + this.parentNode = parentNode; + } + + @Override + public Class getType() { + return type; + } + + @Override + public void add(T rule) { + this.rules.add(rule); + } + + @Override + public RuleNode getParentNode() { + return parentNode; + } + + @Override + public List getChildren() { + return Collections.unmodifiableList(rules); + } + + @Override + public Iterator iterator() { + return rules.iterator(); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleRoot.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleRoot.java new file mode 100644 index 0000000..8d730be --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleRoot.java @@ -0,0 +1,44 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule; + + +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; +import com.jonpereiradev.jfile.reader.validator.rule.file.FileRule; +import com.jonpereiradev.jfile.reader.validator.rule.line.LineRule; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface RuleRoot { + + RuleNode getFileRootNode(); + + RuleNode getLineRootNode(); + + RuleNode getColumnRootNode(); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleRootImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleRootImpl.java new file mode 100644 index 0000000..527be23 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleRootImpl.java @@ -0,0 +1,53 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule; + + +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; +import com.jonpereiradev.jfile.reader.validator.rule.file.FileRule; +import com.jonpereiradev.jfile.reader.validator.rule.line.LineRule; + + +public final class RuleRootImpl implements RuleRoot { + + private final RuleNode fileRuleNode = new RuleNodeImpl<>(null, null); + private final RuleNode lineRuleNode = new RuleNodeImpl<>(null, null); + private final RuleNode columnRuleNode = new RuleNodeImpl<>(null, null); + + @Override + public RuleNode getFileRootNode() { + return fileRuleNode; + } + + @Override + public RuleNode getLineRootNode() { + return lineRuleNode; + } + + @Override + public RuleNode getColumnRootNode() { + return columnRuleNode; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleUtils.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleUtils.java new file mode 100644 index 0000000..a8963f2 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleUtils.java @@ -0,0 +1,145 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule; + + +public class RuleUtils { + + private RuleUtils() { + } + + public static boolean isNotEmpty(final CharSequence cs) { + return !isEmpty(cs); + } + + public static boolean isEmpty(final CharSequence cs) { + return cs == null || cs.length() == 0; + } + + public static boolean isNotBlank(final CharSequence cs) { + return !isBlank(cs); + } + + public static boolean isBlank(final CharSequence cs) { + int strLen; + if (cs == null || (strLen = cs.length()) == 0) { + return true; + } + for (int i = 0; i < strLen; i++) { + if (!Character.isWhitespace(cs.charAt(i))) { + return false; + } + } + return true; + } + + public static String trimToEmpty(final String str) { + return str == null ? "" : str.trim(); + } + + public static Boolean toBooleanObject(final String str) { + if (str == "true") { + return Boolean.TRUE; + } + if (str == null) { + return null; + } + switch (str.length()) { + case 1: { + final char ch0 = str.charAt(0); + if (ch0 == 'y' || ch0 == 'Y' || + ch0 == 't' || ch0 == 'T') { + return Boolean.TRUE; + } + if (ch0 == 'n' || ch0 == 'N' || + ch0 == 'f' || ch0 == 'F') { + return Boolean.FALSE; + } + break; + } + case 2: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'n' || ch1 == 'N')) { + return Boolean.TRUE; + } + if ((ch0 == 'n' || ch0 == 'N') && + (ch1 == 'o' || ch1 == 'O')) { + return Boolean.FALSE; + } + break; + } + case 3: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + if ((ch0 == 'y' || ch0 == 'Y') && + (ch1 == 'e' || ch1 == 'E') && + (ch2 == 's' || ch2 == 'S')) { + return Boolean.TRUE; + } + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'f' || ch1 == 'F') && + (ch2 == 'f' || ch2 == 'F')) { + return Boolean.FALSE; + } + break; + } + case 4: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + if ((ch0 == 't' || ch0 == 'T') && + (ch1 == 'r' || ch1 == 'R') && + (ch2 == 'u' || ch2 == 'U') && + (ch3 == 'e' || ch3 == 'E')) { + return Boolean.TRUE; + } + break; + } + case 5: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + final char ch4 = str.charAt(4); + if ((ch0 == 'f' || ch0 == 'F') && + (ch1 == 'a' || ch1 == 'A') && + (ch2 == 'l' || ch2 == 'L') && + (ch3 == 's' || ch3 == 'S') && + (ch4 == 'e' || ch4 == 'E')) { + return Boolean.FALSE; + } + break; + } + default: + break; + } + + return null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleViolation.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleViolation.java new file mode 100644 index 0000000..af7b946 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleViolation.java @@ -0,0 +1,41 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface RuleViolation { + + int getLineNumber(); + + int getColumnNumber(); + + String getContent(); + + String getRule(); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleViolationImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleViolationImpl.java new file mode 100644 index 0000000..ca27b86 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/RuleViolationImpl.java @@ -0,0 +1,84 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule; + + +import java.util.Objects; + + +public final class RuleViolationImpl implements RuleViolation { + + private int lineNumber; + private int columnNumber; + private String content; + private String rule; + + @Override + public int hashCode() { + return Objects.hash(lineNumber, columnNumber, rule); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + RuleViolationImpl that = (RuleViolationImpl) o; + return lineNumber == that.lineNumber && + columnNumber == that.columnNumber && + Objects.equals(rule, that.rule); + } + + public int getLineNumber() { + return lineNumber; + } + + public void setLineNumber(int lineNumber) { + this.lineNumber = lineNumber; + } + + public int getColumnNumber() { + return columnNumber; + } + + public void setColumnNumber(int columnNumber) { + this.columnNumber = columnNumber; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public String getRule() { + return rule; + } + + public void setRule(String rule) { + this.rule = rule; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/AbstractColumnRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/AbstractColumnRule.java new file mode 100644 index 0000000..8dc424c --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/AbstractColumnRule.java @@ -0,0 +1,78 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; +import com.jonpereiradev.jfile.reader.file.LineValue; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.RuleUtils; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public abstract class AbstractColumnRule implements ColumnRule { + + private final int columnNumber; + + private LineValue lineValue; + private RuleNode ruleNode; + + public AbstractColumnRule(int columnNumber) { + this.columnNumber = columnNumber; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return RuleUtils.isNotBlank(columnValue.getText()); + } + + @Override + public int getColumnNumber() { + return columnNumber; + } + + @Override + public LineValue getLineValue() { + return lineValue; + } + + @Override + public void setLineValue(LineValue lineValue) { + this.lineValue = lineValue; + } + + @Override + public RuleNode getRuleNode() { + return ruleNode; + } + + @Override + public void setRuleNode(RuleNode ruleNode) { + this.ruleNode = ruleNode; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/AbstractRefRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/AbstractRefRule.java new file mode 100644 index 0000000..8ce4d1a --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/AbstractRefRule.java @@ -0,0 +1,84 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; +import com.jonpereiradev.jfile.reader.file.LineValue; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public abstract class AbstractRefRule implements RefRule { + + private final int columnNumber; + private final int refColumnNumber; + + private LineValue lineValue; + private RuleNode ruleNode; + + public AbstractRefRule(int refColumnNumber, int columnNumber) { + this.columnNumber = columnNumber; + this.refColumnNumber = refColumnNumber; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return true; + } + + @Override + public int getColumnNumber() { + return columnNumber; + } + + @Override + public LineValue getLineValue() { + return lineValue; + } + + @Override + public void setLineValue(LineValue lineValue) { + this.lineValue = lineValue; + } + + @Override + public RuleNode getRuleNode() { + return ruleNode; + } + + @Override + public void setRuleNode(RuleNode ruleNode) { + this.ruleNode = ruleNode; + } + + @Override + public int getRefColumnNumber() { + return refColumnNumber; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/ArrayOfTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/ArrayOfTypeRule.java new file mode 100644 index 0000000..dc07bc4 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/ArrayOfTypeRule.java @@ -0,0 +1,71 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.util.Arrays; +import java.util.List; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class ArrayOfTypeRule extends AbstractColumnRule { + + private final Pattern pattern; + + public ArrayOfTypeRule(int columnNumber, Pattern pattern) { + super(columnNumber); + this.pattern = pattern; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return true; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return true; + } + + public List split(ColumnValue columnValue) { + String[] values = pattern.split(columnValue.getText()); + + return Arrays + .stream(values) + .map(columnContent -> getColumnValue(columnValue, columnContent)) + .collect(Collectors.toList()); + } + + private ColumnValue getColumnValue(ColumnValue columnValue, String columnContent) { + return ColumnValue.newColumnValue(columnValue.getPatternConfig(), getColumnNumber(), columnContent); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/BigDecimalTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/BigDecimalTypeRule.java new file mode 100644 index 0000000..af6bb15 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/BigDecimalTypeRule.java @@ -0,0 +1,59 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.text.DecimalFormat; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class BigDecimalTypeRule extends AbstractColumnRule { + + private final DecimalFormat decimalFormat; + + public BigDecimalTypeRule(int columnNumber, DecimalFormat decimalFormat) { + super(columnNumber); + this.decimalFormat = decimalFormat; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + try { + return columnValue.getText().isEmpty() || columnValue.getBigDecimal(decimalFormat) != null; + } catch (NumberFormatException e) { + return false; + } + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return true; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/BigIntegerTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/BigIntegerTypeRule.java new file mode 100644 index 0000000..d8f9c46 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/BigIntegerTypeRule.java @@ -0,0 +1,54 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class BigIntegerTypeRule extends AbstractColumnRule { + + public BigIntegerTypeRule(int columnNumber) { + super(columnNumber); + } + + @Override + public boolean isValid(ColumnValue columnValue) { + try { + return columnValue.getText().isEmpty() || columnValue.getBigInteger() != null; + } catch (NumberFormatException e) { + return false; + } + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return true; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/BooleanTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/BooleanTypeRule.java new file mode 100644 index 0000000..36f6303 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/BooleanTypeRule.java @@ -0,0 +1,50 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class BooleanTypeRule extends AbstractColumnRule { + + public BooleanTypeRule(int columnNumber) { + super(columnNumber); + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return columnValue.getText().isEmpty() || columnValue.getBoolean() != null; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return true; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/CharacterTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/CharacterTypeRule.java new file mode 100644 index 0000000..0c1ee94 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/CharacterTypeRule.java @@ -0,0 +1,54 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class CharacterTypeRule extends AbstractColumnRule { + + public CharacterTypeRule(int columnNumber) { + super(columnNumber); + } + + @Override + public boolean isValid(ColumnValue columnValue) { + try { + return columnValue.getText().isEmpty() || columnValue.getCharacter() != null; + } catch (IllegalStateException e) { + return false; + } + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return true; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/CnpjRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/CnpjRule.java new file mode 100644 index 0000000..e31832a --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/CnpjRule.java @@ -0,0 +1,98 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.util.Arrays; +import java.util.List; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class CnpjRule extends AbstractColumnRule { + + private static final List INVALID_CNPJS = Arrays.asList( + "00000000000000", + "11111111111111", + "22222222222222", + "33333333333333", + "44444444444444", + "55555555555555", + "66666666666666", + "77777777777777", + "88888888888888", + "99999999999999" + ); + + public CnpjRule(int columnNumber) { + super(columnNumber); + } + + @Override + public boolean isValid(ColumnValue columnValue) { + String cnpj = columnValue.getText(); + + if (cnpj.length() != 14 || INVALID_CNPJS.contains(cnpj)) { + return false; + } + + int i; + int j; + int digit; + int coeficient; + int sum; + int[] foundDv = {0, 0}; + + int dv1 = Integer.parseInt(String.valueOf(cnpj.charAt(cnpj.length() - 2))); + int dv2 = Integer.parseInt(String.valueOf(cnpj.charAt(cnpj.length() - 1))); + + for (j = 0; j < 2; j++) { + sum = 0; + coeficient = 2; + + for (i = cnpj.length() - 3 + j; i >= 0; i--) { + digit = Integer.parseInt(String.valueOf(cnpj.charAt(i))); + sum += digit * coeficient; + coeficient++; + + if (coeficient > 9) { + coeficient = 2; + } + } + + foundDv[j] = 11 - sum % 11; + + if (foundDv[j] >= 10) { + foundDv[j] = 0; + } + } + + return dv1 == foundDv[0] && dv2 == foundDv[1]; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/ColumnRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/ColumnRule.java new file mode 100644 index 0000000..edd0916 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/ColumnRule.java @@ -0,0 +1,58 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; +import com.jonpereiradev.jfile.reader.file.LineValue; +import com.jonpereiradev.jfile.reader.validator.rule.Rule; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.RuleUtils; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface ColumnRule extends Rule { + + @Override + boolean isValid(ColumnValue columnValue); + + @Override + default boolean canValidate(ColumnValue columnValue) { + return RuleUtils.isNotBlank(columnValue.getText()); + } + + int getColumnNumber(); + + LineValue getLineValue(); + + void setLineValue(LineValue lineValue); + + RuleNode getRuleNode(); + + void setRuleNode(RuleNode ruleNode); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/CpfRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/CpfRule.java new file mode 100644 index 0000000..5b90877 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/CpfRule.java @@ -0,0 +1,94 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.util.Arrays; +import java.util.List; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class CpfRule extends AbstractColumnRule { + + private static final List INVALID_CPFS = Arrays.asList( + "00000000000", + "11111111111", + "22222222222", + "33333333333", + "44444444444", + "55555555555", + "66666666666", + "77777777777", + "88888888888", + "99999999999" + ); + + public CpfRule(int columnNumber) { + super(columnNumber); + } + + @Override + public boolean isValid(ColumnValue columnValue) { + String cpf = columnValue.getText(); + + if (cpf.length() != 11 || INVALID_CPFS.contains(cpf)) { + return false; + } + + int i; + int j; + int digit; + int coeficient; + int sum; + int[] foundDv = {0, 0}; + + int dv1 = Integer.parseInt(String.valueOf(cpf.charAt(cpf.length() - 2))); + int dv2 = Integer.parseInt(String.valueOf(cpf.charAt(cpf.length() - 1))); + + for (j = 0; j < 2; j++) { + sum = 0; + coeficient = 2; + + for (i = cpf.length() - 3 + j; i >= 0; i--) { + digit = Integer.parseInt(String.valueOf(cpf.charAt(i))); + sum += digit * coeficient; + coeficient++; + } + + foundDv[j] = 11 - sum % 11; + + if (foundDv[j] >= 10) { + foundDv[j] = 0; + } + } + + return dv1 == foundDv[0] && dv2 == foundDv[1]; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DateAfterRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DateAfterRule.java new file mode 100644 index 0000000..d2d4bac --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DateAfterRule.java @@ -0,0 +1,80 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.text.DateFormat; +import java.util.Date; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class DateAfterRule extends AbstractColumnRule { + + private final DateFormat dateFormat; + private final Date min; + private final int afterColumnNumber; + + public DateAfterRule(int afterColumnNumber, DateFormat dateFormat, Date min) { + super(afterColumnNumber); + this.dateFormat = dateFormat; + this.min = min; + this.afterColumnNumber = -1; + } + + public DateAfterRule(int columnNumber, DateFormat dateFormat, int afterColumnNumber) { + super(columnNumber); + this.dateFormat = dateFormat; + this.min = null; + this.afterColumnNumber = afterColumnNumber; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + Date date = columnValue.getDate(dateFormat); + return date.compareTo(getComparingDate()) > 0; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getDate(dateFormat) != null && getComparingDate() != null; + } + + private Date getComparingDate() { + if (afterColumnNumber == -1) { + return min; + } + + try { + return getLineValue().getColumnValue(afterColumnNumber).getDate(dateFormat); + } catch (IllegalStateException e) { + return null; + } + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DateBeforeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DateBeforeRule.java new file mode 100644 index 0000000..7cd67f1 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DateBeforeRule.java @@ -0,0 +1,80 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.text.DateFormat; +import java.util.Date; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class DateBeforeRule extends AbstractColumnRule { + + private final DateFormat dateFormat; + private final Date max; + private final int afterColumnNumber; + + public DateBeforeRule(int afterColumnNumber, DateFormat dateFormat, Date max) { + super(afterColumnNumber); + this.dateFormat = dateFormat; + this.max = max; + this.afterColumnNumber = -1; + } + + public DateBeforeRule(Integer columnNumber, DateFormat dateFormat, int afterColumnNumber) { + super(columnNumber); + this.dateFormat = dateFormat; + this.max = null; + this.afterColumnNumber = afterColumnNumber; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + Date date = columnValue.getDate(dateFormat); + return date.compareTo(getComparingDate()) < 0; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getDate(dateFormat) != null && getComparingDate() != null; + } + + private Date getComparingDate() { + if (afterColumnNumber == -1) { + return max; + } + + try { + return getLineValue().getColumnValue(afterColumnNumber).getDate(dateFormat); + } catch (IllegalStateException e) { + return null; + } + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DateFutureOrPresentRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DateFutureOrPresentRule.java new file mode 100644 index 0000000..d6e53f6 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DateFutureOrPresentRule.java @@ -0,0 +1,59 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.text.DateFormat; +import java.util.Calendar; +import java.util.Date; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class DateFutureOrPresentRule extends AbstractColumnRule { + + private final DateFormat dateFormat; + + public DateFutureOrPresentRule(int columnNumber, DateFormat dateFormat) { + super(columnNumber); + this.dateFormat = dateFormat; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + Date date = columnValue.getDate(dateFormat); + Date current = Calendar.getInstance().getTime(); + return current.compareTo(date) <= 0; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getDate(dateFormat) != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DateFutureRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DateFutureRule.java new file mode 100644 index 0000000..509a11e --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DateFutureRule.java @@ -0,0 +1,59 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.text.DateFormat; +import java.util.Calendar; +import java.util.Date; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class DateFutureRule extends AbstractColumnRule { + + private final DateFormat dateFormat; + + public DateFutureRule(int columnNumber, DateFormat dateFormat) { + super(columnNumber); + this.dateFormat = dateFormat; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + Date date = columnValue.getDate(dateFormat); + Date current = Calendar.getInstance().getTime(); + return current.before(date); + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getDate(dateFormat) != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DatePastOrPresentRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DatePastOrPresentRule.java new file mode 100644 index 0000000..75fc73a --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DatePastOrPresentRule.java @@ -0,0 +1,59 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.text.DateFormat; +import java.util.Calendar; +import java.util.Date; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class DatePastOrPresentRule extends AbstractColumnRule { + + private final DateFormat dateFormat; + + public DatePastOrPresentRule(int columnNumber, DateFormat dateFormat) { + super(columnNumber); + this.dateFormat = dateFormat; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + Date date = columnValue.getDate(dateFormat); + Date current = Calendar.getInstance().getTime(); + return current.compareTo(date) >= 0; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getDate(dateFormat) != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DatePastRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DatePastRule.java new file mode 100644 index 0000000..a1dd76e --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DatePastRule.java @@ -0,0 +1,59 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.text.DateFormat; +import java.util.Calendar; +import java.util.Date; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class DatePastRule extends AbstractColumnRule { + + private final DateFormat dateFormat; + + public DatePastRule(int columnNumber, DateFormat dateFormat) { + super(columnNumber); + this.dateFormat = dateFormat; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + Date date = columnValue.getDate(dateFormat); + Date current = Calendar.getInstance().getTime(); + return current.after(date); + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getDate(dateFormat) != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DateTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DateTypeRule.java new file mode 100644 index 0000000..86a9e48 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DateTypeRule.java @@ -0,0 +1,59 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.text.DateFormat; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class DateTypeRule extends AbstractColumnRule { + + private final DateFormat pattern; + + public DateTypeRule(int columnNumber, DateFormat pattern) { + super(columnNumber); + this.pattern = pattern; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + try { + return columnValue.getText().isEmpty() || columnValue.getDate(pattern) != null; + } catch (IllegalStateException e) { + return false; + } + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return true; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DomainCharacterRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DomainCharacterRule.java new file mode 100644 index 0000000..4c70425 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DomainCharacterRule.java @@ -0,0 +1,55 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.util.List; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class DomainCharacterRule extends AbstractColumnRule { + + private final List domains; + + public DomainCharacterRule(int columnNumber, List domains) { + super(columnNumber); + this.domains = domains; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return domains.contains(columnValue.getCharacter()); + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getCharacter() != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DomainIntegerRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DomainIntegerRule.java new file mode 100644 index 0000000..8140ade --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DomainIntegerRule.java @@ -0,0 +1,55 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.util.List; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class DomainIntegerRule extends AbstractColumnRule { + + private final List domains; + + public DomainIntegerRule(int columnNumber, List domains) { + super(columnNumber); + this.domains = domains; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return domains.contains(columnValue.getInt()); + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getInt() != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DomainLongRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DomainLongRule.java new file mode 100644 index 0000000..797460a --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DomainLongRule.java @@ -0,0 +1,55 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.util.List; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class DomainLongRule extends AbstractColumnRule { + + private final List domains; + + public DomainLongRule(int columnNumber, List domains) { + super(columnNumber); + this.domains = domains; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return domains.contains(columnValue.getLong()); + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getLong() != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DomainRefRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DomainRefRule.java new file mode 100644 index 0000000..d8b9dc1 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DomainRefRule.java @@ -0,0 +1,53 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.util.List; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class DomainRefRule extends AbstractRefRule { + + private final List domains; + private final Class clazz; + + @SuppressWarnings("unchecked") + public DomainRefRule(int refColumnNumber, int columnNumber, List domains) { + super(refColumnNumber, columnNumber); + this.domains = domains; + this.clazz = (Class) domains.get(0).getClass(); + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return domains.contains(columnValue.getContent(clazz)); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DomainShortRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DomainShortRule.java new file mode 100644 index 0000000..d173ec0 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DomainShortRule.java @@ -0,0 +1,55 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.util.List; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class DomainShortRule extends AbstractColumnRule { + + private final List domains; + + public DomainShortRule(int columnNumber, List domains) { + super(columnNumber); + this.domains = domains; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return domains.contains(columnValue.getShort()); + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getShort() != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DomainStringRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DomainStringRule.java new file mode 100644 index 0000000..90becb1 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DomainStringRule.java @@ -0,0 +1,50 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.util.List; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class DomainStringRule extends AbstractColumnRule { + + private final List domains; + + public DomainStringRule(int columnNumber, List domains) { + super(columnNumber); + this.domains = domains; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return domains.contains(columnValue.getText()); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DoubleTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DoubleTypeRule.java new file mode 100644 index 0000000..af2ee69 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/DoubleTypeRule.java @@ -0,0 +1,54 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class DoubleTypeRule extends AbstractColumnRule { + + public DoubleTypeRule(int columnNumber) { + super(columnNumber); + } + + @Override + public boolean isValid(ColumnValue columnValue) { + try { + return columnValue.getText().isEmpty() || columnValue.getDouble() != null; + } catch (NumberFormatException e) { + return false; + } + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return true; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/EmailRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/EmailRule.java new file mode 100644 index 0000000..1459807 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/EmailRule.java @@ -0,0 +1,43 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import java.util.regex.Pattern; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class EmailRule extends RegexRule { + + public EmailRule(int columnNumber) { + super( + columnNumber, + Pattern.compile("^[_A-Za-z0-9-+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$") + ); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/EmptyRefRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/EmptyRefRule.java new file mode 100644 index 0000000..1955dca --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/EmptyRefRule.java @@ -0,0 +1,45 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class EmptyRefRule extends AbstractRefRule { + + public EmptyRefRule(int refColumnNumber, int columnNumber) { + super(refColumnNumber, columnNumber); + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return new OnlyNullRule(columnValue.getColumnNumber()).isValid(columnValue); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/ExactLengthStringRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/ExactLengthStringRule.java new file mode 100644 index 0000000..4b15463 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/ExactLengthStringRule.java @@ -0,0 +1,48 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class ExactLengthStringRule extends AbstractColumnRule { + + private final int exactLength; + + public ExactLengthStringRule(int columnNumber, int exactLength) { + super(columnNumber); + this.exactLength = exactLength; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return columnValue.getText().trim().length() == exactLength; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/FilledRefRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/FilledRefRule.java new file mode 100644 index 0000000..02cb57c --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/FilledRefRule.java @@ -0,0 +1,45 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class FilledRefRule extends AbstractRefRule { + + public FilledRefRule(int refColumnNumber, int columnNumber) { + super(refColumnNumber, columnNumber); + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return new NotEmptyRule(columnValue.getColumnNumber()).isValid(columnValue); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/FloatTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/FloatTypeRule.java new file mode 100644 index 0000000..b729daa --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/FloatTypeRule.java @@ -0,0 +1,54 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class FloatTypeRule extends AbstractColumnRule { + + public FloatTypeRule(int columnNumber) { + super(columnNumber); + } + + @Override + public boolean isValid(ColumnValue columnValue) { + try { + return columnValue.getText().isEmpty() || columnValue.getFloat() != null; + } catch (NumberFormatException e) { + return false; + } + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return true; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/IntegerTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/IntegerTypeRule.java new file mode 100644 index 0000000..127a1b3 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/IntegerTypeRule.java @@ -0,0 +1,54 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class IntegerTypeRule extends AbstractColumnRule { + + public IntegerTypeRule(int columnNumber) { + super(columnNumber); + } + + @Override + public boolean isValid(ColumnValue columnValue) { + try { + return columnValue.getText().isEmpty() || columnValue.getInt() != null; + } catch (NumberFormatException e) { + return false; + } + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return true; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateAfterRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateAfterRule.java new file mode 100644 index 0000000..ce1e4ee --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateAfterRule.java @@ -0,0 +1,82 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class LocalDateAfterRule extends AbstractColumnRule { + + private final DateTimeFormatter dateTimeFormatter; + private final LocalDate min; + private final int refColumnNumber; + + public LocalDateAfterRule(int columnNumber, DateTimeFormatter dateTimeFormatter, LocalDate min) { + super(columnNumber); + this.dateTimeFormatter = dateTimeFormatter; + this.min = min; + this.refColumnNumber = -1; + } + + public LocalDateAfterRule(int columnNumber, DateTimeFormatter dateTimeFormatter, int refColumnNumber) { + super(columnNumber); + this.dateTimeFormatter = dateTimeFormatter; + this.min = null; + this.refColumnNumber = refColumnNumber; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + LocalDate date = columnValue.getLocalDate(dateTimeFormatter); + return date.compareTo(getComparingDate()) > 0; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getLocalDate(dateTimeFormatter) != null && getComparingDate() != null; + } + + private LocalDate getComparingDate() { + if (refColumnNumber == -1) { + return min; + } + + try { + return getLineValue().getColumnValue(refColumnNumber).getLocalDate(dateTimeFormatter); + } catch (DateTimeParseException e) { + return null; + } + + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateBeforeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateBeforeRule.java new file mode 100644 index 0000000..d7fddb5 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateBeforeRule.java @@ -0,0 +1,76 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class LocalDateBeforeRule extends AbstractColumnRule { + + private final DateTimeFormatter dateTimeFormatter; + private final LocalDate max; + private final int refColumnNumber; + + public LocalDateBeforeRule(int columnNumber, DateTimeFormatter dateTimeFormatter, LocalDate max) { + super(columnNumber); + this.dateTimeFormatter = dateTimeFormatter; + this.max = max; + this.refColumnNumber = -1; + } + + public LocalDateBeforeRule(int columnNumber, DateTimeFormatter dateTimeFormatter, int refColumnNumber) { + super(columnNumber); + this.dateTimeFormatter = dateTimeFormatter; + this.max = null; + this.refColumnNumber = refColumnNumber; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + LocalDate date = columnValue.getLocalDate(dateTimeFormatter); + return date.compareTo(getComparingDate()) < 0; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getLocalDate(dateTimeFormatter) != null && getComparingDate() != null; + } + + private LocalDate getComparingDate() { + if (refColumnNumber == -1) { + return max; + } + + return getLineValue().getColumnValue(refColumnNumber).getLocalDate(dateTimeFormatter); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateFutureOrPresentRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateFutureOrPresentRule.java new file mode 100644 index 0000000..8e54393 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateFutureOrPresentRule.java @@ -0,0 +1,58 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class LocalDateFutureOrPresentRule extends AbstractColumnRule { + + private final DateTimeFormatter dateTimeFormatter; + + public LocalDateFutureOrPresentRule(int columnNumber, DateTimeFormatter dateTimeFormatter) { + super(columnNumber); + this.dateTimeFormatter = dateTimeFormatter; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + LocalDate date = columnValue.getLocalDate(dateTimeFormatter); + LocalDate current = LocalDate.now(); + return current.compareTo(date) <= 0; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getLocalDate(dateTimeFormatter) != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateFutureRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateFutureRule.java new file mode 100644 index 0000000..41a1805 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateFutureRule.java @@ -0,0 +1,58 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class LocalDateFutureRule extends AbstractColumnRule { + + private final DateTimeFormatter dateTimeFormatter; + + public LocalDateFutureRule(int columnNumber, DateTimeFormatter dateTimeFormatter) { + super(columnNumber); + this.dateTimeFormatter = dateTimeFormatter; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + LocalDate date = columnValue.getLocalDate(dateTimeFormatter); + LocalDate current = LocalDate.now(); + return current.compareTo(date) < 0; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getLocalDate(dateTimeFormatter) != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDatePastOrPresentRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDatePastOrPresentRule.java new file mode 100644 index 0000000..241110b --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDatePastOrPresentRule.java @@ -0,0 +1,58 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class LocalDatePastOrPresentRule extends AbstractColumnRule { + + private final DateTimeFormatter dateTimeFormatter; + + public LocalDatePastOrPresentRule(int columnNumber, DateTimeFormatter dateTimeFormatter) { + super(columnNumber); + this.dateTimeFormatter = dateTimeFormatter; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + LocalDate date = columnValue.getLocalDate(dateTimeFormatter); + LocalDate current = LocalDate.now(); + return current.compareTo(date) >= 0; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getLocalDate(dateTimeFormatter) != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDatePastRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDatePastRule.java new file mode 100644 index 0000000..a2b7340 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDatePastRule.java @@ -0,0 +1,58 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class LocalDatePastRule extends AbstractColumnRule { + + private final DateTimeFormatter dateTimeFormatter; + + public LocalDatePastRule(int columnNumber, DateTimeFormatter dateTimeFormatter) { + super(columnNumber); + this.dateTimeFormatter = dateTimeFormatter; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + LocalDate date = columnValue.getLocalDate(dateTimeFormatter); + LocalDate current = LocalDate.now(); + return current.compareTo(date) > 0; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getLocalDate(dateTimeFormatter) != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimeAfterRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimeAfterRule.java new file mode 100644 index 0000000..9977451 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimeAfterRule.java @@ -0,0 +1,76 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class LocalDateTimeAfterRule extends AbstractColumnRule { + + private final DateTimeFormatter dateTimeFormatter; + private final LocalDateTime min; + private final int refColumnNumber; + + public LocalDateTimeAfterRule(int columnNumber, DateTimeFormatter dateTimeFormatter, LocalDateTime min) { + super(columnNumber); + this.dateTimeFormatter = dateTimeFormatter; + this.min = min; + this.refColumnNumber = -1; + } + + public LocalDateTimeAfterRule(int columnNumber, DateTimeFormatter dateTimeFormatter, int refColumnNumber) { + super(columnNumber); + this.dateTimeFormatter = dateTimeFormatter; + this.min = null; + this.refColumnNumber = refColumnNumber; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + LocalDateTime date = columnValue.getLocalDateTime(dateTimeFormatter); + return date.compareTo(getComparingDate()) > 0; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getLocalDateTime(dateTimeFormatter) != null && getComparingDate() != null; + } + + private LocalDateTime getComparingDate() { + if (refColumnNumber == -1) { + return min; + } + + return getLineValue().getColumnValue(refColumnNumber).getLocalDateTime(dateTimeFormatter); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimeBeforeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimeBeforeRule.java new file mode 100644 index 0000000..a23da98 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimeBeforeRule.java @@ -0,0 +1,76 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class LocalDateTimeBeforeRule extends AbstractColumnRule { + + private final DateTimeFormatter dateTimeFormatter; + private final LocalDateTime max; + private final int refColumnNumber; + + public LocalDateTimeBeforeRule(int columnNumber, DateTimeFormatter dateTimeFormatter, LocalDateTime max) { + super(columnNumber); + this.dateTimeFormatter = dateTimeFormatter; + this.max = max; + this.refColumnNumber = -1; + } + + public LocalDateTimeBeforeRule(int columnNumber, DateTimeFormatter dateTimeFormatter, int refColumnNumber) { + super(columnNumber); + this.dateTimeFormatter = dateTimeFormatter; + this.max = null; + this.refColumnNumber = refColumnNumber; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + LocalDateTime date = columnValue.getLocalDateTime(dateTimeFormatter); + return date.compareTo(getComparingDate()) < 0; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getLocalDateTime(dateTimeFormatter) != null && getComparingDate() != null; + } + + private LocalDateTime getComparingDate() { + if (refColumnNumber == -1) { + return max; + } + + return getLineValue().getColumnValue(refColumnNumber).getLocalDateTime(dateTimeFormatter); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimeFutureOrPresentRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimeFutureOrPresentRule.java new file mode 100644 index 0000000..39dca4c --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimeFutureOrPresentRule.java @@ -0,0 +1,58 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class LocalDateTimeFutureOrPresentRule extends AbstractColumnRule { + + private final DateTimeFormatter dateTimeFormatter; + + public LocalDateTimeFutureOrPresentRule(int columnNumber, DateTimeFormatter dateTimeFormatter) { + super(columnNumber); + this.dateTimeFormatter = dateTimeFormatter; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + LocalDateTime date = columnValue.getLocalDateTime(dateTimeFormatter); + LocalDateTime current = LocalDateTime.now(); + return current.compareTo(date) <= 0; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getLocalDateTime(dateTimeFormatter) != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimeFutureRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimeFutureRule.java new file mode 100644 index 0000000..2a97812 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimeFutureRule.java @@ -0,0 +1,58 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class LocalDateTimeFutureRule extends AbstractColumnRule { + + private final DateTimeFormatter dateTimeFormatter; + + public LocalDateTimeFutureRule(int columnNumber, DateTimeFormatter dateTimeFormatter) { + super(columnNumber); + this.dateTimeFormatter = dateTimeFormatter; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + LocalDateTime date = columnValue.getLocalDateTime(dateTimeFormatter); + LocalDateTime current = LocalDateTime.now(); + return current.compareTo(date) < 0; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getLocalDateTime(dateTimeFormatter) != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimePastOrPresentRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimePastOrPresentRule.java new file mode 100644 index 0000000..bd11a27 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimePastOrPresentRule.java @@ -0,0 +1,58 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class LocalDateTimePastOrPresentRule extends AbstractColumnRule { + + private final DateTimeFormatter dateTimeFormatter; + + public LocalDateTimePastOrPresentRule(int columnNumber, DateTimeFormatter dateTimeFormatter) { + super(columnNumber); + this.dateTimeFormatter = dateTimeFormatter; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + LocalDateTime date = columnValue.getLocalDateTime(dateTimeFormatter); + LocalDateTime current = LocalDateTime.now(); + return current.compareTo(date) >= 0; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getLocalDateTime(dateTimeFormatter) != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimePastRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimePastRule.java new file mode 100644 index 0000000..f7b96cb --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimePastRule.java @@ -0,0 +1,58 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class LocalDateTimePastRule extends AbstractColumnRule { + + private final DateTimeFormatter dateTimeFormatter; + + public LocalDateTimePastRule(int columnNumber, DateTimeFormatter dateTimeFormatter) { + super(columnNumber); + this.dateTimeFormatter = dateTimeFormatter; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + LocalDateTime date = columnValue.getLocalDateTime(dateTimeFormatter); + LocalDateTime current = LocalDateTime.now(); + return current.compareTo(date) > 0; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getLocalDateTime(dateTimeFormatter) != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimeTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimeTypeRule.java new file mode 100644 index 0000000..6b8da48 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimeTypeRule.java @@ -0,0 +1,60 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class LocalDateTimeTypeRule extends AbstractColumnRule { + + private final DateTimeFormatter dateTimeFormatter; + + public LocalDateTimeTypeRule(int columnNumber, DateTimeFormatter dateTimeFormatter) { + super(columnNumber); + this.dateTimeFormatter = dateTimeFormatter; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + try { + return columnValue.getText().isEmpty() || columnValue.getLocalDateTime(dateTimeFormatter) != null; + } catch (DateTimeParseException e) { + return false; + } + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return true; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTypeRule.java new file mode 100644 index 0000000..fb438a0 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTypeRule.java @@ -0,0 +1,60 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class LocalDateTypeRule extends AbstractColumnRule { + + private final DateTimeFormatter dateTimeFormatter; + + public LocalDateTypeRule(int columnNumber, DateTimeFormatter dateTimeFormatter) { + super(columnNumber); + this.dateTimeFormatter = dateTimeFormatter; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + try { + return columnValue.getText().isEmpty() || columnValue.getLocalDate(dateTimeFormatter) != null; + } catch (DateTimeParseException e) { + return false; + } + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return true; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LongTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LongTypeRule.java new file mode 100644 index 0000000..c4b638e --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/LongTypeRule.java @@ -0,0 +1,54 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class LongTypeRule extends AbstractColumnRule { + + public LongTypeRule(int columnNumber) { + super(columnNumber); + } + + @Override + public boolean isValid(ColumnValue columnValue) { + try { + return columnValue.getText().isEmpty() || columnValue.getLong() != null; + } catch (NumberFormatException e) { + return false; + } + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return true; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxBigDecimalRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxBigDecimalRule.java new file mode 100644 index 0000000..e19c843 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxBigDecimalRule.java @@ -0,0 +1,58 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.math.BigDecimal; +import java.text.DecimalFormat; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class MaxBigDecimalRule extends AbstractColumnRule { + + private final BigDecimal max; + private final DecimalFormat decimalFormat; + + public MaxBigDecimalRule(int columnNumber, BigDecimal max, DecimalFormat decimalFormat) { + super(columnNumber); + this.max = max; + this.decimalFormat = decimalFormat; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return columnValue.getBigDecimal(decimalFormat).compareTo(max) <= 0; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getBigDecimal(decimalFormat) != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxBigIntegerRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxBigIntegerRule.java new file mode 100644 index 0000000..93e9f24 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxBigIntegerRule.java @@ -0,0 +1,55 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.math.BigInteger; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class MaxBigIntegerRule extends AbstractColumnRule { + + private final BigInteger max; + + public MaxBigIntegerRule(int columnNumber, BigInteger max) { + super(columnNumber); + this.max = max; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return columnValue.getBigInteger().compareTo(max) <= 0; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getBigInteger() != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxDoubleRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxDoubleRule.java new file mode 100644 index 0000000..61d263c --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxDoubleRule.java @@ -0,0 +1,53 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class MaxDoubleRule extends AbstractColumnRule { + + private final double max; + + public MaxDoubleRule(int columnNumber, double max) { + super(columnNumber); + this.max = max; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return columnValue.getDouble() <= max; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getDouble() != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxFloatRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxFloatRule.java new file mode 100644 index 0000000..2a216b7 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxFloatRule.java @@ -0,0 +1,53 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class MaxFloatRule extends AbstractColumnRule { + + private final float max; + + public MaxFloatRule(int columnNumber, float max) { + super(columnNumber); + this.max = max; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return columnValue.getFloat() <= max; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getFloat() != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxIntegerRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxIntegerRule.java new file mode 100644 index 0000000..c652348 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxIntegerRule.java @@ -0,0 +1,53 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class MaxIntegerRule extends AbstractColumnRule { + + private final int max; + + public MaxIntegerRule(int columnNumber, int max) { + super(columnNumber); + this.max = max; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return columnValue.getInt() <= max; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getInt() != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxLengthStringRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxLengthStringRule.java new file mode 100644 index 0000000..b876eb1 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxLengthStringRule.java @@ -0,0 +1,48 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class MaxLengthStringRule extends AbstractColumnRule { + + private final int max; + + public MaxLengthStringRule(int columnNumber, int max) { + super(columnNumber); + this.max = max; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return columnValue.getText().trim().length() <= max; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxLongRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxLongRule.java new file mode 100644 index 0000000..afa8264 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxLongRule.java @@ -0,0 +1,53 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class MaxLongRule extends AbstractColumnRule { + + private final long max; + + public MaxLongRule(int columnNumber, long max) { + super(columnNumber); + this.max = max; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return columnValue.getLong() <= max; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getLong() != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxShortRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxShortRule.java new file mode 100644 index 0000000..dd91af3 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MaxShortRule.java @@ -0,0 +1,53 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class MaxShortRule extends AbstractColumnRule { + + private final short max; + + public MaxShortRule(int columnNumber, short max) { + super(columnNumber); + this.max = max; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return columnValue.getShort() <= max; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getShort() != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinBigDecimalRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinBigDecimalRule.java new file mode 100644 index 0000000..c3cd78d --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinBigDecimalRule.java @@ -0,0 +1,58 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.math.BigDecimal; +import java.text.DecimalFormat; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class MinBigDecimalRule extends AbstractColumnRule { + + private final BigDecimal min; + private final DecimalFormat decimalFormat; + + public MinBigDecimalRule(int columnNumber, BigDecimal min, DecimalFormat decimalFormat) { + super(columnNumber); + this.min = min; + this.decimalFormat = decimalFormat; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return columnValue.getBigDecimal(decimalFormat).compareTo(min) >= 0; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getBigDecimal(decimalFormat) != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinBigIntegerRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinBigIntegerRule.java new file mode 100644 index 0000000..b5945ee --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinBigIntegerRule.java @@ -0,0 +1,55 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.math.BigInteger; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class MinBigIntegerRule extends AbstractColumnRule { + + private final BigInteger min; + + public MinBigIntegerRule(int columnNumber, BigInteger min) { + super(columnNumber); + this.min = min; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return columnValue.getBigInteger().compareTo(min) >= 0; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getBigInteger() != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinDoubleRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinDoubleRule.java new file mode 100644 index 0000000..a26d0bd --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinDoubleRule.java @@ -0,0 +1,53 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class MinDoubleRule extends AbstractColumnRule { + + private final double min; + + public MinDoubleRule(int columnNumber, double min) { + super(columnNumber); + this.min = min; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return columnValue.getDouble() >= min; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getDouble() != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinFloatRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinFloatRule.java new file mode 100644 index 0000000..3ca8c2f --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinFloatRule.java @@ -0,0 +1,53 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class MinFloatRule extends AbstractColumnRule { + + private final float min; + + public MinFloatRule(int columnNumber, float min) { + super(columnNumber); + this.min = min; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return columnValue.getFloat() >= min; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getFloat() != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinIntegerRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinIntegerRule.java new file mode 100644 index 0000000..98ca626 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinIntegerRule.java @@ -0,0 +1,53 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class MinIntegerRule extends AbstractColumnRule { + + private final int min; + + public MinIntegerRule(int columnNumber, int min) { + super(columnNumber); + this.min = min; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return columnValue.getInt() >= min; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getInt() != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinLengthStringRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinLengthStringRule.java new file mode 100644 index 0000000..08a3fa0 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinLengthStringRule.java @@ -0,0 +1,48 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class MinLengthStringRule extends AbstractColumnRule { + + private final int min; + + public MinLengthStringRule(int columnNumber, int min) { + super(columnNumber); + this.min = min; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return columnValue.getText().trim().length() >= min; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinLongRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinLongRule.java new file mode 100644 index 0000000..5ce9b9b --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinLongRule.java @@ -0,0 +1,53 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class MinLongRule extends AbstractColumnRule { + + private final long min; + + public MinLongRule(int columnNumber, long min) { + super(columnNumber); + this.min = min; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return columnValue.getLong() >= min; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getLong() != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinShortRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinShortRule.java new file mode 100644 index 0000000..58917dd --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/MinShortRule.java @@ -0,0 +1,53 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class MinShortRule extends AbstractColumnRule { + + private final short min; + + public MinShortRule(int columnNumber, short min) { + super(columnNumber); + this.min = min; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return columnValue.getShort() >= min; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return columnValue.getShort() != null; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/NotEmptyRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/NotEmptyRule.java new file mode 100644 index 0000000..c84c085 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/NotEmptyRule.java @@ -0,0 +1,51 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; +import com.jonpereiradev.jfile.reader.validator.rule.RuleUtils; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class NotEmptyRule extends AbstractColumnRule { + + public NotEmptyRule(int columnNumber) { + super(columnNumber); + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return RuleUtils.isNotBlank(columnValue.getText()); + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return true; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/NotNullRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/NotNullRule.java new file mode 100644 index 0000000..945f439 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/NotNullRule.java @@ -0,0 +1,51 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; +import com.jonpereiradev.jfile.reader.validator.rule.RuleUtils; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class NotNullRule extends AbstractColumnRule { + + public NotNullRule(int columnNumber) { + super(columnNumber); + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return RuleUtils.isNotEmpty(columnValue.getText()); + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return true; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/OnlyNullRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/OnlyNullRule.java new file mode 100644 index 0000000..26ed19c --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/OnlyNullRule.java @@ -0,0 +1,51 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; +import com.jonpereiradev.jfile.reader.validator.rule.RuleUtils; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class OnlyNullRule extends AbstractColumnRule { + + public OnlyNullRule(int columnNumber) { + super(columnNumber); + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return RuleUtils.isBlank(columnValue.getText()); + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return true; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/RefRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/RefRule.java new file mode 100644 index 0000000..679b87f --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/RefRule.java @@ -0,0 +1,35 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface RefRule extends ColumnRule { + + int getRefColumnNumber(); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/RegexRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/RegexRule.java new file mode 100644 index 0000000..96afeb6 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/RegexRule.java @@ -0,0 +1,50 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + +import java.util.regex.Pattern; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class RegexRule extends AbstractColumnRule { + + private final Pattern regex; + + public RegexRule(int columnNumber, Pattern pattern) { + super(columnNumber); + this.regex = pattern; + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return regex.matcher(columnValue.getText()).matches(); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/ShortTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/ShortTypeRule.java new file mode 100644 index 0000000..37f0dd7 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/ShortTypeRule.java @@ -0,0 +1,54 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class ShortTypeRule extends AbstractColumnRule { + + public ShortTypeRule(int columnNumber) { + super(columnNumber); + } + + @Override + public boolean isValid(ColumnValue columnValue) { + try { + return columnValue.getText().isEmpty() || columnValue.getShort() != null; + } catch (NumberFormatException e) { + return false; + } + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return true; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/StringTypeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/StringTypeRule.java new file mode 100644 index 0000000..e3ecd8e --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/column/StringTypeRule.java @@ -0,0 +1,50 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.file.ColumnValue; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public class StringTypeRule extends AbstractColumnRule { + + public StringTypeRule(int columnNumber) { + super(columnNumber); + } + + @Override + public boolean isValid(ColumnValue columnValue) { + return true; + } + + @Override + public boolean canValidate(ColumnValue columnValue) { + return true; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/AbstractRuleConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/AbstractRuleConfigurator.java new file mode 100644 index 0000000..b2bcf29 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/AbstractRuleConfigurator.java @@ -0,0 +1,112 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNodeImpl; +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.NotNullRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.OnlyNullRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.RefRule; + +import java.util.function.Function; + + +abstract class AbstractRuleConfigurator> implements TypedRuleConfigurator { + + private final int columnNumber; + private final JFileValidatorConfig configuration; + + private RuleNode ruleNode; + + AbstractRuleConfigurator(int columnNumber, JFileValidatorConfig configuration, RuleNode ruleNode) { + this.columnNumber = columnNumber; + this.configuration = configuration; + this.ruleNode = ruleNode; + } + + @Override + public T notNull() { + return rule(NotNullRule::new); + } + + @Override + public T onlyNull() { + return rule(OnlyNullRule::new); + } + + @Override + @SuppressWarnings("unchecked") + public T apply() { + if (ruleNode.getParentNode() != null) { + setRuleNode(ruleNode.getParentNode()); + } + + return (T) this; + } + + @Override + @SuppressWarnings("unchecked") + public T rule(Function rule) { + ColumnRule columnRule = rule.apply(columnNumber); + columnRule.setRuleNode(new RuleNodeImpl<>(columnRule.getClass(), ruleNode)); + ruleNode.add(columnRule); + return (T) this; + } + + @Override + public GenericTypeConfigurator column(int columnNumber) { + return new GenericTypeConfiguratorImpl(columnNumber, configuration, getParentNode()); + } + + @Override + @SuppressWarnings("unchecked") + public RefRuleConfigurator depends(int columnNumber) { + RuleNode parentNode = ruleNode; + + if (ruleNode.getParentNode() != null && RefRule.class.isAssignableFrom(ruleNode.getType())) { + parentNode = ruleNode.getParentNode(); + } + + return new RefRuleConfiguratorImpl<>(columnNumber, this.columnNumber, parentNode, (T) this); + } + + @Override + public void setRuleNode(RuleNode ruleNode) { + this.ruleNode = ruleNode; + } + + private RuleNode getParentNode() { + RuleNode node = ruleNode; + + while (node.getParentNode() != null) { + node = node.getParentNode(); + } + + return node; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/ArrayTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/ArrayTypeConfigurator.java new file mode 100644 index 0000000..3744330 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/ArrayTypeConfigurator.java @@ -0,0 +1,165 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import java.text.DateFormat; +import java.text.DecimalFormat; +import java.time.format.DateTimeFormatter; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface ArrayTypeConfigurator { + + /** + * Defines a rule for a column of {@link Short} type. + * + * @return the {@link Short} type configurator. + */ + ShortTypeConfigurator shortType(); + + /** + * Defines a rule for a column of {@link Integer} type. + * + * @return the {@link Integer} type configurator. + */ + IntegerTypeConfigurator integerType(); + + /** + * Defines a rule for a column of {@link Long} type. + * + * @return the {@link Long} type configurator. + */ + LongTypeConfigurator longType(); + + /** + * Defines a rule for a column of {@link Float} type. + * + * @return the {@link Float} type configurator. + */ + FloatTypeConfigurator floatType(); + + /** + * Defines a rule for a column of {@link Double} type. + * + * @return the {@link Double} type configurator. + */ + DoubleTypeConfigurator doubleType(); + + /** + * Defines a rule for a column of {@link Boolean} type. + * + * @return the {@link Boolean} type configurator. + */ + BooleanTypeConfigurator booleanType(); + + /** + * Defines a rule for a column of {@link Character} type. + * + * @return the {@link Character} type configurator. + */ + CharacterTypeConfigurator characterType(); + + /** + * Defines a rule for a column of {@link String} type. + * + * @return the {@link String} type configurator. + */ + StringTypeConfigurator stringType(); + + /** + * Defines a rule for a column of {@link java.math.BigInteger} type. + * + * @return the {@link java.math.BigInteger} type configurator. + */ + BigIntegerTypeConfigurator bigIntegerType(); + + /** + * Defines a rule for a column of {@link java.math.BigDecimal} type. + * + * @return the {@link java.math.BigDecimal} type configurator. + */ + BigDecimalTypeConfigurator bigDecimalType(); + + /** + * Defines a rule for a column of {@link java.math.BigDecimal} type. + * + * @param decimalFormat the formatter that matches the column type. + * + * @return the {@link java.math.BigDecimal} type configurator. + */ + BigDecimalTypeConfigurator bigDecimalType(DecimalFormat decimalFormat); + + /** + * Defines a rule for a column of {@link java.util.Date} type. + * + * @return the {@link java.util.Date} type configurator. + */ + DateTypeConfigurator dateType(); + + /** + * Defines a rule for a column of {@link java.util.Date} type. + * + * @param dateFormat the formatter that matches the column type. + * + * @return the {@link java.util.Date} type configurator. + */ + DateTypeConfigurator dateType(DateFormat dateFormat); + + /** + * Defines a rule for a column of {@link java.time.LocalDate} type. + * + * @return the {@link java.time.LocalDate} type configurator. + */ + LocalDateTypeConfigurator localDateType(); + + /** + * Defines a rule for a column of {@link java.time.LocalDate} type. + * + * @param dateTimeFormatter the formatter that matches the column type. + * + * @return the {@link java.time.LocalDate} type configurator. + */ + LocalDateTypeConfigurator localDateType(DateTimeFormatter dateTimeFormatter); + + /** + * Defines a rule for a column of {@link java.time.LocalDateTime} type. + * + * @return the {@link java.time.LocalDateTime} type configurator. + */ + LocalDateTimeTypeConfigurator localDateTimeType(); + + /** + * Defines a rule for a column of {@link java.time.LocalDateTime} type. + * + * @param dateTimeFormatter the formatter that matches the column type. + * + * @return the {@link java.time.LocalDateTime} type configurator. + */ + LocalDateTimeTypeConfigurator localDateTimeType(DateTimeFormatter dateTimeFormatter); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/ArrayTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/ArrayTypeConfiguratorImpl.java similarity index 62% rename from src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/ArrayTypeConfiguratorImpl.java rename to src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/ArrayTypeConfiguratorImpl.java index 1f397a7..623e6c6 100644 --- a/src/main/java/com/jonpereiradev/jfile/reader/rule/configurator/ArrayTypeConfiguratorImpl.java +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/ArrayTypeConfiguratorImpl.java @@ -1,19 +1,47 @@ -package com.jonpereiradev.jfile.reader.rule.configurator; - -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.rule.RuleNode; -import com.jonpereiradev.jfile.reader.rule.column.ColumnRule; +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; import java.text.DateFormat; import java.text.DecimalFormat; import java.time.format.DateTimeFormatter; + final class ArrayTypeConfiguratorImpl implements ArrayTypeConfigurator { private final GenericTypeConfiguratorImpl genericTypeConfigurator; - ArrayTypeConfiguratorImpl(int position, ReaderConfiguration configuration, RuleNode ruleNode) { - this.genericTypeConfigurator = new GenericTypeConfiguratorImpl(position, configuration, ruleNode); + ArrayTypeConfiguratorImpl( + int columnNumber, + JFileValidatorConfig configuration, + RuleNode ruleNode) { + this.genericTypeConfigurator = new GenericTypeConfiguratorImpl(columnNumber, configuration, ruleNode); } @Override diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/BigDecimalTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/BigDecimalTypeConfigurator.java new file mode 100644 index 0000000..f5abe65 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/BigDecimalTypeConfigurator.java @@ -0,0 +1,54 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import java.math.BigDecimal; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface BigDecimalTypeConfigurator extends TypedRuleConfigurator { + + /** + * Defines a minimum value rule validation. + * + * @param min a {@link BigDecimal} minimum number that the column accepts. + * + * @return the configurator with the min rule configured. + */ + BigDecimalTypeConfigurator min(BigDecimal min); + + /** + * Defines a maximum value rule validation. + * + * @param max a {@link BigDecimal} maximum number that the column accepts. + * + * @return the configurator with the max rule configured. + */ + BigDecimalTypeConfigurator max(BigDecimal max); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/BigDecimalTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/BigDecimalTypeConfiguratorImpl.java new file mode 100644 index 0000000..fb43745 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/BigDecimalTypeConfiguratorImpl.java @@ -0,0 +1,61 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.MaxBigDecimalRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.MinBigDecimalRule; + +import java.math.BigDecimal; +import java.text.DecimalFormat; + + +final class BigDecimalTypeConfiguratorImpl + extends AbstractRuleConfigurator implements BigDecimalTypeConfigurator { + + private final DecimalFormat decimalFormat; + + BigDecimalTypeConfiguratorImpl( + int columnNumber, + DecimalFormat decimalFormat, + JFileValidatorConfig configuration, + RuleNode ruleNode) { + super(columnNumber, configuration, ruleNode); + this.decimalFormat = decimalFormat; + } + + @Override + public BigDecimalTypeConfigurator min(BigDecimal min) { + return rule(columnNumber -> new MinBigDecimalRule(columnNumber, min, decimalFormat)); + } + + @Override + public BigDecimalTypeConfigurator max(BigDecimal max) { + return rule(columnNumber -> new MaxBigDecimalRule(columnNumber, max, decimalFormat)); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/BigIntegerTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/BigIntegerTypeConfigurator.java new file mode 100644 index 0000000..d682340 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/BigIntegerTypeConfigurator.java @@ -0,0 +1,54 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import java.math.BigInteger; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface BigIntegerTypeConfigurator extends TypedRuleConfigurator { + + /** + * Defines a minimum value rule validation. + * + * @param min a {@link BigInteger} minimum number that the column accepts. + * + * @return the configurator with the min rule configured. + */ + BigIntegerTypeConfigurator min(BigInteger min); + + /** + * Defines a maximum value rule validation. + * + * @param max a {@link BigInteger} maximum number that the column accepts. + * + * @return the configurator with the max rule configured. + */ + BigIntegerTypeConfigurator max(BigInteger max); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/BigIntegerTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/BigIntegerTypeConfiguratorImpl.java new file mode 100644 index 0000000..5fcba91 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/BigIntegerTypeConfiguratorImpl.java @@ -0,0 +1,56 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.MaxBigIntegerRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.MinBigIntegerRule; + +import java.math.BigInteger; + + +final class BigIntegerTypeConfiguratorImpl + extends AbstractRuleConfigurator implements BigIntegerTypeConfigurator { + + BigIntegerTypeConfiguratorImpl( + int columnNumber, + JFileValidatorConfig configuration, + RuleNode ruleNode) { + super(columnNumber, configuration, ruleNode); + } + + @Override + public BigIntegerTypeConfigurator min(BigInteger min) { + return rule(columnNumber -> new MinBigIntegerRule(columnNumber, min)); + } + + @Override + public BigIntegerTypeConfigurator max(BigInteger max) { + return rule(columnNumber -> new MaxBigIntegerRule(columnNumber, max)); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/BooleanTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/BooleanTypeConfigurator.java new file mode 100644 index 0000000..edab257 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/BooleanTypeConfigurator.java @@ -0,0 +1,42 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface BooleanTypeConfigurator extends TypedRuleConfigurator { + + /** + * Defines a domain rule validation with possible options. + * + * @param domains the valid values for a boolean type, like 'Y' or 'N', '0', or '1', etc. + * + * @return the configurator with the domain rule configured. + */ + BooleanTypeConfigurator domain(Character... domains); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/BooleanTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/BooleanTypeConfiguratorImpl.java new file mode 100644 index 0000000..2b23089 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/BooleanTypeConfiguratorImpl.java @@ -0,0 +1,49 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.DomainCharacterRule; + +import java.util.Arrays; + + +final class BooleanTypeConfiguratorImpl extends AbstractRuleConfigurator implements BooleanTypeConfigurator { + + BooleanTypeConfiguratorImpl( + int columnNumber, + JFileValidatorConfig configuration, + RuleNode ruleNode) { + super(columnNumber, configuration, ruleNode); + } + + @Override + public BooleanTypeConfigurator domain(Character... domains) { + return rule(columnNumber -> new DomainCharacterRule(columnNumber, Arrays.asList(domains))); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/CharacterTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/CharacterTypeConfigurator.java new file mode 100644 index 0000000..87f4c8e --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/CharacterTypeConfigurator.java @@ -0,0 +1,42 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface CharacterTypeConfigurator extends TypedRuleConfigurator { + + /** + * Defines a domain rule validation with possible options. + * + * @param domains the valid domains for a char type. + * + * @return the configurator with the domain rule configured. + */ + CharacterTypeConfigurator domain(Character... domains); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/CharacterTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/CharacterTypeConfiguratorImpl.java new file mode 100644 index 0000000..d3933a5 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/CharacterTypeConfiguratorImpl.java @@ -0,0 +1,50 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.DomainCharacterRule; + +import java.util.Arrays; + + +final class CharacterTypeConfiguratorImpl + extends AbstractRuleConfigurator implements CharacterTypeConfigurator { + + CharacterTypeConfiguratorImpl( + int columnNumber, + JFileValidatorConfig configuration, + RuleNode ruleNode) { + super(columnNumber, configuration, ruleNode); + } + + @Override + public CharacterTypeConfigurator domain(Character... domains) { + return rule(columnNumber -> new DomainCharacterRule(columnNumber, Arrays.asList(domains))); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/ColumnRuleConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/ColumnRuleConfigurator.java new file mode 100644 index 0000000..426bad7 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/ColumnRuleConfigurator.java @@ -0,0 +1,56 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface ColumnRuleConfigurator { + + /** + * Creates the ColumnRuleConfigurator. + * + * @param configuration the validator configuration. + * + * @return a configurator that configure rules for columns. + */ + static ColumnRuleConfigurator defaultConfigurator(JFileValidatorConfig configuration) { + return new ColumnRuleConfiguratorImpl(configuration); + } + + /** + * Creates the rule config for the column at the given position. + * + * @param columnNumber the int number from the position of the column in a line. + * + * @return a configurator that configure rules for the selected column. + */ + GenericTypeConfigurator column(int columnNumber); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/ColumnRuleConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/ColumnRuleConfiguratorImpl.java new file mode 100644 index 0000000..e2da368 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/ColumnRuleConfiguratorImpl.java @@ -0,0 +1,47 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; + + +final class ColumnRuleConfiguratorImpl implements ColumnRuleConfigurator { + + private final JFileValidatorConfig configuration; + + ColumnRuleConfiguratorImpl(JFileValidatorConfig configuration) { + this.configuration = configuration; + } + + @Override + public GenericTypeConfigurator column(int columnNumber) { + return new GenericTypeConfiguratorImpl( + columnNumber, + configuration, + configuration.getRuleRoot().getColumnRootNode() + ); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/DateTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/DateTypeConfigurator.java new file mode 100644 index 0000000..add7ea6 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/DateTypeConfigurator.java @@ -0,0 +1,104 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import java.util.Date; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface DateTypeConfigurator extends TypedRuleConfigurator { + + /** + * Defines a future value rule validation. Used for validate that a date must be after the present date. + * + * @return the configurator with the future rule configured. + */ + DateTypeConfigurator future(); + + /** + * Defines a future or present value rule validation. Used for validate that a date must be equal or after the + * present date. + * + * @return the configurator with the future or present rule configured. + */ + DateTypeConfigurator futureOrPresent(); + + /** + * Defines a past value rule validation. Used for validate that a date must be before the present date. + * + * @return the configurator with the before rule configured. + */ + DateTypeConfigurator past(); + + /** + * Defines a before or present value rule validation. Used for validate that a date must be before or equal the + * present date. + * + * @return the configurator with the before or present rule configured. + */ + DateTypeConfigurator pastOrPresent(); + + /** + * Defines a after date rule validation. Used for validate that a date must be after the date parameter. + * + * @param date the date that is used to compare in the rule. + * + * @return the configurator with the after date rule configured. + */ + DateTypeConfigurator after(Date date); + + /** + * Defines after other column value rule validation. Used for validate that a date must be after another date column + * in the line. + * + * @param columnNumber an int columnNumber reference to other date column in the line. + * + * @return the configurator with the after column value rule configured. + */ + DateTypeConfigurator after(int columnNumber); + + /** + * Defines a before date rule validation. Used for validate that a date must be before the date parameter. + * + * @param date the date that is used to compare in the rule. + * + * @return the configurator with the before date rule configured. + */ + DateTypeConfigurator before(Date date); + + /** + * Defines before other column value rule validation. Used for validate that a date must be before another date + * column in the line. + * + * @param columnNumber an int position reference to other date column in the line. + * + * @return the configurator with the before column value rule configured. + */ + DateTypeConfigurator before(int columnNumber); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/DateTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/DateTypeConfiguratorImpl.java new file mode 100644 index 0000000..a722b41 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/DateTypeConfiguratorImpl.java @@ -0,0 +1,93 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.DateAfterRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.DateBeforeRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.DateFutureOrPresentRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.DateFutureRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.DatePastOrPresentRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.DatePastRule; + +import java.text.DateFormat; +import java.util.Date; + + +final class DateTypeConfiguratorImpl extends AbstractRuleConfigurator implements DateTypeConfigurator { + + private final DateFormat dateFormat; + + DateTypeConfiguratorImpl( + int columnNumber, + DateFormat dateFormat, + JFileValidatorConfig configuration, RuleNode ruleNode) { + super(columnNumber, configuration, ruleNode); + this.dateFormat = dateFormat; + } + + @Override + public DateTypeConfigurator future() { + return rule(columnNumber -> new DateFutureRule(columnNumber, dateFormat)); + } + + @Override + public DateTypeConfigurator futureOrPresent() { + return rule(columnNumber -> new DateFutureOrPresentRule(columnNumber, dateFormat)); + } + + @Override + public DateTypeConfigurator past() { + return rule(columnNumber -> new DatePastRule(columnNumber, dateFormat)); + } + + @Override + public DateTypeConfigurator pastOrPresent() { + return rule(columnNumber -> new DatePastOrPresentRule(columnNumber, dateFormat)); + } + + @Override + public DateTypeConfigurator after(Date date) { + return rule(columnNumber -> new DateAfterRule(columnNumber, dateFormat, date)); + } + + @Override + public DateTypeConfigurator after(int columnNumber) { + return rule(number -> new DateAfterRule(number, dateFormat, columnNumber)); + } + + @Override + public DateTypeConfigurator before(Date date) { + return rule(columnNumber -> new DateBeforeRule(columnNumber, dateFormat, date)); + } + + @Override + public DateTypeConfigurator before(int columnNumber) { + return rule(number -> new DateBeforeRule(number, dateFormat, columnNumber)); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/DoubleTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/DoubleTypeConfigurator.java new file mode 100644 index 0000000..c15b058 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/DoubleTypeConfigurator.java @@ -0,0 +1,51 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface DoubleTypeConfigurator extends TypedRuleConfigurator { + + /** + * Defines a min value rule validation. + * + * @param min the minimum value that is valid to the column. + * + * @return the object with the min rule configured. + */ + DoubleTypeConfigurator min(double min); + + /** + * Defines a max value rule validation. + * + * @param max the maximum value that is valid to the column. + * + * @return the object with the max rule configured. + */ + DoubleTypeConfigurator max(double max); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/DoubleTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/DoubleTypeConfiguratorImpl.java new file mode 100644 index 0000000..dddb3df --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/DoubleTypeConfiguratorImpl.java @@ -0,0 +1,53 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.MaxDoubleRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.MinDoubleRule; + + +final class DoubleTypeConfiguratorImpl extends AbstractRuleConfigurator implements DoubleTypeConfigurator { + + DoubleTypeConfiguratorImpl( + int columnNumber, + JFileValidatorConfig configuration, + RuleNode ruleNode) { + super(columnNumber, configuration, ruleNode); + } + + @Override + public DoubleTypeConfigurator min(double min) { + return rule(columnNumber -> new MinDoubleRule(columnNumber, min)); + } + + @Override + public DoubleTypeConfigurator max(double max) { + return rule(columnNumber -> new MaxDoubleRule(columnNumber, max)); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/FileRuleConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/FileRuleConfigurator.java new file mode 100644 index 0000000..df824cc --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/FileRuleConfigurator.java @@ -0,0 +1,47 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface FileRuleConfigurator { + + static FileRuleConfigurator defaultConfigurator(JFileValidatorConfig configuration) { + return new FileRuleConfiguratorImpl(configuration); + } + + /** + * Creates the rule configurator for lines. + * + * @return the configurator to configure lines rules. + */ + LineRuleConfigurator lines(); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/FileRuleConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/FileRuleConfiguratorImpl.java new file mode 100644 index 0000000..6e7ca69 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/FileRuleConfiguratorImpl.java @@ -0,0 +1,43 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; + + +final class FileRuleConfiguratorImpl implements FileRuleConfigurator { + + private final JFileValidatorConfig configuration; + + FileRuleConfiguratorImpl(JFileValidatorConfig configuration) { + this.configuration = configuration; + } + + @Override + public LineRuleConfigurator lines() { + return new LineRuleConfiguratorImpl(configuration); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/FloatTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/FloatTypeConfigurator.java new file mode 100644 index 0000000..42f5b48 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/FloatTypeConfigurator.java @@ -0,0 +1,51 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface FloatTypeConfigurator extends TypedRuleConfigurator { + + /** + * Defines a min value rule validation. + * + * @param min the minimum value that is valid to the column. + * + * @return the object with the min rule configured. + */ + FloatTypeConfigurator min(float min); + + /** + * Defines a max value rule validation. + * + * @param max the maximum value that is valid to the column. + * + * @return the object with the max rule configured. + */ + FloatTypeConfigurator max(float max); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/FloatTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/FloatTypeConfiguratorImpl.java new file mode 100644 index 0000000..b8475bb --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/FloatTypeConfiguratorImpl.java @@ -0,0 +1,53 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.MaxFloatRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.MinFloatRule; + + +final class FloatTypeConfiguratorImpl extends AbstractRuleConfigurator implements FloatTypeConfigurator { + + FloatTypeConfiguratorImpl( + int columnNumber, + JFileValidatorConfig configuration, + RuleNode ruleNode) { + super(columnNumber, configuration, ruleNode); + } + + @Override + public FloatTypeConfigurator min(float min) { + return rule(columnNumber -> new MinFloatRule(columnNumber, min)); + } + + @Override + public FloatTypeConfigurator max(float max) { + return rule(columnNumber -> new MaxFloatRule(columnNumber, max)); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/GenericTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/GenericTypeConfigurator.java new file mode 100644 index 0000000..3146955 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/GenericTypeConfigurator.java @@ -0,0 +1,182 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import java.text.DateFormat; +import java.text.DecimalFormat; +import java.time.format.DateTimeFormatter; +import java.util.regex.Pattern; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface GenericTypeConfigurator { + + /** + * Defines a rule for a column of {@link Short} type. + * + * @return the {@link Short} type configurator. + */ + ShortTypeConfigurator shortType(); + + /** + * Defines a rule for a column of {@link Integer} type. + * + * @return the {@link Integer} type configurator. + */ + IntegerTypeConfigurator integerType(); + + /** + * Defines a rule for a column of {@link Long} type. + * + * @return the {@link Long} type configurator. + */ + LongTypeConfigurator longType(); + + /** + * Defines a rule for a column of {@link Float} type. + * + * @return the {@link Float} type configurator. + */ + FloatTypeConfigurator floatType(); + + /** + * Defines a rule for a column of {@link Double} type. + * + * @return the {@link Double} type configurator. + */ + DoubleTypeConfigurator doubleType(); + + /** + * Defines a rule for a column of {@link Boolean} type. + * + * @return the {@link Boolean} type configurator. + */ + BooleanTypeConfigurator booleanType(); + + /** + * Defines a rule for a column of {@link Character} type. + * + * @return the {@link Character} type configurator. + */ + CharacterTypeConfigurator characterType(); + + /** + * Defines a rule for a column of {@link String} type. + * + * @return the {@link String} type configurator. + */ + StringTypeConfigurator stringType(); + + /** + * Defines a rule for a column of {@link java.math.BigInteger} type. + * + * @return the {@link java.math.BigInteger} type configurator. + */ + BigIntegerTypeConfigurator bigIntegerType(); + + /** + * Defines a rule for a column of {@link java.math.BigDecimal} type. + * + * @return the {@link java.math.BigDecimal} type configurator. + */ + BigDecimalTypeConfigurator bigDecimalType(); + + /** + * Defines a rule for a column of {@link java.math.BigDecimal} type. + * + * @param decimalFormat the formatter that matches the column type. + * + * @return the {@link java.math.BigDecimal} type configurator. + */ + BigDecimalTypeConfigurator bigDecimalType(DecimalFormat decimalFormat); + + /** + * Defines a rule for a column of {@link java.util.Date}. + * + * @return the {@link java.util.Date} type configurator. + */ + DateTypeConfigurator dateType(); + + /** + * Defines a rule for a column of {@link java.util.Date} type. + * + * @param dateFormat the formatter that matches the column type. + * + * @return the {@link java.util.Date} type configurator. + */ + DateTypeConfigurator dateType(DateFormat dateFormat); + + /** + * Defines a rule for a column of {@link java.time.LocalDate} type. + * + * @return the {@link java.time.LocalDate} type configurator. + */ + LocalDateTypeConfigurator localDateType(); + + /** + * Defines a rule for a column of {@link java.time.LocalDate} type. + * + * @param dateTimeFormatter the formatter that matches the column type. + * + * @return the {@link java.time.LocalDate} type configurator. + */ + LocalDateTypeConfigurator localDateType(DateTimeFormatter dateTimeFormatter); + + /** + * Defines a rule for a column of {@link java.time.LocalDateTime} type. + * + * @return the {@link java.time.LocalDateTime} type configurator. + */ + LocalDateTimeTypeConfigurator localDateTimeType(); + + /** + * Defines a rule for a column of {@link java.time.LocalDateTime} type. + * + * @param dateTimeFormatter the formatter that matches the column type. + * + * @return the {@link java.time.LocalDateTime} type configurator. + */ + LocalDateTimeTypeConfigurator localDateTimeType(DateTimeFormatter dateTimeFormatter); + + /** + * Defines a rule for a column of array type. + * + * @return the {@link ArrayTypeConfigurator} type configurator. + */ + ArrayTypeConfigurator arrayOf(); + + /** + * Defines a rule for a column of array type. + * + * @param pattern the pattern that is used to split a column string into an array. + * + * @return the {@link ArrayTypeConfigurator} type configurator. + */ + ArrayTypeConfigurator arrayOf(Pattern pattern); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/GenericTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/GenericTypeConfiguratorImpl.java new file mode 100644 index 0000000..dfe0748 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/GenericTypeConfiguratorImpl.java @@ -0,0 +1,211 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNodeImpl; +import com.jonpereiradev.jfile.reader.validator.rule.column.ArrayOfTypeRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.BigDecimalTypeRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.BigIntegerTypeRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.BooleanTypeRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.CharacterTypeRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.DateTypeRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.DoubleTypeRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.FloatTypeRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.IntegerTypeRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.LocalDateTimeTypeRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.LocalDateTypeRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.LongTypeRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.ShortTypeRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.StringTypeRule; + +import java.text.DateFormat; +import java.text.DecimalFormat; +import java.time.format.DateTimeFormatter; +import java.util.regex.Pattern; + + +final class GenericTypeConfiguratorImpl implements GenericTypeConfigurator { + + private static final Pattern DEFAULT_ARRAY_SEPARATOR = Pattern.compile(",\\s*"); + + private final int columnNumber; + private final JFileValidatorConfig configuration; + private final RuleNode ruleNode; + + GenericTypeConfiguratorImpl( + int columnNumber, + JFileValidatorConfig configuration, + RuleNode ruleNode) { + this.columnNumber = columnNumber; + this.configuration = configuration; + this.ruleNode = ruleNode; + } + + @Override + public ShortTypeConfigurator shortType() { + ShortTypeRule rule = new ShortTypeRule(columnNumber); + rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); + ruleNode.add(rule); + return new ShortTypeConfiguratorImpl(columnNumber, configuration, rule.getRuleNode()); + } + + @Override + public IntegerTypeConfigurator integerType() { + IntegerTypeRule rule = new IntegerTypeRule(columnNumber); + rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); + ruleNode.add(rule); + return new IntegerTypeConfiguratorImpl(columnNumber, configuration, rule.getRuleNode()); + } + + @Override + public LongTypeConfigurator longType() { + LongTypeRule rule = new LongTypeRule(columnNumber); + rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); + ruleNode.add(rule); + return new LongTypeConfiguratorImpl(columnNumber, configuration, rule.getRuleNode()); + } + + @Override + public FloatTypeConfigurator floatType() { + FloatTypeRule rule = new FloatTypeRule(columnNumber); + rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); + ruleNode.add(rule); + return new FloatTypeConfiguratorImpl(columnNumber, configuration, rule.getRuleNode()); + } + + @Override + public DoubleTypeConfigurator doubleType() { + DoubleTypeRule rule = new DoubleTypeRule(columnNumber); + rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); + ruleNode.add(rule); + return new DoubleTypeConfiguratorImpl(columnNumber, configuration, rule.getRuleNode()); + } + + @Override + public BooleanTypeConfigurator booleanType() { + BooleanTypeRule rule = new BooleanTypeRule(columnNumber); + rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); + ruleNode.add(rule); + return new BooleanTypeConfiguratorImpl(columnNumber, configuration, rule.getRuleNode()); + } + + @Override + public CharacterTypeConfigurator characterType() { + CharacterTypeRule rule = new CharacterTypeRule(columnNumber); + rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); + ruleNode.add(rule); + return new CharacterTypeConfiguratorImpl(columnNumber, configuration, rule.getRuleNode()); + } + + @Override + public StringTypeConfigurator stringType() { + StringTypeRule rule = new StringTypeRule(columnNumber); + rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); + ruleNode.add(rule); + return new StringTypeConfiguratorImpl(columnNumber, configuration, rule.getRuleNode()); + } + + @Override + public BigIntegerTypeConfigurator bigIntegerType() { + BigIntegerTypeRule rule = new BigIntegerTypeRule(columnNumber); + rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); + ruleNode.add(rule); + return new BigIntegerTypeConfiguratorImpl(columnNumber, configuration, rule.getRuleNode()); + } + + @Override + public BigDecimalTypeConfigurator bigDecimalType() { + return bigDecimalType(configuration.getBigDecimalFormatter()); + } + + @Override + public BigDecimalTypeConfigurator bigDecimalType(DecimalFormat decimalFormat) { + BigDecimalTypeRule rule = new BigDecimalTypeRule(columnNumber, decimalFormat); + rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); + ruleNode.add(rule); + return new BigDecimalTypeConfiguratorImpl(columnNumber, decimalFormat, configuration, rule.getRuleNode()); + } + + @Override + public DateTypeConfigurator dateType() { + return dateType(configuration.getDateFormat()); + } + + @Override + public DateTypeConfigurator dateType(DateFormat dateFormat) { + DateTypeRule rule = new DateTypeRule(columnNumber, dateFormat); + rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); + ruleNode.add(rule); + return new DateTypeConfiguratorImpl(columnNumber, dateFormat, configuration, rule.getRuleNode()); + } + + @Override + public LocalDateTypeConfigurator localDateType() { + return localDateType(configuration.getLocalDateFormatter()); + } + + @Override + public LocalDateTypeConfigurator localDateType(DateTimeFormatter dateTimeFormatter) { + LocalDateTypeRule rule = new LocalDateTypeRule(columnNumber, dateTimeFormatter); + rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); + ruleNode.add(rule); + return new LocalDateTypeConfiguratorImpl(columnNumber, dateTimeFormatter, configuration, rule.getRuleNode()); + } + + @Override + public LocalDateTimeTypeConfigurator localDateTimeType() { + return localDateTimeType(configuration.getLocalDateTimeFormatter()); + } + + @Override + public LocalDateTimeTypeConfigurator localDateTimeType(DateTimeFormatter dateTimeFormatter) { + LocalDateTimeTypeRule rule = new LocalDateTimeTypeRule(columnNumber, dateTimeFormatter); + rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); + ruleNode.add(rule); + return new LocalDateTimeTypeConfiguratorImpl( + columnNumber, + dateTimeFormatter, + configuration, + rule.getRuleNode() + ); + } + + @Override + public ArrayTypeConfigurator arrayOf() { + return arrayOf(DEFAULT_ARRAY_SEPARATOR); + } + + @Override + public ArrayTypeConfigurator arrayOf(Pattern pattern) { + ArrayOfTypeRule rule = new ArrayOfTypeRule(columnNumber, pattern); + rule.setRuleNode(new RuleNodeImpl<>(rule.getClass(), ruleNode)); + ruleNode.add(rule); + return new ArrayTypeConfiguratorImpl(columnNumber, configuration, rule.getRuleNode()); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/IntegerTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/IntegerTypeConfigurator.java new file mode 100644 index 0000000..6cb04e2 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/IntegerTypeConfigurator.java @@ -0,0 +1,60 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface IntegerTypeConfigurator extends TypedRuleConfigurator { + + /** + * Defines a min value rule validation. + * + * @param min the minimum value that is valid to the column. + * + * @return the object with the min rule configured. + */ + IntegerTypeConfigurator min(int min); + + /** + * Defines a max value rule validation. + * + * @param max the maximum value that is valid to the column. + * + * @return the object with the max rule configured. + */ + IntegerTypeConfigurator max(int max); + + /** + * Defines a domain rule validation with possible values options. + * + * @param values the possible values to a column. + * + * @return the object with the domain rule configured. + */ + IntegerTypeConfigurator domain(Integer... values); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/IntegerTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/IntegerTypeConfiguratorImpl.java new file mode 100644 index 0000000..2051092 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/IntegerTypeConfiguratorImpl.java @@ -0,0 +1,58 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.DomainIntegerRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.MaxIntegerRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.MinIntegerRule; + +import java.util.Arrays; + + +final class IntegerTypeConfiguratorImpl extends AbstractRuleConfigurator implements IntegerTypeConfigurator { + + IntegerTypeConfiguratorImpl(int columnNumber, JFileValidatorConfig configuration, RuleNode ruleNode) { + super(columnNumber, configuration, ruleNode); + } + + @Override + public IntegerTypeConfigurator min(int min) { + return rule(columnNumber -> new MinIntegerRule(columnNumber, min)); + } + + @Override + public IntegerTypeConfigurator max(int max) { + return rule(columnNumber -> new MaxIntegerRule(columnNumber, max)); + } + + @Override + public IntegerTypeConfigurator domain(Integer... values) { + return rule(columnNumber -> new DomainIntegerRule(columnNumber, Arrays.asList(values))); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LineRuleConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LineRuleConfigurator.java new file mode 100644 index 0000000..fae6278 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LineRuleConfigurator.java @@ -0,0 +1,56 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface LineRuleConfigurator { + + static LineRuleConfigurator defaultConfigurator(JFileValidatorConfig configuration) { + return new LineRuleConfiguratorImpl(configuration); + } + + /** + * Define the number of columns for one line. + * + * @param size the number of columns that is valid to the line. + * + * @return the object with the column size rule configured. + */ + LineRuleConfigurator columnsSize(int size); + + /** + * Creates the rule config for the column at position. + * + * @return the configurator to configure column rules. + */ + ColumnRuleConfigurator columns(); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LineRuleConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LineRuleConfiguratorImpl.java new file mode 100644 index 0000000..c466cdd --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LineRuleConfiguratorImpl.java @@ -0,0 +1,50 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; +import com.jonpereiradev.jfile.reader.validator.rule.line.LineColumnSizeRule; + + +final class LineRuleConfiguratorImpl implements LineRuleConfigurator { + + private final JFileValidatorConfig configuration; + + LineRuleConfiguratorImpl(JFileValidatorConfig configuration) { + this.configuration = configuration; + } + + @Override + public LineRuleConfigurator columnsSize(int size) { + configuration.getRuleRoot().getLineRootNode().add(new LineColumnSizeRule(size)); + return this; + } + + @Override + public ColumnRuleConfigurator columns() { + return new ColumnRuleConfiguratorImpl(configuration); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LocalDateTimeTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LocalDateTimeTypeConfigurator.java new file mode 100644 index 0000000..b493804 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LocalDateTimeTypeConfigurator.java @@ -0,0 +1,100 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import java.time.LocalDateTime; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface LocalDateTimeTypeConfigurator extends TypedRuleConfigurator { + + /** + * Defines a future rule validation. + * + * @return the object with the future rule configured. + */ + LocalDateTimeTypeConfigurator future(); + + /** + * Defines a future or present rule validation. + * + * @return the object with the future or present rule configured. + */ + LocalDateTimeTypeConfigurator futureOrPresent(); + + /** + * Defines a past rule validation. + * + * @return the object with the past rule configured. + */ + LocalDateTimeTypeConfigurator past(); + + /** + * Defines a past or present rule validation. + * + * @return the object with the past or present rule configured. + */ + LocalDateTimeTypeConfigurator pastOrPresent(); + + /** + * Defines a minimum date rule validation. + * + * @param min the minimum value for the date. + * + * @return the object with the after rule configured. + */ + LocalDateTimeTypeConfigurator after(LocalDateTime min); + + /** + * Defines a minimum date rule validation comparing to another column. + * + * @param columnNumber the column number that is compared to the current column value. + * + * @return the object with the after rule configured. + */ + LocalDateTimeTypeConfigurator after(int columnNumber); + + /** + * Defines a maximum date rule validation. + * + * @param max the maximum value for the date. + * + * @return the object with the before rule configured. + */ + LocalDateTimeTypeConfigurator before(LocalDateTime max); + + /** + * Defines a maximum date rule validation comparing to another column. + * + * @param columnNumber the column number that is compared to the current column value. + * + * @return the object with the before rule configured. + */ + LocalDateTimeTypeConfigurator before(int columnNumber); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LocalDateTimeTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LocalDateTimeTypeConfiguratorImpl.java new file mode 100644 index 0000000..4540258 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LocalDateTimeTypeConfiguratorImpl.java @@ -0,0 +1,96 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.LocalDateTimeAfterRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.LocalDateTimeBeforeRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.LocalDateTimeFutureOrPresentRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.LocalDateTimeFutureRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.LocalDateTimePastOrPresentRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.LocalDateTimePastRule; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + + +final class LocalDateTimeTypeConfiguratorImpl + extends AbstractRuleConfigurator implements LocalDateTimeTypeConfigurator { + + private final DateTimeFormatter dateTimeFormatter; + + LocalDateTimeTypeConfiguratorImpl( + int columnNumber, + DateTimeFormatter dateTimeFormatter, + JFileValidatorConfig configuration, + RuleNode ruleNode + ) { + super(columnNumber, configuration, ruleNode); + this.dateTimeFormatter = dateTimeFormatter; + } + + @Override + public LocalDateTimeTypeConfigurator future() { + return rule(columnNumber -> new LocalDateTimeFutureRule(columnNumber, dateTimeFormatter)); + } + + @Override + public LocalDateTimeTypeConfigurator futureOrPresent() { + return rule(columnNumber -> new LocalDateTimeFutureOrPresentRule(columnNumber, dateTimeFormatter)); + } + + @Override + public LocalDateTimeTypeConfigurator past() { + return rule(columnNumber -> new LocalDateTimePastRule(columnNumber, dateTimeFormatter)); + } + + @Override + public LocalDateTimeTypeConfigurator pastOrPresent() { + return rule(columnNumber -> new LocalDateTimePastOrPresentRule(columnNumber, dateTimeFormatter)); + } + + @Override + public LocalDateTimeTypeConfigurator after(LocalDateTime min) { + return rule(columnNumber -> new LocalDateTimeAfterRule(columnNumber, dateTimeFormatter, min)); + } + + @Override + public LocalDateTimeTypeConfigurator after(int column) { + return rule(columnNumber -> new LocalDateTimeAfterRule(columnNumber, dateTimeFormatter, column)); + } + + @Override + public LocalDateTimeTypeConfigurator before(LocalDateTime max) { + return rule(columnNumber -> new LocalDateTimeBeforeRule(columnNumber, dateTimeFormatter, max)); + } + + @Override + public LocalDateTimeTypeConfigurator before(int column) { + return rule(columnNumber -> new LocalDateTimeBeforeRule(columnNumber, dateTimeFormatter, column)); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LocalDateTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LocalDateTypeConfigurator.java new file mode 100644 index 0000000..cca540f --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LocalDateTypeConfigurator.java @@ -0,0 +1,100 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import java.time.LocalDate; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface LocalDateTypeConfigurator extends TypedRuleConfigurator { + + /** + * Defines a future rule validation. + * + * @return the object with the future rule configured. + */ + LocalDateTypeConfigurator future(); + + /** + * Defines a future or present rule validation. + * + * @return the object with the future or present rule configured. + */ + LocalDateTypeConfigurator futureOrPresent(); + + /** + * Defines a past rule validation. + * + * @return the object with the past rule configured. + */ + LocalDateTypeConfigurator past(); + + /** + * Defines a past or present rule validation. + * + * @return the object with the past or present rule configured. + */ + LocalDateTypeConfigurator pastOrPresent(); + + /** + * Defines a minimum date rule validation. + * + * @param min the minimum value for the date. + * + * @return the object with the after rule configured. + */ + LocalDateTypeConfigurator after(LocalDate min); + + /** + * Defines a minimum date rule validation comparing to another column. + * + * @param columnNumber the column number that is compared to the current column value. + * + * @return the object with the after rule configured. + */ + LocalDateTypeConfigurator after(int columnNumber); + + /** + * Defines a maximum date rule validation. + * + * @param max the maximum value for the date. + * + * @return the object with the before rule configured. + */ + LocalDateTypeConfigurator before(LocalDate max); + + /** + * Defines a maximum date rule validation comparing to another column. + * + * @param columnNumber the column number that is compared to the current column value. + * + * @return the object with the before rule configured. + */ + LocalDateTypeConfigurator before(int columnNumber); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LocalDateTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LocalDateTypeConfiguratorImpl.java new file mode 100644 index 0000000..ecb0e7e --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LocalDateTypeConfiguratorImpl.java @@ -0,0 +1,97 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.LocalDateAfterRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.LocalDateBeforeRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.LocalDateFutureOrPresentRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.LocalDateFutureRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.LocalDatePastOrPresentRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.LocalDatePastRule; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + + +final class LocalDateTypeConfiguratorImpl + extends AbstractRuleConfigurator implements LocalDateTypeConfigurator { + + private final DateTimeFormatter dateTimeFormatter; + + LocalDateTypeConfiguratorImpl( + int columnNumber, + DateTimeFormatter dateTimeFormatter, + JFileValidatorConfig configuration, + RuleNode ruleNode + ) { + super(columnNumber, configuration, ruleNode); + this.dateTimeFormatter = dateTimeFormatter; + } + + @Override + public LocalDateTypeConfigurator future() { + return rule(columnNumber -> new LocalDateFutureRule(columnNumber, dateTimeFormatter)); + } + + @Override + public LocalDateTypeConfigurator futureOrPresent() { + return rule(columnNumber -> new LocalDateFutureOrPresentRule(columnNumber, dateTimeFormatter)); + } + + @Override + public LocalDateTypeConfigurator past() { + return rule(columnNumber -> new LocalDatePastRule(columnNumber, dateTimeFormatter)); + } + + @Override + public LocalDateTypeConfigurator pastOrPresent() { + return rule(columnNumber -> new LocalDatePastOrPresentRule(columnNumber, dateTimeFormatter)); + } + + @Override + public LocalDateTypeConfigurator after(LocalDate min) { + return rule(columnNumber -> new LocalDateAfterRule(columnNumber, dateTimeFormatter, min)); + } + + @Override + public LocalDateTypeConfigurator after(int columnNumber) { + return rule(number -> new LocalDateAfterRule(number, dateTimeFormatter, columnNumber)); + } + + @Override + public LocalDateTypeConfigurator before(LocalDate max) { + return rule(columnNumber -> new LocalDateBeforeRule(columnNumber, dateTimeFormatter, max)); + } + + @Override + public LocalDateTypeConfigurator before(int columnNumber) { + return rule(number -> new LocalDateBeforeRule(number, dateTimeFormatter, columnNumber)); + } + + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LongTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LongTypeConfigurator.java new file mode 100644 index 0000000..1d83bcb --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LongTypeConfigurator.java @@ -0,0 +1,60 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface LongTypeConfigurator extends TypedRuleConfigurator { + + /** + * Defines a min value rule validation. + * + * @param min the minimum value that is valid to the column. + * + * @return the object with the min rule configured. + */ + LongTypeConfigurator min(long min); + + /** + * Defines a max value rule validation. + * + * @param max the maximum value that is valid to the column. + * + * @return the object with the max rule configured. + */ + LongTypeConfigurator max(long max); + + /** + * Defines a domain rule validation with possible values options. + * + * @param values the possible values to a column. + * + * @return the object with the domain rule configured. + */ + LongTypeConfigurator domain(Long... values); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LongTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LongTypeConfiguratorImpl.java new file mode 100644 index 0000000..2aa7e2c --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/LongTypeConfiguratorImpl.java @@ -0,0 +1,58 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.DomainLongRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.MaxLongRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.MinLongRule; + +import java.util.Arrays; + + +final class LongTypeConfiguratorImpl extends AbstractRuleConfigurator implements LongTypeConfigurator { + + LongTypeConfiguratorImpl(int columnNumber, JFileValidatorConfig configuration, RuleNode ruleNode) { + super(columnNumber, configuration, ruleNode); + } + + @Override + public LongTypeConfigurator min(long min) { + return rule(columnNumber -> new MinLongRule(columnNumber, min)); + } + + @Override + public LongTypeConfigurator max(long max) { + return rule(columnNumber -> new MaxLongRule(columnNumber, max)); + } + + @Override + public LongTypeConfigurator domain(Long... values) { + return rule(columnNumber -> new DomainLongRule(columnNumber, Arrays.asList(values))); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/RefRuleConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/RefRuleConfigurator.java new file mode 100644 index 0000000..d33ba29 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/RefRuleConfigurator.java @@ -0,0 +1,60 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +/** + * Configurate the reference column rule. + * + * @param the type of rule configurator returned. + * + * @author jonpereiradev + * @since 0.1.0 + */ +public interface RefRuleConfigurator> { + + /** + * Creates a rule for not empty column. + * + * @return the object with the rule filled configured. + */ + T filled(); + + /** + * Creates a rule for column with specific values. + * + * @param values the values that the column must have to activate the ref rule. + * + * @return the object with the rule filled configured. + */ + T filled(Object... values); + + /** + * Creates a rule for column with empty value. + * + * @return the object with the rule empty configured. + */ + T empty(); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/RefRuleConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/RefRuleConfiguratorImpl.java new file mode 100644 index 0000000..9aa5cd0 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/RefRuleConfiguratorImpl.java @@ -0,0 +1,80 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNodeImpl; +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.DomainRefRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.EmptyRefRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.FilledRefRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.RefRule; + +import java.util.Arrays; + + +final class RefRuleConfiguratorImpl> implements RefRuleConfigurator { + + private final int refPosition; + private final int columnNumber; + private final T currentConfigurator; + private final RuleNode rule; + + RefRuleConfiguratorImpl(int refPosition, int columnNumber, RuleNode ruleNode, T currentConfigurator) { + this.refPosition = refPosition; + this.columnNumber = columnNumber; + this.currentConfigurator = currentConfigurator; + this.rule = ruleNode; + } + + @Override + public T filled() { + RefRule ref = new FilledRefRule(refPosition, columnNumber); + ref.setRuleNode(new RuleNodeImpl<>(ref.getClass(), rule)); + rule.add(ref); + currentConfigurator.setRuleNode(ref.getRuleNode()); + return currentConfigurator; + } + + @Override + @SuppressWarnings("unchecked") + public T filled(Object... values) { + RefRule ref = new DomainRefRule(refPosition, columnNumber, Arrays.asList(values)); + ref.setRuleNode(new RuleNodeImpl<>(ref.getClass(), rule)); + rule.add(ref); + currentConfigurator.setRuleNode(ref.getRuleNode()); + return currentConfigurator; + } + + @Override + public T empty() { + RefRule ref = new EmptyRefRule(refPosition, columnNumber); + ref.setRuleNode(new RuleNodeImpl<>(ref.getClass(), rule)); + rule.add(ref); + currentConfigurator.setRuleNode(ref.getRuleNode()); + return currentConfigurator; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/ShortTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/ShortTypeConfigurator.java new file mode 100644 index 0000000..7b41a06 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/ShortTypeConfigurator.java @@ -0,0 +1,60 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface ShortTypeConfigurator extends TypedRuleConfigurator { + + /** + * Defines a min value rule validation. + * + * @param min the minimum value that is valid to the column. + * + * @return the object with the min rule configured. + */ + ShortTypeConfigurator min(short min); + + /** + * Defines a max value rule validation. + * + * @param max the maximum value that is valid to the column. + * + * @return the object with the max rule configured. + */ + ShortTypeConfigurator max(short max); + + /** + * Defines a domain rule validation with possible values options. + * + * @param values the possible values to a column. + * + * @return the object with the domain rule configured. + */ + ShortTypeConfigurator domain(Short... values); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/ShortTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/ShortTypeConfiguratorImpl.java new file mode 100644 index 0000000..d01cd04 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/ShortTypeConfiguratorImpl.java @@ -0,0 +1,58 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.DomainShortRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.MaxShortRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.MinShortRule; + +import java.util.Arrays; + + +final class ShortTypeConfiguratorImpl extends AbstractRuleConfigurator implements ShortTypeConfigurator { + + ShortTypeConfiguratorImpl(int columnNumber, JFileValidatorConfig configuration, RuleNode ruleNode) { + super(columnNumber, configuration, ruleNode); + } + + @Override + public ShortTypeConfigurator min(short min) { + return rule(columnNumber -> new MinShortRule(columnNumber, min)); + } + + @Override + public ShortTypeConfigurator max(short max) { + return rule(columnNumber -> new MaxShortRule(columnNumber, max)); + } + + @Override + public ShortTypeConfigurator domain(Short... values) { + return rule(columnNumber -> new DomainShortRule(columnNumber, Arrays.asList(values))); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/StringTypeConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/StringTypeConfigurator.java new file mode 100644 index 0000000..8caa1b5 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/StringTypeConfigurator.java @@ -0,0 +1,109 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import java.util.regex.Pattern; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface StringTypeConfigurator extends TypedRuleConfigurator { + + /** + * Defines a not empty rule validation. + * + * @return the object with the not empty rule configured. + */ + StringTypeConfigurator notEmpty(); + + /** + * Defines a min length rule validation. + * + * @param minLength the minimum length of the string. + * + * @return the object with the min length rule configured. + */ + StringTypeConfigurator minLength(int minLength); + + /** + * Defines a max length rule validation. + * + * @param maxLength the maximum length of the string. + * + * @return the object with the max length rule configured. + */ + StringTypeConfigurator maxLength(int maxLength); + + /** + * Defines a exact length rule validation. + * + * @param length the exact length of the string. + * + * @return the object with the exact length rule configured. + */ + StringTypeConfigurator exactLength(int length); + + /** + * Defines a domain rule validation with possible values options. + * + * @param values the possible values to a column. + * + * @return the object with the domain rule configured. + */ + StringTypeConfigurator domain(String... values); + + /** + * Defines an email rule validation. + * + * @return the object with the email rule configured. + */ + StringTypeConfigurator email(); + + /** + * Defines a brazilian document CPF rule validation. + * + * @return the object with the cpf rule configured. + */ + StringTypeConfigurator cpf(); + + /** + * Defines a brazilian document CNPJ rule validation. + * + * @return the object with the cnpj rule configured. + */ + StringTypeConfigurator cnpj(); + + /** + * Defines a pattern rule validation. + * + * @param pattern a pattern to define valid string state. + * + * @return the object with the pattern rule configured. + */ + StringTypeConfigurator regex(Pattern pattern); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/StringTypeConfiguratorImpl.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/StringTypeConfiguratorImpl.java new file mode 100644 index 0000000..b07418f --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/StringTypeConfiguratorImpl.java @@ -0,0 +1,95 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.column.CnpjRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.CpfRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.DomainStringRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.EmailRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.ExactLengthStringRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.MaxLengthStringRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.MinLengthStringRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.NotEmptyRule; +import com.jonpereiradev.jfile.reader.validator.rule.column.RegexRule; + +import java.util.Arrays; +import java.util.regex.Pattern; + + +final class StringTypeConfiguratorImpl extends AbstractRuleConfigurator implements StringTypeConfigurator { + + StringTypeConfiguratorImpl(int columnNumber, JFileValidatorConfig configuration, RuleNode ruleNode) { + super(columnNumber, configuration, ruleNode); + } + + @Override + public StringTypeConfigurator notEmpty() { + return rule(NotEmptyRule::new); + } + + @Override + public StringTypeConfigurator minLength(int minLength) { + return rule(columnNumber -> new MinLengthStringRule(columnNumber, minLength)); + } + + @Override + public StringTypeConfigurator maxLength(int maxLength) { + return rule(columnNumber -> new MaxLengthStringRule(columnNumber, maxLength)); + } + + @Override + public StringTypeConfigurator exactLength(int length) { + return rule(columnNumber -> new ExactLengthStringRule(columnNumber, length)); + } + + @Override + public StringTypeConfigurator domain(String... values) { + return rule(columnNumber -> new DomainStringRule(columnNumber, Arrays.asList(values))); + } + + @Override + public StringTypeConfigurator email() { + return rule(EmailRule::new); + } + + @Override + public StringTypeConfigurator cpf() { + return rule(CpfRule::new); + } + + @Override + public StringTypeConfigurator cnpj() { + return rule(CnpjRule::new); + } + + @Override + public StringTypeConfigurator regex(Pattern pattern) { + return rule(columnNumber -> new RegexRule(columnNumber, pattern)); + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/TypedRuleConfigurator.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/TypedRuleConfigurator.java new file mode 100644 index 0000000..a888f74 --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/configurator/TypedRuleConfigurator.java @@ -0,0 +1,94 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.configurator; + + +import com.jonpereiradev.jfile.reader.validator.rule.RuleNode; +import com.jonpereiradev.jfile.reader.validator.rule.column.ColumnRule; + +import java.util.function.Function; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface TypedRuleConfigurator> { + + /** + * Apply the rule of not null validation. + * + * @return the configurator with the Not Null rule configured. + */ + T notNull(); + + /** + * Apply the rule of only null validation. + * + * @return the configurator with the Only Null rule configured. + */ + T onlyNull(); + + /** + * Applies the current depends config and return to the root config node. + * + * @return the configurator with the Dependency rule configured. + */ + T apply(); + + /** + * Define a custom rule validation. + * + * @param rule a custom rule to validate the column. + * + * @return the configurator with the Custom rule configured. + */ + T rule(Function rule); + + /** + * Creates the rule config for the column at columnNumber. + * + * @param columnNumber the columnNumber of the column in the line. + * + * @return the configurator to configure a column. + */ + GenericTypeConfigurator column(int columnNumber); + + /** + * Creates the dependency rule validation between columns. + * + * @param columnNumber the position of the column in the line that the current column depends on. + * + * @return the configurator for the Dependency rule. + */ + RefRuleConfigurator depends(int columnNumber); + + /** + * Changes the current node rule validation. + * + * @param ruleNode the node for the current rule. + */ + void setRuleNode(RuleNode ruleNode); + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/file/FileRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/file/FileRule.java new file mode 100644 index 0000000..43917fb --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/file/FileRule.java @@ -0,0 +1,38 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.file; + + +import com.jonpereiradev.jfile.reader.validator.rule.Rule; + +import java.io.File; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface FileRule extends Rule { + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/line/LineColumnSizeRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/line/LineColumnSizeRule.java new file mode 100644 index 0000000..d500c1a --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/line/LineColumnSizeRule.java @@ -0,0 +1,42 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.line; + + +import com.jonpereiradev.jfile.reader.file.LineValue; + + +public class LineColumnSizeRule implements LineRule { + + private final int size; + + public LineColumnSizeRule(int size) { + this.size = size; + } + + public boolean isValid(LineValue lineValue) { + return lineValue.getColumnValues().size() == size; + } + +} diff --git a/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/line/LineRule.java b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/line/LineRule.java new file mode 100644 index 0000000..804a08b --- /dev/null +++ b/src/main/java/com/jonpereiradev/jfile/reader/validator/rule/line/LineRule.java @@ -0,0 +1,43 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.line; + + +import com.jonpereiradev.jfile.reader.file.LineValue; +import com.jonpereiradev.jfile.reader.validator.rule.Rule; +import com.jonpereiradev.jfile.reader.validator.rule.RuleUtils; + + +/** + * @author jonpereiradev + * @since 0.1.0 + */ +public interface LineRule extends Rule { + + @Override + default boolean canValidate(LineValue lineValue) { + return RuleUtils.isNotBlank(lineValue.getContent()); + } + +} diff --git a/src/test/java/com/jonpereiradev/jfile/reader/JFileReaderTest.java b/src/test/java/com/jonpereiradev/jfile/reader/JFileReaderTest.java index 2598ee6..f4e5945 100644 --- a/src/test/java/com/jonpereiradev/jfile/reader/JFileReaderTest.java +++ b/src/test/java/com/jonpereiradev/jfile/reader/JFileReaderTest.java @@ -1,7 +1,30 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package com.jonpereiradev.jfile.reader; -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.file.JFileLine; + +import com.jonpereiradev.jfile.reader.file.LineValue; import com.jonpereiradev.jfile.reader.infrastructure.AbstractFileReaderTest; import com.jonpereiradev.jfile.reader.model.Example; import org.junit.Assert; @@ -9,41 +32,43 @@ import java.io.IOException; import java.math.BigDecimal; -import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Iterator; + public class JFileReaderTest extends AbstractFileReaderTest { @Test public void mustCreateImplementationPerFileType() throws IOException { - Path path = Files.createTempFile("tmp", "1"); + Path path = createFileWithContent("1"); + JFileReaderConfig configuration = JFileReaderFactory.newUtf8ReaderConfig("."); - try (JFileReader fileReader = JFileReaderFactory.newInstance(path, ReaderConfiguration.utf8Reader("."))) { - Assert.assertEquals(JFileReaderImpl.class, fileReader.getClass()); + try (JFileReader fileReader = JFileReaderFactory.newJFileReader(path, configuration)) { + Assert.assertEquals(JFileReaderEngine.class, fileReader.getClass()); } } @Test - public void mustReportFileNotExists() throws IOException { + public void mustReportFileNotExists() { Path path = Paths.get("file-not-found.csv"); - ReaderConfiguration readerConfiguration = ReaderConfiguration.utf8Reader("."); + JFileReaderConfig readerConfig = JFileReaderFactory.newUtf8ReaderConfig("."); - try (JFileReader fileReader = JFileReaderFactory.newInstance(path, readerConfiguration)) { + try (JFileReader fileReader = JFileReaderFactory.newJFileReader(path, readerConfig)) { Assert.fail("File must not exists"); - } catch (IllegalArgumentException e) { - Assert.assertEquals("java.nio.file.NoSuchFileException: file-not-found.csv", e.getMessage()); + } catch (IOException e) { + Assert.assertEquals("file-not-found.csv", e.getMessage()); } } @Test public void mustReportFileExists() throws IOException { - Path path = Files.createTempFile("tmp", "1"); + Path path = createFileWithContent("1"); + JFileReaderConfig readerConfig = JFileReaderFactory.newUtf8ReaderConfig("."); - try (JFileReader fileReader = JFileReaderFactory.newInstance(path, ReaderConfiguration.utf8Reader("."))) { + try (JFileReader fileReader = JFileReaderFactory.newJFileReader(path, readerConfig)) { Assert.assertNotNull(fileReader); } catch (IllegalArgumentException e) { Assert.fail("File must exists"); @@ -54,18 +79,18 @@ public void mustReportFileExists() throws IOException { public void mustReadFileByConfiguration() throws IOException { Path path = createFileWithContent("1;2;19121991"); - ReaderConfiguration configuration = ReaderConfiguration - .utf8Reader("\\;") - .withLocalDateFormatter(DateTimeFormatter.ofPattern("ddMMyyyy")); + JFileReaderConfig configuration = JFileReaderFactory + .newUtf8ReaderConfig("\\;") + .localDateFormatter(DateTimeFormatter.ofPattern("ddMMyyyy")); - try (JFileReader fileReader = JFileReaderFactory.newInstance(path, configuration)) { - Iterator iterator = fileReader.iterator(); + try (JFileReader fileReader = JFileReaderFactory.newJFileReader(path, configuration)) { + Iterator iterator = fileReader.iterator(); Assert.assertTrue("Deve retornar uma linha do arquivo.", iterator.hasNext()); - JFileLine fileLine = iterator.next(); - Assert.assertEquals("1;2;19121991", fileLine.getContent()); - Assert.assertEquals(3, fileLine.getColumns().size()); + LineValue lineValue = iterator.next(); + Assert.assertEquals("1;2;19121991", lineValue.getContent()); + Assert.assertEquals(3, lineValue.getColumnValues().size()); Assert.assertFalse("Não deve retornar porque o arquivo só tem uma linha.", iterator.hasNext()); } @@ -74,9 +99,9 @@ public void mustReadFileByConfiguration() throws IOException { @Test public void mustParserFileContentToObject() throws IOException { Path path = createFileWithContent("1;descricao;1;1;100,37;93,42;19121991;justificativa"); - ReaderConfiguration configuration = ReaderConfiguration.utf8Reader("\\;"); + JFileReaderConfig configuration = JFileReaderFactory.newUtf8ReaderConfig("\\;"); - try (JFileReader fileReader = JFileReaderFactory.newInstance(path, configuration)) { + try (JFileReader fileReader = JFileReaderFactory.newJFileReader(path, configuration)) { fileReader.forEach(Example.class, example -> { Short id = (short) 1; @@ -91,4 +116,5 @@ public void mustParserFileContentToObject() throws IOException { }); } } + } diff --git a/src/test/java/com/jonpereiradev/jfile/reader/benchmark/JFileReaderBenchmark.java b/src/test/java/com/jonpereiradev/jfile/reader/benchmark/JFileReaderBenchmark.java index afefd24..5eeb87e 100644 --- a/src/test/java/com/jonpereiradev/jfile/reader/benchmark/JFileReaderBenchmark.java +++ b/src/test/java/com/jonpereiradev/jfile/reader/benchmark/JFileReaderBenchmark.java @@ -1,14 +1,41 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package com.jonpereiradev.jfile.reader.benchmark; + import com.jonpereiradev.jfile.reader.JFileReader; +import com.jonpereiradev.jfile.reader.JFileReaderConfig; import com.jonpereiradev.jfile.reader.JFileReaderFactory; -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.file.JFileLine; +import com.jonpereiradev.jfile.reader.converter.FileColumn; +import com.jonpereiradev.jfile.reader.file.LineValue; import com.jonpereiradev.jfile.reader.infrastructure.AbstractFileReaderTest; -import com.jonpereiradev.jfile.reader.parser.FileColumn; -import com.jonpereiradev.jfile.reader.rule.RuleConfigurator; -import com.jonpereiradev.jfile.reader.validation.Report; +import com.jonpereiradev.jfile.reader.validator.JFileValidator; +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; +import com.jonpereiradev.jfile.reader.validator.JFileValidatorFactory; +import com.jonpereiradev.jfile.reader.validator.ValidationReport; import org.junit.Assert; +import org.junit.Ignore; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Fork; @@ -27,6 +54,7 @@ import java.nio.file.Path; import java.util.concurrent.TimeUnit; + @State(Scope.Thread) @BenchmarkMode(Mode.AverageTime) @Threads(1) @@ -51,12 +79,13 @@ public void tearDown() throws IOException { } @Benchmark - public void measureReadingPerformance() { - JFileReader reader = JFileReaderFactory.newInstance(pathToFakeFile, ReaderConfiguration.utf8Reader(";")); + public void measureReadingPerformance() throws IOException { + JFileReaderConfig readerConfig = JFileReaderFactory.newUtf8ReaderConfig(";"); + JFileReader reader = JFileReaderFactory.newJFileReader(pathToFakeFile, readerConfig); int countNumberOfLines = 0; - for (JFileLine line : reader) { + for (LineValue line : reader) { countNumberOfLines++; } @@ -64,10 +93,12 @@ public void measureReadingPerformance() { } @Benchmark - public void measureValidatingWithSuccessPerformance() { - ReaderConfiguration configuration = ReaderConfiguration.utf8Reader(";"); + public void measureValidatingWithSuccessPerformance() throws IOException { + JFileReaderConfig configuration = JFileReaderFactory.newUtf8ReaderConfig(";"); + JFileValidatorConfig validatorConfig = JFileValidatorFactory.newValidatorConfig(); - RuleConfigurator.defaultConfigurator(configuration).files().lines() + validatorConfig + .columns() .column(1).stringType().notNull() .column(2).stringType().notNull() .column(3).integerType().notNull() @@ -75,18 +106,21 @@ public void measureValidatingWithSuccessPerformance() { .column(5).stringType().notNull() .column(6).stringType().notNull(); - JFileReader reader = JFileReaderFactory.newInstance(pathToFakeFile, configuration); + JFileReader reader = JFileReaderFactory.newJFileReader(pathToFakeFile, configuration); + JFileValidator validator = JFileValidatorFactory.newJFileValidator(validatorConfig); - for (JFileLine line : reader) { - reader.validate(line); + for (LineValue line : reader) { + validator.validate(line); } } @Benchmark - public void measureValidatingWithViolationPerformance() { - ReaderConfiguration configuration = ReaderConfiguration.utf8Reader(";"); + public void measureValidatingWithViolationPerformance() throws IOException { + JFileReaderConfig configuration = JFileReaderFactory.newUtf8ReaderConfig(";"); + JFileValidatorConfig validatorConfig = JFileValidatorFactory.newValidatorConfig(); - RuleConfigurator.defaultConfigurator(configuration).files().lines() + validatorConfig + .columns() .column(1).integerType().notNull() .column(2).stringType().notNull() .column(3).integerType().notNull() @@ -94,20 +128,24 @@ public void measureValidatingWithViolationPerformance() { .column(5).stringType().notNull() .column(6).stringType().notNull(); - JFileReader reader = JFileReaderFactory.newInstance(pathToFakeFile, configuration); + JFileReader reader = JFileReaderFactory.newJFileReader(pathToFakeFile, configuration); + JFileValidator validator = JFileValidatorFactory.newJFileValidator(validatorConfig); - for (JFileLine line : reader) { - Report validation = reader.validate(line); - Assert.assertTrue(validation.isInvalid()); + for (LineValue line : reader) { + ValidationReport validation = validator.validate(line); + Assert.assertTrue(validation.isNotValid()); Assert.assertFalse(validation.getViolations().isEmpty()); } } @Benchmark - public void measureValidatingAllFileWithViolationPerformance() { - ReaderConfiguration configuration = ReaderConfiguration.utf8Reader(";").withMaxViolationSize(100); + @Ignore + public void measureValidatingAllFileWithViolationPerformance() throws IOException { + JFileReaderConfig configuration = JFileReaderFactory.newUtf8ReaderConfig(";"); + JFileValidatorConfig validatorConfig = JFileValidatorFactory.newValidatorConfig().maxViolationSize(100); - RuleConfigurator.defaultConfigurator(configuration).files().lines() + validatorConfig + .columns() .column(1).integerType().notNull() .column(2).stringType().notNull() .column(3).integerType().notNull() @@ -115,22 +153,25 @@ public void measureValidatingAllFileWithViolationPerformance() { .column(5).stringType().notNull() .column(6).stringType().notNull(); - JFileReader reader = JFileReaderFactory.newInstance(pathToFakeFile, configuration); - Report report = reader.validate(); + JFileReader reader = JFileReaderFactory.newJFileReader(pathToFakeFile, configuration); + JFileValidator validator = JFileValidatorFactory.newJFileValidator(validatorConfig); - Assert.assertTrue(report.isInvalid()); - Assert.assertFalse(report.getViolations().isEmpty()); + reader.forEach(lineValue -> { + ValidationReport report = validator.validate(lineValue); + Assert.assertTrue(report.isNotValid()); + Assert.assertFalse(report.getViolations().isEmpty()); + }); } @Benchmark - public void measureConvertingPerformance() { - ReaderConfiguration configuration = ReaderConfiguration.utf8Reader(";"); - JFileReader reader = JFileReaderFactory.newInstance(pathToFakeFile, configuration); + public void measureConvertingPerformance() throws IOException { + JFileReaderConfig configuration = JFileReaderFactory.newUtf8ReaderConfig(";"); + JFileReader reader = JFileReaderFactory.newJFileReader(pathToFakeFile, configuration); int countNumberOfLines = 0; - for (JFileLine line : reader) { - reader.parse(line, PerformanceObject.class); + for (LineValue line : reader) { + reader.convert(line, PerformanceObject.class); countNumberOfLines++; } @@ -138,10 +179,12 @@ public void measureConvertingPerformance() { } @Benchmark - public void measureValidatingAndConvertingPerformance() { - ReaderConfiguration configuration = ReaderConfiguration.utf8Reader(";"); + public void measureValidatingAndConvertingPerformance() throws IOException { + JFileReaderConfig configuration = JFileReaderFactory.newUtf8ReaderConfig(";"); + JFileValidatorConfig validatorConfig = JFileValidatorFactory.newValidatorConfig(); - RuleConfigurator.defaultConfigurator(configuration).files().lines() + validatorConfig + .columns() .column(1).stringType().notNull() .column(2).stringType().notNull() .column(3).integerType().notNull() @@ -149,13 +192,14 @@ public void measureValidatingAndConvertingPerformance() { .column(5).stringType().notNull() .column(6).stringType().notNull(); - JFileReader reader = JFileReaderFactory.newInstance(pathToFakeFile, configuration); + JFileReader reader = JFileReaderFactory.newJFileReader(pathToFakeFile, configuration); + JFileValidator validator = JFileValidatorFactory.newJFileValidator(validatorConfig); int countNumberOfLines = 0; - for (JFileLine line : reader) { - reader.validate(line); - reader.parse(line, PerformanceObject.class); + for (LineValue line : reader) { + validator.validate(line); + reader.convert(line, PerformanceObject.class); countNumberOfLines++; } @@ -229,5 +273,7 @@ public String getSix() { public void setSix(String six) { this.six = six; } + } + } diff --git a/src/test/java/com/jonpereiradev/jfile/reader/infrastructure/AbstractFileReaderTest.java b/src/test/java/com/jonpereiradev/jfile/reader/infrastructure/AbstractFileReaderTest.java index 62bf9b1..fee8884 100644 --- a/src/test/java/com/jonpereiradev/jfile/reader/infrastructure/AbstractFileReaderTest.java +++ b/src/test/java/com/jonpereiradev/jfile/reader/infrastructure/AbstractFileReaderTest.java @@ -1,5 +1,29 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package com.jonpereiradev.jfile.reader.infrastructure; + import com.github.javafaker.Faker; import java.io.BufferedWriter; @@ -7,6 +31,7 @@ import java.nio.file.Files; import java.nio.file.Path; + public abstract class AbstractFileReaderTest { protected static final char SEPARATOR = ';'; diff --git a/src/test/java/com/jonpereiradev/jfile/reader/model/Example.java b/src/test/java/com/jonpereiradev/jfile/reader/model/Example.java index 7b40b98..316aaa7 100644 --- a/src/test/java/com/jonpereiradev/jfile/reader/model/Example.java +++ b/src/test/java/com/jonpereiradev/jfile/reader/model/Example.java @@ -1,12 +1,37 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package com.jonpereiradev.jfile.reader.model; -import com.jonpereiradev.jfile.reader.parser.DateTimeFormatter; -import com.jonpereiradev.jfile.reader.parser.DecimalFormatter; -import com.jonpereiradev.jfile.reader.parser.FileColumn; + +import com.jonpereiradev.jfile.reader.converter.DateTimeFormatter; +import com.jonpereiradev.jfile.reader.converter.DecimalFormatter; +import com.jonpereiradev.jfile.reader.converter.FileColumn; import java.math.BigDecimal; import java.time.LocalDate; + public class Example { @FileColumn(1) @@ -99,4 +124,5 @@ public String getJustificativa() { public void setJustificativa(String justificativa) { this.justificativa = justificativa; } + } diff --git a/src/test/java/com/jonpereiradev/jfile/reader/rule/ColumnRuleConfigurationSuiteTests.java b/src/test/java/com/jonpereiradev/jfile/reader/rule/ColumnRuleConfigurationSuiteTests.java deleted file mode 100644 index da9b842..0000000 --- a/src/test/java/com/jonpereiradev/jfile/reader/rule/ColumnRuleConfigurationSuiteTests.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule; - -import com.jonpereiradev.jfile.reader.rule.column.*; -import org.junit.runner.RunWith; -import org.junit.runners.Suite; - -@RunWith(Suite.class) -@Suite.SuiteClasses({ - BigDecimalRuleConfigurationTest.class, - BigIntegerRuleConfigurationTest.class, - BooleanRuleConfigurationTest.class, - CharacterRuleConfigurationTest.class, - DateRuleConfigurationTest.class, - DoubleRuleConfigurationTest.class, - FloatRuleConfigurationTest.class, - IntegerRuleConfigurationTest.class, - LocalDateRuleConfigurationTest.class, - LocalDateTimeRuleConfigurationTest.class, - LongRuleConfigurationTest.class, - ShortRuleConfigurationTest.class, - StringRuleConfigurationTest.class, - RefRuleConfigurationTest.class -}) -public class ColumnRuleConfigurationSuiteTests { -} diff --git a/src/test/java/com/jonpereiradev/jfile/reader/rule/LineRuleConfigurationSuiteTests.java b/src/test/java/com/jonpereiradev/jfile/reader/rule/LineRuleConfigurationSuiteTests.java deleted file mode 100644 index c8425fe..0000000 --- a/src/test/java/com/jonpereiradev/jfile/reader/rule/LineRuleConfigurationSuiteTests.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule; - -import com.jonpereiradev.jfile.reader.rule.line.LineColumnSizeRuleConfigurationTest; -import org.junit.runner.RunWith; -import org.junit.runners.Suite; - -@RunWith(Suite.class) -@Suite.SuiteClasses({ - LineColumnSizeRuleConfigurationTest.class -}) -public class LineRuleConfigurationSuiteTests { -} diff --git a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/AbstractColumnRuleConfigurationTest.java b/src/test/java/com/jonpereiradev/jfile/reader/rule/column/AbstractColumnRuleConfigurationTest.java deleted file mode 100644 index b3e19a4..0000000 --- a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/AbstractColumnRuleConfigurationTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.JFileReader; -import com.jonpereiradev.jfile.reader.JFileReaderFactory; -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.infrastructure.AbstractFileReaderTest; -import com.jonpereiradev.jfile.reader.rule.RuleConfigurator; -import com.jonpereiradev.jfile.reader.rule.RuleViolation; -import com.jonpereiradev.jfile.reader.rule.configurator.LineRuleConfigurator; -import org.junit.Before; - -import java.nio.file.Path; -import java.util.List; - -abstract class AbstractColumnRuleConfigurationTest extends AbstractFileReaderTest { - - private LineRuleConfigurator ruleConfigurator; - private ReaderConfiguration readerConfiguration; - - @Before - public void beforeEach() { - readerConfiguration = ReaderConfiguration.utf8Reader("\\|"); - ruleConfigurator = RuleConfigurator.defaultConfigurator(readerConfiguration).files().lines(); - } - - protected List validate(Path path) { - JFileReader reader = JFileReaderFactory.newInstance(path, readerConfiguration); - return reader.validate().getViolations(); - } - - public LineRuleConfigurator getRuleConfigurator() { - return ruleConfigurator; - } - - public ReaderConfiguration getReaderConfiguration() { - return readerConfiguration; - } -} diff --git a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/ArrayRuleConfigurationTest.java b/src/test/java/com/jonpereiradev/jfile/reader/rule/column/ArrayRuleConfigurationTest.java deleted file mode 100644 index 39d90bb..0000000 --- a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/ArrayRuleConfigurationTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.rule.RuleViolation; -import org.junit.Assert; -import org.junit.Test; - -import java.io.IOException; -import java.nio.file.Path; -import java.util.List; - -public class ArrayRuleConfigurationTest extends AbstractColumnRuleConfigurationTest { - - @Test - public void mustValidateArrayShortCommaSeparator() throws IOException { - Path path = createFileWithContent("a, b, c"); - getRuleConfigurator().column(1).arrayOf().characterType().domain('1', '2', '3'); - List violations = validate(path); - - Assert.assertFalse(violations.isEmpty()); - Assert.assertEquals(DomainCharacterRule.class.getName(), violations.get(0).getRule()); - } - -} diff --git a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/BigIntegerRuleConfigurationTest.java b/src/test/java/com/jonpereiradev/jfile/reader/rule/column/BigIntegerRuleConfigurationTest.java deleted file mode 100644 index fc16740..0000000 --- a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/BigIntegerRuleConfigurationTest.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.rule.RuleViolation; -import org.junit.Assert; -import org.junit.Test; - -import java.io.IOException; -import java.math.BigInteger; -import java.nio.file.Path; -import java.util.List; - -public class BigIntegerRuleConfigurationTest extends AbstractColumnRuleConfigurationTest { - - @Test - public void mustViolateTypeRule() throws IOException { - Path path = createFileWithContent("a"); - getRuleConfigurator().column(1).bigIntegerType(); - List violations = validate(path); - - Assert.assertFalse(violations.isEmpty()); - Assert.assertEquals(BigIntegerTypeRule.class.getName(), violations.get(0).getRule()); - } - - @Test - public void mustViolateNotNullRule() throws IOException { - Path path = createFileWithContent("a||c"); - getRuleConfigurator().column(2).bigIntegerType().notNull(); - List violations = validate(path); - - Assert.assertFalse(violations.isEmpty()); - Assert.assertEquals(NotNullRule.class.getName(), violations.get(0).getRule()); - } - - @Test - public void mustViolateMinRule() throws IOException { - Path path = createFileWithContent("1"); - getRuleConfigurator().column(1).bigIntegerType().min(BigInteger.valueOf(2)); - List violations = validate(path); - - Assert.assertFalse(violations.isEmpty()); - Assert.assertEquals(MinBigIntegerRule.class.getName(), violations.get(0).getRule()); - } - - @Test - public void mustViolateMaxRule() throws IOException { - Path path = createFileWithContent("3"); - getRuleConfigurator().column(1).bigIntegerType().max(BigInteger.valueOf(2)); - List violations = validate(path); - - Assert.assertFalse(violations.isEmpty()); - Assert.assertEquals(MaxBigIntegerRule.class.getName(), violations.get(0).getRule()); - } - -} diff --git a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/BooleanRuleConfigurationTest.java b/src/test/java/com/jonpereiradev/jfile/reader/rule/column/BooleanRuleConfigurationTest.java deleted file mode 100644 index cf36c65..0000000 --- a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/BooleanRuleConfigurationTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.rule.RuleViolation; -import org.junit.Assert; -import org.junit.Test; - -import java.io.IOException; -import java.nio.file.Path; -import java.util.List; - -public class BooleanRuleConfigurationTest extends AbstractColumnRuleConfigurationTest { - - @Test - public void mustViolateTypeRule() throws IOException { - Path path = createFileWithContent("a|a|c"); - getRuleConfigurator().column(2).booleanType(); - List violations = validate(path); - - Assert.assertFalse(violations.isEmpty()); - Assert.assertEquals(BooleanTypeRule.class.getName(), violations.get(0).getRule()); - } - - @Test - public void mustViolateDomainRule() throws IOException { - Path path = createFileWithContent("a|t|c"); - getRuleConfigurator().column(2).booleanType().domain('0', '1'); - List violations = validate(path); - - Assert.assertFalse(violations.isEmpty()); - Assert.assertEquals(DomainCharacterRule.class.getName(), violations.get(0).getRule()); - } - -} diff --git a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/CharacterRuleConfigurationTest.java b/src/test/java/com/jonpereiradev/jfile/reader/rule/column/CharacterRuleConfigurationTest.java deleted file mode 100644 index ade9e3d..0000000 --- a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/CharacterRuleConfigurationTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.rule.RuleViolation; -import org.junit.Assert; -import org.junit.Test; - -import java.io.IOException; -import java.nio.file.Path; -import java.util.List; - -public class CharacterRuleConfigurationTest extends AbstractColumnRuleConfigurationTest { - - @Test - public void mustViolateTypeRule() throws IOException { - Path path = createFileWithContent("a|ab|c"); - getRuleConfigurator().column(2).characterType(); - List violations = validate(path); - - Assert.assertFalse(violations.isEmpty()); - Assert.assertEquals(CharacterTypeRule.class.getName(), violations.get(0).getRule()); - } - - @Test - public void mustViolateDomainRule() throws IOException { - Path path = createFileWithContent("a|a|c"); - getRuleConfigurator().column(2).characterType().domain('0'); - List violations = validate(path); - - Assert.assertFalse(violations.isEmpty()); - Assert.assertEquals(DomainCharacterRule.class.getName(), violations.get(0).getRule()); - } - -} diff --git a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/DoubleRuleConfigurationTest.java b/src/test/java/com/jonpereiradev/jfile/reader/rule/column/DoubleRuleConfigurationTest.java deleted file mode 100644 index 21ec9c9..0000000 --- a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/DoubleRuleConfigurationTest.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.rule.RuleViolation; -import org.junit.Assert; -import org.junit.Test; - -import java.io.IOException; -import java.nio.file.Path; -import java.util.List; - -public class DoubleRuleConfigurationTest extends AbstractColumnRuleConfigurationTest { - - @Test - public void mustViolateTypeRule() throws IOException { - Path path = createFileWithContent("a"); - getRuleConfigurator().column(1).doubleType(); - List violations = validate(path); - - Assert.assertFalse(violations.isEmpty()); - Assert.assertEquals(DoubleTypeRule.class.getName(), violations.get(0).getRule()); - } - - @Test - public void mustViolateNotNullRule() throws IOException { - Path path = createFileWithContent("a||c"); - getRuleConfigurator().column(2).doubleType().notNull(); - List violations = validate(path); - - Assert.assertFalse(violations.isEmpty()); - Assert.assertEquals(NotNullRule.class.getName(), violations.get(0).getRule()); - } - - @Test - public void mustViolateMinRule() throws IOException { - Path path = createFileWithContent("1"); - getRuleConfigurator().column(1).doubleType().min(2); - List violations = validate(path); - - Assert.assertFalse(violations.isEmpty()); - Assert.assertEquals(MinDoubleRule.class.getName(), violations.get(0).getRule()); - } - - @Test - public void mustViolateMaxRule() throws IOException { - Path path = createFileWithContent("3"); - getRuleConfigurator().column(1).doubleType().max(2); - List violations = validate(path); - - Assert.assertFalse(violations.isEmpty()); - Assert.assertEquals(MaxDoubleRule.class.getName(), violations.get(0).getRule()); - } - -} diff --git a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/FloatRuleConfigurationTest.java b/src/test/java/com/jonpereiradev/jfile/reader/rule/column/FloatRuleConfigurationTest.java deleted file mode 100644 index a056bab..0000000 --- a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/FloatRuleConfigurationTest.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.rule.RuleViolation; -import org.junit.Assert; -import org.junit.Test; - -import java.io.IOException; -import java.nio.file.Path; -import java.util.List; - -public class FloatRuleConfigurationTest extends AbstractColumnRuleConfigurationTest { - - @Test - public void mustViolateTypeRule() throws IOException { - Path path = createFileWithContent("a"); - getRuleConfigurator().column(1).floatType(); - List violations = validate(path); - - Assert.assertFalse(violations.isEmpty()); - Assert.assertEquals(FloatTypeRule.class.getName(), violations.get(0).getRule()); - } - - @Test - public void mustViolateNotNullRule() throws IOException { - Path path = createFileWithContent("a||c"); - getRuleConfigurator().column(2).floatType().notNull(); - List violations = validate(path); - - Assert.assertFalse(violations.isEmpty()); - Assert.assertEquals(NotNullRule.class.getName(), violations.get(0).getRule()); - } - - @Test - public void mustViolateMinRule() throws IOException { - Path path = createFileWithContent("1"); - getRuleConfigurator().column(1).floatType().min(2); - List violations = validate(path); - - Assert.assertFalse(violations.isEmpty()); - Assert.assertEquals(MinFloatRule.class.getName(), violations.get(0).getRule()); - } - - @Test - public void mustViolateMaxRule() throws IOException { - Path path = createFileWithContent("3"); - getRuleConfigurator().column(1).floatType().max(2); - List violations = validate(path); - - Assert.assertFalse(violations.isEmpty()); - Assert.assertEquals(MaxFloatRule.class.getName(), violations.get(0).getRule()); - } - -} diff --git a/src/test/java/com/jonpereiradev/jfile/reader/rule/line/LineColumnSizeRuleConfigurationTest.java b/src/test/java/com/jonpereiradev/jfile/reader/rule/line/LineColumnSizeRuleConfigurationTest.java deleted file mode 100644 index 2e21c8c..0000000 --- a/src/test/java/com/jonpereiradev/jfile/reader/rule/line/LineColumnSizeRuleConfigurationTest.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.jonpereiradev.jfile.reader.rule.line; - -import com.jonpereiradev.jfile.reader.JFileReader; -import com.jonpereiradev.jfile.reader.JFileReaderFactory; -import com.jonpereiradev.jfile.reader.configuration.ReaderConfiguration; -import com.jonpereiradev.jfile.reader.infrastructure.AbstractFileReaderTest; -import com.jonpereiradev.jfile.reader.rule.RuleConfigurator; -import com.jonpereiradev.jfile.reader.rule.RuleViolation; -import org.junit.Assert; -import org.junit.Test; - -import java.io.IOException; -import java.nio.file.Path; -import java.util.List; - -public class LineColumnSizeRuleConfigurationTest extends AbstractFileReaderTest { - - @Test - public void mustViolateRule() throws IOException { - Path path = createFileWithContent("1|2|3|4"); - ReaderConfiguration readerConfiguration = ReaderConfiguration.utf8Reader("\\|"); - RuleConfigurator.defaultConfigurator(readerConfiguration).files().lines().columns(5); - - JFileReader reader = JFileReaderFactory.newInstance(path, readerConfiguration); - List violations = reader.validate().getViolations(); - - Assert.assertFalse(violations.isEmpty()); - } - - @Test - public void mustSuccessRule() throws IOException { - Path path = createFileWithContent("1|2|3|4|5"); - ReaderConfiguration readerConfiguration = ReaderConfiguration.utf8Reader("\\|"); - RuleConfigurator.defaultConfigurator(readerConfiguration).files().lines().columns(5); - - JFileReader reader = JFileReaderFactory.newInstance(path, readerConfiguration); - List violations = reader.validate().getViolations(); - - Assert.assertTrue(violations.isEmpty()); - } - -} diff --git a/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/ColumnRuleSuiteTests.java b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/ColumnRuleSuiteTests.java new file mode 100644 index 0000000..5ca0695 --- /dev/null +++ b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/ColumnRuleSuiteTests.java @@ -0,0 +1,64 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule; + + +import com.jonpereiradev.jfile.reader.validator.rule.column.BigDecimalRuleTest; +import com.jonpereiradev.jfile.reader.validator.rule.column.BigIntegerRuleTest; +import com.jonpereiradev.jfile.reader.validator.rule.column.BooleanRuleTest; +import com.jonpereiradev.jfile.reader.validator.rule.column.CharacterRuleTest; +import com.jonpereiradev.jfile.reader.validator.rule.column.DateRuleTest; +import com.jonpereiradev.jfile.reader.validator.rule.column.DoubleRuleTest; +import com.jonpereiradev.jfile.reader.validator.rule.column.FloatRuleTest; +import com.jonpereiradev.jfile.reader.validator.rule.column.IntegerRuleTest; +import com.jonpereiradev.jfile.reader.validator.rule.column.LocalDateRuleTest; +import com.jonpereiradev.jfile.reader.validator.rule.column.LocalDateTimeRuleTest; +import com.jonpereiradev.jfile.reader.validator.rule.column.LongRuleTest; +import com.jonpereiradev.jfile.reader.validator.rule.column.RefRuleTest; +import com.jonpereiradev.jfile.reader.validator.rule.column.ShortRuleTest; +import com.jonpereiradev.jfile.reader.validator.rule.column.StringRuleTest; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + + +@RunWith(Suite.class) +@Suite.SuiteClasses({ + BigDecimalRuleTest.class, + BigIntegerRuleTest.class, + BooleanRuleTest.class, + CharacterRuleTest.class, + DateRuleTest.class, + DoubleRuleTest.class, + FloatRuleTest.class, + IntegerRuleTest.class, + LocalDateRuleTest.class, + LocalDateTimeRuleTest.class, + LongRuleTest.class, + ShortRuleTest.class, + StringRuleTest.class, + RefRuleTest.class +}) +public class ColumnRuleSuiteTests { + +} diff --git a/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/LineRuleSuiteTests.java b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/LineRuleSuiteTests.java new file mode 100644 index 0000000..0cc178e --- /dev/null +++ b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/LineRuleSuiteTests.java @@ -0,0 +1,38 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule; + + +import com.jonpereiradev.jfile.reader.validator.rule.line.ColumnValueSizeRuleTest; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + + +@RunWith(Suite.class) +@Suite.SuiteClasses({ + ColumnValueSizeRuleTest.class +}) +public class LineRuleSuiteTests { + +} diff --git a/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/AbstractColumnRuleTest.java b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/AbstractColumnRuleTest.java new file mode 100644 index 0000000..bc13586 --- /dev/null +++ b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/AbstractColumnRuleTest.java @@ -0,0 +1,81 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.JFileReader; +import com.jonpereiradev.jfile.reader.JFileReaderConfig; +import com.jonpereiradev.jfile.reader.JFileReaderFactory; +import com.jonpereiradev.jfile.reader.infrastructure.AbstractFileReaderTest; +import com.jonpereiradev.jfile.reader.validator.JFileValidator; +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; +import com.jonpereiradev.jfile.reader.validator.JFileValidatorFactory; +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolation; +import com.jonpereiradev.jfile.reader.validator.rule.configurator.LineRuleConfigurator; +import org.junit.Before; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; + + +abstract class AbstractColumnRuleTest extends AbstractFileReaderTest { + + private LineRuleConfigurator ruleConfigurator; + private JFileReaderConfig readerConfig; + private JFileValidatorConfig validatorConfig; + + @Before + public void beforeEach() { + readerConfig = JFileReaderFactory.newUtf8ReaderConfig("\\|"); + validatorConfig = JFileValidatorFactory.newValidatorConfig(); + ruleConfigurator = validatorConfig.files().lines(); + } + + protected List validate(Path path) throws IOException { + JFileReader reader = JFileReaderFactory.newJFileReader(path, readerConfig); + JFileValidator validator = JFileValidatorFactory.newJFileValidator(validatorConfig); + List ruleViolations = new ArrayList<>(); + + reader.forEach(lineValue -> { + ruleViolations.addAll(validator.validate(lineValue).getViolations()); + }); + + return ruleViolations; + } + + public LineRuleConfigurator getRuleConfigurator() { + return ruleConfigurator; + } + + public JFileReaderConfig getReaderConfig() { + return readerConfig; + } + + public JFileValidatorConfig getValidatorConfig() { + return validatorConfig; + } + +} diff --git a/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/ArrayRuleTest.java b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/ArrayRuleTest.java new file mode 100644 index 0000000..c02b84d --- /dev/null +++ b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/ArrayRuleTest.java @@ -0,0 +1,48 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolation; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; + + +public class ArrayRuleTest extends AbstractColumnRuleTest { + + @Test + public void mustValidateArrayShortCommaSeparator() throws IOException { + Path path = createFileWithContent("a, b, c"); + getRuleConfigurator().columns().column(1).arrayOf().characterType().domain('1', '2', '3'); + List violations = validate(path); + + Assert.assertFalse(violations.isEmpty()); + Assert.assertEquals(DomainCharacterRule.class.getName(), violations.get(0).getRule()); + } + +} diff --git a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/BigDecimalRuleConfigurationTest.java b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/BigDecimalRuleTest.java similarity index 56% rename from src/test/java/com/jonpereiradev/jfile/reader/rule/column/BigDecimalRuleConfigurationTest.java rename to src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/BigDecimalRuleTest.java index 456c40b..26332ef 100644 --- a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/BigDecimalRuleConfigurationTest.java +++ b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/BigDecimalRuleTest.java @@ -1,6 +1,30 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.rule.RuleViolation; +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolation; import org.junit.Assert; import org.junit.Test; @@ -10,12 +34,13 @@ import java.text.DecimalFormat; import java.util.List; -public class BigDecimalRuleConfigurationTest extends AbstractColumnRuleConfigurationTest { + +public class BigDecimalRuleTest extends AbstractColumnRuleTest { @Test public void mustViolateTypeRule() throws IOException { Path path = createFileWithContent("a"); - getRuleConfigurator().column(1).bigDecimalType(getBigDecimalFormat()); + getRuleConfigurator().columns().column(1).bigDecimalType(getBigDecimalFormat()); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -27,7 +52,7 @@ public void mustViolateNotNullRule() throws IOException { Path path = createFileWithContent("a||c"); getRuleConfigurator() - .column(2) + .columns().column(2) .bigDecimalType(getBigDecimalFormat()) .notNull(); @@ -42,7 +67,7 @@ public void mustViolateMinRule() throws IOException { Path path = createFileWithContent("1"); getRuleConfigurator() - .column(1) + .columns().column(1) .bigDecimalType(getBigDecimalFormat()) .min(BigDecimal.valueOf(2)); @@ -57,7 +82,7 @@ public void mustViolateMaxRule() throws IOException { Path path = createFileWithContent("3"); getRuleConfigurator() - .column(1) + .columns().column(1) .bigDecimalType(getBigDecimalFormat()) .max(BigDecimal.valueOf(2)); diff --git a/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/BigIntegerRuleTest.java b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/BigIntegerRuleTest.java new file mode 100644 index 0000000..89d547b --- /dev/null +++ b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/BigIntegerRuleTest.java @@ -0,0 +1,79 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolation; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.math.BigInteger; +import java.nio.file.Path; +import java.util.List; + + +public class BigIntegerRuleTest extends AbstractColumnRuleTest { + + @Test + public void mustViolateTypeRule() throws IOException { + Path path = createFileWithContent("a"); + getRuleConfigurator().columns().column(1).bigIntegerType(); + List violations = validate(path); + + Assert.assertFalse(violations.isEmpty()); + Assert.assertEquals(BigIntegerTypeRule.class.getName(), violations.get(0).getRule()); + } + + @Test + public void mustViolateNotNullRule() throws IOException { + Path path = createFileWithContent("a||c"); + getRuleConfigurator().columns().column(2).bigIntegerType().notNull(); + List violations = validate(path); + + Assert.assertFalse(violations.isEmpty()); + Assert.assertEquals(NotNullRule.class.getName(), violations.get(0).getRule()); + } + + @Test + public void mustViolateMinRule() throws IOException { + Path path = createFileWithContent("1"); + getRuleConfigurator().columns().column(1).bigIntegerType().min(BigInteger.valueOf(2)); + List violations = validate(path); + + Assert.assertFalse(violations.isEmpty()); + Assert.assertEquals(MinBigIntegerRule.class.getName(), violations.get(0).getRule()); + } + + @Test + public void mustViolateMaxRule() throws IOException { + Path path = createFileWithContent("3"); + getRuleConfigurator().columns().column(1).bigIntegerType().max(BigInteger.valueOf(2)); + List violations = validate(path); + + Assert.assertFalse(violations.isEmpty()); + Assert.assertEquals(MaxBigIntegerRule.class.getName(), violations.get(0).getRule()); + } + +} diff --git a/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/BooleanRuleTest.java b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/BooleanRuleTest.java new file mode 100644 index 0000000..b23d552 --- /dev/null +++ b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/BooleanRuleTest.java @@ -0,0 +1,58 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolation; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; + + +public class BooleanRuleTest extends AbstractColumnRuleTest { + + @Test + public void mustViolateTypeRule() throws IOException { + Path path = createFileWithContent("a|a|c"); + getRuleConfigurator().columns().column(2).booleanType(); + List violations = validate(path); + + Assert.assertFalse(violations.isEmpty()); + Assert.assertEquals(BooleanTypeRule.class.getName(), violations.get(0).getRule()); + } + + @Test + public void mustViolateDomainRule() throws IOException { + Path path = createFileWithContent("a|t|c"); + getRuleConfigurator().columns().column(2).booleanType().domain('0', '1'); + List violations = validate(path); + + Assert.assertFalse(violations.isEmpty()); + Assert.assertEquals(DomainCharacterRule.class.getName(), violations.get(0).getRule()); + } + +} diff --git a/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/CharacterRuleTest.java b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/CharacterRuleTest.java new file mode 100644 index 0000000..66c4449 --- /dev/null +++ b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/CharacterRuleTest.java @@ -0,0 +1,58 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolation; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; + + +public class CharacterRuleTest extends AbstractColumnRuleTest { + + @Test + public void mustViolateTypeRule() throws IOException { + Path path = createFileWithContent("a|ab|c"); + getRuleConfigurator().columns().column(2).characterType(); + List violations = validate(path); + + Assert.assertFalse(violations.isEmpty()); + Assert.assertEquals(CharacterTypeRule.class.getName(), violations.get(0).getRule()); + } + + @Test + public void mustViolateDomainRule() throws IOException { + Path path = createFileWithContent("a|a|c"); + getRuleConfigurator().columns().column(2).characterType().domain('0'); + List violations = validate(path); + + Assert.assertFalse(violations.isEmpty()); + Assert.assertEquals(DomainCharacterRule.class.getName(), violations.get(0).getRule()); + } + +} diff --git a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/DateRuleConfigurationTest.java b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/DateRuleTest.java similarity index 70% rename from src/test/java/com/jonpereiradev/jfile/reader/rule/column/DateRuleConfigurationTest.java rename to src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/DateRuleTest.java index 4308fd9..f8b32e2 100644 --- a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/DateRuleConfigurationTest.java +++ b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/DateRuleTest.java @@ -1,6 +1,30 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.rule.RuleViolation; +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolation; import org.junit.Assert; import org.junit.Test; @@ -12,13 +36,14 @@ import java.util.List; import java.util.Locale; -public class DateRuleConfigurationTest extends AbstractColumnRuleConfigurationTest { + +public class DateRuleTest extends AbstractColumnRuleTest { @Test public void mustViolateTypeRule() throws IOException { Path path = createFileWithContent("a"); SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); - getRuleConfigurator().column(1).dateType(dateFormat); + getRuleConfigurator().columns().column(1).dateType(dateFormat); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -29,7 +54,7 @@ public void mustViolateTypeRule() throws IOException { public void mustViolateNotNullRule() throws IOException { Path path = createFileWithContent("a||c"); SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); - getRuleConfigurator().column(2).dateType(dateFormat).notNull(); + getRuleConfigurator().columns().column(2).dateType(dateFormat).notNull(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -40,7 +65,7 @@ public void mustViolateNotNullRule() throws IOException { public void mustViolateFutureRule() throws IOException { Path path = createFileWithContent("19/12/1991"); SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); - getRuleConfigurator().column(1).dateType(dateFormat).future(); + getRuleConfigurator().columns().column(1).dateType(dateFormat).future(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -51,7 +76,7 @@ public void mustViolateFutureRule() throws IOException { public void mustViolateFutureOrPresentRule() throws IOException { Path path = createFileWithContent("19/12/1991"); SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); - getRuleConfigurator().column(1).dateType(dateFormat).futureOrPresent(); + getRuleConfigurator().columns().column(1).dateType(dateFormat).futureOrPresent(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -63,7 +88,7 @@ public void mustViolatePastRule() throws IOException { String dataAtual = getDateWithDayCalculated(1); Path path = createFileWithContent(dataAtual); SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); - getRuleConfigurator().column(1).dateType(dateFormat).past(); + getRuleConfigurator().columns().column(1).dateType(dateFormat).past(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -75,7 +100,7 @@ public void mustViolatePastOrPresentRule() throws IOException { String dataAtual = getDateWithDayCalculated(1); Path path = createFileWithContent(dataAtual); SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); - getRuleConfigurator().column(1).dateType(dateFormat).pastOrPresent(); + getRuleConfigurator().columns().column(1).dateType(dateFormat).pastOrPresent(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -87,7 +112,7 @@ public void mustViolateAfterRule() throws IOException { String dataAtual = getDateWithDayCalculated(-1); Path path = createFileWithContent(dataAtual); SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); - getRuleConfigurator().column(1).dateType(dateFormat).after(new Date()); + getRuleConfigurator().columns().column(1).dateType(dateFormat).after(new Date()); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -100,7 +125,7 @@ public void mustViolateAfterOtherColumnRule() throws IOException { String dataFim = getDateWithDayCalculated(-1); Path path = createFileWithContent(dataInicio + "|" + dataFim); SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); - getRuleConfigurator().column(2).dateType(dateFormat).after(1); + getRuleConfigurator().columns().column(2).dateType(dateFormat).after(1); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -112,7 +137,7 @@ public void mustViolateBeforeRule() throws IOException { String dataAtual = getDateWithDayCalculated(1); Path path = createFileWithContent(dataAtual); SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); - getRuleConfigurator().column(1).dateType(dateFormat).before(new Date()); + getRuleConfigurator().columns().column(1).dateType(dateFormat).before(new Date()); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -125,7 +150,7 @@ public void mustViolateBeforeOtherColumnRule() throws IOException { String dataFim = getDateWithDayCalculated(-1); Path path = createFileWithContent(dataInicio + "|" + dataFim); SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); - getRuleConfigurator().column(1).dateType(dateFormat).before(2); + getRuleConfigurator().columns().column(1).dateType(dateFormat).before(2); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); diff --git a/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/DoubleRuleTest.java b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/DoubleRuleTest.java new file mode 100644 index 0000000..8357084 --- /dev/null +++ b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/DoubleRuleTest.java @@ -0,0 +1,78 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolation; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; + + +public class DoubleRuleTest extends AbstractColumnRuleTest { + + @Test + public void mustViolateTypeRule() throws IOException { + Path path = createFileWithContent("a"); + getRuleConfigurator().columns().column(1).doubleType(); + List violations = validate(path); + + Assert.assertFalse(violations.isEmpty()); + Assert.assertEquals(DoubleTypeRule.class.getName(), violations.get(0).getRule()); + } + + @Test + public void mustViolateNotNullRule() throws IOException { + Path path = createFileWithContent("a||c"); + getRuleConfigurator().columns().column(2).doubleType().notNull(); + List violations = validate(path); + + Assert.assertFalse(violations.isEmpty()); + Assert.assertEquals(NotNullRule.class.getName(), violations.get(0).getRule()); + } + + @Test + public void mustViolateMinRule() throws IOException { + Path path = createFileWithContent("1"); + getRuleConfigurator().columns().column(1).doubleType().min(2); + List violations = validate(path); + + Assert.assertFalse(violations.isEmpty()); + Assert.assertEquals(MinDoubleRule.class.getName(), violations.get(0).getRule()); + } + + @Test + public void mustViolateMaxRule() throws IOException { + Path path = createFileWithContent("3"); + getRuleConfigurator().columns().column(1).doubleType().max(2); + List violations = validate(path); + + Assert.assertFalse(violations.isEmpty()); + Assert.assertEquals(MaxDoubleRule.class.getName(), violations.get(0).getRule()); + } + +} diff --git a/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/FloatRuleTest.java b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/FloatRuleTest.java new file mode 100644 index 0000000..5e5c4e5 --- /dev/null +++ b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/FloatRuleTest.java @@ -0,0 +1,78 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolation; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; + + +public class FloatRuleTest extends AbstractColumnRuleTest { + + @Test + public void mustViolateTypeRule() throws IOException { + Path path = createFileWithContent("a"); + getRuleConfigurator().columns().column(1).floatType(); + List violations = validate(path); + + Assert.assertFalse(violations.isEmpty()); + Assert.assertEquals(FloatTypeRule.class.getName(), violations.get(0).getRule()); + } + + @Test + public void mustViolateNotNullRule() throws IOException { + Path path = createFileWithContent("a||c"); + getRuleConfigurator().columns().column(2).floatType().notNull(); + List violations = validate(path); + + Assert.assertFalse(violations.isEmpty()); + Assert.assertEquals(NotNullRule.class.getName(), violations.get(0).getRule()); + } + + @Test + public void mustViolateMinRule() throws IOException { + Path path = createFileWithContent("1"); + getRuleConfigurator().columns().column(1).floatType().min(2); + List violations = validate(path); + + Assert.assertFalse(violations.isEmpty()); + Assert.assertEquals(MinFloatRule.class.getName(), violations.get(0).getRule()); + } + + @Test + public void mustViolateMaxRule() throws IOException { + Path path = createFileWithContent("3"); + getRuleConfigurator().columns().column(1).floatType().max(2); + List violations = validate(path); + + Assert.assertFalse(violations.isEmpty()); + Assert.assertEquals(MaxFloatRule.class.getName(), violations.get(0).getRule()); + } + +} diff --git a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/IntegerRuleConfigurationTest.java b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/IntegerRuleTest.java similarity index 50% rename from src/test/java/com/jonpereiradev/jfile/reader/rule/column/IntegerRuleConfigurationTest.java rename to src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/IntegerRuleTest.java index 1abca2f..de1006f 100644 --- a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/IntegerRuleConfigurationTest.java +++ b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/IntegerRuleTest.java @@ -1,6 +1,30 @@ -package com.jonpereiradev.jfile.reader.rule.column; +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; -import com.jonpereiradev.jfile.reader.rule.RuleViolation; + +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolation; import org.junit.Assert; import org.junit.Test; @@ -8,12 +32,13 @@ import java.nio.file.Path; import java.util.List; -public class IntegerRuleConfigurationTest extends AbstractColumnRuleConfigurationTest { + +public class IntegerRuleTest extends AbstractColumnRuleTest { @Test public void mustViolateTypeRule() throws IOException { Path path = createFileWithContent("a"); - getRuleConfigurator().column(1).integerType(); + getRuleConfigurator().columns().column(1).integerType(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -23,7 +48,7 @@ public void mustViolateTypeRule() throws IOException { @Test public void mustViolateNotNullRule() throws IOException { Path path = createFileWithContent("a||c"); - getRuleConfigurator().column(2).integerType().notNull(); + getRuleConfigurator().columns().column(2).integerType().notNull(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -33,7 +58,7 @@ public void mustViolateNotNullRule() throws IOException { @Test public void mustViolateMinIntegerRule() throws IOException { Path path = createFileWithContent("1"); - getRuleConfigurator().column(1).integerType().min(2); + getRuleConfigurator().columns().column(1).integerType().min(2); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -43,7 +68,7 @@ public void mustViolateMinIntegerRule() throws IOException { @Test public void mustViolateMaxIntegerRule() throws IOException { Path path = createFileWithContent("3"); - getRuleConfigurator().column(1).integerType().max(2); + getRuleConfigurator().columns().column(1).integerType().max(2); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -53,7 +78,7 @@ public void mustViolateMaxIntegerRule() throws IOException { @Test public void mustViolateDomainIntegerRule() throws IOException { Path path = createFileWithContent("5"); - getRuleConfigurator().column(1).integerType().domain(1, 2); + getRuleConfigurator().columns().column(1).integerType().domain(1, 2); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); diff --git a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateRuleConfigurationTest.java b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateRuleTest.java similarity index 69% rename from src/test/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateRuleConfigurationTest.java rename to src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateRuleTest.java index b3c2caa..7d8bf15 100644 --- a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateRuleConfigurationTest.java +++ b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateRuleTest.java @@ -1,6 +1,30 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.rule.RuleViolation; +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolation; import org.junit.Assert; import org.junit.Test; @@ -13,13 +37,14 @@ import java.util.List; import java.util.Locale; -public class LocalDateRuleConfigurationTest extends AbstractColumnRuleConfigurationTest { + +public class LocalDateRuleTest extends AbstractColumnRuleTest { @Test public void mustViolateTypeRule() throws IOException { Path path = createFileWithContent("a"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); - getRuleConfigurator().column(1).localDateType(formatter); + getRuleConfigurator().columns().column(1).localDateType(formatter); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -30,7 +55,7 @@ public void mustViolateTypeRule() throws IOException { public void mustViolateNotNullRule() throws IOException { Path path = createFileWithContent("a||c"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); - getRuleConfigurator().column(2).localDateType(formatter).notNull(); + getRuleConfigurator().columns().column(2).localDateType(formatter).notNull(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -41,7 +66,7 @@ public void mustViolateNotNullRule() throws IOException { public void mustViolateFutureRule() throws IOException { Path path = createFileWithContent("19/12/1991"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); - getRuleConfigurator().column(1).localDateType(formatter).future(); + getRuleConfigurator().columns().column(1).localDateType(formatter).future(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -52,7 +77,7 @@ public void mustViolateFutureRule() throws IOException { public void mustViolateFutureOrPresentRule() throws IOException { Path path = createFileWithContent("19/12/1991"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); - getRuleConfigurator().column(1).localDateType(formatter).futureOrPresent(); + getRuleConfigurator().columns().column(1).localDateType(formatter).futureOrPresent(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -64,7 +89,7 @@ public void mustViolatePastRule() throws IOException { String dataAtual = getDateWithDayCalculated(1); Path path = createFileWithContent(dataAtual); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); - getRuleConfigurator().column(1).localDateType(formatter).past(); + getRuleConfigurator().columns().column(1).localDateType(formatter).past(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -76,7 +101,7 @@ public void mustViolatePastOrPresentRule() throws IOException { String dataAtual = getDateWithDayCalculated(1); Path path = createFileWithContent(dataAtual); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); - getRuleConfigurator().column(1).localDateType(formatter).pastOrPresent(); + getRuleConfigurator().columns().column(1).localDateType(formatter).pastOrPresent(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -88,7 +113,7 @@ public void mustViolateAfterRule() throws IOException { Path path = createFileWithContent("19/12/1991"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); LocalDate min = LocalDate.of(1992, 12, 19); - getRuleConfigurator().column(1).localDateType(formatter).after(min); + getRuleConfigurator().columns().column(1).localDateType(formatter).after(min); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -99,7 +124,7 @@ public void mustViolateAfterRule() throws IOException { public void mustViolateAfterColumnRule() throws IOException { Path path = createFileWithContent("19/12/1991|18/12/1991"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); - getRuleConfigurator().column(2).localDateType(formatter).after(1); + getRuleConfigurator().columns().column(2).localDateType(formatter).after(1); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -111,7 +136,7 @@ public void mustViolateBeforeRule() throws IOException { Path path = createFileWithContent("19/12/1991"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); LocalDate max = LocalDate.of(1991, 12, 18); - getRuleConfigurator().column(1).localDateType(formatter).before(max); + getRuleConfigurator().columns().column(1).localDateType(formatter).before(max); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -122,7 +147,7 @@ public void mustViolateBeforeRule() throws IOException { public void mustViolateBeforeColumnRule() throws IOException { Path path = createFileWithContent("19/12/1991|18/12/1991"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); - getRuleConfigurator().column(1).localDateType(formatter).before(2); + getRuleConfigurator().columns().column(1).localDateType(formatter).before(2); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); diff --git a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimeRuleConfigurationTest.java b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimeRuleTest.java similarity index 74% rename from src/test/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimeRuleConfigurationTest.java rename to src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimeRuleTest.java index db2b8fd..c979ccf 100644 --- a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/LocalDateTimeRuleConfigurationTest.java +++ b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/LocalDateTimeRuleTest.java @@ -1,6 +1,30 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.rule.RuleViolation; +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolation; import org.junit.Assert; import org.junit.Test; @@ -13,13 +37,14 @@ import java.util.List; import java.util.Locale; -public class LocalDateTimeRuleConfigurationTest extends AbstractColumnRuleConfigurationTest { + +public class LocalDateTimeRuleTest extends AbstractColumnRuleTest { @Test public void mustViolateTypeRule() throws IOException { Path path = createFileWithContent("a"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); - getRuleConfigurator().column(1).localDateTimeType(formatter); + getRuleConfigurator().columns().column(1).localDateTimeType(formatter); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -30,7 +55,7 @@ public void mustViolateTypeRule() throws IOException { public void mustViolateNotNullRule() throws IOException { Path path = createFileWithContent("a||c"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); - getRuleConfigurator().column(2).localDateTimeType(formatter).notNull(); + getRuleConfigurator().columns().column(2).localDateTimeType(formatter).notNull(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -43,7 +68,7 @@ public void mustViolateFutureRule() throws IOException { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss.SSS"); getRuleConfigurator() - .column(1) + .columns().column(1) .localDateTimeType(formatter) .future(); @@ -59,7 +84,7 @@ public void mustViolateFutureOrPresentRule() throws IOException { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss.SSS"); getRuleConfigurator() - .column(1) + .columns().column(1) .localDateTimeType(formatter) .futureOrPresent(); @@ -76,7 +101,7 @@ public void mustViolatePastRule() throws IOException { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss.SSS"); getRuleConfigurator() - .column(1) + .columns().column(1) .localDateTimeType(formatter) .past(); @@ -93,7 +118,7 @@ public void mustViolatePastOrPresentRule() throws IOException { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss.SSS"); getRuleConfigurator() - .column(1) + .columns().column(1) .localDateTimeType(formatter) .pastOrPresent(); @@ -109,7 +134,7 @@ public void mustViolateAfterRule() throws IOException { Path path = createFileWithContent(dataAtual); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss.SSS"); LocalDateTime min = LocalDateTime.now().plusDays(1); - getRuleConfigurator().column(1).localDateTimeType(formatter).after(min); + getRuleConfigurator().columns().column(1).localDateTimeType(formatter).after(min); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -122,7 +147,7 @@ public void mustViolateAfterColumnRule() throws IOException { String dataFim = getDateWithDayCalculated(-1); Path path = createFileWithContent(dataInicio + "|" + dataFim); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss.SSS"); - getRuleConfigurator().column(2).localDateTimeType(formatter).after(1); + getRuleConfigurator().columns().column(2).localDateTimeType(formatter).after(1); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -135,7 +160,7 @@ public void mustViolateBeforeRule() throws IOException { Path path = createFileWithContent(dataAtual); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss.SSS"); LocalDateTime max = LocalDateTime.now().minusDays(1); - getRuleConfigurator().column(1).localDateTimeType(formatter).before(max); + getRuleConfigurator().columns().column(1).localDateTimeType(formatter).before(max); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -148,7 +173,7 @@ public void mustViolateBeforeColumnRule() throws IOException { String dataFim = getDateWithDayCalculated(-1); Path path = createFileWithContent(dataInicio + "|" + dataFim); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss.SSS"); - getRuleConfigurator().column(1).localDateTimeType(formatter).before(2); + getRuleConfigurator().columns().column(1).localDateTimeType(formatter).before(2); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); diff --git a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/LongRuleConfigurationTest.java b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/LongRuleTest.java similarity index 50% rename from src/test/java/com/jonpereiradev/jfile/reader/rule/column/LongRuleConfigurationTest.java rename to src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/LongRuleTest.java index 7810a96..1e10604 100644 --- a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/LongRuleConfigurationTest.java +++ b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/LongRuleTest.java @@ -1,6 +1,30 @@ -package com.jonpereiradev.jfile.reader.rule.column; +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; -import com.jonpereiradev.jfile.reader.rule.RuleViolation; + +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolation; import org.junit.Assert; import org.junit.Test; @@ -8,12 +32,13 @@ import java.nio.file.Path; import java.util.List; -public class LongRuleConfigurationTest extends AbstractColumnRuleConfigurationTest { + +public class LongRuleTest extends AbstractColumnRuleTest { @Test public void mustViolateTypeRule() throws IOException { Path path = createFileWithContent("a"); - getRuleConfigurator().column(1).longType(); + getRuleConfigurator().columns().column(1).longType(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -23,7 +48,7 @@ public void mustViolateTypeRule() throws IOException { @Test public void mustViolateNotNullRule() throws IOException { Path path = createFileWithContent("a||c"); - getRuleConfigurator().column(2).longType().notNull(); + getRuleConfigurator().columns().column(2).longType().notNull(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -33,7 +58,7 @@ public void mustViolateNotNullRule() throws IOException { @Test public void mustViolateMinLongRule() throws IOException { Path path = createFileWithContent("1"); - getRuleConfigurator().column(1).longType().min(2); + getRuleConfigurator().columns().column(1).longType().min(2); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -43,7 +68,7 @@ public void mustViolateMinLongRule() throws IOException { @Test public void mustViolateMaxLongRule() throws IOException { Path path = createFileWithContent("3"); - getRuleConfigurator().column(1).longType().max(2); + getRuleConfigurator().columns().column(1).longType().max(2); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -53,7 +78,7 @@ public void mustViolateMaxLongRule() throws IOException { @Test public void mustViolateDomainLongRule() throws IOException { Path path = createFileWithContent("5"); - getRuleConfigurator().column(1).longType().domain(1L, 2L); + getRuleConfigurator().columns().column(1).longType().domain(1L, 2L); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); diff --git a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/RefRuleConfigurationTest.java b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/RefRuleTest.java similarity index 55% rename from src/test/java/com/jonpereiradev/jfile/reader/rule/column/RefRuleConfigurationTest.java rename to src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/RefRuleTest.java index 6e25c78..a7bcf2e 100644 --- a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/RefRuleConfigurationTest.java +++ b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/RefRuleTest.java @@ -1,6 +1,30 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.rule.RuleViolation; +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolation; import org.junit.Assert; import org.junit.Test; @@ -8,12 +32,13 @@ import java.nio.file.Path; import java.util.List; -public class RefRuleConfigurationTest extends AbstractColumnRuleConfigurationTest { + +public class RefRuleTest extends AbstractColumnRuleTest { @Test public void mustViolateRefFilledRule() throws IOException { Path path = createFileWithContent("1||3"); - getRuleConfigurator().column(2).integerType().depends(1).filled().notNull(); + getRuleConfigurator().columns().column(2).integerType().depends(1).filled().notNull(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -23,7 +48,7 @@ public void mustViolateRefFilledRule() throws IOException { @Test public void mustViolateRefFilledDomainRule() throws IOException { Path path = createFileWithContent("1||3"); - getRuleConfigurator().column(2).integerType().depends(1).filled('1').notNull(); + getRuleConfigurator().columns().column(2).integerType().depends(1).filled('1').notNull(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -33,7 +58,7 @@ public void mustViolateRefFilledDomainRule() throws IOException { @Test public void mustViolateRefEmptyRule() throws IOException { Path path = createFileWithContent("||3"); - getRuleConfigurator().column(1).integerType().depends(2).empty().notNull(); + getRuleConfigurator().columns().column(1).integerType().depends(2).empty().notNull(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -45,7 +70,7 @@ public void mustViolateMultipleRefRule() throws IOException { Path path = createFileWithContent("2||3"); getRuleConfigurator() - .column(1) + .columns().column(1) .integerType() .depends(2) .filled(1) @@ -65,7 +90,7 @@ public void mustViolateOnlyNullRule() throws IOException { Path path = createFileWithContent("2||3"); getRuleConfigurator() - .column(1) + .columns().column(1) .integerType() .depends(2) .filled(1) diff --git a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/ShortRuleConfigurationTest.java b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/ShortRuleTest.java similarity index 50% rename from src/test/java/com/jonpereiradev/jfile/reader/rule/column/ShortRuleConfigurationTest.java rename to src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/ShortRuleTest.java index ac53d9e..cea0b7e 100644 --- a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/ShortRuleConfigurationTest.java +++ b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/ShortRuleTest.java @@ -1,6 +1,30 @@ -package com.jonpereiradev.jfile.reader.rule.column; +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; -import com.jonpereiradev.jfile.reader.rule.RuleViolation; + +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolation; import org.junit.Assert; import org.junit.Test; @@ -8,12 +32,13 @@ import java.nio.file.Path; import java.util.List; -public class ShortRuleConfigurationTest extends AbstractColumnRuleConfigurationTest { + +public class ShortRuleTest extends AbstractColumnRuleTest { @Test public void mustViolateTypeRule() throws IOException { Path path = createFileWithContent("a"); - getRuleConfigurator().column(1).shortType(); + getRuleConfigurator().columns().column(1).shortType(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -23,7 +48,7 @@ public void mustViolateTypeRule() throws IOException { @Test public void mustViolateNotNullRule() throws IOException { Path path = createFileWithContent("a||c"); - getRuleConfigurator().column(2).shortType().notNull(); + getRuleConfigurator().columns().column(2).shortType().notNull(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -33,7 +58,7 @@ public void mustViolateNotNullRule() throws IOException { @Test public void mustViolateMinRule() throws IOException { Path path = createFileWithContent("1"); - getRuleConfigurator().column(1).shortType().min((short) 2); + getRuleConfigurator().columns().column(1).shortType().min((short) 2); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -43,7 +68,7 @@ public void mustViolateMinRule() throws IOException { @Test public void mustViolateMaxRule() throws IOException { Path path = createFileWithContent("3"); - getRuleConfigurator().column(1).shortType().max((short) 2); + getRuleConfigurator().columns().column(1).shortType().max((short) 2); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -53,7 +78,7 @@ public void mustViolateMaxRule() throws IOException { @Test public void mustViolateDomainRule() throws IOException { Path path = createFileWithContent("5"); - getRuleConfigurator().column(1).shortType().domain((short) 1); + getRuleConfigurator().columns().column(1).shortType().domain((short) 1); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); diff --git a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/StringRuleConfigurationTest.java b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/StringRuleTest.java similarity index 59% rename from src/test/java/com/jonpereiradev/jfile/reader/rule/column/StringRuleConfigurationTest.java rename to src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/StringRuleTest.java index 61e8f66..742b86d 100644 --- a/src/test/java/com/jonpereiradev/jfile/reader/rule/column/StringRuleConfigurationTest.java +++ b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/column/StringRuleTest.java @@ -1,6 +1,30 @@ -package com.jonpereiradev.jfile.reader.rule.column; - -import com.jonpereiradev.jfile.reader.rule.RuleViolation; +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.column; + + +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolation; import org.junit.Assert; import org.junit.Test; @@ -9,12 +33,13 @@ import java.util.List; import java.util.regex.Pattern; -public class StringRuleConfigurationTest extends AbstractColumnRuleConfigurationTest { + +public class StringRuleTest extends AbstractColumnRuleTest { @Test public void mustViolateNotNullRule() throws IOException { Path path = createFileWithContent("a||c"); - getRuleConfigurator().column(2).stringType().notNull(); + getRuleConfigurator().columns().column(2).stringType().notNull(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -24,7 +49,7 @@ public void mustViolateNotNullRule() throws IOException { @Test public void mustViolateNotEmptyRule() throws IOException { Path path = createFileWithContent("a| |c"); - getRuleConfigurator().column(2).stringType().notEmpty(); + getRuleConfigurator().columns().column(2).stringType().notEmpty(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -34,27 +59,27 @@ public void mustViolateNotEmptyRule() throws IOException { @Test public void mustViolateMinRule() throws IOException { Path path = createFileWithContent("a| ab |c"); - getRuleConfigurator().column(2).stringType().min(3); + getRuleConfigurator().columns().column(2).stringType().minLength(3); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); - Assert.assertEquals(MinStringRule.class.getName(), violations.get(0).getRule()); + Assert.assertEquals(MinLengthStringRule.class.getName(), violations.get(0).getRule()); } @Test public void mustViolateMaxRule() throws IOException { Path path = createFileWithContent("a| abcde |c"); - getRuleConfigurator().column(2).stringType().max(3); + getRuleConfigurator().columns().column(2).stringType().maxLength(3); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); - Assert.assertEquals(MaxStringRule.class.getName(), violations.get(0).getRule()); + Assert.assertEquals(MaxLengthStringRule.class.getName(), violations.get(0).getRule()); } @Test public void mustViolateDomainRule() throws IOException { Path path = createFileWithContent("a|2|c"); - getRuleConfigurator().column(2).stringType().domain("1"); + getRuleConfigurator().columns().column(2).stringType().domain("1"); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -65,7 +90,7 @@ public void mustViolateDomainRule() throws IOException { public void mustViolateRegexRule() throws IOException { Path path = createFileWithContent("a|a|c"); Pattern pattern = Pattern.compile("\\d+"); - getRuleConfigurator().column(2).stringType().regex(pattern); + getRuleConfigurator().columns().column(2).stringType().regex(pattern); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -75,7 +100,7 @@ public void mustViolateRegexRule() throws IOException { @Test public void mustViolateEmailRule() throws IOException { Path path = createFileWithContent("a|a|c"); - getRuleConfigurator().column(2).stringType().email(); + getRuleConfigurator().columns().column(2).stringType().email(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -85,7 +110,7 @@ public void mustViolateEmailRule() throws IOException { @Test public void mustViolateCnpjRule() throws IOException { Path path = createFileWithContent("a|a|c"); - getRuleConfigurator().column(2).stringType().cnpj(); + getRuleConfigurator().columns().column(2).stringType().cnpj(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -95,7 +120,7 @@ public void mustViolateCnpjRule() throws IOException { @Test public void mustViolateCpfRule() throws IOException { Path path = createFileWithContent("a|a|c"); - getRuleConfigurator().column(2).stringType().cpf(); + getRuleConfigurator().columns().column(2).stringType().cpf(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); @@ -105,7 +130,7 @@ public void mustViolateCpfRule() throws IOException { @Test public void mustViolateOnlyNullRule() throws IOException { Path path = createFileWithContent("a|a|c"); - getRuleConfigurator().column(2).stringType().onlyNull(); + getRuleConfigurator().columns().column(2).stringType().onlyNull(); List violations = validate(path); Assert.assertFalse(violations.isEmpty()); diff --git a/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/line/ColumnValueSizeRuleTest.java b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/line/ColumnValueSizeRuleTest.java new file mode 100644 index 0000000..5c01f90 --- /dev/null +++ b/src/test/java/com/jonpereiradev/jfile/reader/validator/rule/line/ColumnValueSizeRuleTest.java @@ -0,0 +1,75 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jonathan de Almeida Pereira + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.jonpereiradev.jfile.reader.validator.rule.line; + + +import com.jonpereiradev.jfile.reader.JFileReader; +import com.jonpereiradev.jfile.reader.JFileReaderConfig; +import com.jonpereiradev.jfile.reader.JFileReaderFactory; +import com.jonpereiradev.jfile.reader.infrastructure.AbstractFileReaderTest; +import com.jonpereiradev.jfile.reader.validator.JFileValidator; +import com.jonpereiradev.jfile.reader.validator.JFileValidatorConfig; +import com.jonpereiradev.jfile.reader.validator.JFileValidatorFactory; +import com.jonpereiradev.jfile.reader.validator.rule.RuleViolation; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; + + +public class ColumnValueSizeRuleTest extends AbstractFileReaderTest { + + @Test + public void mustViolateRule() throws IOException { + Path path = createFileWithContent("1|2|3|4"); + JFileReaderConfig readerConfig = JFileReaderFactory.newUtf8ReaderConfig("\\|"); + JFileReader reader = JFileReaderFactory.newJFileReader(path, readerConfig); + + JFileValidatorConfig validatorConfig = JFileValidatorFactory.newValidatorConfig(); + JFileValidator validator = JFileValidatorFactory.newJFileValidator(validatorConfig); + + validatorConfig.files().lines().columnsSize(5); + + List violations = validator.validate(reader.iterator().next()).getViolations(); + Assert.assertFalse(violations.isEmpty()); + } + + @Test + public void mustSuccessRule() throws IOException { + Path path = createFileWithContent("1|2|3|4|5"); + JFileReaderConfig readerConfig = JFileReaderFactory.newUtf8ReaderConfig("\\|"); + JFileReader reader = JFileReaderFactory.newJFileReader(path, readerConfig); + + JFileValidatorConfig validatorConfig = JFileValidatorFactory.newValidatorConfig(); + JFileValidator validator = JFileValidatorFactory.newJFileValidator(validatorConfig); + + validatorConfig.files().lines().columnsSize(5); + + List violations = validator.validate(reader.iterator().next()).getViolations(); + Assert.assertTrue(violations.isEmpty()); + } + +}