OptionalUtil.java

1
package com.yubico.internal.util;
2
3
import java.util.Optional;
4
import java.util.function.BinaryOperator;
5
import java.util.function.Supplier;
6
import java.util.stream.Stream;
7
import lombok.NonNull;
8
import lombok.experimental.UtilityClass;
9
10
/** Utilities for working with {@link Optional} values. */
11
@UtilityClass
12
public class OptionalUtil {
13
14
  /**
15
   * If <code>primary</code> is present, return it unchanged. Otherwise return <code>
16
   * secondary</code>.
17
   */
18
  public static <T> Optional<T> orOptional(Optional<T> primary, Optional<T> secondary) {
19 1 1. orOptional : negated conditional → NO_COVERAGE
    if (primary.isPresent()) {
20 1 1. orOptional : replaced return value with Optional.empty for com/yubico/internal/util/OptionalUtil::orOptional → NO_COVERAGE
      return primary;
21
    } else {
22 1 1. orOptional : replaced return value with Optional.empty for com/yubico/internal/util/OptionalUtil::orOptional → NO_COVERAGE
      return secondary;
23
    }
24
  }
25
26
  /**
27
   * If <code>primary</code> is present, return it unchanged. Otherwise return the result of <code>
28
   * recover</code>.
29
   */
30
  public static <T> Optional<T> orElseOptional(Optional<T> primary, Supplier<Optional<T>> recover) {
31 1 1. orElseOptional : negated conditional → NO_COVERAGE
    if (primary.isPresent()) {
32 1 1. orElseOptional : replaced return value with Optional.empty for com/yubico/internal/util/OptionalUtil::orElseOptional → NO_COVERAGE
      return primary;
33
    } else {
34 1 1. orElseOptional : replaced return value with Optional.empty for com/yubico/internal/util/OptionalUtil::orElseOptional → NO_COVERAGE
      return recover.get();
35
    }
36
  }
37
38
  /**
39
   * Returns a sequential {@link Stream} with this {@link Optional} as its source.
40
   *
41
   * @param o the {@link Optional} to interpret as a {@link Stream}
42
   * @return a sequential {@link Stream} containing the value of this {@link Optional} if present,
43
   *     otherwise an empty {@link Stream}.
44
   */
45 1 1. stream : negated conditional → NO_COVERAGE
  public static <T> Stream<T> stream(@NonNull Optional<T> o) {
46 1 1. stream : replaced return value with Stream.empty for com/yubico/internal/util/OptionalUtil::stream → NO_COVERAGE
    return o.map(Stream::of).orElseGet(Stream::empty);
47
  }
48
49
  /**
50
   * If both <code>a</code> and <code>b</code> are present, return <code>f(a, b)</code>.
51
   *
52
   * <p>If only <code>a</code> is present, return <code>a</code>.
53
   *
54
   * <p>Otherwise, return <code>b</code>.
55
   */
56
  public static <T> Optional<T> zipWith(Optional<T> a, Optional<T> b, BinaryOperator<T> f) {
57 2 1. zipWith : negated conditional → NO_COVERAGE
2. zipWith : negated conditional → NO_COVERAGE
    if (a.isPresent() && b.isPresent()) {
58 1 1. zipWith : replaced return value with Optional.empty for com/yubico/internal/util/OptionalUtil::zipWith → NO_COVERAGE
      return Optional.of(f.apply(a.get(), b.get()));
59 1 1. zipWith : negated conditional → NO_COVERAGE
    } else if (a.isPresent()) {
60 1 1. zipWith : replaced return value with Optional.empty for com/yubico/internal/util/OptionalUtil::zipWith → NO_COVERAGE
      return a;
61
    } else {
62 1 1. zipWith : replaced return value with Optional.empty for com/yubico/internal/util/OptionalUtil::zipWith → NO_COVERAGE
      return b;
63
    }
64
  }
65
}

Mutations

19

1.1
Location : orOptional
Killed by : none
negated conditional → NO_COVERAGE

20

1.1
Location : orOptional
Killed by : none
replaced return value with Optional.empty for com/yubico/internal/util/OptionalUtil::orOptional → NO_COVERAGE

22

1.1
Location : orOptional
Killed by : none
replaced return value with Optional.empty for com/yubico/internal/util/OptionalUtil::orOptional → NO_COVERAGE

31

1.1
Location : orElseOptional
Killed by : none
negated conditional → NO_COVERAGE

32

1.1
Location : orElseOptional
Killed by : none
replaced return value with Optional.empty for com/yubico/internal/util/OptionalUtil::orElseOptional → NO_COVERAGE

34

1.1
Location : orElseOptional
Killed by : none
replaced return value with Optional.empty for com/yubico/internal/util/OptionalUtil::orElseOptional → NO_COVERAGE

45

1.1
Location : stream
Killed by : none
negated conditional → NO_COVERAGE

46

1.1
Location : stream
Killed by : none
replaced return value with Stream.empty for com/yubico/internal/util/OptionalUtil::stream → NO_COVERAGE

57

1.1
Location : zipWith
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : zipWith
Killed by : none
negated conditional → NO_COVERAGE

58

1.1
Location : zipWith
Killed by : none
replaced return value with Optional.empty for com/yubico/internal/util/OptionalUtil::zipWith → NO_COVERAGE

59

1.1
Location : zipWith
Killed by : none
negated conditional → NO_COVERAGE

60

1.1
Location : zipWith
Killed by : none
replaced return value with Optional.empty for com/yubico/internal/util/OptionalUtil::zipWith → NO_COVERAGE

62

1.1
Location : zipWith
Killed by : none
replaced return value with Optional.empty for com/yubico/internal/util/OptionalUtil::zipWith → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.15.0