/
NumberToNumberTransform.java
67 lines (59 loc) · 2.57 KB
/
NumberToNumberTransform.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package org.teiid.core.types.basic;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.teiid.core.CorePlugin;
import org.teiid.core.types.Transform;
import org.teiid.core.types.TransformationException;
public abstract class NumberToNumberTransform extends Transform {
private Class<?> sourceType;
private Comparable<?> min;
private Comparable<?> max;
public NumberToNumberTransform(Number min, Number max, Class<?> sourceType) {
this.sourceType = sourceType;
if (sourceType == Short.class) {
this.min = min.shortValue();
this.max = max.shortValue();
} else if (sourceType == Integer.class) {
this.min = min.intValue();
this.max = max.intValue();
} else if (sourceType == Long.class) {
this.min = min.longValue();
this.max = max.longValue();
} else if (sourceType == Float.class) {
this.min = min.floatValue();
this.max = max.floatValue();
} else if (sourceType == Double.class) {
this.min = min.doubleValue();
this.max = max.doubleValue();
} else if (sourceType == BigInteger.class) {
if (min instanceof Double || min instanceof Float) {
this.min = BigDecimal.valueOf(min.doubleValue()).toBigInteger();
this.max = BigDecimal.valueOf(max.doubleValue()).toBigInteger();
} else {
this.min = BigInteger.valueOf(min.longValue());
this.max = BigInteger.valueOf(max.longValue());
}
} else if (sourceType == BigDecimal.class) {
if (min instanceof Double || min instanceof Float) {
this.min = BigDecimal.valueOf(min.doubleValue());
this.max = BigDecimal.valueOf(max.doubleValue());
} else {
this.min = BigDecimal.valueOf(min.longValue());
this.max = BigDecimal.valueOf(max.longValue());
}
} else if (sourceType == Byte.class) {
} else {
throw new AssertionError();
}
}
@Override
public Class<?> getSourceType() {
return sourceType;
}
protected void checkValueRange(Object value)
throws TransformationException {
if (((Comparable)value).compareTo(min) < 0 || ((Comparable)value).compareTo(max) > 0) {
throw new TransformationException(CorePlugin.Event.TEIID10058, CorePlugin.Util.gs(CorePlugin.Event.TEIID10058, value, getSourceType().getSimpleName(), getTargetType().getSimpleName()));
}
}
}