View Javadoc

1   /*
2    * Copyright 2006 Tim Wood
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package com.lexicalscope.jewel.cli.validation;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  import java.util.Map;
20  
21  import com.lexicalscope.jewel.cli.specification.OptionsSpecification;
22  import com.lexicalscope.jewel.cli.specification.ParsedOptionSpecification;
23  
24  
25  class OptionCollectionImpl implements OptionCollection
26  {
27      private final OptionsSpecification<?> specification;
28      private final Map<ParsedOptionSpecification, List<String>> arguments;
29      private final List<String> unparsed;
30  
31      public OptionCollectionImpl(
32              final OptionsSpecification<?> specification,
33              final Map<ParsedOptionSpecification,
34              List<String>> arguments,
35              final List<String> unparsed)
36      {
37          this.specification = specification;
38          this.arguments = arguments;
39          this.unparsed = unparsed;
40      }
41  
42      @Override public List<String> getUnparsed()
43      {
44          return new ArrayList<String>(unparsed);
45      }
46  
47      @Override public Argument getArgument(final ParsedOptionSpecification option) {
48          if (arguments.containsKey(option)) {
49              return new ArgumentImpl(option, arguments.get(option));
50          }
51          return null;
52      }
53  
54      @Override public List<String> getValues(final String... options)
55      {
56          for (final String option : options)
57          {
58              final ParsedOptionSpecification optionSpecification = specification.getSpecification(option);
59              if (arguments.containsKey(optionSpecification))
60              {
61                  return arguments.get(optionSpecification);
62              }
63          }
64          return null;
65      }
66  }