001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.storage.util;
011:
012: import java.text.MessageFormat;
013:
014: /**
015: * The TypeMapping class helps translating MMBase types to storage-specific type descriptions.
016: * Examples of type mappings are mappings that convert to database field types.
017: * I.e., a STRING with size 0-255 could be configured to translate to 'varchar({0})', '{0}, in this case,.
018: * being the size of the actual field.
019: * <br />
020: * TypeMapping is a comparable class, which allows it to be used in a sorted map, set or list.
021: * However, Typemapping needs fuzzy matching so it is easy to locate the appropriate type-mapping for a field.
022: * As such, it's natural ordering is NOT consistent with equals. A typemapping may be considered 'equal' while still having
023: * a different ordering position in the class.
024: * This allows for an easy search on a sorted list of TypeMappings: By using 'IndexOf' you can quickly find a TypeMapping in a list,
025: * even if the min or max sizes for the type do not completely match.
026: * You typically use this if you searxh for a TypeMapping whose size you set with 'getSize()', rather than setting a specific range
027: * (using the minSize/maxSize properties).
028: *
029: * @author Pierre van Rooden
030: * @version $Id: TypeMapping.java,v 1.10 2008/02/03 17:33:57 nklasens Exp $
031: * @since MMBase-1.7
032: */
033: public class TypeMapping implements Comparable<TypeMapping> {
034:
035: /**
036: * The expression this type should translate to.
037: * You can access this property directly, but you can use {@link #getType(int)} to obtain an expanded expression.
038: */
039: public String type;
040: /**
041: * The name of the MMBase type to map
042: */
043: public String name;
044: /**
045: * The minimum size of the MMBase type to map.
046: * A value of -1 indicates no mimimum.
047: */
048: public long minSize;
049: /**
050: * The maximum size of the MMBase type to map.
051: * A value of -1 indicates no maximum.
052: */
053: public long maxSize;
054:
055: public TypeMapping() {
056: }
057:
058: /**
059: * Sets a fixed size for this TypeMapping.
060: * Effectively, this sets the minimimum and maximum size of the type mapping to the specified value, ensuring this TypeMapping object
061: * 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
062: * that this size.
063: * @param size the size to set
064: */
065: public void setFixedSize(long size) {
066: minSize = size;
067: maxSize = size;
068: }
069:
070: // javadoc inherited
071: public int compareTo(TypeMapping o) {
072: TypeMapping t = o;
073: if (!name.equals(t.name)) {
074: return name.compareTo(t.name);
075: } else if (minSize != t.minSize) {
076: long diff = t.minSize - minSize;
077: if (diff == 0)
078: return 0;
079: return diff < 0 ? -1 : 1;
080: } else if (maxSize == -1) {
081: if (t.maxSize == -1) {
082: return 0;
083: } else {
084: return -1;
085: }
086: } else {
087: long diff = t.maxSize - maxSize;
088: if (diff == 0)
089: return 0;
090: return diff < 0 ? -1 : 1;
091: }
092: }
093:
094: // javadoc inherited
095: public boolean equals(Object o) {
096: if (o == null)
097: return false;
098: if (o instanceof TypeMapping) {
099: TypeMapping tm = (TypeMapping) o;
100: // A typemapping equals another type-mapping when the one contains the other.
101: // Because of this the 'fixed' size type-mappings (created DatabaseStorageManager) are found by indexOf of the typeMappings Collection of DatabaseStorageManager.
102: // In this typeMappings Collection there are normally only 'ranged' of sizes (as defined in th XML)
103: return (name == null ? tm.name == null : name
104: .equals(tm.name))
105: && (((minSize >= tm.minSize || (tm.minSize <= 0)) && (maxSize <= tm.maxSize || (tm.maxSize <= 0))) // contained by.
106: || ((tm.minSize >= minSize || (minSize <= 0)) && (tm.maxSize <= maxSize || (maxSize <= 0))) // containing
107: );
108: } else {
109: return false;
110: }
111: }
112:
113: // javadoc inherited
114: public int hashCode() {
115: // because of the complicated equals implementation minSize and maxSize cannot be present in hashCode
116: return name == null ? 0 : name.hashCode();
117: }
118:
119: /**
120: * Returns the mappings type.
121: */
122: public String getType(long size) {
123: return MessageFormat.format(type, new Object[] { Long
124: .valueOf(size) });
125: }
126:
127: // javadoc inherited
128: public String toString() {
129: return name + " (" + minSize + "," + maxSize + ")+>" + type;
130: }
131:
132: }
|