KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > dialect > TypeNames


1 //$Id: TypeNames.java,v 1.3 2005/03/30 18:01:41 oneovthafew Exp $
2
package org.hibernate.dialect;
3
4 import java.util.Map JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.TreeMap JavaDoc;
7 import java.util.Iterator JavaDoc;
8
9 import org.hibernate.MappingException;
10 import org.hibernate.util.StringHelper;
11
12 /**
13  * This class maps a type to names. Associations
14  * may be marked with a capacity. Calling the get()
15  * method with a type and actual size n will return
16  * the associated name with smallest capacity >= n,
17  * if available and an unmarked default type otherwise.
18  * Eg, setting
19  * <pre>
20  * names.put(type, "TEXT" );
21  * names.put(type, 255, "VARCHAR($l)" );
22  * names.put(type, 65534, "LONGVARCHAR($l)" );
23  * </pre>
24  * will give you back the following:
25  * <pre>
26  * names.get(type) // --> "TEXT" (default)
27  * names.get(type, 100) // --> "VARCHAR(100)" (100 is in [0:255])
28  * names.get(type, 1000) // --> "LONGVARCHAR(1000)" (1000 is in [256:65534])
29  * names.get(type, 100000) // --> "TEXT" (default)
30  * </pre>
31  * On the other hand, simply putting
32  * <pre>
33  * names.put(type, "VARCHAR($l)" );
34  * </pre>
35  * would result in
36  * <pre>
37  * names.get(type) // --> "VARCHAR($l)" (will cause trouble)
38  * names.get(type, 100) // --> "VARCHAR(100)"
39  * names.get(type, 10000) // --> "VARCHAR(10000)"
40  * </pre>
41  *
42  * @author Christoph Beck
43  */

44 public class TypeNames {
45
46     private HashMap JavaDoc weighted = new HashMap JavaDoc();
47     private HashMap JavaDoc defaults = new HashMap JavaDoc();
48
49     /**
50      * get default type name for specified type
51      * @param typecode the type key
52      * @return the default type name associated with specified key
53      */

54     public String JavaDoc get(int typecode) throws MappingException {
55         String JavaDoc result = (String JavaDoc) defaults.get( new Integer JavaDoc(typecode) );
56         if (result==null) throw new MappingException("No Dialect mapping for JDBC type: " + typecode);
57         return result;
58     }
59
60     /**
61      * get type name for specified type and size
62      * @param typecode the type key
63      * @param size the SQL length
64      * @param scale the SQL scale
65      * @param precision the SQL precision
66      * @return the associated name with smallest capacity >= size,
67      * if available and the default type name otherwise
68      */

69     public String JavaDoc get(int typecode, int size, int precision, int scale) throws MappingException {
70         Map JavaDoc map = (Map JavaDoc) weighted.get( new Integer JavaDoc(typecode) );
71         if ( map!=null && map.size()>0 ) {
72             // iterate entries ordered by capacity to find first fit
73
Iterator JavaDoc entries = map.entrySet().iterator();
74             while ( entries.hasNext() ) {
75                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)entries.next();
76                 if ( size <= ( (Integer JavaDoc) entry.getKey() ).intValue() ) {
77                     return replace( (String JavaDoc) entry.getValue(), size, precision, scale );
78                 }
79             }
80         }
81         return replace( get(typecode), size, precision, scale );
82     }
83     
84     private static String JavaDoc replace(String JavaDoc type, int size, int precision, int scale) {
85         type = StringHelper.replaceOnce(type, "$s", Integer.toString(scale) );
86         type = StringHelper.replaceOnce(type, "$l", Integer.toString(size) );
87         return StringHelper.replaceOnce(type, "$p", Integer.toString(precision) );
88     }
89
90     /**
91      * set a type name for specified type key and capacity
92      * @param typecode the type key
93      */

94     public void put(int typecode, int capacity, String JavaDoc value) {
95         TreeMap JavaDoc map = (TreeMap JavaDoc)weighted.get( new Integer JavaDoc(typecode) );
96         if (map == null) {// add new ordered map
97
map = new TreeMap JavaDoc();
98             weighted.put( new Integer JavaDoc(typecode), map );
99         }
100         map.put(new Integer JavaDoc(capacity), value);
101     }
102
103     /**
104      * set a default type name for specified type key
105      * @param typecode the type key
106      */

107     public void put(int typecode, String JavaDoc value) {
108         defaults.put( new Integer JavaDoc(typecode), value );
109     }
110 }
111
112
113
114
115
116
117
Popular Tags