Coverage Report - com.lexicalscope.jewel.cli.ArgumentPresenterImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ArgumentPresenterImpl
100%
33/33
90%
20/22
3.75
 
 1  
 package com.lexicalscope.jewel.cli;
 2  
 
 3  
 import static com.lexicalscope.jewel.cli.ConvertTypeOfObject.converterTo;
 4  
 
 5  
 import java.util.LinkedHashMap;
 6  
 import java.util.List;
 7  
 import java.util.Map;
 8  
 
 9  
 import com.lexicalscope.jewel.cli.specification.OptionSpecification;
 10  
 import com.lexicalscope.jewel.cli.specification.OptionsSpecification;
 11  
 import com.lexicalscope.jewel.cli.specification.ParsedOptionSpecification;
 12  
 import com.lexicalscope.jewel.cli.specification.UnparsedOptionSpecification;
 13  
 import com.lexicalscope.jewel.cli.validation.Argument;
 14  
 import com.lexicalscope.jewel.cli.validation.OptionCollection;
 15  
 
 16  
 
 17  
 class ArgumentPresenterImpl<O> implements ArgumentPresenter<O> {
 18  
     private final OptionsSpecification<O> specification;
 19  
     private final ArgumentPresentingStrategy<O> argumentPresentingStrategy;
 20  
 
 21  
     public ArgumentPresenterImpl(
 22  
             final OptionsSpecification<O> specification,
 23  204
             final ArgumentPresentingStrategy<O> argumentPresentingStrategy) {
 24  204
         this.specification = specification;
 25  204
         this.argumentPresentingStrategy = argumentPresentingStrategy;
 26  204
     }
 27  
 
 28  
     @Option public O presentArguments(final OptionCollection validatedArguments) throws ArgumentValidationException {
 29  146
         final Map<String, Object> argumentMap = new LinkedHashMap<String, Object>();
 30  
 
 31  146
         final ValidationErrorBuilder validationErrorBuilder = new ValidationErrorBuilderImpl();
 32  
 
 33  146
         for (final ParsedOptionSpecification optionSpecification : specification) {
 34  274
             final ConvertTypeOfObject<?> convertTypeOfObject =
 35  
                     converterTo(validationErrorBuilder, optionSpecification, optionSpecification.getMethod());
 36  274
             putDefaultInMap(argumentMap, optionSpecification, convertTypeOfObject);
 37  
 
 38  274
             final Argument argument = validatedArguments.getArgument(optionSpecification);
 39  274
             if (argument != null) {
 40  162
                 putValuesInMap(argumentMap, optionSpecification, convertTypeOfObject, argument.getValues());
 41  
             }
 42  274
         }
 43  
 
 44  146
         if (specification.hasUnparsedSpecification()) {
 45  32
             final UnparsedOptionSpecification unparsedSpecification = specification.getUnparsedSpecification();
 46  
 
 47  32
             final ConvertTypeOfObject<?> convertTypeOfObject =
 48  
                     converterTo(
 49  
                             validationErrorBuilder,
 50  
                             unparsedSpecification,
 51  
                             unparsedSpecification.getMethod());
 52  
 
 53  32
             putDefaultInMap(argumentMap, unparsedSpecification, convertTypeOfObject);
 54  32
             if (!validatedArguments.getUnparsed().isEmpty()) {
 55  16
                 putValuesInMap(
 56  
                         argumentMap,
 57  
                         unparsedSpecification,
 58  
                         convertTypeOfObject,
 59  
                         validatedArguments.getUnparsed());
 60  
             }
 61  
         }
 62  146
         validationErrorBuilder.validate();
 63  138
         return argumentPresentingStrategy.presentArguments(argumentMap);
 64  
     }
 65  
 
 66  
     private void putValuesInMap(
 67  
             final Map<String, Object> argumentMap,
 68  
             final OptionSpecification specification,
 69  
             final ConvertTypeOfObject<?> convertTypeOfObject,
 70  
             final List<String> values) {
 71  178
         if (specification.isMultiValued())
 72  
         {
 73  32
             argumentMap.put(
 74  
                     specification.getCanonicalIdentifier(),
 75  
                     convertTypeOfObject.convert(values));
 76  
         }
 77  146
         else if (!values.isEmpty())
 78  
         {
 79  102
             argumentMap.put(
 80  
                     specification.getCanonicalIdentifier(),
 81  
                     convertTypeOfObject.convert(values.get(0)));
 82  44
         } else if (values.isEmpty()) {
 83  44
             argumentMap.put(specification.getCanonicalIdentifier(), null);
 84  
         }
 85  178
     }
 86  
 
 87  
     private void putDefaultInMap(
 88  
             final Map<String, Object> argumentMap,
 89  
             final OptionSpecification optionSpecification,
 90  
             final ConvertTypeOfObject<?> convertTypeOfObject) {
 91  306
         if (optionSpecification.isMultiValued() && optionSpecification.hasDefaultValue())
 92  
         {
 93  44
             argumentMap.put(
 94  
                     optionSpecification.getCanonicalIdentifier(),
 95  
                     convertTypeOfObject.convert(optionSpecification.getDefaultValue()));
 96  
         }
 97  262
         else if (optionSpecification.hasDefaultValue() && optionSpecification.getDefaultValue().size() > 0)
 98  
         {
 99  12
             argumentMap.put(
 100  
                     optionSpecification.getCanonicalIdentifier(),
 101  
                     convertTypeOfObject.convert(optionSpecification.getDefaultValue().get(0)));
 102  
         }
 103  306
     }
 104  
 }