Skip to content
/ kbool Public

a simple kotlin library providing a "transparent" boolean algebra.

License

Notifications You must be signed in to change notification settings

xeroli/kbool

Repository files navigation

KBool

Build Status Apache License Codacy Badge BCH compliance

Summary

KBool is a simple kotlin library providing a transparent boolean algebra.

The reason for the development of this library was an application that was too complex to understand at any time why some actions were not executable at a time.

So the question was:

Wouldn't it be nice if you just implement the conditions for disabling the buttons as usual and something would automatically return the resulting boolean value including the responsible subconditions. A tooltip for disabled buttons would then explain the reason for the missing functionality.

Basically, one is always looking for the answer to the question: Which part of a condition is currently not fulfilled?

This is what KBool tries to reach.

Code example

val sunIsShining = true.asBool("sun is shinig?")
val isRaining = false.asBool().named("is raining?")
val haveUmbrella = true.asBool("have umbrella?")

val walkingInTheWood = sunIsShining and (!isRaining or haveUmbrella)

println(walkingInTheWood.isTrue())   // -> true, but why?
println(walkingInTheWood.getCause()) // -> 'sun is shining?' - 'true, is raining' - false
                                     //    so an umbrella doesn't change a thing today ;-)

To enable a short-circuit evaluation, calculated Booleans must be specified as supplying lambdas:

var a: String? = null
var notNull = Bool.of{a != null}.named("String is not null")
var longEnough = Bool.of{ (a!!.length > 7) }.named("String has at least 7 characters")
                                             // not using a lambda would result in a NPE

println((notNull and longEnough).booleanValue()) // -> false because of a is null

a = "Hallo Welt!"
println((notNull and longEnough).getCause()) // 'String is not null' - true, 'String has at least 7 characters' - true

And even if you are not using the cause in your application, the debugger shows it. 😊

Screenshot from Debugger

For a simple tuorial see the wiki.

Latest Stable Release

Download

Download

KBool is available on jcenter().

Maven

...
<repositories>
  <repository>
    <id>jcenter</id>
    <url>https://jcenter.bintray.com/</url>
  </repository>
</repositories>

<dependencies>
  <dependency>
    <groupId>de.xeroli.kbool</groupId>
    <artifactId>kbool</artifactId>
    <version>0.5.1</version>
  </dependency>
</dependencies>
...

Gradle

repositories {
  jcenter()
}

dependencies {
  compile('de.xeroli.kbool:kbool:0.5.1')
}

Snapshot

Build Status

You can access the latest snapshot by adding "-SNAPSHOT" to the version number and adding the repository https://oss.jfrog.org/artifactory/oss-snapshot-local to your build.

You can also reference a specific snapshot like 0.2.0-20200125.081709-1. Here's the list of snapshot versions.

The sourcecode of snapshots are in the branch named 'snapshot'.

Maven (Snapshot)

...
<repositories>
  <repository>
    <id>oss-snapshot-local</id>
    <url>https://oss.jfrog.org/webapp/#/artifacts/browse/tree/General/oss-snapshot-local/de/xeroli/kbool/kbool</url>
  </repository>
</repositories>

<dependencies>
  <dependency>
    <groupId>de.xeroli.kbool</groupId>
    <artifactId>kbool</artifactId>
    <version>0.6.0-SNAPSHOT</version>
  </dependency>
</dependencies>
...

Gradle (Snapshot)

repositories {
  maven { url 'https://oss.jfrog.org/artifactory/oss-snapshot-local' }
}

dependencies {
  compile('de.xeroli.kbool:kbool:0.6.0-SNAPSHOT')
}

About

a simple kotlin library providing a "transparent" boolean algebra.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages