Android-IFx

Additional

Language
Java
Version
0.2.0 (Nov 7, 2015)
Created
Oct 28, 2015
Updated
Nov 28, 2015 (Retired)
Owner
petitviolet
Contributor
petitviolet
1
Activity
Badge
Generate
Download
Source code
APK file

Show card

Android-IFx

This library is deprecated. I recommend use petitviolet/Android-scaex instead.

This library for Android provides if-expression like Scala. Strongly inspired by jshosomichi/ifx.IFx enabled Java if-statement to return value.


How to Use

Set up

dependencies {
    compile 'net.petitviolet.android:ifx:latest-version'
}

Interface

/** returns primitive value pattern **/
// if ~ else if ~ else
String result = IFx.<String>of(false).then("hoge")
        .ElseIf(false).then("foo")
        .Else("bar");
assert result == "bar";

// if ~ else if ~
String result2 = IFx.<String>of(true).then("hoge")
        .ElseIf(false).then("foo")
        .get();
assert result2 == "hoge";

// not matched 
String result3 = IFx.<String>of(false).then("hoge").get()
assert result3 == null;


/** returns value the result of given method invoked **/
String result4 = IFx.<String>of(false).then(new Action<String>() {
    @Override
    public String run() {
        return "hoge";
    }
}).ElseIf(true).then(new Action<String>() {
    @Override
    public String run() {
        return "foo";
    }
}).Else(new Action<String>() {
    @Override
    public String run() {
        Log.d(TAG, "in else condition!");
        return "bar";
    }
});

assert result4 == "foo";

// with lambda expression
String result5 = IFx.<String>of(false).then(() -> "hoge")
    .ElseIf(true).then(() -> "foo")
    .Else(() -> {
        Log.d(TAG, "in else condition!");
        return "bar";
    });

assert result5 == "foo";

Lisence

This code is licensed under the Apache Software License 2.0.