Git Product home page Git Product logo

jaffirm's Introduction

jaffirm

Maven Central Maven Central (snapshot) Codecov Java Version

com.io7m.jaffirm

JVM Platform Status
OpenJDK (Temurin) Current Linux Build (OpenJDK (Temurin) Current, Linux)
OpenJDK (Temurin) LTS Linux Build (OpenJDK (Temurin) LTS, Linux)
OpenJDK (Temurin) Current Windows Build (OpenJDK (Temurin) Current, Windows)
OpenJDK (Temurin) LTS Windows Build (OpenJDK (Temurin) LTS, Windows)

jaffirm

The jaffirm package provides simple and fast pre/postcondition and invariant checking functions.

Features

  • Static invocation, zero-allocation code paths for the common case of non-failing contracts.
  • Specialized and generic variants of all functions for use in low-latency software.
  • Detailed contract failure messages by construction.
  • Written in pure Java 17.
  • High coverage test suite.
  • OSGi-ready
  • JPMS-ready
  • ISC license.

Usage

Declare preconditions with the Preconditions class. Declare postconditions with the Postconditions class. Declare invariants with the Invariants class.

import static com.io7m.jaffirm.core.Contracts.conditionI;
import static com.io7m.jaffirm.core.Preconditions.checkPreconditionI;
import static com.io7m.jaffirm.core.Preconditions.checkPreconditionsI;

int exampleSingles(final int x)
{
  checkPreconditionI(x, x > 0,      i -> "Input " + i + " must be > 0");
  checkPreconditionI(x, x % 2 == 0, i -> "Input " + i + " must be even");
  return x * 2;
}

int exampleMultis(final int x)
{
  checkPreconditionsI(
    x,
    conditionI(i -> i > 0,      i -> "Input " + i + " must be > 0"),
    conditionI(i -> i % 2 == 0, i -> "Input " + i + " must be even"));
  return x * 2;
}

> exampleSingles(0)
Exception in thread "main" com.io7m.jaffirm.core.PreconditionViolationException: Precondition violation.
  Received: 0
  Violated conditions:
    [0]: Input 0 must be > 0

> exampleSingles(1)
Exception in thread "main" com.io7m.jaffirm.core.PreconditionViolationException: Precondition violation.
  Received: 1
  Violated conditions:
    [0]: Input 1 must be even

> exampleMultis(-1)
Exception in thread "main" com.io7m.jaffirm.core.PreconditionViolationException: Precondition violation.
  Received: -1
  Violated conditions:
    [0]: Input -1 must be > 0
    [1]: Input -1 must be even

jaffirm's People

Contributors

dependabot[bot] avatar io7m avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

gungorfeyz

jaffirm's Issues

Build failing due to JDK 11 Javadoc issue

Exit code: 1 - javadoc: error - The code being documented uses modules but the packages defined in https://docs.oracle.com/javase/8/docs/api/ are in the unnamed module.

Command line was: /home/travis/oraclejdk11/bin/javadoc @options @packages

Refer to the generated Javadoc files in '/home/travis/build/io7m/jaccord/com.io7m.jaccord.core/target/apidocs' dir.

Add pre/post/invariant checks for equality

Experience has shown that this package gets used to write a lot of code like the following:

            Invariants.checkInvariantL(
             target.capacity(),
              size == target.capacity(),
              x -> "Allocated target buffer must be the correct size");

Note that size cannot appear in the contract message without turning the message lambda into a capturing lambda, which entails an allocation. Some entry points should be added that allow passing in an expected value so that it can appear in any error messages without any extra cost.

Release 1.0.0

The API seems unlikely to change in a backwards incompatible way now.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.