Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing squid:S1604 #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions src/main/java/io/airlift/airline/Accessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,7 @@ public Accessor(Iterable<Field> 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<Field, String>()
{
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());
Expand Down Expand Up @@ -152,10 +146,10 @@ public String toString()
private static Collection<Object> newCollection(Class<?> type)
{
if (Collection.class.equals(type) || List.class.equals(type)) {
return new ArrayList<Object>();
return new ArrayList<>();
}
if (Set.class.equals(type)) {
return new HashSet<Object>();
return new HashSet<>();
}
if (SortedSet.class.equals(type)) {
return new TreeSet();
Expand Down
17 changes: 6 additions & 11 deletions src/main/java/io/airlift/airline/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class Cli<C>
public static <T> CliBuilder<T> builder(String name)
{
Preconditions.checkNotNull(name, "name is null");
return new CliBuilder<T>(name);
return new CliBuilder<>(name);
}

@Deprecated
Expand Down Expand Up @@ -77,14 +77,9 @@ private Cli(String name,

List<CommandMetadata> defaultCommandGroup = MetadataLoader.loadCommands(defaultGroupCommands);

List<CommandGroupMetadata> commandGroups = ImmutableList.copyOf(Iterables.transform(groups, new Function<GroupBuilder<C>, CommandGroupMetadata>()
{
@Override
public CommandGroupMetadata apply(GroupBuilder<C> group)
{
return MetadataLoader.loadCommandGroup(group.name, group.description, MetadataLoader.loadCommand(group.defaultCommand), MetadataLoader.loadCommands(group.commands));
}
}));
List<CommandGroupMetadata> 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);
}
Expand Down Expand Up @@ -286,14 +281,14 @@ public GroupBuilder<C> withGroup(String name)
return groups.get(name);
}

GroupBuilder<C> group = new GroupBuilder<C>(name);
GroupBuilder<C> group = new GroupBuilder<>(name);
groups.put(name, group);
return group;
}

public Cli<C> build()
{
return new Cli<C>(name, description, typeConverter, defaultCommand, defaultCommandGroupCommands, groups.values());
return new Cli<>(name, description, typeConverter, defaultCommand, defaultCommandGroupCommands, groups.values());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/airlift/airline/CommandUsage.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void usage(@Nullable String programName, @Nullable String groupName, Stri
private List<OptionMetadata> sortOptions(List<OptionMetadata> options)
{
if (optionComparator != null) {
options = new ArrayList<OptionMetadata>(options);
options = new ArrayList<>(options);
Collections.sort(options, optionComparator);
}
return options;
Expand Down
21 changes: 4 additions & 17 deletions src/main/java/io/airlift/airline/GlobalUsageSummary.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,8 @@ public void usage(GlobalMetadata global, UsagePrinter out)

// build arguments
List<String> commandArguments = newArrayList();
commandArguments.addAll(Collections2.transform(global.getOptions(), new Function<OptionMetadata, String>()
{
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())
Expand All @@ -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<Entry<String, String>, Iterable<String>>()
{
public Iterable<String> apply(Entry<String, String> 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 <command>' for more information on a specific command.").newline();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/airlift/airline/SingleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class SingleCommand<C>
{
public static <C> SingleCommand<C> singleCommand(Class<C> command)
{
return new SingleCommand<C>(command);
return new SingleCommand<>(command);
}

private final CommandMetadata commandMetadata;
Expand Down
102 changes: 27 additions & 75 deletions src/main/java/io/airlift/airline/UsageHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,67 +21,42 @@

public class UsageHelper
{
public static final Comparator<OptionMetadata> DEFAULT_OPTION_COMPARATOR = new Comparator<OptionMetadata>()
{
@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<CommandMetadata> DEFAULT_COMMAND_COMPARATOR = new Comparator<CommandMetadata>()
{
@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<OptionMetadata> 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<CommandMetadata> 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<String> options = option.getOptions();
StringBuilder stringBuilder = new StringBuilder();

final String argumentString;
if (option.getArity() > 0) {
argumentString = Joiner.on(" ").join(Lists.transform(ImmutableList.of(option.getTitle()), new Function<String, String>()
{
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<String, String>()
{
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();
}

Expand Down Expand Up @@ -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<String, String>()
{
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<String, String>()
{
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(')');
Expand Down Expand Up @@ -174,12 +132,6 @@ public static String toUsage(ArgumentsMetadata arguments)

public static List<String> toSynopsisUsage(List<OptionMetadata> options)
{
return ImmutableList.copyOf(transform(filter(options, isHiddenPredicate()), new Function<OptionMetadata, String>()
{
public String apply(OptionMetadata option)
{
return toUsage(option);
}
}));
return ImmutableList.copyOf(transform(filter(options, isHiddenPredicate()), option -> toUsage(option)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ public String toString()

public static Function<CommandGroupMetadata, String> nameGetter()
{
return new Function<CommandGroupMetadata, String>()
{
public String apply(CommandGroupMetadata input)
{
return input.getName();
}
};
return input -> input.getName();
}
}
8 changes: 1 addition & 7 deletions src/main/java/io/airlift/airline/model/CommandMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,6 @@ public String toString()

public static Function<CommandMetadata, String> nameGetter()
{
return new Function<CommandMetadata, String>()
{
public String apply(CommandMetadata input)
{
return input.getName();
}
};
return input -> input.getName();
}
}
17 changes: 2 additions & 15 deletions src/main/java/io/airlift/airline/model/MetadataLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,7 @@ public static CommandGroupMetadata loadCommandGroup(String name, String descript

public static <T> ImmutableList<CommandMetadata> loadCommands(Iterable<Class<? extends T>> defaultCommands)
{
return ImmutableList.copyOf(Iterables.transform(defaultCommands, new Function<Class<?>, CommandMetadata>()
{
public CommandMetadata apply(Class<?> commandType)
{
return loadCommand(commandType);
}
}));
return ImmutableList.copyOf(Iterables.transform(defaultCommands, commandType -> loadCommand(commandType)));
}

public static CommandMetadata loadCommand(Class<?> commandType)
Expand Down Expand Up @@ -210,14 +204,7 @@ private static List<OptionMetadata> mergeOptionSet(List<OptionMetadata> options)
metadataIndex.put(option, option);
}

options = ImmutableList.copyOf(transform(metadataIndex.asMap().values(), new Function<Collection<OptionMetadata>, OptionMetadata>()
{
@Override
public OptionMetadata apply(@Nullable Collection<OptionMetadata> options)
{
return new OptionMetadata(options);
}
}));
options = ImmutableList.copyOf(transform(metadataIndex.asMap().values(), optionsDatas -> new OptionMetadata(optionsDatas)));

Map<String, OptionMetadata> optionIndex = newHashMap();
for (OptionMetadata option : options) {
Expand Down
17 changes: 2 additions & 15 deletions src/main/java/io/airlift/airline/model/OptionMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,24 +221,11 @@ public String toString()

public static Function<OptionMetadata, Set<String>> optionsGetter()
{
return new Function<OptionMetadata, Set<String>>()
{
public Set<String> apply(OptionMetadata input)
{
return input.getOptions();
}
};
return input -> input.getOptions();
}

public static Predicate<OptionMetadata> isHiddenPredicate()
{
return new Predicate<OptionMetadata>()
{
@Override
public boolean apply(@Nullable OptionMetadata input)
{
return !input.isHidden();
}
};
return input -> !input.isHidden();
}
}