All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.airlift.command.ParserUtil Maven / Gradle / Ivy

Go to download

Airline is a Java annotation-based framework for parsing Git like command line structures.

The newest version!
package io.airlift.command;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ListMultimap;
import io.airlift.command.model.ArgumentsMetadata;
import io.airlift.command.model.OptionMetadata;

import java.util.List;
import java.util.Map;

import static com.google.common.collect.Iterables.concat;

public class ParserUtil
{
    public static  T createInstance(Class type)
    {
        if (type != null) {
            try {
                return type.getConstructor().newInstance();
            }
            catch (Exception e) {
                throw new ParseException(e, "Unable to create instance %s", type.getName());
            }
        }
        return null;
    }

    public static  T createInstance(Class type,
        Iterable options,
        ListMultimap parsedOptions,
        ArgumentsMetadata arguments,
        Iterable parsedArguments,
        Iterable metadataInjection,
        Map, Object> bindings)
    {
        return createInstance(type, options, parsedOptions, arguments, parsedArguments, metadataInjection, bindings, new CommandFactoryDefault());
    }
    
    
    public static  T injectOptions(T commandInstance,
        Iterable options,
        ListMultimap parsedOptions,
        ArgumentsMetadata arguments,
        Iterable parsedArguments,
        Iterable metadataInjection,
        Map, Object> bindings)
    {      
        // inject options
        for (OptionMetadata option : options) {
            List values = parsedOptions.get(option);
            if (option.getArity() > 1 && !values.isEmpty()) {
                // hack: flatten the collection
                values = ImmutableList.copyOf(concat((Iterable>) values));
            }
            if (values != null && !values.isEmpty()) {
                for (Accessor accessor : option.getAccessors()) {
                    accessor.addValues(commandInstance, values);
                }
            }
        }
  
        // inject args
        if (arguments != null && parsedArguments != null) {
            for (Accessor accessor : arguments.getAccessors()) {
                accessor.addValues(commandInstance, parsedArguments);
            }
        }
  
        for (Accessor accessor : metadataInjection) {
            Object injectee = bindings.get(accessor.getJavaType());
  
            if (injectee != null) {
                accessor.addValues(commandInstance, ImmutableList.of(injectee));
            }
        }
  
        return commandInstance;
    }
    
    
    public static  T createInstance(Class type,
            Iterable options,
            ListMultimap parsedOptions,
            ArgumentsMetadata arguments,
            Iterable parsedArguments,
            Iterable metadataInjection,
            Map, Object> bindings,
            CommandFactory commandFactory)
    {
        // create the command instance
        T commandInstance = (T) commandFactory.createInstance(type);

        return injectOptions(commandInstance, options, parsedOptions, arguments, parsedArguments, metadataInjection, bindings);        
    }
}