KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > datetime > JdtConverterManager


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.datetime;
4
5 import jodd.datetime.converter.*;
6
7 import java.util.Map JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Calendar JavaDoc;
11
12 /**
13  * Manager for {@link JdtConverter} instances.
14  * @see #registerDefaults()
15  */

16 public class JdtConverterManager {
17
18     // ---------------------------------------------------------------- manager
19

20     private static Map JavaDoc converters = new HashMap JavaDoc();
21
22     static {
23         registerDefaults();
24     }
25
26     /**
27      * Unregister all converters.
28      */

29     public static void unregisterAll() {
30         converters.clear();
31     }
32
33
34     /**
35      * Registers default set of converters.
36      * @see #register(Class, jodd.datetime.converter.JdtConverter)
37      */

38     public static void registerDefaults() {
39         register(DateTimeStamp.class, new DateTimeStampConverter());
40         register(Calendar JavaDoc.class, new CalendarConverter());
41         register(java.util.GregorianCalendar JavaDoc.class, new GregorianCalendarConverter());
42         register(java.util.Date JavaDoc.class, new DateConverter());
43         register(java.sql.Date JavaDoc.class, new SqlDateConverter());
44         register(java.sql.Timestamp JavaDoc.class, new SqlTimestampConverter());
45     }
46
47     /**
48      * Registers converter for an objects of specific type.
49
50      * @see #registerDefaults()
51      */

52     public static void register(Class JavaDoc type, JdtConverter converter) {
53         converters.put(type, converter);
54     }
55
56     public static void unregister(Class JavaDoc type) {
57         converters.remove(type);
58     }
59
60
61     // ---------------------------------------------------------------- lookup
62

63
64     /**
65      * Returns converter for the specific object type.
66      * @return converter instance, <code>null</code> if nothing found.
67      */

68     public static JdtConverter lookup(Class JavaDoc type) {
69         return (JdtConverter) converters.get(type);
70     }
71
72     /**
73      * Performs more throughly search for converter loader. It examines all available
74      * converters and returns the first that matches the object type.
75      */

76     public static JdtConverter lookup(Object JavaDoc source) {
77         JdtConverter converter = lookup(source.getClass());
78         if (converter == null) { // class not found, scan for instanceof matching
79
Iterator JavaDoc it = converters.keySet().iterator();
80             while (it.hasNext()) {
81                 Class JavaDoc key = (Class JavaDoc) it.next();
82                 if (key.isInstance(source)) {
83                     converter = lookup(key);
84                     break;
85                 }
86             }
87         }
88         return converter;
89     }
90
91 }
92
Popular Tags