Package: ArgumentsUtils
ArgumentsUtils
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
findBadOptions(ApplicationArguments, Set) |
|
|
|
|
|
||||||||||||||||||||
getIntOption(ApplicationArguments, String) |
|
|
|
|
|
||||||||||||||||||||
getStringOption(ApplicationArguments, String) |
|
|
|
|
|
||||||||||||||||||||
lambda$getStringOption$0(List) |
|
|
|
|
|
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * SolidBlue 3: Data safety
5: * http://tidalwave.it/projects/solidblue3
6: *
7: * Copyright (C) 2023 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/solidblue3j-src
23: * git clone https://github.com/tidalwave-it/solidblue3j-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.datamanager.application.nogui.args;
28:
29: import jakarta.annotation.Nonnull;
30: import java.util.Optional;
31: import java.util.Set;
32: import java.util.TreeSet;
33: import org.springframework.boot.ApplicationArguments;
34: import lombok.NoArgsConstructor;
35: import static lombok.AccessLevel.PRIVATE;
36:
37: /***********************************************************************************************************************
38: *
39: * Some generic utils to manipulate {@link ApplicationArguments}.
40: *
41: * @author Fabrizio Giudici
42: *
43: **********************************************************************************************************************/
44: @NoArgsConstructor(access = PRIVATE)
45: public final class ArgumentsUtils
46: {
47: /*******************************************************************************************************************
48: *
49: * Extracts an optional integer option.
50: *
51: * @param args the argument
52: * @param name the name of the option
53: * @return the value of the option
54: *
55: ******************************************************************************************************************/
56: @Nonnull
57: public static Optional<Integer> getIntOption (@Nonnull final ApplicationArguments args, @Nonnull final String name)
58: {
59: return getStringOption(args, name).map(Integer::parseInt);
60: }
61:
62: /*******************************************************************************************************************
63: *
64: * Extracts an optional string option.
65: *
66: * @param args the argument
67: * @param name the name of the option
68: * @return the value of the option
69: *
70: ******************************************************************************************************************/
71: @Nonnull
72: public static Optional<String> getStringOption (@Nonnull final ApplicationArguments args,
73: @Nonnull final String name)
74: {
75: return Optional.ofNullable(args.getOptionValues(name)).flatMap(l -> l.stream().findFirst());
76: }
77:
78: /*******************************************************************************************************************
79: *
80: * Find options that are not contained in a given valid collection.
81: *
82: * @param args the application arguments
83: * @param validOptions the valid options
84: * @return the bad options
85: *
86: ******************************************************************************************************************/
87: @Nonnull
88: public static Set<String> findBadOptions (@Nonnull final ApplicationArguments args,
89: @Nonnull final Set<String> validOptions)
90: {
91: final var set = new TreeSet<>(args.getOptionNames());
92: set.removeAll(validOptions);
93: return set;
94: }
95: }