remove clone left over and fixed alias resolving.

This commit is contained in:
Willem 2016-10-24 17:38:35 +02:00
parent 5076d2be55
commit 5d95084252
12 changed files with 35 additions and 44 deletions

View file

@ -49,6 +49,7 @@ public class UnitXCTypeCompoundExponentBuilder extends AbstractUnitXCBuilder<Uni
super(parent,new Object(), (p,v) -> {});
this.unitGroupId = unitGroupId;
this.exponent = exponent;
UnitXCGroup model = parent.getValue();
parent.getValue().setTypeGenerator(new UnitXCTypeGenerator() {
UnitXCGroup getGroup() {
@ -82,8 +83,24 @@ public class UnitXCTypeCompoundExponentBuilder extends AbstractUnitXCBuilder<Uni
};
}
public UnitXCType getUnitTypeAlias(String id) {
for (UnitXCType type:model.getUnitTypes()) {
if (type.getId().equals(id)) {
return type;
}
if (type.getName().equals(id)) {
return type;
}
}
return null;
}
@Override
public UnitXCType getUnitType(String id) {
UnitXCType al = getUnitTypeAlias(id);
if (al != null) {
return al;
}
if (!id.endsWith(unitIdPostfix)) {
return null;
}

View file

@ -50,7 +50,7 @@ public class UnitXConverter {
}
private UnitXCType getUnitType(String id) {
return unitManager.getUnitType(id);
return Validate.notNull(unitManager.getUnitType(id),"Could not resolve unit type for id; '"+id+"'");
}
public double convert(double value, String fromTypeId, String toTypeId) {

View file

@ -66,6 +66,18 @@ public final class UnitXConverterEngine {
}
public UnitXConverterResult convertStepped(BigDecimal valueConvert2,UnitXCType fromType,UnitXCType toType, Map<String,UnitXConverterParameterValue> parameters) {
Validate.notNull(valueConvert2,"Can't convert null to number");
Validate.notNull(fromType,"Can't convert from null unit type");
Validate.notNull(toType,"Can't convert to null unit type");
if (fromType.getAliasOfType() != null) {
fromType = Validate.notNull(manager.getUnitType(fromType.getAliasOfType()),"Coultn't resolve alias id: "+fromType.getAliasOfType()+" for: "+fromType.getId());
}
if (toType.getAliasOfType() != null) {
toType = Validate.notNull(manager.getUnitType(toType.getAliasOfType()),"Coultn't resolve alias id: "+toType.getAliasOfType()+" for: "+toType.getId());
}
double valueConvert = valueConvert2.doubleValue();
long startTime = System.currentTimeMillis();
MathContext mathContext = MathContext.DECIMAL128;
@ -178,7 +190,7 @@ public final class UnitXConverterEngine {
step.runStep(this);
long convertTime = System.currentTimeMillis()-startTime;
System.out.println("runStep: "+step+" res: "+step.getStepReasons());
// System.out.println("runStep: "+step+" res: "+step.getStepReasons());
UnitXConverterResultStep resultStep = new UnitXConverterResultStep();
resultStep.setStartValue(valueOld);

View file

@ -52,13 +52,6 @@ public class UnitXConverterStepValueNamedParameter implements UnitXConverterStep
return ctx.getNamedParameter(parameterName).valueRead(ctx);
}
@Override
public UnitXConverterStepValueRead clone() {
UnitXConverterStepValueNamedParameter clone = new UnitXConverterStepValueNamedParameter();
clone.setParameterName(getParameterName());
return clone;
}
/**
* @return the parameterName
*/

View file

@ -58,14 +58,6 @@ public class UnitXConverterStepValueNamedVariable implements UnitXConverterStepV
public void valueWrite(UnitXConverterStepContext ctx,Object value) {
ctx.getNamedVariable(variableName).valueWrite(ctx,value);
}
@Override
public UnitXConverterStepValueReadWrite clone() {
UnitXConverterStepValueNamedVariable clone = new UnitXConverterStepValueNamedVariable();
clone.setValueType(getValueType());
clone.setVariableName(getVariableName());
return clone;
}
/**
* @return the variableName

View file

@ -31,9 +31,7 @@ import net.forwardfire.unitxc.model.step.UnitXConverterStepContext;
* @author Willem Cazander
* @version 1.0 Apr 1, 2016
*/
public interface UnitXConverterStepValueRead extends Cloneable {
public interface UnitXConverterStepValueRead {
Object valueRead(UnitXConverterStepContext ctx);
UnitXConverterStepValueRead clone();
}

View file

@ -34,6 +34,4 @@ import net.forwardfire.unitxc.model.step.UnitXConverterStepContext;
public interface UnitXConverterStepValueReadWrite extends UnitXConverterStepValueRead {
void valueWrite(UnitXConverterStepContext ctx,Object value);
UnitXConverterStepValueReadWrite clone();
}

View file

@ -57,11 +57,6 @@ public class UnitXConverterStepValueReferenceDouble implements UnitXConverterSte
this.value = Double.class.cast(value);
}
@Override
public UnitXConverterStepValueReadWrite clone() {
return new UnitXConverterStepValueReferenceDouble(getValue());
}
/**
* @return the value
*/

View file

@ -57,11 +57,6 @@ public class UnitXConverterStepValueReferenceDoubleFraction implements UnitXConv
return (Double)(((Integer)numerator).doubleValue()/((Integer)denominator).doubleValue()); // FIXME
}
@Override
public UnitXConverterStepValueRead clone() {
return new UnitXConverterStepValueReferenceDoubleFraction(getNumerator(),getDenominator());
}
/**
* @return the numerator
*/

View file

@ -57,11 +57,6 @@ public class UnitXConverterStepValueReferenceInteger implements UnitXConverterSt
this.value = Integer.class.cast(value);
}
@Override
public UnitXConverterStepValueReadWrite clone() {
return new UnitXConverterStepValueReferenceInteger(getValue());
}
/**
* @return the value
*/

View file

@ -57,11 +57,6 @@ public class UnitXConverterStepValueReferenceString implements UnitXConverterSte
this.value = String.class.cast(value);
}
@Override
public UnitXConverterStepValueReadWrite clone() {
return new UnitXConverterStepValueReferenceString(getValue());
}
/**
* @return the value
*/

View file

@ -1,6 +1,7 @@
package net.forwardfire.unitxc;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
@ -27,9 +28,9 @@ public class UnitXCManagerTest {
assertNotNull(test.getUnitType(null));
}
@Test(expected=NullPointerException.class)
@Test
public void testGetUnitTypeInvalid() {
assertNotNull(test.getUnitType(getClass().getName()));
assertNull(test.getUnitType(getClass().getName()));
}
@Test