From ded36208fd01008f0e6dc72c3ee8ea48f0aa36d5 Mon Sep 17 00:00:00 2001 From: Marcos Tischer Vallim Date: Fri, 26 Feb 2021 19:29:22 -0300 Subject: [PATCH 01/10] Update AbstractValidator.java --- .../fluentvalidator/AbstractValidator.java | 49 ++++++++++++++----- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/src/main/java/br/com/fluentvalidator/AbstractValidator.java b/src/main/java/br/com/fluentvalidator/AbstractValidator.java index 38e7b8e..5609209 100644 --- a/src/main/java/br/com/fluentvalidator/AbstractValidator.java +++ b/src/main/java/br/com/fluentvalidator/AbstractValidator.java @@ -22,26 +22,49 @@ public abstract class AbstractValidator implements Validator { private final List> rules = new LinkedList<>(); - private final Runnable initialize; + private final Initializer initialize; private String property; private RuleProcessorStrategy ruleProcessor = RuleProcessorStrategy.getDefault(); - protected AbstractValidator() { - this.initialize = new Runnable() { - - private boolean initialized; - - @Override - public synchronized void run() { - if (!initialized) { - rules(); - this.initialized = true; + private static class Initializer { + + private final AtomicReference atomicReference = new AtomicReference<>(Boolean.FALSE); + + private final Validator validator; + + Initializer(final Validator validator) { + this.validator = validator; + } + + /** + * This method cause Race Condition. We are using Compare And Swap (CAS) + *

+ * {@link https://en.wikipedia.org/wiki/Race_condition} + * {@link https://en.wikipedia.org/wiki/Compare-and-swap} + */ + public void init() { + if (isNotInitialized()) { + synchronized (atomicReference) { + if (isNotInitialized()) { // double check if was initialized + validator.rules(); + final Boolean oldValue = atomicReference.get(); + final Boolean newValue = Boolean.TRUE; + atomicReference.compareAndSet(oldValue, newValue); + } } } + } - }; + private boolean isNotInitialized() { + return Boolean.FALSE.equals(atomicReference.get()); + } + + } + + protected AbstractValidator() { + this.initialize = new Initializer(); } /** @@ -114,7 +137,7 @@ public List validate(final Collection instances, final ValidationResul */ @Override public boolean apply(final T instance) { - this.initialize.run(); + this.initialize.init(); ValidationContext.get().setProperty(this.property, instance); return ruleProcessor.process(instance, instance, rules); } From 01ae12a0df7c7c66359b157382d38c585a074572 Mon Sep 17 00:00:00 2001 From: Marcos Tischer Vallim Date: Fri, 26 Feb 2021 19:36:00 -0300 Subject: [PATCH 02/10] Fix concurrency on method run --- documentation/1-quick-start.md | 2 +- .../java/br/com/fluentvalidator/AbstractValidator.java | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/documentation/1-quick-start.md b/documentation/1-quick-start.md index 066ed73..a17c3fd 100644 --- a/documentation/1-quick-start.md +++ b/documentation/1-quick-start.md @@ -12,7 +12,7 @@ You can pull it from the central Maven repositories: com.github.mvallim java-fluent-validator - 1.9.2 + 1.9.3 ``` diff --git a/src/main/java/br/com/fluentvalidator/AbstractValidator.java b/src/main/java/br/com/fluentvalidator/AbstractValidator.java index 5609209..e6d5afc 100644 --- a/src/main/java/br/com/fluentvalidator/AbstractValidator.java +++ b/src/main/java/br/com/fluentvalidator/AbstractValidator.java @@ -4,6 +4,7 @@ import java.util.Collections; import java.util.LinkedList; import java.util.List; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.Function; import java.util.stream.Collectors; @@ -41,7 +42,7 @@ private static class Initializer { /** * This method cause Race Condition. We are using Compare And Swap (CAS) *

- * {@link https://en.wikipedia.org/wiki/Race_condition} + * {@link https://en.wikipedia.org/wiki/Race_condition} * {@link https://en.wikipedia.org/wiki/Compare-and-swap} */ public void init() { @@ -62,9 +63,9 @@ private boolean isNotInitialized() { } } - + protected AbstractValidator() { - this.initialize = new Initializer(); + this.initialize = new Initializer<>(this); } /** From d2c47977ec96390a3867d697ce81957a8f6de0b4 Mon Sep 17 00:00:00 2001 From: Marcos Tischer Vallim Date: Fri, 26 Feb 2021 19:37:02 -0300 Subject: [PATCH 03/10] [maven-release-plugin] prepare release java-fluent-validator-1.9.3 --- pom.xml | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index c886ad2..1b562be 100644 --- a/pom.xml +++ b/pom.xml @@ -1,11 +1,9 @@ - + 4.0.0 com.github.mvallim java-fluent-validator - 1.9.3-SNAPSHOT + 1.9.3 jar @@ -19,7 +17,7 @@ scm:git:git@github.com:mvallim/java-fluent-validator scm:git:git@github.com:mvallim/java-fluent-validator scm:git:git@github.com:mvallim/java-fluent-validator - HEAD + java-fluent-validator-1.9.3 java-fluent-validator From 77fe22c43864b76ca723797c69599b3911354f6a Mon Sep 17 00:00:00 2001 From: Marcos Tischer Vallim Date: Fri, 26 Feb 2021 19:37:02 -0300 Subject: [PATCH 04/10] [maven-release-plugin] prepare for next development iteration --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 1b562be..9b1706e 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ com.github.mvallim java-fluent-validator - 1.9.3 + 1.9.4-SNAPSHOT jar @@ -17,7 +17,7 @@ scm:git:git@github.com:mvallim/java-fluent-validator scm:git:git@github.com:mvallim/java-fluent-validator scm:git:git@github.com:mvallim/java-fluent-validator - java-fluent-validator-1.9.3 + HEAD java-fluent-validator From 07ee5a4badac4e0934790aa9627dac5888de8c2c Mon Sep 17 00:00:00 2001 From: Marcos Tischer Vallim Date: Fri, 26 Feb 2021 19:49:45 -0300 Subject: [PATCH 05/10] Update maven.yml --- .github/workflows/maven.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 49fd298..039a1b0 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -39,10 +39,10 @@ jobs: needs: [build, test] steps: - uses: actions/checkout@v2 - - name: Set up JDK 1.8 + - name: Set up JDK 11 uses: actions/setup-java@v1 with: - java-version: 1.8 + java-version: 11 - name: SonarCloud Scan run: mvn clean test jacoco:report org.jacoco:jacoco-maven-plugin:prepare-agent sonar:sonar -Dsonar.projectKey=java-fluent-validator -Dsonar.organization=mvallim -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=$SONAR_TOKEN env: From 1db2b71f2c3d75cf7ec89d23df263359aa5eb96e Mon Sep 17 00:00:00 2001 From: Marcos Tischer Vallim Date: Fri, 26 Feb 2021 20:27:45 -0300 Subject: [PATCH 06/10] Update aspectj-maven-plugin --- pom.xml | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 9b1706e..3b42810 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,7 @@ - + + 4.0.0 com.github.mvallim @@ -7,7 +10,7 @@ jar - 1.8 + 11 UTF-8 1.9.6 **/test/**,**/ValidationExceptionAdvice** @@ -70,9 +73,9 @@ - org.codehaus.mojo + com.nickwongdev aspectj-maven-plugin - 1.11 + 1.12.6 adviceDidNotMatch=ignore ${java.version} From 681ed520f97f862f6d51cadd0340217e05fb8a07 Mon Sep 17 00:00:00 2001 From: Marcos Tischer Vallim Date: Fri, 26 Feb 2021 20:33:19 -0300 Subject: [PATCH 07/10] Update fava version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 3b42810..65aa76f 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ jar - 11 + 1.8 UTF-8 1.9.6 **/test/**,**/ValidationExceptionAdvice** From d9d9c37cda2631c665020a035cf2d0f6efd78ced Mon Sep 17 00:00:00 2001 From: Marcos Tischer Vallim Date: Fri, 26 Mar 2021 00:25:50 -0300 Subject: [PATCH 08/10] Update maven.yml --- .github/workflows/maven.yml | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 039a1b0..061e835 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -13,24 +13,30 @@ jobs: build: runs-on: ubuntu-latest + strategy: + matrix: + java-version: [ 8, 11, 15 ] steps: - uses: actions/checkout@v2 - - name: Set up JDK 1.8 + - name: Set up JDK uses: actions/setup-java@v1 with: - java-version: 1.8 + java-version: ${{ matrix.java-version }} - name: Compile with Maven run: mvn -T 1C clean generate-sources compile --file pom.xml test: runs-on: ubuntu-latest + strategy: + matrix: + java-version: [ 8, 11, 15 ] needs: [build] steps: - uses: actions/checkout@v2 - - name: Set up JDK 1.8 + - name: Set up JDK uses: actions/setup-java@v1 with: - java-version: 1.8 + java-version: ${{ matrix.java-version }} - name: Test with Maven run: mvn -T 1C test-compile test --file pom.xml From ef839b3fe2da57a1b9e783a84790b44d9669c839 Mon Sep 17 00:00:00 2001 From: Marcos Tischer Vallim Date: Fri, 26 Mar 2021 00:26:26 -0300 Subject: [PATCH 09/10] Update pom.xml --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 65aa76f..f924fa8 100644 --- a/pom.xml +++ b/pom.xml @@ -108,7 +108,7 @@ org.jacoco jacoco-maven-plugin - 0.8.5 + 0.8.6 **/test/** @@ -367,4 +367,4 @@ - \ No newline at end of file + From a3f2d8bec28cee5353381973e6cac4240d2deb2a Mon Sep 17 00:00:00 2001 From: Marcos Tischer Vallim Date: Fri, 26 Mar 2021 00:29:14 -0300 Subject: [PATCH 10/10] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 3c01b83..bc3e734 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ Validating data is a common task that occurs throughout any application, especially the business logic layer. As for some quite complex scenarios, often the same or similar validations are scattered everywhere, thus it is hard to reuse code and break the [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) rule. +_**Compatible JDK 8, 11 and 15**_ + This library supports **`Kotlin`** aswell ## Sample