Skip to content

Optional

Danila Rassokhin edited this page Feb 4, 2023 · 4 revisions

Aide Optional contains:

  • Optional classes for different types
  • Conditionals
  • Utils

Optionals

Aide provides extended optionals for some types. All optionals implement GenericOptional and can be converted to standard Optional with generic().

BooleanOptional

BooleanOptional stores boolean value. Null values are not supported and NullPointerException will be thrown.

Example

BooleanOptional.of(Modifier.isPublic(executable.getModifiers()))
        .ifFalseThrow(() -> ReflectionException.format("Wrapping is supported for PUBLIC methods only!"));

StringOptional

BooleanOptional stores string value. Null values are not supported and NullPointerException will be thrown.

Example

StringOptional stringOptional = StringOptional.of("String");

String actual = stringOptional.mapOnCondition(s -> true, v -> "My" + v).get();
assert "MyString".equals(actual);

Throwable Optional

ThrowableOptional allows you to handle checked exceptions in lambda expressions.

Expression

public void exceptional() throws Throwable {
    
}

// No try-catch needed
ThrowableOptional.sneaky(() -> exceptional());

Conditionals

Conditionals are conditional statements as standard if-else for example, but in functional style.

IfTrue Conditional

IfTrueConditional works like standard if-else: it evaluates predicates and return value from first success branch.

Example

AbstractSignature signature = IfTrueConditional.create()
        .ifTrue(exact).then(() -> ExactMethodSignature.from(method))
        .orElseGet(() -> MethodSignature.from(method));

When Conditional

WhenConditional works like IfTrueConditional except it doesn't return any value and just evaluates action on first success branch.

Example

WhenConditional.create()
    .when(someObj, Objects::nonNull).then(MyClass::nonNull)
    .when(someObj, Objects::isNull).then(MyClass::isNull)
    .orDoNothing();

Utils

ObjectUtil

ObjectUtils contains some useful methods for Object