Coverage Report - com.lexicalscope.jewel.cli.AbstractOptionAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractOptionAdapter
100%
19/19
85%
17/20
2.429
 
 1  
 package com.lexicalscope.jewel.cli;
 2  
 
 3  
 import static com.lexicalscope.fluentreflection.FluentReflection.type;
 4  
 import static com.lexicalscope.fluentreflection.ReflectionMatchers.*;
 5  
 
 6  
 import java.util.Collection;
 7  
 import java.util.List;
 8  
 import java.util.Set;
 9  
 
 10  
 import com.lexicalscope.fluentreflection.FluentClass;
 11  
 import com.lexicalscope.fluentreflection.FluentMethod;
 12  
 
 13  
 /*
 14  
  * Copyright 2011 Tim Wood
 15  
  *
 16  
  * Licensed under the Apache License, Version 2.0 (the "License");
 17  
  * you may not use this file except in compliance with the License.
 18  
  * You may obtain a copy of the License at
 19  
  *
 20  
  * http://www.apache.org/licenses/LICENSE-2.0
 21  
  *
 22  
  * Unless required by applicable law or agreed to in writing, software
 23  
  * distributed under the License is distributed on an "AS IS" BASIS,
 24  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 25  
  * See the License for the specific language governing permissions and
 26  
  * limitations under the License.
 27  
  */
 28  
 
 29  
 abstract class AbstractOptionAdapter implements OptionAdapter {
 30  
     private final FluentClass<?> klass;
 31  
     protected final FluentMethod method;
 32  
     private final FluentClass<?> methodType;
 33  
 
 34  
     AbstractOptionAdapter(
 35  
             final FluentClass<?> klass,
 36  574
             final FluentMethod method) {
 37  574
         this.klass = klass;
 38  574
         this.method = method;
 39  574
         if (isMutator().matches(method))
 40  
         {
 41  50
             this.methodType = method.args().get(0);
 42  
         }
 43  
         else
 44  
         {
 45  524
             this.methodType = method.type();
 46  
         }
 47  574
     }
 48  
 
 49  
     @Override public final FluentMethod method() {
 50  2166
         return method;
 51  
     }
 52  
 
 53  
     @Override public final FluentMethod correspondingOptionalityMethod() {
 54  1596
         if (isMutator().matches(method))
 55  
         {
 56  176
             return null;
 57  
         }
 58  
 
 59  1420
         final List<FluentMethod> methods =
 60  
                 klass.methods(
 61  
                         hasName(addPrefix("is", method.property())).and(isExistence()));
 62  1420
         if (!methods.isEmpty()) {
 63  408
             return methods.get(0);
 64  
         }
 65  1012
         return null;
 66  
     }
 67  
 
 68  
     private String addPrefix(final String prefix, final String name) {
 69  1420
         return prefix + name.substring(0, 1).toUpperCase() + name.substring(1);
 70  
     }
 71  
 
 72  
     @Override public final boolean isMultiValued() {
 73  4026
         return methodType.isType(reflectingOn(Collection.class)) && (methodType.assignableFrom(List.class) || methodType.assignableFrom(Set.class));
 74  
     }
 75  
 
 76  
     @Override public final FluentClass<? extends Object> getValueType() {
 77  2890
         final FluentClass<? extends Object> valueType =
 78  
                 isMultiValued()
 79  
                         ? methodType.asType(reflectingOn(Collection.class)).typeArgument(0)
 80  
                         : methodType;
 81  
 
 82  2890
         return reflectingOn(Object.class).matches(valueType)
 83  
                 ? type(String.class)
 84  
                 : valueType;
 85  
     }
 86  
 
 87  
     @Override public final boolean hasDefaultValue() {
 88  574
         return !(defaultValue().length == 1 && defaultValue()[0].equals(Option.stringToMarkNoDefault));
 89  
     }
 90  
 }