MCPcopy Create free account
hub / github.com/tomaszpolanski/Options

github.com/tomaszpolanski/Options @1.3.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 1.3.0 ↗ · + Follow

Option type for Android Java projects

201 symbols 527 edges 22 files 48 documented · 24% 1 cross-repo links ● updated 9y agolatest release 1.3.0 · 2017-01-15★ 87

Browse by type

⨍ Functions 178◇ Types & classes 23
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Options

Build Status codecov.io Method count

Functional Option that can be used with Java 1.6 and Android.

Option

Similar to Java 8 Optional but targeting Android and Java 6.

Why to use them?

In 1965, Sir Tony Hoare introduced null reference. Since then he apologised for that and called it The Billion Dollar Mistake. If the inventor of null pointers and references has condemned them, why the rest of us should be using them?

Where to use them?

Currently there are several ways to say that an object could be null. Most popular way in Android nowadays is using Nullable or NonNull annotations. The problem with those annotations is that you cannot say what kind of objects are in an array, or if you use Rx, in an Observable. With Options you can use instead List<Option<String>> or in Rx Observable<Option<Integer>>.

How to use them?

If you have been using RxJava, this API will look really similar to RxJava. Still Option is synchronous API that does not have too much to do with Reactive Programming.

Basic usage

Code with nulls:

String input = ...;
String result = null;

if (input != null && input.length() > 0) {
    result = "Length of the string is " + input.length();
} else {
    result = "The string is null or empty";
}

Code with Options:

String input = ...;

String result = Option.ofObj(input)
                      .filter(str -> str.length() > 0)
                      .match(str -> "Length of the string is " + str.length(),
                             ()  -> "The string is null or empty");

Advance usage

Code with nulls:

String input = ...;
String result = null;

if (input != null) {
    try {
        int intValue = Integer.parseInt(input);
        result = "Input can be parsed to number: " + intValue;
    } catch (NumberFormatException e) {
        result = "Input is not a number";
    }
}

Code with Options:

String input = ...;

String result = Option.ofObj(input)
                      .flatMap(str -> Option.tryAsOption(() -> Integer.parseInt(str)))
                      .map(intValue -> "Input can be parsed to number: " + intValue)
                      .orDefault(() -> "Input is not a number");

How to include in your project?

Options are available on maven via jitpack. Just add to your gradle files:

To your top level gradle.build file add repository:

repositories { 
    jcenter()
    maven { url "https://jitpack.io" }
}

To your module level gradle.build add dependency:

dependencies {
    // other dependencies
    compile 'com.github.tomaszpolanski:options:1.3.0'
}

References

This library was strongly influenced by C# Functional Language Extensions.

Extension points exported contracts — how you extend this code

browse all types & interfaces →

Core symbols most depended-on inside this repo

browse all functions →

Shape

Method 178
Class 11
Interface 11
Enum 1

Languages

Java60%
Kotlin40%

Modules by API surface

options/core/src/main/java/polanski/option/Some.java24 symbols
options/core/src/main/java/polanski/option/Option.java22 symbols
options/core/src/main/java/polanski/option/None.java20 symbols
options/core/src/main/java/polanski/option/OptionAssertion.java10 symbols
options/core/src/main/java/polanski/option/OptionUnsafe.java4 symbols
options/core/src/main/java/polanski/option/AtomicOption.java4 symbols
options/core/src/main/java/polanski/option/Unit.java3 symbols
options/functions/src/main/java/polanski/option/function/FuncN.java2 symbols
options/functions/src/main/java/polanski/option/function/Func4.java2 symbols
options/functions/src/main/java/polanski/option/function/Func3.java2 symbols
options/functions/src/main/java/polanski/option/function/Func2.java2 symbols
options/functions/src/main/java/polanski/option/function/Func1.java2 symbols

Used by 1 indexed graphs manifest dependencies, hub-wide

For agents

$ claude mcp add --transport http codehub https://codehub.bot/mcp

One hosted endpoint, no key: your agent can search, read and trace tomaszpolanski/Options and every other graph here. Cursor, Windsurf and other clients →

⬇ download graph artifact

Ask about this repo answers extend the page