Revision 2082

View differences:

org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/DataTypesManager.java
23 23
 */
24 24
package org.gvsig.tools.dataTypes;
25 25

  
26
import java.math.MathContext;
27
import java.math.RoundingMode;
28 26
import java.util.Iterator;
29 27

  
30 28
/**
......
65 63
    public int addtype(int type, String subtype, String name,
66 64
        Class defaultClass, Coercion coercion);
67 65

  
66
    public int addtype(int type, String subtype, String name,
67
        Class defaultClass, Coercion coercion, String iconName, 
68
        int max_precision, int default_precision, int default_scale);
69

  
68 70
    public Coercion getCoercion(int type);
69 71

  
70 72
//    public void setCoercion(int type, Coercion coercion);
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/DataType.java
27 27

  
28 28
public interface DataType extends LabeledValue<DataType>, org.gvsig.tools.lang.Cloneable {
29 29

  
30
  public static final int BYTE_MAX_PRECISION = 3; // max byte 255
31
  public static final int BYTE_DEFAULT_PRECISION = BYTE_MAX_PRECISION;
32

  
33
  public static final int INT_MAX_PRECISION = 10; // max int 2147483647.
34
  public static final int INT_DEFAULT_PRECISION = INT_MAX_PRECISION;
35

  
36
  public static final int LONG_MAX_PRECISION = 19; // max long 9223372036854775807.
37
  public static final int LONG_DEFAULT_PRECISION = LONG_MAX_PRECISION;
38

  
39
  // IEEE 754 Single-precision floating-point
40
  // https://en.m.wikipedia.org/wiki/Single-precision_floating-point_format
41
  public static final int FLOAT_MAX_PRECISION = 8; // 24bits
42
  public static final int FLOAT_DEFAULT_PRECISION = FLOAT_MAX_PRECISION;
43
  public static final int FLOAT_DEFAULT_SCALE = 3;
44

  
45
  // IEEE 754 Double-precision floating-point
46
  // https://en.m.wikipedia.org/wiki/Double-precision_floating-point_format
47
  public static final int DOUBLE_MAX_PRECISION = 16; // 54bits
48
  public static final int DOUBLE_DEFAULT_PRECISION = DOUBLE_MAX_PRECISION;
49
  public static final int DOUBLE_DEFAULT_SCALE = 8;
50

  
51
  // La precision de un BigDecimal es la de un BigInteger, que esta 
52
  // en 2^500000000, vamos a dejarla en que es algo mas de 2^5000000, siendo
53
  // consciente de que algunos proveedores no podran suportarla y la recortaran.
54
  public static final int DECIMAL_MAX_PRECISION = 2000000;
55
  public static final int DECIMAL_DEFAULT_PRECISION = 20;
56
  public static final int DECIMAL_DEFAULT_SCALE = 3;
57

  
58
  
59
  public interface NumberPrecisionAndScale {
60
    public int getPrecision();
61
    
62
    public int getScale();
63
  }
64
  
30 65
  public boolean isObject();
31 66

  
32 67
  public boolean isDynObject();
......
57 92
  @Override
58 93
  public DataType clone() throws CloneNotSupportedException;
59 94

  
95
  public boolean supportSize();
96
  
97
  public boolean supportPrecision();
98
  
99
  public boolean supportScale();
100

  
101
  public NumberPrecisionAndScale fixPrecisionAndScale(int precision, int scale);
60 102
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/Coercions.java
1
package org.gvsig.tools.dataTypes.impl;
2

  
3
import java.util.ArrayList;
4
import java.util.List;
5
import org.gvsig.tools.dataTypes.Coercion;
6
import org.gvsig.tools.dataTypes.CoercionContext;
7
import org.gvsig.tools.dataTypes.CoercionException;
8

  
9
/**
10
 *
11
 * @author jjdelcerro
12
 */
13
class Coercions implements Coercion, org.gvsig.tools.lang.Cloneable {
14
  
15
  List<Coercion> coercions;
16

  
17
  public Coercions() {
18
    this.coercions = new ArrayList<>();
19
  }
20

  
21
  public void add(Coercion coercion) {
22
    this.coercions.add(coercion);
23
  }
24

  
25
  @Override
26
  public Coercions clone() throws CloneNotSupportedException {
27
    Coercions other = (Coercions) super.clone();
28
    other.coercions = new ArrayList<>();
29
    other.coercions.addAll(this.coercions);
30
    return other;
31
  }
32

  
33
  @Override
34
  public Object coerce(Object value) throws CoercionException {
35
    return coerce(value, null);
36
  }
37

  
38
  @Override
39
  @SuppressWarnings(value = "UnusedAssignment")
40
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
41
    for (int i = this.coercions.size(); --i >= 0;) {
42
      Coercion coercion = this.coercions.get(i);
43
      try {
44
        return coercion.coerce(value, context);
45
      } catch (CoercionException e) {
46
        // Do nothing, go to next coercion
47
        coercion = null; // To allow set break point
48
      }
49
    }
50
    throw new CoercionException();
51
  }
52
  
53
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/DefaultDataTypesManager.java
38 38
import org.gvsig.tools.dataTypes.Coercion;
39 39
import org.gvsig.tools.dataTypes.CoercionException;
40 40
import org.gvsig.tools.dataTypes.DataType;
41
import static org.gvsig.tools.dataTypes.DataType.DECIMAL_DEFAULT_PRECISION;
42
import static org.gvsig.tools.dataTypes.DataType.DECIMAL_DEFAULT_SCALE;
43
import static org.gvsig.tools.dataTypes.DataType.DECIMAL_MAX_PRECISION;
44
import static org.gvsig.tools.dataTypes.DataType.DOUBLE_DEFAULT_PRECISION;
45
import static org.gvsig.tools.dataTypes.DataType.DOUBLE_DEFAULT_SCALE;
46
import static org.gvsig.tools.dataTypes.DataType.DOUBLE_MAX_PRECISION;
47
import static org.gvsig.tools.dataTypes.DataType.FLOAT_DEFAULT_PRECISION;
48
import static org.gvsig.tools.dataTypes.DataType.FLOAT_DEFAULT_SCALE;
49
import static org.gvsig.tools.dataTypes.DataType.FLOAT_MAX_PRECISION;
41 50
import org.gvsig.tools.dataTypes.DataTypes;
42 51
import org.gvsig.tools.dataTypes.DataTypesManager;
43 52
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDecimal;
......
78 87
        this.addtype(CHAR, null, "Char", Character.class, new CoerceToString());
79 88
        this.addtype(INT, null, "Integer", Integer.class, new CoerceToInt(), "datatype-integer");
80 89
        this.addtype(LONG, null, "Long", Long.class, new CoerceToLong(),"datatype-long");
81
        this.addtype(FLOAT, null, "Float", Float.class, new CoerceToFloat(),"datatype-float");
82
        this.addtype(DOUBLE, null, "Double", Double.class, new CoerceToDouble(),"datatype-double");
83
        this.addtype(DECIMAL, null, "Decimal", BigDecimal.class, new CoerceToDecimal());
90
        this.addtype(FLOAT, null, "Float", Float.class, new CoerceToFloat(),"datatype-float", FLOAT_MAX_PRECISION, FLOAT_DEFAULT_PRECISION, FLOAT_DEFAULT_SCALE);
91
        this.addtype(DOUBLE, null, "Double", Double.class, new CoerceToDouble(),"datatype-double", DOUBLE_MAX_PRECISION, DOUBLE_DEFAULT_PRECISION, DOUBLE_DEFAULT_SCALE);
92
        this.addtype(DECIMAL, null, "Decimal", BigDecimal.class, new CoerceToDecimal(),"datatype-decimal",DECIMAL_MAX_PRECISION, DECIMAL_DEFAULT_PRECISION, DECIMAL_DEFAULT_SCALE);
84 93
        this.addtype(STRING, null, "String", String.class, new CoerceToString(), "datatype-string");
85 94
        this.addtype(DATE, SUBTYPE_DATE, "Date", Date.class, new CoerceToDate(),"datatype-date");
86 95
        this.addtype(TIME, SUBTYPE_DATE, "Time", Date.class, new CoerceToTime(),"datatype-time");
......
110 119
    @Override
111 120
    public final synchronized int addtype(int type, String subtype, String name,
112 121
        Class defaultClass, Coercion coercion) {
113
        return addtype(type, subtype, name, defaultClass, coercion, null);
122
        return addtype(type, subtype, name, defaultClass, coercion, null, 0, 0, 0);
114 123
    }
115 124
    
116 125
    public final synchronized int addtype(int type, String subtype, String name,
117 126
        Class defaultClass, Coercion coercion, String iconName) {
127
        return addtype(type, subtype, name, defaultClass, coercion, iconName, 0, 0, 0);
128
    }
129
    
130
    @Override
131
    public final synchronized int addtype(int type, String subtype, String name,
132
        Class defaultClass, Coercion coercion, String iconName, 
133
        int max_precision, int default_precision, int default_scale) {
118 134
        if (type == INVALID) {
119 135
            type = getNewObjectIndexType();
120 136
        }
......
133 149
            // OK, the type is still not registered.
134 150
        }
135 151

  
136
        DataType dataType =
137
            new DefaultDataType(type, subtype, name, defaultClass, coercion, iconName);
152
        DataType dataType = new DefaultDataType(
153
                type, subtype, name, defaultClass, coercion, iconName,
154
                max_precision, default_precision, default_scale
155
        );
138 156
        types[type] = dataType;
139 157
        LOG.info("Registered data type {}.", dataType.toString());
140 158
        return type;
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/DefaultDataType.java
24 24
package org.gvsig.tools.dataTypes.impl;
25 25

  
26 26
import java.text.MessageFormat;
27
import java.util.ArrayList;
28
import java.util.List;
29 27

  
30 28
import org.gvsig.tools.dataTypes.CoercionException;
31 29
import org.gvsig.tools.dataTypes.DataType;
......
39 37

  
40 38
  private static final Logger LOG = LoggerFactory.getLogger(DefaultDataTypesManager.class);
41 39

  
40
  private static class DefaultNumberPrecisionAndScale implements NumberPrecisionAndScale {
41

  
42
    private int precision;
43
    private int scale;
44

  
45
    public DefaultNumberPrecisionAndScale(int precision, int scale) {
46
      this.precision = precision;
47
      this.scale = scale;
48
    }
49

  
50
    @Override
51
    public int getPrecision() {
52
      return this.precision;
53
    }
54

  
55
    @Override
56
    public int getScale() {
57
      return this.scale;
58
    }
59

  
60
    public void setPrecision(int precision) {
61
      this.precision = precision;
62
    }
63

  
64
    public void setScale(int scale) {
65
      this.scale = scale;
66
    }
67
  }
68

  
69
  private static final NumberPrecisionAndScale BYTE_PRECISION_AND_SCALE = new DefaultNumberPrecisionAndScale(BYTE_MAX_PRECISION, 0);
70
  private static final NumberPrecisionAndScale INT_PRECISION_AND_SCALE = new DefaultNumberPrecisionAndScale(INT_MAX_PRECISION, 0);
71
  private static final NumberPrecisionAndScale LONG_PRECISION_AND_SCALE = new DefaultNumberPrecisionAndScale(LONG_MAX_PRECISION, 0);
72
  private static final NumberPrecisionAndScale EMPTY_PRECISION_AND_SCALE = new DefaultNumberPrecisionAndScale(0, 0);
73

  
42 74
  private Coercion coercion;
43 75
  private Class defaultClass;
44 76
  private String subtype;
45 77
  private int type;
46 78
  private String name;
47 79
  private String iconName;
80
  private int max_precision;
81
  private int default_precision;
82
  private int default_scale;
48 83

  
49 84
  DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion) {
50 85
    this(type, subtype, name, defaultClass, coercion, "datatype-any");
51 86
  }
52 87

  
53 88
  DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion, String iconName) {
89
    this(type, subtype, name, defaultClass, coercion, "datatype-any", 0, 0, 0);
90
  }
54 91

  
92
  DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion, String iconName, int max_precision, int default_precision, int default_scale) {
93

  
55 94
    if (name == null) {
56 95
      LOG.trace("Can't register null type name for type {}.", new Object[]{Integer.toHexString(type).toUpperCase()});
57 96
      throw new IllegalArgumentException();
......
62 101
    this.defaultClass = defaultClass;
63 102
    this.coercion = coercion;
64 103
    this.iconName = iconName == null ? "datatype-any" : iconName;
104
    this.max_precision = max_precision;
105
    this.default_precision = default_precision;
106
    this.default_scale = default_scale;
65 107
  }
66 108

  
67 109
  @Override
......
99 141
    throw new CoercionException();
100 142
  }
101 143

  
102

  
103 144
  @Override
104 145
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
105 146
    // http://en.wikipedia.org/wiki/Type_conversion#Implicit_type_conversion
......
202 243
  public boolean isNumeric() {
203 244
    if (type == DataTypes.DOUBLE
204 245
            || type == DataTypes.FLOAT
246
            || type == DataTypes.BYTE
205 247
            || type == DataTypes.INT
206 248
            || type == DataTypes.LONG
207 249
            || type == DataTypes.DECIMAL) {
......
210 252
    return false;
211 253
  }
212 254

  
213
  private static class Coercions
214
          implements Coercion, org.gvsig.tools.lang.Cloneable {
215

  
216
    List<Coercion> coercions;
217

  
218
    public Coercions() {
219
      this.coercions = new ArrayList<>();
255
  @Override
256
  public boolean supportSize() {
257
    switch (this.type) {
258
      case DataTypes.STRING:
259
      case DataTypes.BYTEARRAY:
260
        return true;
261
      default:
262
        return false;
220 263
    }
264
  }
221 265

  
222
    public void add(Coercion coercion) {
223
      this.coercions.add(coercion);
266
  @Override
267
  public boolean supportPrecision() {
268
    if( this.max_precision<=0 ) {
269
      return false;
224 270
    }
271
    return true;
272
  }
225 273

  
226
    @Override
227
    public Coercions clone() throws CloneNotSupportedException {
228
      Coercions other = (Coercions) super.clone();
229
      other.coercions = new ArrayList<>();
230
      other.coercions.addAll(this.coercions);
231
      return other;
274
  @Override
275
  public boolean supportScale() {
276
    if( this.max_precision<=0 ) {
277
      return false;
232 278
    }
233

  
234
    @Override
235
    public Object coerce(Object value) throws CoercionException {
236
      return coerce(value, null);
279
    switch (this.type) {
280
      case DataTypes.FLOAT:
281
      case DataTypes.DOUBLE:
282
      case DataTypes.DECIMAL:
283
        return true;
284
      default:
285
        return false;
237 286
    }
287
  }
238 288

  
239
    @Override
240
    @SuppressWarnings("UnusedAssignment")
241
    public Object coerce(Object value, CoercionContext context) throws CoercionException {
242
      for (int i = this.coercions.size(); --i >= 0;) {
243
        Coercion coercion = this.coercions.get(i);
244
        try {
245
          return coercion.coerce(value, context);
246
        } catch (CoercionException e) {
247
          // Do nothing, go to next coercion
248
          coercion = null; // To allow set break point
289
  @Override
290
  public NumberPrecisionAndScale fixPrecisionAndScale(int precision, int scale) {
291
    if( this.max_precision > 0 ) {
292
      // Si el tipo tiene asignada una precision maxima asumimos que debemos
293
      // controlar la precision y la escala.
294
      DefaultNumberPrecisionAndScale r = new DefaultNumberPrecisionAndScale(
295
              precision, 
296
              scale
297
      );
298
      if (r.precision < 1) {
299
        // Sin precision
300
        if (r.scale < 1) {
301
          // Sin precision y sin escala 
302
          // Asinamos los valores por defecto.
303
          r.precision = default_precision;
304
          r.scale = default_scale;
305
        } else {
306
          // Sin precision y con escala.
307
          if (r.scale <= max_precision) {
308
            // Sin precision y con escala < maxima precision.
309
            // Asignamos la precision a la maxima.
310
            r.precision = max_precision;
311
          } else {
312
            // Sin precision y con escala > maxima precision.
313
            // Esto es un error.
314
            // Asignamos la precision a la maxima y reducimos la escala
315
            // a la maxima precision.
316
            r.precision = max_precision;
317
            r.scale = max_precision;
318
          }
249 319
        }
320
      } else {
321
        // Con precicion
322
        if (r.scale < 1) {
323
          // Con precision y sin escala.
324
          // Esto no es del todo correto. 
325
          // Probablemente algunos proveedores lo convertirian a int. 
326
          // Vamos a forzar que siempre tenga 1 decimal.
327
          if (r.precision < max_precision) {
328
            // Con precision < maxima precision y sin escala.
329
            // Aumentamos la precision en 1 para a?adirle un decimal.
330
            r.precision += 1;
331
            r.scale = 1;
332
          } else {
333
            // Con precision >= maxima precision y sin escala.
334
            // No podemos aumentar la precision para a?adirle un decimal. 
335
            // Le a?adiremos 1 decimal, y dejaremos la precision a la maxima.
336
            // Perdemos precision.
337
            r.precision = max_precision;
338
            r.scale = 1;
339
          }
340
        } else {
341
          // Con precision y escala.     
342
          if (r.precision > max_precision) {
343
            // Con precision mayor que la maxima y con escala.
344
            // Reducimos la precision a la maxima.
345
            r.precision = max_precision;
346
          }
347
          if (r.precision >= r.scale) {
348
            // Con precision y escala menor que la precision.     
349
            // Es correcto, no hacemos nada.
350
          } else {
351
            // Con precision y escala mayor que la precision.
352
            if (r.scale <= max_precision) {
353
              // Con precision y escala mayor que la precision y menor que la maxima precision.
354
              // Aumentamos la precision para soportar la escala indicada.
355
              r.precision = r.scale;
356
            } else {
357
              // Con precision y escala mayor que la precision y mayor que la maxima precision.
358
              // Ponemos la maxima precision y reducimos la escala a esta.
359
              // Perdemos precision.
360
              r.precision = max_precision;
361
              r.scale = max_precision;
362
            }
363
          }
364
        }
250 365
      }
251
      throw new CoercionException();
366
      return r;
252 367
    }
253

  
368
    // Para los tipos enteros basicos, ya tienen una precision y escala fijas.
369
    switch(this.type) {
370
      case DataTypes.BYTE:
371
        return BYTE_PRECISION_AND_SCALE;
372
        
373
      case DataTypes.INT:
374
        return INT_PRECISION_AND_SCALE;
375
        
376
      case DataTypes.LONG:
377
        return LONG_PRECISION_AND_SCALE;
378
        
379
      default:
380
        return EMPTY_PRECISION_AND_SCALE;
381
    }
254 382
  }
255

  
256 383
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/src/main/java/org/gvsig/tools/swing/impl/ToolsSwingDefaultImplLibrary.java
26 26
import org.gvsig.tools.library.AbstractLibrary;
27 27
import org.gvsig.tools.library.Library;
28 28
import org.gvsig.tools.library.LibraryException;
29
import org.gvsig.tools.swing.api.JWebBrowserFactory;
30 29
import org.gvsig.tools.swing.api.ToolsSwingLibrary;
31 30
import org.gvsig.tools.swing.api.ToolsSwingLocator;
32 31
import org.gvsig.tools.swing.icontheme.IconTheme;
......
102 101
            new String[] { "DataTypes", "datatype-bytearray" },
103 102
            new String[] { "DataTypes", "datatype-date" },
104 103
            new String[] { "DataTypes", "datatype-double" },
104
            new String[] { "DataTypes", "datatype-decimal" },
105 105
            new String[] { "DataTypes", "datatype-float" },
106 106
            new String[] { "DataTypes", "datatype-image" },
107 107
            new String[] { "DataTypes", "datatype-integer" },
......
110 110
            new String[] { "DataTypes", "datatype-string" },
111 111
            new String[] { "DataTypes", "datatype-text" },
112 112
            new String[] { "DataTypes", "datatype-time" },
113
            new String[] { "DataTypes", "datatype-timestamp" },
114
            new String[] { "DataTypes", "datatype-timestamp" },
115 113
            new String[] { "DataTypes", "datatype-timestamp" }
116 114
        };
117 115
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent();

Also available in: Unified diff