KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > storage > util > TypeMapping


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.storage.util;
11
12 import java.text.MessageFormat JavaDoc;
13
14 /**
15  * The TypeMapping class helps translating MMBase types to storage-specific type descriptions.
16  * Examples of type mappings are mappings that convert to database field types.
17  * I.e., a STRING with size 0-255 could be configured to translate to 'varchar({0})', '{0}, in this case,.
18  * being the size of the actual field.
19  * <br />
20  * TypeMapping is a comparable class, which allows it to be used in a sorted map, set or list.
21  * However, Typemapping needs fuzzy matching so it is easy to locate the appropriate type-mapping for a field.
22  * As such, it's natural ordering is NOT consistent with equals. A typemapping may be considered 'equal' while still having
23  * a different ordering position in the class.
24  * This allows for an easy search on a sorted list of TypeMappings: By using 'IndexOf' you can quickly find a TypeMapping in a list,
25  * even if the min or max sizes for the type do not completely match.
26  * You typically use this if you searxh for a TypeMapping whose size you set with 'getSize()', rather than setting a specific range
27  * (using the minSize/maxSize properties).
28  *
29  * @author Pierre van Rooden
30  * @version $Id: TypeMapping.java,v 1.5 2005/01/30 16:46:35 nico Exp $
31  * @since MMBase-1.7
32  */

33 public class TypeMapping implements Comparable JavaDoc {
34
35     /**
36      * The expression this type should translate to.
37      * You can access this property directly, but you can use {@link #getType(int)} to obtain an expanded expression.
38      */

39     public String JavaDoc type;
40     /**
41      * The name of the MMBase type to map
42      */

43     public String JavaDoc name;
44     /**
45      * The minimum size of the MMBase type to map.
46      * A value of -1 indicates no mimimum.
47      */

48     public int minSize;
49     /**
50      * The maximum size of the MMBase type to map.
51      * A value of -1 indicates no maximum.
52      */

53     public int maxSize;
54
55     public TypeMapping() {
56     }
57
58     /**
59      * Sets a fixed size for this TypeMapping.
60      * Effectively, this sets the minimimum and maximum size of the type mapping to the specified value, ensuring this TypeMapping object
61      * is equal to all TypeMappings whos minimum size is equal to or smaller than the size, and teh maximum size is equal to or greater
62      * that this size.
63      * @param size the size to set
64      */

65     public void setFixedSize(int size) {
66         minSize = size;
67         maxSize = size;
68     }
69
70     // javadoc inherited
71
public int compareTo(Object JavaDoc o) {
72         TypeMapping t = (TypeMapping) o;
73         if (!name.equals(t.name)) {
74             return name.compareTo(t.name);
75         } else if (minSize != t.minSize) {
76             return t.minSize - minSize;
77         } else if (maxSize == -1) {
78             if (t.maxSize == -1) {
79                 return 0;
80             } else {
81                 return -1;
82             }
83         } else {
84             return t.maxSize - maxSize;
85         }
86     }
87
88     // javadoc inherited
89
public boolean equals(Object JavaDoc o) {
90         if (o == null) return false;
91         if (o instanceof TypeMapping) {
92             TypeMapping tm = (TypeMapping) o;
93             // A typemapping equals another type-mapping when the one contains the other.
94
// Because of this the 'fixed' size type-mappings (created DatabaseStorageManager) are found by indexOf of the typeMappings Collection of DatabaseStorageManager.
95
// In this typeMappings Collection there are normally only 'ranged' of sizes (as defined in th XML)
96
return (name == null ? tm.name == null : name.equals(tm.name)) &&
97                (
98                 ( (minSize >= tm.minSize || (tm.minSize <= 0)) && (maxSize <= tm.maxSize || (tm.maxSize <= 0)) ) // contained by.
99
||
100                 ( (tm.minSize >= minSize || (minSize <= 0)) && (tm.maxSize <= maxSize || (maxSize <= 0)) ) // containing
101
)
102                 ;
103         } else {
104             return false;
105         }
106     }
107     // javadoc inherited
108
public int hashCode() {
109         // because of the complicated equals implementation minSize and maxSize cannot be present in hashCode
110
return name == null ? 0 : name.hashCode();
111     }
112
113     /**
114      * Returns the mappings type.
115      */

116     public String JavaDoc getType(int size) {
117         return MessageFormat.format(type,new Object JavaDoc[]{ new Integer JavaDoc(size) });
118     }
119
120     // javadoc inherited
121
public String JavaDoc toString() {
122         return name+" ("+minSize+","+maxSize+")+>"+type;
123     }
124
125 }
126
Popular Tags