• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2010 the original author or authors.
3  * See the notice.md file distributed with this work for additional
4  * information regarding copyright ownership.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 package com.beust.jcommander.internal;
20 
21 import com.beust.jcommander.IStringConverter;
22 import com.beust.jcommander.IStringConverterFactory;
23 import com.beust.jcommander.converters.BigDecimalConverter;
24 import com.beust.jcommander.converters.BooleanConverter;
25 import com.beust.jcommander.converters.DoubleConverter;
26 import com.beust.jcommander.converters.FileConverter;
27 import com.beust.jcommander.converters.FloatConverter;
28 import com.beust.jcommander.converters.ISO8601DateConverter;
29 import com.beust.jcommander.converters.IntegerConverter;
30 import com.beust.jcommander.converters.LongConverter;
31 import com.beust.jcommander.converters.StringConverter;
32 import com.beust.jcommander.converters.URIConverter;
33 import com.beust.jcommander.converters.URLConverter;
34 
35 import java.io.File;
36 import java.math.BigDecimal;
37 import java.util.Date;
38 import java.net.URI;
39 import java.net.URL;
40 import java.util.Map;
41 
42 public class DefaultConverterFactory implements IStringConverterFactory {
43   /**
44    * A map of converters per class.
45    */
46   private static Map<Class, Class<? extends IStringConverter<?>>> m_classConverters;
47 
48   static {
49     m_classConverters = Maps.newHashMap();
m_classConverters.put(String.class, StringConverter.class)50     m_classConverters.put(String.class, StringConverter.class);
m_classConverters.put(Integer.class, IntegerConverter.class)51     m_classConverters.put(Integer.class, IntegerConverter.class);
m_classConverters.put(int.class, IntegerConverter.class)52     m_classConverters.put(int.class, IntegerConverter.class);
m_classConverters.put(Long.class, LongConverter.class)53     m_classConverters.put(Long.class, LongConverter.class);
m_classConverters.put(long.class, LongConverter.class)54     m_classConverters.put(long.class, LongConverter.class);
m_classConverters.put(Float.class, FloatConverter.class)55     m_classConverters.put(Float.class, FloatConverter.class);
m_classConverters.put(float.class, FloatConverter.class)56     m_classConverters.put(float.class, FloatConverter.class);
m_classConverters.put(Double.class, DoubleConverter.class)57     m_classConverters.put(Double.class, DoubleConverter.class);
m_classConverters.put(double.class, DoubleConverter.class)58     m_classConverters.put(double.class, DoubleConverter.class);
m_classConverters.put(Boolean.class, BooleanConverter.class)59     m_classConverters.put(Boolean.class, BooleanConverter.class);
m_classConverters.put(boolean.class, BooleanConverter.class)60     m_classConverters.put(boolean.class, BooleanConverter.class);
m_classConverters.put(File.class, FileConverter.class)61     m_classConverters.put(File.class, FileConverter.class);
m_classConverters.put(BigDecimal.class, BigDecimalConverter.class)62     m_classConverters.put(BigDecimal.class, BigDecimalConverter.class);
m_classConverters.put(Date.class, ISO8601DateConverter.class)63     m_classConverters.put(Date.class, ISO8601DateConverter.class);
64     try {
65       Class<?> pathClass = Class.forName("java.nio.file.Path");
66       Class<?> pathConverterClass = Class.forName("com.beust.jcommander.converters.PathConverter");
m_classConverters.put(pathClass, (Class<? extends IStringConverter<?>>)pathConverterClass)67       m_classConverters.put(pathClass, (Class<? extends IStringConverter<?>>)pathConverterClass);
68     } catch (ClassNotFoundException e) {
69       // Do nothing: Android does not have java.nio.file.Path
70     }
m_classConverters.put(URI.class, URIConverter.class)71     m_classConverters.put(URI.class, URIConverter.class);
m_classConverters.put(URL.class, URLConverter.class)72     m_classConverters.put(URL.class, URLConverter.class);
73   }
74 
getConverter(Class forType)75   public Class<? extends IStringConverter<?>> getConverter(Class forType) {
76     return m_classConverters.get(forType);
77   }
78 
79 }
80