1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * https://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 */
19 package org.apache.rat.ui;
20
21 import java.util.ArrayList;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.stream.Stream;
26
27 import org.apache.commons.cli.Option;
28 import org.apache.commons.cli.OptionGroup;
29 import org.apache.commons.cli.Options;
30
31 /**
32 * A collection of UpdatableOptionGroups.
33 */
34 public class UpdatableOptionGroupCollection {
35 /** the contained UpdatableOptionGroups */
36 private final List<UpdatableOptionGroup> updatableOptionGroups;
37
38 /**
39 * Creates an empty collection.
40 */
41 public UpdatableOptionGroupCollection() {
42 updatableOptionGroups = new ArrayList<>();
43 }
44
45 /**
46 * Adds an OptionGroup to the collection. If the OptionGroup is not an UpdatableOptionGroup
47 * it is converted first.
48 * @param optionGroup an OptionGroup to add.
49 * @return the UpdatableOptionGroup that was added.
50 */
51 public UpdatableOptionGroup add(final OptionGroup optionGroup) {
52 UpdatableOptionGroup uog = UpdatableOptionGroup.create(optionGroup);
53 updatableOptionGroups.add(uog);
54 return uog;
55 }
56
57 /**
58 * Gets an Options object from this collection.
59 * @return an Options object.
60 */
61 public Options options() {
62 Options result = new Options();
63 updatableOptionGroups.forEach(result::addOptionGroup);
64 return result;
65 }
66
67 /**
68 * Gets ll the UpdatableOptionGroups that the option is in.
69 * @param option the option to searhc for.
70 * @return the stream of UpdatableOptionGroups the option is in.
71 */
72 public Stream<UpdatableOptionGroup> findGroups(final Option option) {
73 return updatableOptionGroups.stream().filter(og -> og.getOptions().contains(option));
74 }
75
76 /**
77 * Gets the set of removed Options from the collection.
78 * @return the set of removed options.
79 */
80 public Set<Option> removedOptions() {
81 Set<Option> result = new HashSet<>();
82 updatableOptionGroups.forEach(uog -> uog.getDisableOptions().forEach(result::add));
83 return result;
84 }
85
86 /**
87 * Gets the unsupported options
88 * If multiple options from the a group are disabled they will be added to the
89 * options in a group together.
90 * @return the Options object containing all the unsupported options.
91 */
92 public Options unsupportedOptions() {
93 Options result = new Options();
94 for (UpdatableOptionGroup uog : updatableOptionGroups) {
95 OptionGroup group = new OptionGroup();
96 uog.getDisableOptions().forEach(group::addOption);
97 result.addOptionGroup(group);
98 }
99 return result;
100 }
101
102 /**
103 * Returns true if the option is in any of the groups.
104 * @param option the option.
105 * @return {@code true} if the option is in any of the groups.
106 */
107 public boolean contains(final Option option) {
108 return updatableOptionGroups.stream().anyMatch(og -> og.getOptions().contains(option));
109 }
110 }