summaryrefslogtreecommitdiffstats
path: root/StOptional.java
blob: 308d29ccc40261e8f30d27c2f27e5994f2973519 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
 * copyright (c) squibid 2024 under the Beerware license.
 *
 * If we meet some day, and you think this stuff is worth it, you can buy me a
 * beer in return.
 */

import java.util.Optional;

/**
 * Stop Optionals from being used!
 * safely reducing optionals back to their original types.
 */
public final class StOptional {
  /**
   * Reduce any optional down to it's original type
   *
   * @param o Optional<T> to reduce to it's original type
   * @return <T> value of optional in original type or null
   */
  public static final <T> T reduce(Optional<T> o) {
    return nullish(o) ? null : o.get();
  }

  /**
   * Checks if Optional<?> is null or empty
   *
   * @param o Optional<?> optional to check for null like properties
   * @return boolean true if null or empty, false otherwise
   */
  public static final boolean nullish(Optional<?> o) {
    if (o == null || o.isEmpty())
      return true;
    return false;
  }
}