Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge #80

Merged
merged 12 commits into from
Apr 12, 2021
18 changes: 12 additions & 6 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -39,10 +45,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:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion documentation/1-quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ You can pull it from the central Maven repositories:
<dependency>
<groupId>com.github.mvallim</groupId>
<artifactId>java-fluent-validator</artifactId>
<version>1.9.2</version>
<version>1.9.3</version>
</dependency>
```

Expand Down
11 changes: 6 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.github.mvallim</groupId>
<artifactId>java-fluent-validator</artifactId>
<version>1.9.3-SNAPSHOT</version>
<version>1.9.4-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
Expand Down Expand Up @@ -72,9 +73,9 @@
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<groupId>com.nickwongdev</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<version>1.12.6</version>
<configuration>
<Xlint>adviceDidNotMatch=ignore</Xlint>
<complianceLevel>${java.version}</complianceLevel>
Expand Down Expand Up @@ -107,7 +108,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<version>0.8.6</version>
<configuration>
<excludes>
<exclude>**/test/**</exclude>
Expand Down Expand Up @@ -366,4 +367,4 @@
</build>
</profile>
</profiles>
</project>
</project>
50 changes: 37 additions & 13 deletions src/main/java/br/com/fluentvalidator/AbstractValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,26 +23,49 @@ public abstract class AbstractValidator<T> implements Validator<T> {

private final List<Rule<T>> rules = new LinkedList<>();

private final Runnable initialize;
private final Initializer<T> 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<T> {

private final AtomicReference<Boolean> atomicReference = new AtomicReference<>(Boolean.FALSE);

private final Validator<T> validator;

Initializer(final Validator<T> validator) {
this.validator = validator;
}

/**
* This method cause Race Condition. We are using Compare And Swap (CAS)
* <p>
* {@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<>(this);
}

/**
Expand Down Expand Up @@ -114,7 +138,7 @@ public <E> List<E> validate(final Collection<T> 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);
}
Expand Down