Skip to content

Commit

Permalink
TEIID-5008 using stored min/max
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Jul 26, 2017
1 parent c89703d commit f57ee25
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 69 deletions.
8 changes: 0 additions & 8 deletions common-core/src/main/java/org/teiid/core/types/Transform.java
Expand Up @@ -18,7 +18,6 @@

package org.teiid.core.types;

import org.teiid.core.CorePlugin;

/**
* This interface represents the transformation from one data type to
Expand Down Expand Up @@ -102,11 +101,4 @@ public String toString() {
return getDisplayName();
}

protected void checkValueRange(Object value, Number min, Number max)
throws TransformationException {
if (((Comparable)value).compareTo(DataTypeManager.transformValue(min, getSourceType())) < 0 || ((Comparable)value).compareTo(DataTypeManager.transformValue(max, getSourceType())) > 0) {
throw new TransformationException(CorePlugin.Event.TEIID10058, CorePlugin.Util.gs(CorePlugin.Event.TEIID10058, value, getSourceType().getSimpleName(), getTargetType().getSimpleName()));
}
}

}
Expand Up @@ -19,20 +19,12 @@
package org.teiid.core.types.basic;

import org.teiid.core.types.DataTypeManager;
import org.teiid.core.types.Transform;
import org.teiid.core.types.TransformationException;

public class NumberToByteTransform extends Transform {

private Class<?> sourceType;
public class NumberToByteTransform extends NumberToNumberTransform {

public NumberToByteTransform(Class<?> sourceType) {
this.sourceType = sourceType;
}

@Override
public Class<?> getSourceType() {
return sourceType;
super(Byte.MIN_VALUE, Byte.MAX_VALUE, sourceType);
}

/**
Expand All @@ -44,7 +36,7 @@ public Class<?> getSourceType() {
* the transformation fails
*/
public Object transformDirect(Object value) throws TransformationException {
checkValueRange(value, Byte.MIN_VALUE, Byte.MAX_VALUE);
checkValueRange(value);
return Byte.valueOf(((Number)value).byteValue());
}

Expand Down
Expand Up @@ -19,26 +19,19 @@
package org.teiid.core.types.basic;

import org.teiid.core.types.DataTypeManager;
import org.teiid.core.types.Transform;
import org.teiid.core.types.TransformationException;

public class NumberToDoubleTransform extends Transform {
public class NumberToDoubleTransform extends NumberToNumberTransform {

private Class<?> sourceType;
private boolean isNarrowing;
private boolean isLossy;

public NumberToDoubleTransform(Class<?> sourceType, boolean isNarrowing, boolean isLossy) {
this.sourceType = sourceType;
super(-Double.MAX_VALUE, Double.MAX_VALUE, sourceType);
this.isNarrowing = isNarrowing;
this.isLossy = isLossy;
}

@Override
public Class<?> getSourceType() {
return sourceType;
}

/**
* This method transforms a value of the source type into a value
* of the target type.
Expand All @@ -49,7 +42,7 @@ public Class<?> getSourceType() {
*/
public Object transformDirect(Object value) throws TransformationException {
if (isNarrowing) {
checkValueRange(value, -Double.MAX_VALUE, Double.MAX_VALUE);
checkValueRange(value);
}
return Double.valueOf(((Number)value).doubleValue());
}
Expand Down
Expand Up @@ -19,26 +19,19 @@
package org.teiid.core.types.basic;

import org.teiid.core.types.DataTypeManager;
import org.teiid.core.types.Transform;
import org.teiid.core.types.TransformationException;

public class NumberToFloatTransform extends Transform {
public class NumberToFloatTransform extends NumberToNumberTransform {

private Class<?> sourceType;
private boolean isNarrowing;
private boolean isLossy;

public NumberToFloatTransform(Class<?> sourceType, boolean isNarrowing, boolean isLossy) {
this.sourceType = sourceType;
super(-Float.MAX_VALUE, Float.MAX_VALUE, sourceType);
this.isNarrowing = isNarrowing;
this.isLossy = isLossy;
}

@Override
public Class<?> getSourceType() {
return sourceType;
}

/**
* This method transforms a value of the source type into a value
* of the target type.
Expand All @@ -49,7 +42,7 @@ public Class<?> getSourceType() {
*/
public Object transformDirect(Object value) throws TransformationException {
if (isNarrowing) {
checkValueRange(value, -Float.MAX_VALUE, Float.MAX_VALUE);
checkValueRange(value);
}
return Float.valueOf(((Number)value).floatValue());
}
Expand Down
Expand Up @@ -19,24 +19,17 @@
package org.teiid.core.types.basic;

import org.teiid.core.types.DataTypeManager;
import org.teiid.core.types.Transform;
import org.teiid.core.types.TransformationException;

public class NumberToIntegerTransform extends Transform {
public class NumberToIntegerTransform extends NumberToNumberTransform {

private Class<?> sourceType;
private boolean isNarrowing;

public NumberToIntegerTransform(Class<?> sourceType, boolean isNarrowing) {
this.sourceType = sourceType;
super(Integer.MIN_VALUE, Integer.MAX_VALUE, sourceType);
this.isNarrowing = isNarrowing;
}

@Override
public Class<?> getSourceType() {
return sourceType;
}

/**
* This method transforms a value of the source type into a value
* of the target type.
Expand All @@ -47,7 +40,7 @@ public Class<?> getSourceType() {
*/
public Object transformDirect(Object value) throws TransformationException {
if (isNarrowing) {
checkValueRange(value, Integer.MIN_VALUE, Integer.MAX_VALUE);
checkValueRange(value);
}
return Integer.valueOf(((Number)value).intValue());
}
Expand Down
Expand Up @@ -19,26 +19,19 @@
package org.teiid.core.types.basic;

import org.teiid.core.types.DataTypeManager;
import org.teiid.core.types.Transform;
import org.teiid.core.types.TransformationException;

public class NumberToLongTransform extends Transform {
public class NumberToLongTransform extends NumberToNumberTransform {

private Class<?> sourceType;
private boolean isNarrowing;
private boolean isLossy;

public NumberToLongTransform(Class<?> sourceType, boolean isNarrowing, boolean isLossy) {
this.sourceType = sourceType;
super(Long.MIN_VALUE, Long.MAX_VALUE, sourceType);
this.isNarrowing = isNarrowing;
this.isLossy = isLossy;
}

@Override
public Class<?> getSourceType() {
return sourceType;
}

/**
* This method transforms a value of the source type into a value
* of the target type.
Expand All @@ -49,7 +42,7 @@ public Class<?> getSourceType() {
*/
public Object transformDirect(Object value) throws TransformationException {
if (isNarrowing) {
checkValueRange(value, Long.MIN_VALUE, Long.MAX_VALUE);
checkValueRange(value);
}
return Long.valueOf(((Number)value).longValue());
}
Expand Down
@@ -0,0 +1,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()));
}
}

}
Expand Up @@ -19,24 +19,17 @@
package org.teiid.core.types.basic;

import org.teiid.core.types.DataTypeManager;
import org.teiid.core.types.Transform;
import org.teiid.core.types.TransformationException;

public class NumberToShortTransform extends Transform {
public class NumberToShortTransform extends NumberToNumberTransform {

private Class<?> sourceType;
private boolean isNarrowing;

public NumberToShortTransform(Class<?> sourceType, boolean isNarrowing) {
this.sourceType = sourceType;
super(Short.MIN_VALUE, Short.MAX_VALUE, sourceType);
this.isNarrowing = isNarrowing;
}

@Override
public Class<?> getSourceType() {
return sourceType;
}

/**
* This method transforms a value of the source type into a value
* of the target type.
Expand All @@ -47,7 +40,7 @@ public Class<?> getSourceType() {
*/
public Object transformDirect(Object value) throws TransformationException {
if (isNarrowing) {
checkValueRange(value, Short.MIN_VALUE, Short.MAX_VALUE);
checkValueRange(value);
}
return Short.valueOf(((Number)value).shortValue());
}
Expand Down

0 comments on commit f57ee25

Please sign in to comment.