diff options
author | Cedric Beust <cedric@beust.com> | 2011-09-02 10:17:48 -0700 |
---|---|---|
committer | Cedric Beust <cedric@beust.com> | 2011-09-02 10:17:48 -0700 |
commit | cccb105772ddcbef1ca9a9aacbf613a17a5c65f5 (patch) | |
tree | 30678f04e94bc74cf16775a8f24c31645d3154be /src/main/java/com/beust/jcommander | |
parent | ee74c7bf2ea0f7b17093650fdd78b0f8ffa343b9 (diff) | |
parent | 8ce1ca7ee017b4ab886879cc4c0dd878fdbff2f9 (diff) | |
download | platform_external_jcommander-cccb105772ddcbef1ca9a9aacbf613a17a5c65f5.tar.gz platform_external_jcommander-cccb105772ddcbef1ca9a9aacbf613a17a5c65f5.tar.bz2 platform_external_jcommander-cccb105772ddcbef1ca9a9aacbf613a17a5c65f5.zip |
Merge pull request #77 from acornejo/master
added support for double and float types.
Diffstat (limited to 'src/main/java/com/beust/jcommander')
4 files changed, 100 insertions, 2 deletions
diff --git a/src/main/java/com/beust/jcommander/ParameterDescription.java b/src/main/java/com/beust/jcommander/ParameterDescription.java index 0137f43..47e3cd7 100644 --- a/src/main/java/com/beust/jcommander/ParameterDescription.java +++ b/src/main/java/com/beust/jcommander/ParameterDescription.java @@ -261,7 +261,15 @@ public class ParameterDescription { public boolean isNumber() { Class<?> type = m_field.getType(); return type.equals(Integer.class) || type.equals(int.class) - || type.equals(Long.class) || type.equals(long.class); + || type.equals(Long.class) || type.equals(long.class) + || type.equals(Float.class) || type.equals(float.class) + || type.equals(Double.class) || type.equals(double.class); + } + + public boolean isFractional() { + Class<?> type = m_field.getType(); + return type.equals(Float.class) || type.equals(float.class) + || type.equals(Double.class) || type.equals(double.class); } private void p(String string) { diff --git a/src/main/java/com/beust/jcommander/converters/DoubleConverter.java b/src/main/java/com/beust/jcommander/converters/DoubleConverter.java new file mode 100644 index 0000000..0c36c68 --- /dev/null +++ b/src/main/java/com/beust/jcommander/converters/DoubleConverter.java @@ -0,0 +1,42 @@ +/** + * Copyright (C) 2010 the original author or authors. + * See the notice.md file distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.beust.jcommander.converters; + +import com.beust.jcommander.ParameterException; + +/** + * Convert a string to a double. + * + * @author acornejo + */ +public class DoubleConverter extends BaseConverter<Double> { + + public DoubleConverter(String optionName) { + super(optionName); + } + + public Double convert(String value) { + try { + return Double.parseDouble(value); + } catch(NumberFormatException ex) { + throw new ParameterException(getErrorString(value, "a double")); + } + } + +} diff --git a/src/main/java/com/beust/jcommander/converters/FloatConverter.java b/src/main/java/com/beust/jcommander/converters/FloatConverter.java new file mode 100644 index 0000000..2e2eff8 --- /dev/null +++ b/src/main/java/com/beust/jcommander/converters/FloatConverter.java @@ -0,0 +1,42 @@ +/** + * Copyright (C) 2010 the original author or authors. + * See the notice.md file distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.beust.jcommander.converters; + +import com.beust.jcommander.ParameterException; + +/** + * Convert a string to a float. + * + * @author acornejo + */ +public class FloatConverter extends BaseConverter<Float> { + + public FloatConverter(String optionName) { + super(optionName); + } + + public Float convert(String value) { + try { + return Float.parseFloat(value); + } catch(NumberFormatException ex) { + throw new ParameterException(getErrorString(value, "a float")); + } + } + +} diff --git a/src/main/java/com/beust/jcommander/internal/DefaultConverterFactory.java b/src/main/java/com/beust/jcommander/internal/DefaultConverterFactory.java index bbe0cd1..26ab0fc 100644 --- a/src/main/java/com/beust/jcommander/internal/DefaultConverterFactory.java +++ b/src/main/java/com/beust/jcommander/internal/DefaultConverterFactory.java @@ -24,6 +24,8 @@ import com.beust.jcommander.converters.BooleanConverter; import com.beust.jcommander.converters.FileConverter; import com.beust.jcommander.converters.IntegerConverter; import com.beust.jcommander.converters.LongConverter; +import com.beust.jcommander.converters.FloatConverter; +import com.beust.jcommander.converters.DoubleConverter; import com.beust.jcommander.converters.StringConverter; import java.io.File; @@ -34,7 +36,7 @@ public class DefaultConverterFactory implements IStringConverterFactory { * A map of converters per class. */ private static Map<Class, Class<? extends IStringConverter<?>>> m_classConverters; - + static { m_classConverters = Maps.newHashMap(); m_classConverters.put(String.class, StringConverter.class); @@ -42,6 +44,10 @@ public class DefaultConverterFactory implements IStringConverterFactory { m_classConverters.put(int.class, IntegerConverter.class); m_classConverters.put(Long.class, LongConverter.class); m_classConverters.put(long.class, LongConverter.class); + m_classConverters.put(Float.class, FloatConverter.class); + m_classConverters.put(float.class, FloatConverter.class); + m_classConverters.put(Double.class, DoubleConverter.class); + m_classConverters.put(double.class, DoubleConverter.class); m_classConverters.put(Boolean.class, BooleanConverter.class); m_classConverters.put(boolean.class, BooleanConverter.class); m_classConverters.put(File.class, FileConverter.class); |