Skip to content

ThanosFisherman/Elvis

Repository files navigation

Elvis

Elvis Operator is an alternative to Java's Optional Operator that works for me.

License Download

implementation 'com.thanosfisherman.elvis:elvis:3.0'

Why Elvis?

Well I am a fan of Elvis (The king!) and Elvis operator ?: (Also known as null coalescing operator) which many languages like Groovy or kotlin make use. Sadly Java doesn’t support Elvis operator. It was actually proposed to be included in Java 7 but the idea was rejected. :( If you want to read the discussion about it for yourself check out this.

So The ?: binary "Elvis" operator results in the value of the left-hand-side if it is not null, avoiding evaluation of the right-hand-side. But If the left-hand-side is null, the right-hand-side is evaluated and this is the result returned instead.

So if you were to write the following in Java:

private String getStreetName() {
    return streetName != null ? streetName : "Unknown Street";
}

Using Elvis (the real operator) You could instead write:

private String getStreetName() {
	return streetName ?: "Unknown Street";
}

Just for the history, Elvis operator was named like that because ?: looks like an emoji with a pompadour haircut. And who else could that be if not Elvis Elvis

Usage

It's worth mentioning that this lib just as Optional, can also offer safe navigation in between chain method calls plus a few more features we are gonna get into in a while.

Using Elvis (This library) the above snippets could be replaced with:

private String getStreetName() {
	return Elvis.of(streetName).orElse("Unknown Street")
}

Elvis.of() is equivalent to Optional.ofNullable() because come on! This is the most frequently used method and should be short and succinct to use.

I mean the whole idea of Optional is to deal with nulls. It should have been pretty obvious that Optional.of() could also accept null values and not the other way around.

so I did a few renamings and here's the coresponding matches in Elvis vs Optional

Elvis.of() == Optional.ofNullable()

Elvis.ofNonNull() == Optional.of()

Elvis.next() == Optional.map()

I have also included some methods that deal with primitive values.

getBoolean() will return a primitive boolean

getInt() will return a primitive int

getLong() will return a primitive long

getDouble() will return a primitive double

Furthermore the Objects class from Java 8 along with some Functional Interfaces (Such as Consumer, Function etc) are also backported in Elvis lib for a possible use in Android which does not support those classes in low minSdkVersion numbers.

Examples

Find below some usage examples for a better understanding. (to be continued...)

private String getStreetName() {
	return Elvis.of(streetName).orElse("Unknown Street")
}

As mentioned earlier the above method will return the content of streetName if it is not null But if it is null, "Unknown Street" will be returned instead.

private String getStreetName() {
	Computer computer = new Computer()
	return Elvis.of(computer).next(Computer::getSoundcard).next(Soundcard::getUSB).next(USB::getVersion).orElse("UNKNOWN");
}

(to be continued...)