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.Collection;
22 import java.util.HashSet;
23 import java.util.Set;
24 import java.util.stream.Stream;
25
26 import org.apache.commons.cli.Option;
27 import org.apache.commons.cli.OptionGroup;
28
29 /**
30 * An implementation of Apache Commons CLI OptionGroup that allows options to be removed (disabled).
31 */
32 public final class UpdatableOptionGroup extends OptionGroup {
33 /** The set of options to remove */
34 private final Set<Option> disabledOptions = new HashSet<>();
35
36 /**
37 * Converts the group into an UpdatableOptionGroup if it is not already an instance
38 * @param group the group to convert.
39 * @return an UpdatableOptionGroup.
40 */
41 public static UpdatableOptionGroup create(final OptionGroup group) {
42 return group instanceof UpdatableOptionGroup updatableOptionGroup ? updatableOptionGroup : new UpdatableOptionGroup(group);
43 }
44
45 private UpdatableOptionGroup(final OptionGroup group) {
46 group.getOptions().forEach(super::addOption);
47 }
48
49 /**
50 * Disable an option in the group.
51 * @param option The option to disable.
52 */
53 public void disableOption(final Option option) {
54 disabledOptions.add(option);
55 }
56
57 public boolean isEmpty() {
58 return getOptions().isEmpty();
59 }
60
61 /**
62 * Gets the disabled options for this group.
63 * @return the set of disabled options for this group.
64 */
65 public Stream<Option> getDisableOptions() {
66 return disabledOptions.stream();
67 }
68 /**
69 * Reset the group so that all disabled options are re-enabled.
70 */
71 public void reset() {
72 disabledOptions.clear();
73 }
74
75 @Override
76 public Collection<Option> getOptions() {
77 return super.getOptions().stream().filter(opt -> !disabledOptions.contains(opt)).toList();
78 }
79
80 @Override
81 public UpdatableOptionGroup addOption(final Option option) {
82 super.addOption(option);
83 return this;
84 }
85 }