https://github.com/tlrasor/conditions
simple design by contract like DSL for the JVM written in groovy
https://github.com/tlrasor/conditions
Last synced: 4 months ago
JSON representation
simple design by contract like DSL for the JVM written in groovy
- Host: GitHub
- URL: https://github.com/tlrasor/conditions
- Owner: tlrasor
- License: mit
- Created: 2012-06-10T03:58:58.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2016-01-27T22:28:42.000Z (over 9 years ago)
- Last Synced: 2025-01-02T23:47:16.969Z (5 months ago)
- Language: Groovy
- Homepage:
- Size: 104 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
conditions
==========This library implements a simple DSL to help support [Design by Contract], similar to [guava's Preconditions class] or [Microsoft Research's Code Contracts] library written in [groovy] for the JVM.
### Status
[](https://travis-ci.org/tlrasor/conditions)## Usage
Conditions is a fluent API for doing design by contract like conditions. Unlike many Java ecosystem DbC frameworks, conditions is not based on static methods and assert statements and but is instead fully object oriented and uses regular Java exceptions to signal errors. This aspect allows it to better fit into a 'traditional' Java paradigm and makes the system very easy to reason about. Many frameworks are somewhat complicated, using annotations and clunky contexts, conditions is designed to be as simple and plain as possible.
Several different types of conditions are supported:
- Object
- Boolean
- Integer
- String
- CollectionIt is trivial to add support for more classes by extending the ObjectCondition class.
Here are several examples of using the conditions DSL:
```groovy
// Simple boolean conditions are easy to construct
Condition.that((1+1)==2).isTrue()// use try catches to recover from failures
def getConnection(Session s) {
try {
Condition.that(s.isOpen()).isTrue()
} catch (ConditionViolationException e) {
reopenSession(s)
return getConnection(s)
}
}```
For more details, the unit tests section is fairly complete with examples of use for each of the supported condition types.
contracts uses Travis CI for [builds].
Copyright 2016 Travis Rasor [MIT License], [tldr].
[groovy]: https://github.com/apache/groovy
[Design by Contract]: http://en.wikipedia.org/wiki/Design_by_contract
[guava's Preconditions class]: https://code.google.com/p/guava-libraries/wiki/PreconditionsExplained
[Microsoft Research's Code Contracts]: http://research.microsoft.com/en-us/projects/contracts/
[builds]: https://travis-ci.org/tlrasor/conditions
[MIT License]: https://opensource.org/licenses/MIT
[tldr]: https://tldrlegal.com/license/mit-license