View Javadoc
1   package us.codecraft.webmagic.model.formatter;
2   
3   import java.util.Map;
4   import java.util.concurrent.ConcurrentHashMap;
5   
6   /**
7    * @author code4crafter@gmail.com
8    * @since 0.3.2
9    */
10  public class ObjectFormatters {
11  
12      private static Map<Class, Class<? extends ObjectFormatter>> formatterMap = new ConcurrentHashMap<Class, Class<? extends ObjectFormatter>>();
13  
14      static {
15          for (Class<? extends ObjectFormatter> basicTypeFormatter : BasicTypeFormatter.basicTypeFormatters) {
16              put(basicTypeFormatter);
17          }
18          put(DateFormatter.class);
19      }
20  
21      public static void put(Class<? extends ObjectFormatter> objectFormatter) {
22          try {
23              formatterMap.put(objectFormatter.newInstance().clazz(), objectFormatter);
24          } catch (InstantiationException e) {
25              throw new RuntimeException(e);
26          } catch (IllegalAccessException e) {
27              throw new RuntimeException(e);
28          }
29      }
30  
31      public static Class<? extends ObjectFormatter> get(Class<?> clazz){
32          return formatterMap.get(clazz);
33      }
34  }