Giter Club home page Giter Club logo

javar's Introduction

JaVar

A library providing an expressive syntax for defining Java variable values from multiple potential inputs, some of which may be null. It offers a succinct replacement for the following difficult-to-read boilerplate patterns:

void init(@Nullable String prop) {
  this.prop = prop;
  if (this.prop == null) {
    this.prop = lookupPropFromDb(); // slow
  }
  if (this.prop == null) {
    this.prop = DEFAULT_PROP_VALUE;
  }
  if (this.prop == null) {
    throw new IllegalStateException();
  }
}

...or the "simpler" variant:

String str = firstVal != null ? firstVal : secondVal != null ? secondVal : null;

(the latter example is especially problematic when, for example, firstVal is actually myObj.getFirstVal(): one must either assign it to a variable beforehand, or trust that getFirstVal() is a fast method that will return the same value when called multiple times)

In the absence of first-class support for this pattern in the language, this class offers an awkward but relatively elegant solution:

void init(@Nullable String prop) {
  Var<String> var = Var.var();
  this.prop = var.def(
    var.as(prop)
    || var.as(lookupPropFromDb())
    || var.as(System.getenv("PROP")));
}

In the above example, lookupPropFromDb() is only called if prop is null. Also, if all three potential values are null, it will throw an IllegalStateException to indicate that the variable is undefined.

Var<String> var = Var.var();
String str = var.def(var.as(firstVal) || var.as(secondVal) || var.asNull());

The call to var.asNull() allows the variable to contain an explicitly null value, without triggering an IllegalStateException.

javar's People

Contributors

piercehanley avatar

Watchers

James Cloos avatar  avatar

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.