Skip to content

Release 10.0 #12

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

Merged
merged 22 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c94a0f5
* Increase use of boxing because JMH reveals that unnecessary boxing …
cowwoc Jul 28, 2024
4d1b8a9
Fixed broken references
cowwoc Jul 29, 2024
491d230
* Renamed Validator.apply() to Validator.and().
cowwoc Aug 4, 2024
7db4000
* Renamed assumeThat() to that(). Users are expected to invoke "asser…
cowwoc Aug 7, 2024
502b9e3
* Renamed minimumInclusive, maximumInclusive to minimumIsInclusive, m…
cowwoc Aug 13, 2024
fa3663a
* Removed logical operators until the use-case is better understood.
cowwoc Aug 13, 2024
e96ecbe
Worked around GPG bug: https://issues.apache.org/jira/browse/MGPG-136
cowwoc Aug 14, 2024
f9f2b56
* Wrap context values in Optional in order to support null values.
cowwoc Aug 29, 2024
58f47f8
Updated version number to 10.0.0
cowwoc Aug 30, 2024
85e3488
Parameterize Jackson's Javadoc URL
cowwoc Aug 30, 2024
6f43a36
Register a StringMapper for JsonNode
cowwoc Aug 30, 2024
2d8ba0e
Added StringValidator.doesNotContainWhitespace()
cowwoc Aug 31, 2024
510daad
Skip the diff legend for context that does not include a diff.
cowwoc Sep 4, 2024
d95a21d
Seeing as null values almost always imply a validation failure, modif…
cowwoc Sep 7, 2024
a850deb
that() records exceptions instead of throwing them immediately.
cowwoc Sep 9, 2024
fc85601
Cosmetic changes
cowwoc Sep 9, 2024
d75cda7
Removed containsSameNullity() due to poor usability.
cowwoc Sep 16, 2024
b73464f
Added JsonNodeValidator.isBoolean()
cowwoc Sep 16, 2024
8a8a16b
Updated dependencies
cowwoc Sep 21, 2024
53d51a0
Upgraded to maven-javadoc-plugin 3.10.0
cowwoc Sep 24, 2024
8ba56fb
Upgraded to maven-gpg-plugin 3.2.7
cowwoc Sep 27, 2024
8e8b1d0
Updated dependencies
cowwoc Oct 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .github/workflows/cla.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
PERSONAL_ACCESS_TOKEN: ${{ secrets.CLA_ACCESS_TOKEN }}
with:
path-to-signatures: 'cla/version1/signatures/cla.json'
path-to-document: 'https://github.com/cowwoc/requirements.java/cla/version1/cla.md' # e.g. a CLA or a DCO document
path-to-document: 'https://github.com/cowwoc/requirements.java/blob/master/cla/version1/cla.md' # e.g. a CLA or a DCO document
# branch should not be protected
branch: 'main'
allowlist: cowwoc
Expand Down
4 changes: 2 additions & 2 deletions .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.6/apache-maven-3.9.6-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.3.2/maven-wrapper-3.3.2.jar
50 changes: 21 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# <img src="docs/checklist.svg" width=64 height=64 alt="checklist"> Requirements API

[![API](https://img.shields.io/badge/api_docs-5B45D5.svg)](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/)
[![API](https://img.shields.io/badge/api_docs-5B45D5.svg)](https://cowwoc.github.io/requirements.java/10.0/docs/api/)
[![Changelog](https://img.shields.io/badge/changelog-A345D5.svg)](docs/Changelog.md)
[![javascript, typescript](https://img.shields.io/badge/other%20languages-javascript,%20typescript-457FD5.svg)](../../../requirements.js)

Expand Down Expand Up @@ -32,9 +32,9 @@ To get started, add this Maven dependency:
import java.util.List;
import java.util.StringJoiner;

import static com.github.cowwoc.requirements.java.DefaultJavaValidators.assumeThat;
import static com.github.cowwoc.requirements.java.DefaultJavaValidators.checkIf;
import static com.github.cowwoc.requirements.java.DefaultJavaValidators.requireThat;
import static com.github.cowwoc.requirements10.java.DefaultJavaValidators.checkIf;
import static com.github.cowwoc.requirements10.java.DefaultJavaValidators.requireThat;
import static com.github.cowwoc.requirements10.java.DefaultJavaValidators.that;

public final class Cake
{
Expand All @@ -50,11 +50,11 @@ public final class Cake
public int eat()
{
++bitesTaken;
assert assumeThat(bitesTaken, "bitesTaken").isNotNegative().elseThrow();
assert that(bitesTaken, "bitesTaken").isNotNegative().elseThrow();

piecesLeft -= ThreadLocalRandom.current().nextInt(5);

assert assumeThat(piecesLeft, "piecesLeft").isNotNegative().elseThrow();
assert that(piecesLeft, "piecesLeft").isNotNegative().elseThrow();
return piecesLeft;
}

Expand All @@ -77,7 +77,7 @@ You'll get:

```
java.lang.IllegalArgumentException: "piecesLeft" must be positive.
Actual: -1000
actual: -1000
```

If you violate a **class invariant**:
Expand All @@ -92,7 +92,7 @@ You'll get:

```
java.lang.AssertionError: "bitesTaken" may not be negative.
Actual: -128
actual: -128
```

If you violate a **postcondition**:
Expand All @@ -107,7 +107,7 @@ You'll get:

```
java.lang.AssertionError: "piecesLeft" may not be negative.
Actual: -4
actual: -4
```

If you violate **multiple** conditions at once:
Expand All @@ -126,10 +126,10 @@ You'll get:

```
"bitesTaken" may not be negative.
Actual: -1
actual: -1

"piecesLeft" must be greater than 3.
Actual: 2
actual: 2
```

## Features
Expand All @@ -152,29 +152,21 @@ This library offers the following features:
Designed for discovery using your favorite IDE's auto-complete feature.
The main entry points are:

* [requireThat(value, name)](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements/java/DefaultJavaValidators.html#requireThat(T,java.lang.String)) for method preconditions.
* [assumeThat(value, name)](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements/java/DefaultJavaValidators.html#assumeThat(T,java.lang.String)) for class invariants, method postconditions and private methods.
* [checkIfThat(value, name)](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements/java/DefaultJavaValidators.html#checkIf(T,java.lang.String)) for multiple failures and customized error handling.
* [JavaValidators](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements/java/JavaValidators.html) for custom configurations.
* [requireThat(value, name)](https://cowwoc.github.io/requirements.java/10.0/docs/api/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/DefaultJavaValidators.html#requireThat(T,java.lang.String))
for method preconditions.
* [that(value, name)](https://cowwoc.github.io/requirements.java/10.0/docs/api/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/DefaultJavaValidators.html#that(T,java.lang.String))
for [class invariants, method postconditions and private methods](docs/Features.md#assertion-support).
* [checkIf(value, name)](https://cowwoc.github.io/requirements.java/10.0/docs/api/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/DefaultJavaValidators.html#checkIf(T,java.lang.String))
for multiple failures and customized error handling.

The first three methods use a shared configuration, while `JavaValidators` allows you to create an independent
configuration.
See the [API documentation](https://cowwoc.github.io/requirements.java/10.0/docs/api/) for more details.

* `requireThat()` and `assumeThat()` throw an exception on the first validation failure.
* `checkIf()` returns multiple validation failures at once. It is more flexible than the others, but its syntax
is more verbose.
## Best practices

Thrown exceptions may be configured using [ConfigurationUpdater.exceptionTransformer(Function)](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements/java/ConfigurationUpdater.html#exceptionTransformer(java.util.function.Function)).

See the [API documentation](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/) for more details.

## Tips

* Use `assert` with `assumeThat().elseThrow()` for sanity checks. When assertions are disabled, the checks will get removed.
* Use `checkIf().elseGetMessages()` to return failure messages without throwing an exception.
This is the fastest validation approach, ideal for web services.
* To enhance the clarity of failure messages, you should provide parameter names, even when they are optional.
In other words, favor `assumeThat(value, name)` to `assumeThat(value)`.
* To enhance the clarity of failure messages, you should provide parameter names, even when they are optional.
In other words, favor `assert that(value, name)` over `assert that(value)`.

## Third-party libraries and tools

Expand Down
9 changes: 7 additions & 2 deletions annotation/pom.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<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>
<parent>
<groupId>com.github.cowwoc.requirements</groupId>
<artifactId>root</artifactId>
<version>9.0.1-SNAPSHOT</version>
<version>10.0.0-SNAPSHOT</version>
</parent>
<artifactId>annotation</artifactId>
<packaging>jar</packaging>

<properties>
<rootBaseDir>${project.basedir}/..</rootBaseDir>
</properties>

<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.cowwoc.requirements.annotation;
package com.github.cowwoc.requirements10.annotation;

import java.lang.annotation.Target;

Expand Down
5 changes: 3 additions & 2 deletions annotation/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
/**
* Annotations used by the library.
*/
module com.github.cowwoc.requirements.annotation
@SuppressWarnings("JavaModuleNaming")
module com.github.cowwoc.requirements10.annotation
{
exports com.github.cowwoc.requirements.annotation;
exports com.github.cowwoc.requirements10.annotation;
}
10 changes: 7 additions & 3 deletions benchmark/assertj/pom.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<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>
<parent>
<groupId>com.github.cowwoc.requirements</groupId>
<artifactId>benchmark</artifactId>
<version>9.0.1-SNAPSHOT</version>
<version>10.0.0-SNAPSHOT</version>
</parent>
<artifactId>benchmark.assertj</artifactId>
<packaging>jar</packaging>

<properties>
<rootBaseDir>${project.basedir}/../..</rootBaseDir>
</properties>

<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.25.3</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2017 Gili Tzabari
* Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
*/
package com.github.cowwoc.requirements.benchmark.assertj;
package com.github.cowwoc.requirements10.benchmark.assertj;

import org.assertj.core.api.Assertions;
import org.assertj.core.api.SoftAssertions;
Expand All @@ -28,7 +28,7 @@
@SuppressWarnings({"CanBeFinal", "LongLine", "FieldCanBeLocal", "FieldMayBeFinal"})
public class AssertJTest
{
private static final boolean FAST_ESTIMATE = false;
private static final boolean FAST_ESTIMATE = Boolean.getBoolean("FAST_ESTIMATE");
// Fields may not be final:
// https://github.com/openjdk/jmh/blob/cb3c3a90137dad781a2a37fda72dc11ebf253593/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_10_ConstantFold.java#L58
private String name = "map";
Expand Down Expand Up @@ -69,8 +69,8 @@ public void assertThatIsSuccessful(Blackhole bh)
bh.consume(assertThat(value).as(name + ": %s").size().
as("""
%s must be greater than %s.
Actual: %s
Actual.size: %s""", name, 3, value, value.size()).isGreaterThan(3));
actual: %s
actual.size: %s""", name, 3, value, value.size()).isGreaterThan(3));
}

@Benchmark
Expand Down
14 changes: 11 additions & 3 deletions benchmark/assertj/src/test/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
module com.github.cowwoc.requirements.benchmark.assertj
/*
* Copyright 2019 Gili Tzabari.
* Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
*/
/**
* Benchmark of equivalent assertj functionality.
*/
@SuppressWarnings("JavaModuleNaming")
module com.github.cowwoc.requirements10.benchmark.assertj
{
requires jmh.core;
requires org.testng;
requires org.assertj.core;

exports com.github.cowwoc.requirements.benchmark.assertj to org.testng;
exports com.github.cowwoc.requirements.benchmark.assertj.jmh_generated to jmh.core;
exports com.github.cowwoc.requirements10.benchmark.assertj to org.testng;
exports com.github.cowwoc.requirements10.benchmark.assertj.jmh_generated to jmh.core;
}
4 changes: 2 additions & 2 deletions benchmark/assertj/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
<appender-ref ref="STDOUT"/>
</root>
<logger name="org.testng" level="warn"/>
<logger name="com.github.cowwoc.requirements.java.internal.terminal.Terminal" level="warn"/>
<logger name="com.github.cowwoc.requirements.java.internal.util.Exceptions" level="warn"/>
<logger name="com.github.cowwoc.requirements10.java.internal.terminal.Terminal" level="warn"/>
<logger name="com.github.cowwoc.requirements10.java.internal.util.Exceptions" level="warn"/>
</configuration>
Binary file modified benchmark/benchmark.xlsx
Binary file not shown.
9 changes: 7 additions & 2 deletions benchmark/guava/pom.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<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>
<parent>
<groupId>com.github.cowwoc.requirements</groupId>
<artifactId>benchmark</artifactId>
<version>9.0.1-SNAPSHOT</version>
<version>10.0.0-SNAPSHOT</version>
</parent>
<artifactId>benchmark.guava</artifactId>
<packaging>jar</packaging>

<properties>
<rootBaseDir>${project.basedir}/../..</rootBaseDir>
</properties>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* Copyright (c) 2017 Gili Tzabari
* Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
*/
package com.github.cowwoc.requirements.benchmark.guava;
package com.github.cowwoc.requirements10.benchmark.guava;

import com.github.cowwoc.requirements.guava.DefaultGuavaValidators;
import com.github.cowwoc.requirements10.guava.DefaultGuavaValidators;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import org.openjdk.jmh.annotations.Benchmark;
Expand All @@ -25,7 +25,7 @@
@SuppressWarnings({"FieldCanBeLocal", "CanBeFinal", "LongLine"})
public class GuavaTest
{
private static final boolean FAST_ESTIMATE = false;
private static final boolean FAST_ESTIMATE = Boolean.getBoolean("FAST_ESTIMATE");
// Fields may not be final:
// https://github.com/openjdk/jmh/blob/cb3c3a90137dad781a2a37fda72dc11ebf253593/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_10_ConstantFold.java#L58
private String name = "multimap";
Expand Down
16 changes: 12 additions & 4 deletions benchmark/guava/src/test/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
module com.github.cowwoc.requirements.benchmark.guava
/*
* Copyright 2019 Gili Tzabari.
* Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
*/
/**
* Benchmark of the guava module.
*/
@SuppressWarnings("JavaModuleNaming")
module com.github.cowwoc.requirements10.benchmark.guava
{
requires jmh.core;
requires com.google.common;
requires org.testng;
requires com.github.cowwoc.requirements.guava;
requires com.github.cowwoc.requirements10.guava;
requires static com.google.errorprone.annotations;

exports com.github.cowwoc.requirements.benchmark.guava to org.testng;
exports com.github.cowwoc.requirements.benchmark.guava.jmh_generated to jmh.core;
exports com.github.cowwoc.requirements10.benchmark.guava to org.testng;
exports com.github.cowwoc.requirements10.benchmark.guava.jmh_generated to jmh.core;
}
2 changes: 1 addition & 1 deletion benchmark/guava/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
<root level="debug">
<appender-ref ref="STDOUT"/>
</root>
<logger name="com.github.cowwoc.requirements.java.internal.terminal.Terminal" level="warn"/>
<logger name="com.github.cowwoc.requirements10.java.internal.terminal.Terminal" level="warn"/>
</configuration>
9 changes: 7 additions & 2 deletions benchmark/java/pom.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<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>
<parent>
<groupId>com.github.cowwoc.requirements</groupId>
<artifactId>benchmark</artifactId>
<version>9.0.1-SNAPSHOT</version>
<version>10.0.0-SNAPSHOT</version>
</parent>
<artifactId>benchmark.java</artifactId>
<packaging>jar</packaging>

<properties>
<rootBaseDir>${project.basedir}/../..</rootBaseDir>
</properties>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
Expand Down
Loading
Loading