• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SpiNNakerManchester / JavaSpiNNaker / 6233274834

19 Sep 2023 08:46AM UTC coverage: 36.409% (-0.6%) from 36.982%
6233274834

Pull #658

github

dkfellows
Merge branch 'master' into java-17
Pull Request #658: Update Java version to 17

1656 of 1656 new or added lines in 260 files covered. (100.0%)

8373 of 22997 relevant lines covered (36.41%)

0.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/SpiNNaker-utils/src/main/java/uk/ac/manchester/spinnaker/utils/OptionalUtils.java
1
/*
2
 * Copyright (c) 2022 The University of Manchester
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package uk.ac.manchester.spinnaker.utils;
17

18
import java.util.Optional;
19
import java.util.function.Consumer;
20
import java.util.function.Function;
21
import java.util.function.Supplier;
22

23
/**
24
 * Extra utilities for working with {@link Optional} in Java 8.
25
 *
26
 * @author Donal Fellows
27
 */
28
public abstract class OptionalUtils {
29
        private OptionalUtils() {
30
        }
31

32
        /**
33
         * How to apply a sequence of modification transforms to a value in an
34
         * optional if there is a value there at all.
35
         *
36
         * @param <T>
37
         *            The type of value in the optional.
38
         * @param source
39
         *            Where to get the value from.
40
         * @param actions
41
         *            The transformations to apply to the value in the optional if
42
         *            it exists; these are expected to <em>modify</em> that object.
43
         * @return The original optional.
44
         */
45
        @SafeVarargs
46
        public static <T> Optional<T> apply(Optional<T> source,
47
                        Consumer<T>... actions) {
48
                source.ifPresent(t -> {
×
49
                        for (var action : actions) {
×
50
                                action.accept(t);
×
51
                        }
52
                });
×
53
                return source;
×
54
        }
55

56
        /**
57
         * How to do one thing if an {@linkplain Optional optional} is present, and
58
         * another if it is absent.
59
         *
60
         * @param <T>
61
         *            The type of value in the optional.
62
         * @param <U>
63
         *            The type of the result.
64
         * @param source
65
         *            Where to get the value from.
66
         * @param ifPresent
67
         *            What to do if the value is available.
68
         * @param ifAbsent
69
         *            What to do if the value is not available.
70
         * @return The result, depending on which arm was taken.
71
         */
72
        public static <T, U> U ifElse(Optional<T> source, Function<T, U> ifPresent,
73
                        Supplier<U> ifAbsent) {
74
                // Would use ifPresentOrElse, except that's void result
75
                return source.map(ifPresent).orElseGet(ifAbsent);
×
76
        }
77

78
        /**
79
         * How to do one thing if an {@linkplain Optional optional} is present, and
80
         * another if it is absent.
81
         *
82
         * @param <T>
83
         *            The type of value in the optional.
84
         * @param <U>
85
         *            The type of the result.
86
         * @param source
87
         *            Where to get the value from.
88
         * @param ifPresent
89
         *            What to do if the value is available.
90
         * @param ifAbsent
91
         *            What to do if the value is not available.
92
         * @return The result, depending on which arm was taken.
93
         */
94
        public static <T, U> Optional<U> ifElseFlat(Optional<T> source,
95
                        Function<T, U> ifPresent, Supplier<U> ifAbsent) {
96
                return source.map(t -> Optional.ofNullable(ifPresent.apply(t)))
×
97
                                .orElseGet(() -> Optional.ofNullable(ifAbsent.get()));
×
98
        }
99
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc