Skip to content

Commit 44c4ea4

Browse files
committed
* Renamed assumeThat() to that(). Users are expected to invoke "assert that()"
* Removed all public references to the validator configuration and the ability to create custom validator factories. * Renamed Configuration.lazyExceptions to recordStacktrace. * Use normal StampedLock because we don't actually need a reentrant lock.
1 parent 491d230 commit 44c4ea4

File tree

193 files changed

+5222
-6115
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

193 files changed

+5222
-6115
lines changed

README.md

Lines changed: 44 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ To get started, add this Maven dependency:
2020
```xml
2121

2222
<dependency>
23-
<groupId>com.github.cowwoc.requirements</groupId>
24-
<artifactId>java</artifactId>
25-
<version>10.0</version>
23+
<groupId>com.github.cowwoc.requirements</groupId>
24+
<artifactId>java</artifactId>
25+
<version>9.0.0</version>
2626
</dependency>
2727
```
2828

@@ -32,38 +32,38 @@ To get started, add this Maven dependency:
3232
import java.util.List;
3333
import java.util.StringJoiner;
3434

35-
import static com.github.cowwoc.requirements10.java.DefaultJavaValidators.assumeThat;
3635
import static com.github.cowwoc.requirements10.java.DefaultJavaValidators.checkIf;
3736
import static com.github.cowwoc.requirements10.java.DefaultJavaValidators.requireThat;
37+
import static com.github.cowwoc.requirements10.java.DefaultJavaValidators.that;
3838

3939
public final class Cake
4040
{
41-
private byte bitesTaken = 0;
42-
private int piecesLeft;
43-
44-
public Cake(int piecesLeft)
45-
{
46-
requireThat(piecesLeft, "piecesLeft").isPositive();
47-
this.piecesLeft = piecesLeft;
48-
}
49-
50-
public int eat()
51-
{
52-
++bitesTaken;
53-
assert assumeThat(bitesTaken, "bitesTaken").isNotNegative().elseThrow();
54-
55-
piecesLeft -= ThreadLocalRandom.current().nextInt(5);
56-
57-
assert assumeThat(piecesLeft, "piecesLeft").isNotNegative().elseThrow();
58-
return piecesLeft;
59-
}
60-
61-
public List<String> getFailures()
62-
{
63-
return checkIf(bitesTaken, "bitesTaken").isNotNegative().
64-
and(checkIf(piecesLeft, "piecesLeft").isGreaterThan(3)).
65-
elseGetMessages();
66-
}
41+
private byte bitesTaken = 0;
42+
private int piecesLeft;
43+
44+
public Cake(int piecesLeft)
45+
{
46+
requireThat(piecesLeft, "piecesLeft").isPositive();
47+
this.piecesLeft = piecesLeft;
48+
}
49+
50+
public int eat()
51+
{
52+
++bitesTaken;
53+
assert that(bitesTaken, "bitesTaken").isNotNegative();
54+
55+
piecesLeft -= ThreadLocalRandom.current().nextInt(5);
56+
57+
assert that(piecesLeft, "piecesLeft").isNotNegative();
58+
return piecesLeft;
59+
}
60+
61+
public List<String> getFailures()
62+
{
63+
return checkIf(bitesTaken, "bitesTaken").isNotNegative().
64+
and(checkIf(piecesLeft, "piecesLeft").isGreaterThan(3)).
65+
elseGetMessages();
66+
}
6767
}
6868
```
6969

@@ -84,10 +84,8 @@ If you violate a **class invariant**:
8484

8585
```java
8686
Cake cake = new Cake(1_000_000);
87-
while(true)
88-
cake.
89-
90-
eat();
87+
while (true)
88+
cake.eat();
9189
```
9290

9391
You'll get:
@@ -101,10 +99,8 @@ If you violate a **postcondition**:
10199

102100
```java
103101
Cake cake = new Cake(100);
104-
while(true)
105-
cake.
106-
107-
eat();
102+
while (true)
103+
cake.eat();
108104
```
109105

110106
You'll get:
@@ -118,19 +114,12 @@ If you violate **multiple** conditions at once:
118114

119115
```java
120116
Cake cake = new Cake(1);
121-
cake.bitesTaken =-1;
122-
cake.piecesLeft =2;
117+
cake.bitesTaken = -1;
118+
cake.piecesLeft = 2;
123119
StringJoiner failures = new StringJoiner("\n\n");
124-
for(
125-
String failure :cake.
126-
127-
getFailures())
128-
failures.
129-
130-
add(failure);
131-
System.out.
132-
133-
println(failures);
120+
for (String failure : cake.getFailures())
121+
failures.add(failure);
122+
System.out.println(failures);
134123
```
135124

136125
You'll get:
@@ -155,8 +144,6 @@ This library offers the following features:
155144
* [Multiple validation failures](docs/Features.md#multiple-validation-failures) that report all the errors at
156145
once
157146
* [Nested validations](docs/Features.md#nested-validations) that allow you to validate complex objects
158-
* [Logical operators](docs/Features.md#logical-operators) that allow you to combine the validation of
159-
unrelated values
160147
* [String diff](docs/Features.md#string-diff) that shows the differences between two strings
161148
* [Performant and robust](docs/Performance.md)
162149

@@ -167,34 +154,19 @@ The main entry points are:
167154

168155
* [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))
169156
for method preconditions.
170-
* [assumeThat(value, name)](https://cowwoc.github.io/requirements.java/10.0/docs/api/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/DefaultJavaValidators.html#assumeThat(T,java.lang.String))
171-
for class invariants, method postconditions and private methods.
172-
* [checkIfThat(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))
157+
* [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))
158+
for [class invariants, method postconditions and private methods](docs/Features.md#assertion-support).
159+
* [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))
173160
for multiple failures and customized error handling.
174-
* [JavaValidators](https://cowwoc.github.io/requirements.java/10.0/docs/api/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/JavaValidators.html)
175-
for custom configurations.
176-
177-
The first three methods use a shared configuration, while `JavaValidators` allows you to create an independent
178-
configuration.
179-
180-
* `requireThat()` and `assumeThat()` throw an exception on the first validation failure.
181-
* `checkIf()` returns multiple validation failures at once. It is more flexible than the others, but its
182-
syntax
183-
is more verbose.
184-
185-
Thrown exceptions may be configured
186-
using [ConfigurationUpdater.exceptionTransformer(Function)](https://cowwoc.github.io/requirements.java/10.0/docs/api/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/ConfigurationUpdater.html#exceptionTransformer(java.util.function.Function)).
187161

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

190-
## Tips
164+
## Best practices
191165

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

199171
## Third-party libraries and tools
200172

benchmark/assertj/src/test/java/com/github/cowwoc/requirements10/benchmark/assertj/AssertJTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
@SuppressWarnings({"CanBeFinal", "LongLine", "FieldCanBeLocal", "FieldMayBeFinal"})
2929
public class AssertJTest
3030
{
31-
private static final boolean FAST_ESTIMATE = false;
31+
private static final boolean FAST_ESTIMATE = Boolean.getBoolean("FAST_ESTIMATE");
3232
// Fields may not be final:
3333
// https://github.com/openjdk/jmh/blob/cb3c3a90137dad781a2a37fda72dc11ebf253593/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_10_ConstantFold.java#L58
3434
private String name = "map";

benchmark/benchmark.xlsx

25 Bytes
Binary file not shown.

benchmark/guava/src/test/java/com/github/cowwoc/requirements10/benchmark/guava/GuavaTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
@SuppressWarnings({"FieldCanBeLocal", "CanBeFinal", "LongLine"})
2626
public class GuavaTest
2727
{
28-
private static final boolean FAST_ESTIMATE = false;
28+
private static final boolean FAST_ESTIMATE = Boolean.getBoolean("FAST_ESTIMATE");
2929
// Fields may not be final:
3030
// https://github.com/openjdk/jmh/blob/cb3c3a90137dad781a2a37fda72dc11ebf253593/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_10_ConstantFold.java#L58
3131
private String name = "multimap";

benchmark/java/src/test/java/com/github/cowwoc/requirements10/benchmark/java/ExceptionTest.java

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*/
55
package com.github.cowwoc.requirements10.benchmark.java;
66

7-
import com.github.cowwoc.requirements10.java.ConfigurationUpdater;
8-
import com.github.cowwoc.requirements10.java.JavaValidators;
7+
import com.github.cowwoc.requirements10.java.internal.ConfigurationUpdater;
8+
import com.github.cowwoc.requirements10.java.internal.JavaValidators;
99
import org.openjdk.jmh.annotations.Benchmark;
1010
import org.openjdk.jmh.annotations.Mode;
1111
import org.openjdk.jmh.annotations.Scope;
@@ -26,23 +26,23 @@
2626
@SuppressWarnings({"CanBeFinal", "LongLine", "FieldCanBeLocal", "FieldMayBeFinal"})
2727
public class ExceptionTest
2828
{
29-
private static final boolean FAST_ESTIMATE = false;
29+
private static final boolean FAST_ESTIMATE = Boolean.getBoolean("FAST_ESTIMATE");
3030
// Fields may not be final:
3131
// https://github.com/openjdk/jmh/blob/cb3c3a90137dad781a2a37fda72dc11ebf253593/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_10_ConstantFold.java#L58
3232
private String name = "map";
3333
private Map<Integer, Integer> value;
3434
private JavaValidators validators = JavaValidators.newInstance();
3535
private JavaValidators withoutCleanStackTrace = JavaValidators.newInstance();
36-
private JavaValidators withoutLazy = JavaValidators.newInstance();
36+
private JavaValidators withoutRecordStacktrace = JavaValidators.newInstance();
3737

3838
public ExceptionTest()
3939
{
4040
value = HashMap.newHashMap(5);
4141
for (int i = 0; i < 5; ++i)
4242
value.put(i, 5 - i);
43-
try (ConfigurationUpdater config = withoutLazy.updateConfiguration())
43+
try (ConfigurationUpdater config = withoutRecordStacktrace.updateConfiguration())
4444
{
45-
config.lazyExceptions(false);
45+
config.recordStacktrace(false);
4646
}
4747
try (ConfigurationUpdater config = withoutCleanStackTrace.updateConfiguration())
4848
{
@@ -78,57 +78,57 @@ public void requireThatIsSuccessful(Blackhole bh)
7878
bh.consume(validators.requireThat(value, name).size().isGreaterThan(3));
7979
}
8080

81-
@Benchmark
82-
public void throwExceptionCatchException(Blackhole bh)
83-
{
84-
try
85-
{
86-
throw new IllegalArgumentException("Hard-coded exception");
87-
}
88-
catch (IllegalArgumentException e)
89-
{
90-
// The stack trace is not populated unless we explicitly invoke getStackTrace()
91-
bh.consume(e.getStackTrace());
92-
}
93-
}
94-
95-
@Benchmark
96-
public void requireThatWithCleanStackTrace(Blackhole bh)
97-
{
98-
try
99-
{
100-
validators.requireThat(value, name).size().isLessThan(3);
101-
}
102-
catch (IllegalArgumentException e)
103-
{
104-
// The stack trace is not populated unless we explicitly invoke getStackTrace()
105-
bh.consume(e.getStackTrace());
106-
}
107-
}
108-
109-
@Benchmark
110-
public void requireThatWithoutCleanStackTrace(Blackhole bh)
111-
{
112-
try
113-
{
114-
withoutCleanStackTrace.requireThat(value, name).size().isLessThan(3);
115-
}
116-
catch (IllegalArgumentException e)
117-
{
118-
// The stack trace is not populated unless we explicitly invoke getStackTrace()
119-
bh.consume(e.getStackTrace());
120-
}
121-
}
122-
123-
@Benchmark
124-
public void checkIfAndGetMessagesWithLazyExceptions(Blackhole bh)
125-
{
126-
bh.consume(validators.checkIf(value, name).isNull().elseGetMessages());
127-
}
128-
129-
@Benchmark
130-
public void checkIfAndGetMessagesWithoutLazyExceptions(Blackhole bh)
131-
{
132-
bh.consume(withoutLazy.checkIf(value, name).isNull().elseGetMessages());
133-
}
81+
// @Benchmark
82+
// public void throwExceptionCatchException(Blackhole bh)
83+
// {
84+
// try
85+
// {
86+
// throw new IllegalArgumentException("Hard-coded exception");
87+
// }
88+
// catch (IllegalArgumentException e)
89+
// {
90+
// // The stack trace is not populated unless we explicitly invoke getStackTrace()
91+
// bh.consume(e.getStackTrace());
92+
// }
93+
// }
94+
//
95+
// @Benchmark
96+
// public void requireThatWithCleanStackTrace(Blackhole bh)
97+
// {
98+
// try
99+
// {
100+
// validators.requireThat(value, name).size().isLessThan(3);
101+
// }
102+
// catch (IllegalArgumentException e)
103+
// {
104+
// // The stack trace is not populated unless we explicitly invoke getStackTrace()
105+
// bh.consume(e.getStackTrace());
106+
// }
107+
// }
108+
//
109+
// @Benchmark
110+
// public void requireThatWithoutCleanStackTrace(Blackhole bh)
111+
// {
112+
// try
113+
// {
114+
// withoutCleanStackTrace.requireThat(value, name).size().isLessThan(3);
115+
// }
116+
// catch (IllegalArgumentException e)
117+
// {
118+
// // The stack trace is not populated unless we explicitly invoke getStackTrace()
119+
// bh.consume(e.getStackTrace());
120+
// }
121+
// }
122+
//
123+
// @Benchmark
124+
// public void checkIfAndGetMessagesWithRecordStacktrace(Blackhole bh)
125+
// {
126+
// bh.consume(validators.checkIf(value, name).isNull().elseGetMessages());
127+
// }
128+
//
129+
// @Benchmark
130+
// public void checkIfAndGetMessagesWithoutRecordStacktrace(Blackhole bh)
131+
// {
132+
// bh.consume(withoutRecordStacktrace.checkIf(value, name).isNull().elseGetMessages());
133+
// }
134134
}

0 commit comments

Comments
 (0)