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

Introduction of Java 8, dependency to guava resolved (issue #40) #43

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
dd16cd8
issue 40 (Dependency on guava): Some usages of guava reduced (still c…
Novanic Aug 29, 2015
03e793a
issue 40 (Dependency on guava): Constructs of guava replaced by Java …
Novanic Aug 29, 2015
8c6cc8b
issue 40 (Dependency on guava): Outdated ToDo comment removed (there …
Novanic Aug 29, 2015
eda0582
PeekingIterator simplified and reduced to the required logic (iterati…
Novanic Sep 1, 2015
ed475f9
Bug-fix: A NPE occurred when there is an optional option with arity > 1.
Novanic Sep 1, 2015
b642fb3
New tests for global arguments/options added
Novanic Sep 1, 2015
114d264
Refactoring: Reduced the methods where the state is modified. This re…
Novanic Sep 1, 2015
6d23adc
Refactoring: Reduced the methods where the state is modified. This re…
Novanic Sep 1, 2015
69d6088
Refactoring: Simplified iterator handling and reduced the usage of on…
Novanic Sep 1, 2015
96fa85c
Refactoring: Simplified iterator handling
Novanic Sep 1, 2015
a831471
Refactoring: Simplified iterator handling and reduced the usage of on…
Novanic Sep 1, 2015
8485c72
Refactoring: Simplified iterator handling and reduced the usage of on…
Novanic Sep 1, 2015
ba1e6c2
Refactoring: Simplified iterator handling and reduced the usage of on…
Novanic Sep 1, 2015
81d607d
Refactoring: PeekingIterator/TokenIterator resolved. There is no need…
Novanic Sep 2, 2015
c7275e5
Refactoring: PeekingIterator/TokenIterator resolved. There is no need…
Novanic Sep 2, 2015
e4aedff
Refactoring: Code optimization
Novanic Sep 2, 2015
b85e055
Refactoring: Code optimization
Novanic Sep 2, 2015
a561992
Refactoring: Code optimization
Novanic Sep 2, 2015
875556a
Refactoring: Code optimization
Novanic Sep 2, 2015
113d1bc
Refactoring: ToDo comment changed
Novanic Sep 2, 2015
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
24 changes: 19 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,30 @@
<artifactId>annotations</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

<!-- for testing -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- //TODO the Jacoco version of the parent project isn't compatible with Java 8 -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
</plugin>
</plugins>
</build>
</project>
39 changes: 19 additions & 20 deletions src/main/java/io/airlift/airline/Accessor.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package io.airlift.airline;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import io.airlift.airline.util.ArgumentChecker;
import io.airlift.airline.util.CollectionUtils;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;

public class Accessor
{
Expand All @@ -27,23 +26,17 @@ public class Accessor

public Accessor(Field... path)
{
this(ImmutableList.copyOf(path));
this(Arrays.asList(path));
}

public Accessor(Iterable<Field> path)
{
Preconditions.checkNotNull(path, "path is null");
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();
}
}));
ArgumentChecker.checkNotNull(path, "path is null");
ArgumentChecker.checkCondition(path.iterator().hasNext(), "path is empty");

this.path = CollectionUtils.asList(path);

this.name = this.path.get(0).getDeclaringClass().getSimpleName() + '.' + this.path.stream().map(Field::getName).collect(Collectors.joining("."));

Field field = this.path.get(this.path.size() - 1);
multiValued = Collection.class.isAssignableFrom(field.getType());
Expand Down Expand Up @@ -91,7 +84,7 @@ public Object getValue(Object instance)

public void addValues(Object commandInstance, Iterable<?> values)
{
if (Iterables.isEmpty(values)) {
if (!values.iterator().hasNext()) {
return;
}

Expand All @@ -100,13 +93,19 @@ public void addValues(Object commandInstance, Iterable<?> values)

Field field = path.get(path.size() - 1);
field.setAccessible(true);

final List<?> valueList = CollectionUtils.asList(values);
if (Collection.class.isAssignableFrom(field.getType())) {
Collection<Object> collection = getOrCreateCollectionField(name, instance, field);
Iterables.addAll(collection, values);
collection.addAll(valueList);
}
else {
try {
field.set(instance, Iterables.getLast(values));
if(valueList.isEmpty()) {
field.set(instance, null);
} else {
field.set(instance, valueList.get(valueList.size() - 1));
}
}
catch (Exception e) {
throw new ParseException(e, "Error setting %s for argument %s", field.getName(), name);
Expand Down
89 changes: 35 additions & 54 deletions src/main/java/io/airlift/airline/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,29 @@

package io.airlift.airline;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import io.airlift.airline.model.ArgumentsMetadata;
import io.airlift.airline.model.CommandGroupMetadata;
import io.airlift.airline.model.CommandMetadata;
import io.airlift.airline.model.GlobalMetadata;
import io.airlift.airline.model.MetadataLoader;
import io.airlift.airline.model.OptionMetadata;
import io.airlift.airline.util.ArgumentChecker;
import io.airlift.airline.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.newHashMap;
import static io.airlift.airline.ParserUtil.createInstance;

public class Cli<C>
{
public static <T> CliBuilder<T> builder(String name)
{
Preconditions.checkNotNull(name, "name is null");
ArgumentChecker.checkNotNull(name, "name is null");
return new CliBuilder<T>(name);
}

Expand All @@ -66,8 +65,8 @@ private Cli(String name,
Iterable<Class<? extends C>> defaultGroupCommands,
Iterable<GroupBuilder<C>> groups)
{
Preconditions.checkNotNull(name, "name is null");
Preconditions.checkNotNull(typeConverter, "typeConverter is null");
ArgumentChecker.checkNotNull(name, "name is null");
ArgumentChecker.checkNotNull(typeConverter, "typeConverter is null");

CommandMetadata defaultCommandMetadata = null;
if (defaultCommand != null) {
Expand All @@ -76,13 +75,9 @@ private Cli(String name,

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

List<CommandGroupMetadata> commandGroups = ImmutableList.copyOf(Iterables.transform(groups, new Function<GroupBuilder<C>, CommandGroupMetadata>()
{
public CommandGroupMetadata apply(GroupBuilder<C> group)
{
return MetadataLoader.loadCommandGroup(group.name, group.description, MetadataLoader.loadCommand(group.defaultCommand), MetadataLoader.loadCommands(group.commands));
}
}));
List<CommandGroupMetadata> commandGroups = CollectionUtils.asList(groups).stream()
.map(group -> MetadataLoader.loadCommandGroup(group.name, group.description, MetadataLoader.loadCommand(group.defaultCommand), MetadataLoader.loadCommands(group.commands)))
.collect(Collectors.toList());

this.metadata = MetadataLoader.loadGlobal(name, description, defaultCommandMetadata, defaultCommandGroup, commandGroups);
}
Expand All @@ -94,12 +89,12 @@ public GlobalMetadata getMetadata()

public C parse(String... args)
{
return parse(ImmutableList.copyOf(args));
return parse(Arrays.asList(args));
}

public C parse(Iterable<String> args)
{
Preconditions.checkNotNull(args, "args is null");
ArgumentChecker.checkNotNull(args, "args is null");

Parser parser = new Parser();
ParseState state = parser.parse(metadata, args);
Expand All @@ -123,7 +118,7 @@ public C parse(Iterable<String> args)
command.getArguments(),
state.getParsedArguments(),
command.getMetadataInjections(),
ImmutableMap.<Class<?>, Object>of(GlobalMetadata.class, metadata));
CollectionUtils.<Class<?>, Object>asMap(GlobalMetadata.class, metadata));
}

private void validate(ParseState state)
Expand Down Expand Up @@ -170,38 +165,24 @@ public static class CliBuilder<C>
protected TypeConverter typeConverter = new TypeConverter();
protected String optionSeparators;
private Class<? extends C> defaultCommand;
private final List<Class<? extends C>> defaultCommandGroupCommands = newArrayList();
protected final Map<String, GroupBuilder<C>> groups = newHashMap();
private final List<Class<? extends C>> defaultCommandGroupCommands = new ArrayList<>();
protected final Map<String, GroupBuilder<C>> groups = new HashMap<>();

public CliBuilder(String name)
{
Preconditions.checkNotNull(name, "name is null");
Preconditions.checkArgument(!name.isEmpty(), "name is empty");
ArgumentChecker.checkNotNull(name, "name is null");
ArgumentChecker.checkCondition(!name.isEmpty(), "name is empty");
this.name = name;
}

public CliBuilder<C> withDescription(String description)
{
Preconditions.checkNotNull(description, "description is null");
Preconditions.checkArgument(!description.isEmpty(), "description is empty");
ArgumentChecker.checkNotNull(description, "description is null");
ArgumentChecker.checkCondition(!description.isEmpty(), "description is empty");
this.description = description;
return this;
}

// public CliBuilder<C> withTypeConverter(TypeConverter typeConverter)
// {
// Preconditions.checkNotNull(typeConverter, "typeConverter is null");
// this.typeConverter = typeConverter;
// return this;
// }

// public CliBuilder<C> withOptionSeparators(String optionsSeparator)
// {
// Preconditions.checkNotNull(optionsSeparator, "optionsSeparator is null");
// this.optionSeparators = optionsSeparator;
// return this;
// }

public CliBuilder<C> withDefaultCommand(Class<? extends C> defaultCommand)
{
this.defaultCommand = defaultCommand;
Expand All @@ -217,20 +198,20 @@ public CliBuilder<C> withCommand(Class<? extends C> command)
public CliBuilder<C> withCommands(Class<? extends C> command, Class<? extends C>... moreCommands)
{
this.defaultCommandGroupCommands.add(command);
this.defaultCommandGroupCommands.addAll(ImmutableList.copyOf(moreCommands));
this.defaultCommandGroupCommands.addAll(Arrays.asList(moreCommands));
return this;
}

public CliBuilder<C> withCommands(Iterable<Class<? extends C>> commands)
{
this.defaultCommandGroupCommands.addAll(ImmutableList.copyOf(commands));
this.defaultCommandGroupCommands.addAll(CollectionUtils.asList(commands));
return this;
}

public GroupBuilder<C> withGroup(String name)
{
Preconditions.checkNotNull(name, "name is null");
Preconditions.checkArgument(!name.isEmpty(), "name is empty");
ArgumentChecker.checkNotNull(name, "name is null");
ArgumentChecker.checkCondition(!name.isEmpty(), "name is empty");

if (groups.containsKey(name)) {
return groups.get(name);
Expand All @@ -253,48 +234,48 @@ public static class GroupBuilder<C>
private String description = null;
private Class<? extends C> defaultCommand = null;

private final List<Class<? extends C>> commands = newArrayList();
private final List<Class<? extends C>> commands = new ArrayList<>();

private GroupBuilder(String name)
{
Preconditions.checkNotNull(name, "name is null");
ArgumentChecker.checkNotNull(name, "name is null");
this.name = name;
}

public GroupBuilder<C> withDescription(String description)
{
Preconditions.checkNotNull(description, "description is null");
Preconditions.checkArgument(!description.isEmpty(), "description is empty");
Preconditions.checkState(this.description == null, "description is already set");
ArgumentChecker.checkNotNull(description, "description is null");
ArgumentChecker.checkCondition(!description.isEmpty(), "description is empty");
ArgumentChecker.checkCondition(this.description == null, "description is already set");
this.description = description;
return this;
}

public GroupBuilder<C> withDefaultCommand(Class<? extends C> defaultCommand)
{
Preconditions.checkNotNull(defaultCommand, "defaultCommand is null");
Preconditions.checkState(this.defaultCommand == null, "defaultCommand is already set");
ArgumentChecker.checkNotNull(defaultCommand, "defaultCommand is null");
ArgumentChecker.checkCondition(this.defaultCommand == null, "defaultCommand is already set");
this.defaultCommand = defaultCommand;
return this;
}

public GroupBuilder<C> withCommand(Class<? extends C> command)
{
Preconditions.checkNotNull(command, "command is null");
ArgumentChecker.checkNotNull(command, "command is null");
commands.add(command);
return this;
}

public GroupBuilder<C> withCommands(Class<? extends C> command, Class<? extends C>... moreCommands)
{
this.commands.add(command);
this.commands.addAll(ImmutableList.copyOf(moreCommands));
this.commands.addAll(Arrays.asList(moreCommands));
return this;
}

public GroupBuilder<C> withCommands(Iterable<Class<? extends C>> commands)
{
this.commands.addAll(ImmutableList.copyOf(commands));
this.commands.addAll(CollectionUtils.asList(commands));
return this;
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/io/airlift/airline/CommandGroupUsage.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package io.airlift.airline;

import com.google.common.base.Preconditions;
import io.airlift.airline.model.CommandGroupMetadata;
import io.airlift.airline.model.CommandMetadata;
import io.airlift.airline.model.GlobalMetadata;
import io.airlift.airline.model.OptionMetadata;
import io.airlift.airline.util.ArgumentChecker;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;
import static io.airlift.airline.UsageHelper.DEFAULT_COMMAND_COMPARATOR;
import static io.airlift.airline.UsageHelper.DEFAULT_OPTION_COMPARATOR;

Expand Down Expand Up @@ -39,7 +39,7 @@ public CommandGroupUsage(int columnSize, boolean hideGlobalOptions)

public CommandGroupUsage(int columnSize, boolean hideGlobalOptions, @Nullable Comparator<? super OptionMetadata> optionComparator)
{
Preconditions.checkArgument(columnSize > 0, "columnSize must be greater than 0");
ArgumentChecker.checkCondition(columnSize > 0, "columnSize must be greater than 0");
this.columnSize = columnSize;
this.hideGlobalOptions = hideGlobalOptions;
this.optionComparator = optionComparator;
Expand Down Expand Up @@ -84,7 +84,7 @@ public void usage(@Nullable GlobalMetadata global, CommandGroupMetadata group, U
out.append("SYNOPSIS").newline();
UsagePrinter synopsis = out.newIndentedPrinter(8).newPrinterWithHangingIndent(8);

List<CommandMetadata> commands = newArrayList(group.getCommands());
List<CommandMetadata> commands = new ArrayList<>(group.getCommands());
Collections.sort(commands, commandComparator);

if (group.getDefaultCommand() != null) {
Expand Down Expand Up @@ -114,7 +114,7 @@ public void usage(@Nullable GlobalMetadata global, CommandGroupMetadata group, U
//
// OPTIONS
//
List<OptionMetadata> options = newArrayList();
List<OptionMetadata> options = new ArrayList<>();
options.addAll(group.getOptions());
if (global != null && !hideGlobalOptions) {
options.addAll(global.getOptions());
Expand Down
Loading