Generated from at 3.0.0 (amended documentation) 2020-08-18T23:00:39Z



Basic introduction (by example) to Opt classes. Available since 3.0.0.


Opt/OptInt/OptLong/OptDouble and other primitive examples are like spiritual extension to the Optional/OptionalInt/OptionalLong/OptionalDouble (since those cannot be extended), and you can use them as spiritual replacement in 'optional' response. Optional classes from this library aims to provide:

  • more possibility for cases where simply referencing existing method (that can be provided by editor completion).
  • that means more possibility to build 'sentences' that actually tell what happens.


A very abstract example of few mapping methods:

public static interface S { S add(D all); Opt<S> addO(D all); } public static interface D { D add(S all); Opt<D> addO(S all); } @Test public void test1() { Opt<S> optS = Opt.of(null); Opt<S> v1 = null, S::add); Opt<D> v2 = optS.mapWith((D) null, D::add); Opt<S> v3 = optS.flatMap((D) null, S::addO); Opt<D> v4 = optS.flatMapWith((D) null, D::addO); }

Example of filtering:

@Test public void test2() { Opt<Integer> ooo = Opt.obj(5); var result = ooo .uniFilter(Is::equal, 5) // uniFilter resolves ambiguity of argument type .filter(Is::inRange, 5, 7) // 5 is in range from 5 to 7 .filter(Is::between, 5, 7); // 5 is NOT between 5 to 7 assertThat(result.toOpt()).isEmpty(); }

Here is example of is alternative to filter methods along with a must check, since Opt(*) inherit most check methods described here.

@Test(expectedExceptions = IllegalValueException.class, expectedExceptionsMessageRegExp = "Opt \\[\\?\\]: must be 99") public void test3() { Opt<Integer> ooo = Opt.obj(5); assertThat(ooo.uniIs(P::equal, 5)).isTrue(); assertThat(, 5, 7)).isTrue(); assertThat(, 5, 7)).isFalse(); ooo.uniMust2(Be::equal, 99, "must be 99"); }

For the new types of methods following is true in regards to the empty value:

  • is evaluates always false (no predicate is true).
  • must/mustNot will always throw exception that there is no value present (the predicate cannot be asserted)

@Test public void test4() { Opt ooo = Opt.empty(); assertThat(; } @Test(expectedExceptions = NoSuchElementException.class, expectedExceptionsMessageRegExp = "No value present.") public void test5() { OptInt ooo = OptInt.empty(); ooo.must2(Be::equal, 99, "must be 99"); }

Do you need specialized optional for your application?

public final static class OptStr extends OptBase<String, OptStr> { public static final OptStr EMPTY_STR = new OptStr(""); public static final OptStr VOID = new OptStr(null); private OptStr(String value) { super(value);} @Override public OptStr value(String value) { return str(value);} @Override public OptStr voidValue() { return VOID;} public static OptStr str(@Nullable String str) { return new OptStr(str);} public static OptStr emptyOptStr() { return EMPTY_STR;} public String toString() {return value;} //<editor-fold desc="custom methods"> public OptStr replace(String target, String replacement) { return this.uniMap(s -> s.replace(target, replacement)); } public OptStr removeTail(String tail) { return this.uniMap(s -> s.endsWith(tail) ? s.substring(0, s.length() - tail.length()) : s); } //</editor-fold> } @Test public void test6() { OptStr str = OptStr.str("This is optional string.") .filter(P::startWith, "T") .replace("This", "THIS") .removeTail(".") .must2Ex(Be::equalEx, "THIS is optional string"); }

Generated from at 3.0.0 (amended documentation) 2020-08-18T23:00:39Z