1515 */
1616package com .github .rvesse .airline .help .common ;
1717
18- import java .util .ArrayList ;
19- import java .util .Collections ;
2018import java .util .Comparator ;
2119import java .util .List ;
2220import java .util .Set ;
21+ import java .util .function .Predicate ;
22+ import java .util .stream .Collectors ;
2323
24- import org .apache .commons .collections4 .ListUtils ;
2524import org .apache .commons .lang3 .StringUtils ;
2625
2726import com .github .rvesse .airline .help .UsageHelper ;
@@ -80,37 +79,41 @@ protected final Comparator<? super CommandMetadata> getCommandComparator() {
8079 * @return Sorted options
8180 */
8281 protected List <OptionMetadata > sortOptions (List <OptionMetadata > options ) {
83- if (optionComparator != null ) {
84- options = new ArrayList <OptionMetadata >(options );
85- Collections .sort (options , optionComparator );
82+ if (optionComparator == null ) {
83+ return options ;
8684 }
87- return options ;
85+
86+ return options .stream ()
87+ .sorted (optionComparator )
88+ .collect (Collectors .toList ());
8889 }
8990
9091 protected List <HelpHint > sortOptionRestrictions (List <OptionRestriction > restrictions ) {
91- List <HelpHint > hints = new ArrayList <>();
92- for (OptionRestriction restriction : restrictions ) {
93- if (restriction instanceof HelpHint ) {
94- hints .add ((HelpHint )restriction );
95- }
96- }
97- if (hintComparator != null ) {
98- Collections .sort (hints , hintComparator );
92+ var hints = restrictions .stream ()
93+ .filter (HelpHint .class ::isInstance )
94+ .map (HelpHint .class ::cast );
95+
96+ if (hintComparator == null ) {
97+ return hints .collect (Collectors .toList ());
9998 }
100- return hints ;
99+
100+ return hints
101+ .sorted (hintComparator )
102+ .collect (Collectors .toList ());
101103 }
102-
104+
103105 protected List <HelpHint > sortArgumentsRestrictions (List <ArgumentsRestriction > restrictions ) {
104- List <HelpHint > hints = new ArrayList <>();
105- for (ArgumentsRestriction restriction : restrictions ) {
106- if (restriction instanceof HelpHint ) {
107- hints .add ((HelpHint )restriction );
108- }
109- }
110- if (hintComparator != null ) {
111- Collections .sort (hints , hintComparator );
106+ var hints = restrictions .stream ()
107+ .filter (HelpHint .class ::isInstance )
108+ .map (HelpHint .class ::cast );
109+
110+ if (hintComparator == null ) {
111+ return hints .collect (Collectors .toList ());
112112 }
113- return hints ;
113+
114+ return hints
115+ .sorted (hintComparator )
116+ .collect (Collectors .toList ());
114117 }
115118
116119 /**
@@ -122,11 +125,13 @@ protected List<HelpHint> sortArgumentsRestrictions(List<ArgumentsRestriction> re
122125 * @return Sorted commands
123126 */
124127 protected List <CommandMetadata > sortCommands (List <CommandMetadata > commands ) {
125- if (commandComparator != null ) {
126- commands = new ArrayList <>(commands );
127- Collections .sort (commands , commandComparator );
128+ if (commandComparator == null ) {
129+ return commands ;
128130 }
129- return commands ;
131+
132+ return commands .stream ()
133+ .sorted (commandComparator )
134+ .collect (Collectors .toList ());
130135 }
131136
132137 /**
@@ -172,13 +177,11 @@ protected String toDefaultCommand(String command) {
172177 * @return Option synopses
173178 */
174179 protected List <String > toSynopsisUsage (List <OptionMetadata > options ) {
175- List <String > synopsisOptions = new ArrayList <String >();
176- for (OptionMetadata option : options ) {
177- if (option .isHidden () && !includeHidden )
178- continue ;
179- synopsisOptions .add (toUsage (option ));
180- }
181- return List .copyOf (synopsisOptions );
180+ Predicate <OptionMetadata > noHidden = o -> o .isHidden () && !includeHidden ;
181+ return options .stream ()
182+ .filter (noHidden .negate ())
183+ .map (this ::toUsage )
184+ .collect (Collectors .toList ());
182185 }
183186
184187 protected String toUsage (ArgumentsMetadata arguments ) {
0 commit comments