View Javadoc

1   package org.cyclopsgroup.jcli.impl;
2   
3   import java.util.Arrays;
4   import java.util.List;
5   import java.util.Map;
6   
7   import org.cyclopsgroup.jcli.spi.Argument;
8   import org.cyclopsgroup.jcli.spi.Cli;
9   import org.cyclopsgroup.jcli.spi.Option;
10  import org.cyclopsgroup.jcli.spi.ParsingContext;
11  
12  class AnnotationParsingContext<T>
13      implements ParsingContext
14  {
15      private final AnnotationArgument argument;
16  
17      private final AnnotationCli cli;
18  
19      private final List<AnnotationOption> options;
20  
21      private final Map<String, Reference<T>> referenceMap;
22  
23      /**
24       * @param beanType Type of bean
25       * @param referenceMap Map of references
26       * @param options List options
27       * @param cli Command line model
28       * @param argument Argument definition
29       */
30      AnnotationParsingContext( Class<T> beanType, Map<String, Reference<T>> referenceMap,
31                                List<AnnotationOption> options, AnnotationCli cli, AnnotationArgument argument )
32      {
33          this.options = options;
34          this.referenceMap = referenceMap;
35          this.cli = cli;
36          this.argument = argument;
37      }
38  
39      /**
40       * @inheritDoc
41       */
42      @Override
43      public Argument argument()
44      {
45          return argument;
46      }
47  
48      /**
49       * @inheritDoc
50       */
51      @Override
52      public Cli cli()
53      {
54          return cli;
55      }
56  
57      /**
58       * Find reference with given name of option or argument
59       *
60       * @param name Name of option or argument
61       * @param isLongName True if name is a long name
62       * @return Reference that matches name or NULL
63       */
64      Reference<T> lookupReference( String name, boolean isLongName )
65      {
66          if ( isLongName )
67          {
68              for ( Reference<T> r : referenceMap.values() )
69              {
70                  if ( r.longName.equals( name ) )
71                  {
72                      return r;
73                  }
74              }
75              return null;
76          }
77          return referenceMap.get( name );
78      }
79  
80      /**
81       * @inheritDoc
82       */
83      @Override
84      public List<Option> options()
85      {
86          return Arrays.asList( options.toArray( new Option[0] ) );
87      }
88  
89      /**
90       * @inheritDoc
91       */
92      @Override
93      public Option optionWithLongName( String longName )
94      {
95          for ( Option o : options )
96          {
97              if ( o.getLongName().equals( longName ) )
98              {
99                  return o;
100             }
101         }
102         return null;
103     }
104 
105     /**
106      * @inheritDoc
107      */
108     @Override
109     public Option optionWithShortName( String shortName )
110     {
111         for ( Option o : options )
112         {
113             if ( o.getName().equals( shortName ) )
114             {
115                 return o;
116             }
117         }
118         return null;
119     }
120 }