1 /*--------------------------------------------------------------------------
\r
2 * Copyright 2008 Taro L. Saito
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
15 *--------------------------------------------------------------------------*/
\r
16 //--------------------------------------
\r
19 // ArgumentItem.java
\r
20 // Since: 2008/10/27 23:49:28
\r
24 //--------------------------------------
\r
25 package org.xerial.util.opt;
\r
27 import java.lang.reflect.Field;
\r
28 import java.lang.reflect.Method;
\r
30 import org.xerial.util.Range;
\r
31 import org.xerial.util.bean.TypeInfo;
\r
39 public class ArgumentItem
\r
41 private final Argument argumentDescriptor;
\r
42 private final OptionSetter argumentSetter;
\r
44 public ArgumentItem(Method setter)
\r
46 argumentDescriptor = setter.getAnnotation(Argument.class);
\r
47 if (argumentDescriptor == null)
\r
48 throw new IllegalArgumentException(setter + " is not for an option argument");
\r
50 argumentSetter = new OptionSetterViaMethod(setter);
\r
53 public ArgumentItem(Field field)
\r
55 argumentDescriptor = field.getAnnotation(Argument.class);
\r
56 if (argumentDescriptor == null)
\r
57 throw new IllegalArgumentException(field + " is not for an option argument");
\r
59 argumentSetter = new OptionSetterViaField(field);
\r
63 public boolean equals(Object obj)
\r
65 if (obj instanceof ArgumentItem)
\r
67 ArgumentItem other = (ArgumentItem) obj;
\r
68 return argumentDescriptor.equals(other.argumentDescriptor);
\r
75 public int hashCode()
\r
77 return argumentDescriptor.hashCode();
\r
80 public boolean takesMultipleArguments()
\r
82 return TypeInfo.isCollection(argumentSetter.getOptionDataType());
\r
85 public static Range getRangeOf(ArgumentItem arg)
\r
87 int start = arg.getArgumentDescriptor().index();
\r
88 int end = arg.takesMultipleArguments() ? Integer.MAX_VALUE : start;
\r
89 return new Range(start, end);
\r
92 public Range getRange()
\r
94 return getRangeOf(this);
\r
97 public void set(Object bean, Object value) throws OptionParserException
\r
99 argumentSetter.setOption(bean, value);
\r
102 public Argument getArgumentDescriptor()
\r
104 return argumentDescriptor;
\r
107 public void initialize(Object optionHolder) throws OptionParserException
\r
109 argumentSetter.initialize(optionHolder);
\r
113 public String toString()
\r
115 return String.format("name=%s, index=%s, required=%s", argumentDescriptor.name(), getRange().toString(),
\r
116 argumentDescriptor.required());
\r
119 public String getArgumentName()
\r
121 String name = argumentDescriptor.name();
\r
122 if (name == null || name.length() <= 0)
\r
123 name = argumentSetter.getParameterName();
\r