From 25cd7e7def2aaa446b39ea6fe57c874f1ccfb57d Mon Sep 17 00:00:00 2001 From: alainlompo Date: Fri, 3 Nov 2017 23:12:10 +0100 Subject: [PATCH 1/2] Fixing squid:S1604 --- .../java/io/airlift/airline/Accessor.java | 8 +- src/main/java/io/airlift/airline/Cli.java | 11 +- .../airlift/airline/GlobalUsageSummary.java | 21 +--- .../java/io/airlift/airline/UsageHelper.java | 102 +++++------------- .../airline/model/CommandGroupMetadata.java | 8 +- .../airline/model/CommandMetadata.java | 8 +- .../airlift/airline/model/MetadataLoader.java | 17 +-- .../airlift/airline/model/OptionMetadata.java | 17 +-- 8 files changed, 41 insertions(+), 151 deletions(-) diff --git a/src/main/java/io/airlift/airline/Accessor.java b/src/main/java/io/airlift/airline/Accessor.java index 3cb883211..e8256904b 100644 --- a/src/main/java/io/airlift/airline/Accessor.java +++ b/src/main/java/io/airlift/airline/Accessor.java @@ -36,13 +36,7 @@ public Accessor(Iterable path) Preconditions.checkArgument(!Iterables.isEmpty(path), "path is empty"); this.path = ImmutableList.copyOf(path); - this.name = this.path.get(0).getDeclaringClass().getSimpleName() + "." + Joiner.on('.').join(Iterables.transform(this.path, new Function() - { - public String apply(Field field) - { - return field.getName(); - } - })); + this.name = this.path.get(0).getDeclaringClass().getSimpleName() + "." + Joiner.on('.').join(Iterables.transform(this.path, field -> field.getName())); Field field = this.path.get(this.path.size() - 1); multiValued = Collection.class.isAssignableFrom(field.getType()); diff --git a/src/main/java/io/airlift/airline/Cli.java b/src/main/java/io/airlift/airline/Cli.java index 76369960e..23449f093 100644 --- a/src/main/java/io/airlift/airline/Cli.java +++ b/src/main/java/io/airlift/airline/Cli.java @@ -77,14 +77,9 @@ private Cli(String name, List defaultCommandGroup = MetadataLoader.loadCommands(defaultGroupCommands); - List commandGroups = ImmutableList.copyOf(Iterables.transform(groups, new Function, CommandGroupMetadata>() - { - @Override - public CommandGroupMetadata apply(GroupBuilder group) - { - return MetadataLoader.loadCommandGroup(group.name, group.description, MetadataLoader.loadCommand(group.defaultCommand), MetadataLoader.loadCommands(group.commands)); - } - })); + List commandGroups = ImmutableList.copyOf(Iterables.transform(groups, + group -> MetadataLoader.loadCommandGroup(group.name, group.description, MetadataLoader.loadCommand(group.defaultCommand), MetadataLoader.loadCommands(group.commands)) + )); this.metadata = MetadataLoader.loadGlobal(name, description, defaultCommandMetadata, defaultCommandGroup, commandGroups); } diff --git a/src/main/java/io/airlift/airline/GlobalUsageSummary.java b/src/main/java/io/airlift/airline/GlobalUsageSummary.java index d27344684..0e5e9bacd 100644 --- a/src/main/java/io/airlift/airline/GlobalUsageSummary.java +++ b/src/main/java/io/airlift/airline/GlobalUsageSummary.java @@ -60,16 +60,8 @@ public void usage(GlobalMetadata global, UsagePrinter out) // build arguments List commandArguments = newArrayList(); - commandArguments.addAll(Collections2.transform(global.getOptions(), new Function() - { - public String apply(OptionMetadata option) - { - if (option.isHidden()) { - return null; - } - return toUsage(option); - } - })); + commandArguments.addAll(Collections2.transform(global.getOptions(), + optionValue -> (optionValue.isHidden())?null:toUsage(optionValue))); out.newPrinterWithHangingIndent(8) .append("usage:") .append(global.getName()) @@ -93,13 +85,8 @@ public String apply(OptionMetadata option) } out.append("The most commonly used ").append(global.getName()).append(" commands are:").newline(); - out.newIndentedPrinter(4).appendTable(Iterables.transform(commands.entrySet(), new Function, Iterable>() - { - public Iterable apply(Entry entry) - { - return ImmutableList.of(entry.getKey(), Objects.firstNonNull(entry.getValue(), "")); - } - })); + out.newIndentedPrinter(4).appendTable(Iterables.transform(commands.entrySet(), + entry -> ImmutableList.of(entry.getKey(), Objects.firstNonNull(entry.getValue(), "")))); out.newline(); out.append("See").append("'" + global.getName()).append("help ' for more information on a specific command.").newline(); } diff --git a/src/main/java/io/airlift/airline/UsageHelper.java b/src/main/java/io/airlift/airline/UsageHelper.java index 7d71a5fbd..79775f182 100644 --- a/src/main/java/io/airlift/airline/UsageHelper.java +++ b/src/main/java/io/airlift/airline/UsageHelper.java @@ -21,37 +21,28 @@ public class UsageHelper { - public static final Comparator DEFAULT_OPTION_COMPARATOR = new Comparator() - { - @Override - public int compare(OptionMetadata o1, OptionMetadata o2) - { - String option1 = o1.getOptions().iterator().next(); - option1 = option1.replaceFirst("^-+", ""); - - String option2 = o2.getOptions().iterator().next(); - option2 = option2.replaceFirst("^-+", ""); - - return ComparisonChain.start() - .compare(option1.toLowerCase(), option2.toLowerCase()) - .compare(option2, option1) // print lower case letters before upper case - .compare(System.identityHashCode(o1), System.identityHashCode(o2)) - .result(); - } - }; - public static final Comparator DEFAULT_COMMAND_COMPARATOR = new Comparator() - { - @Override - public int compare(CommandMetadata o1, CommandMetadata o2) - { - return ComparisonChain.start() - .compare(o1.getName().toLowerCase(), o2.getName().toLowerCase()) - .compare(o2.getName(), o1.getName()) // print lower case letters before upper case - .compare(System.identityHashCode(o1), System.identityHashCode(o2)) - .result(); - } - }; - + public static final Comparator DEFAULT_OPTION_COMPARATOR = + (o1,o2) -> { + String option1 = o1.getOptions().iterator().next(); + option1 = option1.replaceFirst("^-+", ""); + + String option2 = o2.getOptions().iterator().next(); + option2 = option2.replaceFirst("^-+", ""); + + return ComparisonChain.start() + .compare(option1.toLowerCase(), option2.toLowerCase()) + .compare(option2, option1) // print lower case letters before upper case + .compare(System.identityHashCode(o1), System.identityHashCode(o2)) + .result(); + }; + + public static final Comparator DEFAULT_COMMAND_COMPARATOR = + (o1, o2) -> ComparisonChain.start() + .compare(o1.getName().toLowerCase(), o2.getName().toLowerCase()) + .compare(o2.getName(), o1.getName()) // print lower case letters before upper case + .compare(System.identityHashCode(o1), System.identityHashCode(o2)) + .result(); + public static String toDescription(OptionMetadata option) { Set options = option.getOptions(); @@ -59,29 +50,13 @@ public static String toDescription(OptionMetadata option) final String argumentString; if (option.getArity() > 0) { - argumentString = Joiner.on(" ").join(Lists.transform(ImmutableList.of(option.getTitle()), new Function() - { - public String apply(@Nullable String argument) - { - return "<" + argument + ">"; - } - })); + argumentString = Joiner.on(" ").join(Lists.transform(ImmutableList.of(option.getTitle()), argument -> "<" + argument + ">" )); } else { argumentString = null; } - Joiner.on(", ").appendTo(stringBuilder, transform(options, new Function() - { - public String apply(@Nullable String option) - { - if (argumentString != null) { - return option + " " + argumentString; - } - return option; - } - })); - + Joiner.on(", ").appendTo(stringBuilder, transform(options, optionValue -> (argumentString != null)?optionValue + " " + argumentString:optionValue)); return stringBuilder.toString(); } @@ -109,30 +84,13 @@ public static String toUsage(OptionMetadata option) final String argumentString; if (option.getArity() > 0) { - argumentString = Joiner.on(" ").join(transform(ImmutableList.of(option.getTitle()), new Function() - { - public String apply(@Nullable String argument) - { - return "<" + argument + ">"; - } - })); + argumentString = Joiner.on(" ").join(transform(ImmutableList.of(option.getTitle()), argument -> "<" + argument + ">")); } else { argumentString = null; } - Joiner.on(" | ").appendTo(stringBuilder, transform(options, new Function() - { - public String apply(@Nullable String option) - { - if (argumentString != null) { - return option + " " + argumentString; - } - else { - return option; - } - } - })); + Joiner.on(" | ").appendTo(stringBuilder, transform(options, optionValue -> (argumentString != null)?optionValue + " " + argumentString:optionValue)); if (options.size() > 1) { stringBuilder.append(')'); @@ -174,12 +132,6 @@ public static String toUsage(ArgumentsMetadata arguments) public static List toSynopsisUsage(List options) { - return ImmutableList.copyOf(transform(filter(options, isHiddenPredicate()), new Function() - { - public String apply(OptionMetadata option) - { - return toUsage(option); - } - })); + return ImmutableList.copyOf(transform(filter(options, isHiddenPredicate()), option -> toUsage(option))); } } diff --git a/src/main/java/io/airlift/airline/model/CommandGroupMetadata.java b/src/main/java/io/airlift/airline/model/CommandGroupMetadata.java index a7a778494..5ac7abab2 100644 --- a/src/main/java/io/airlift/airline/model/CommandGroupMetadata.java +++ b/src/main/java/io/airlift/airline/model/CommandGroupMetadata.java @@ -63,12 +63,6 @@ public String toString() public static Function nameGetter() { - return new Function() - { - public String apply(CommandGroupMetadata input) - { - return input.getName(); - } - }; + return input -> input.getName(); } } diff --git a/src/main/java/io/airlift/airline/model/CommandMetadata.java b/src/main/java/io/airlift/airline/model/CommandMetadata.java index a11e86073..6b8b49f9c 100644 --- a/src/main/java/io/airlift/airline/model/CommandMetadata.java +++ b/src/main/java/io/airlift/airline/model/CommandMetadata.java @@ -107,12 +107,6 @@ public String toString() public static Function nameGetter() { - return new Function() - { - public String apply(CommandMetadata input) - { - return input.getName(); - } - }; + return input -> input.getName(); } } diff --git a/src/main/java/io/airlift/airline/model/MetadataLoader.java b/src/main/java/io/airlift/airline/model/MetadataLoader.java index 89f7905a0..d98e7550c 100644 --- a/src/main/java/io/airlift/airline/model/MetadataLoader.java +++ b/src/main/java/io/airlift/airline/model/MetadataLoader.java @@ -64,13 +64,7 @@ public static CommandGroupMetadata loadCommandGroup(String name, String descript public static ImmutableList loadCommands(Iterable> defaultCommands) { - return ImmutableList.copyOf(Iterables.transform(defaultCommands, new Function, CommandMetadata>() - { - public CommandMetadata apply(Class commandType) - { - return loadCommand(commandType); - } - })); + return ImmutableList.copyOf(Iterables.transform(defaultCommands, commandType -> loadCommand(commandType))); } public static CommandMetadata loadCommand(Class commandType) @@ -210,14 +204,7 @@ private static List mergeOptionSet(List options) metadataIndex.put(option, option); } - options = ImmutableList.copyOf(transform(metadataIndex.asMap().values(), new Function, OptionMetadata>() - { - @Override - public OptionMetadata apply(@Nullable Collection options) - { - return new OptionMetadata(options); - } - })); + options = ImmutableList.copyOf(transform(metadataIndex.asMap().values(), optionsDatas -> new OptionMetadata(optionsDatas))); Map optionIndex = newHashMap(); for (OptionMetadata option : options) { diff --git a/src/main/java/io/airlift/airline/model/OptionMetadata.java b/src/main/java/io/airlift/airline/model/OptionMetadata.java index b4a1a24cf..e491e3281 100644 --- a/src/main/java/io/airlift/airline/model/OptionMetadata.java +++ b/src/main/java/io/airlift/airline/model/OptionMetadata.java @@ -221,24 +221,11 @@ public String toString() public static Function> optionsGetter() { - return new Function>() - { - public Set apply(OptionMetadata input) - { - return input.getOptions(); - } - }; + return input -> input.getOptions(); } public static Predicate isHiddenPredicate() { - return new Predicate() - { - @Override - public boolean apply(@Nullable OptionMetadata input) - { - return !input.isHidden(); - } - }; + return input -> !input.isHidden(); } } From fcf70c7a093b9538e9a171575cd30019f1d7c5ae Mon Sep 17 00:00:00 2001 From: alainlompo Date: Sat, 4 Nov 2017 10:18:57 +0100 Subject: [PATCH 2/2] Using diamond operator --- src/main/java/io/airlift/airline/Accessor.java | 4 ++-- src/main/java/io/airlift/airline/Cli.java | 6 +++--- src/main/java/io/airlift/airline/CommandUsage.java | 2 +- src/main/java/io/airlift/airline/SingleCommand.java | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/airlift/airline/Accessor.java b/src/main/java/io/airlift/airline/Accessor.java index e8256904b..3a214a9db 100644 --- a/src/main/java/io/airlift/airline/Accessor.java +++ b/src/main/java/io/airlift/airline/Accessor.java @@ -146,10 +146,10 @@ public String toString() private static Collection newCollection(Class type) { if (Collection.class.equals(type) || List.class.equals(type)) { - return new ArrayList(); + return new ArrayList<>(); } if (Set.class.equals(type)) { - return new HashSet(); + return new HashSet<>(); } if (SortedSet.class.equals(type)) { return new TreeSet(); diff --git a/src/main/java/io/airlift/airline/Cli.java b/src/main/java/io/airlift/airline/Cli.java index 23449f093..24d4a80d4 100644 --- a/src/main/java/io/airlift/airline/Cli.java +++ b/src/main/java/io/airlift/airline/Cli.java @@ -43,7 +43,7 @@ public class Cli public static CliBuilder builder(String name) { Preconditions.checkNotNull(name, "name is null"); - return new CliBuilder(name); + return new CliBuilder<>(name); } @Deprecated @@ -281,14 +281,14 @@ public GroupBuilder withGroup(String name) return groups.get(name); } - GroupBuilder group = new GroupBuilder(name); + GroupBuilder group = new GroupBuilder<>(name); groups.put(name, group); return group; } public Cli build() { - return new Cli(name, description, typeConverter, defaultCommand, defaultCommandGroupCommands, groups.values()); + return new Cli<>(name, description, typeConverter, defaultCommand, defaultCommandGroupCommands, groups.values()); } } diff --git a/src/main/java/io/airlift/airline/CommandUsage.java b/src/main/java/io/airlift/airline/CommandUsage.java index 3ccb340be..7da7063ca 100644 --- a/src/main/java/io/airlift/airline/CommandUsage.java +++ b/src/main/java/io/airlift/airline/CommandUsage.java @@ -147,7 +147,7 @@ public void usage(@Nullable String programName, @Nullable String groupName, Stri private List sortOptions(List options) { if (optionComparator != null) { - options = new ArrayList(options); + options = new ArrayList<>(options); Collections.sort(options, optionComparator); } return options; diff --git a/src/main/java/io/airlift/airline/SingleCommand.java b/src/main/java/io/airlift/airline/SingleCommand.java index 9a9ca4c11..a1690144d 100644 --- a/src/main/java/io/airlift/airline/SingleCommand.java +++ b/src/main/java/io/airlift/airline/SingleCommand.java @@ -34,7 +34,7 @@ public class SingleCommand { public static SingleCommand singleCommand(Class command) { - return new SingleCommand(command); + return new SingleCommand<>(command); } private final CommandMetadata commandMetadata;