String temp;
double mag1 = 1, mag2 = 1;
if( oldType.compareTo(newType) != 0)
throw new SSErrorXelException(SSError.NA);
// Units that have Prefix-enable:
// Weight and mass:
// g, u
//
// Length:
// m, ang
//
// Time:
// sec
//
// Pressure:
// p, atm, mmHg => all can have Prefix
//
// Force:
// N, dy
//
// Energy:
// J, e, c, cal, ev, wh
//
// Power:
// w
//
// Magnet:
// T, ga
//
// Temp:
// K
//
// Liquid:
// l
if ( oldType.equals("mass"))
{
if( oldUnit.endsWith("g") || oldUnit.endsWith("u"))
{
if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("g"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
else if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("u"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
}
if( newUnit.endsWith("g") || newUnit.endsWith("u"))
{
if ((temp = newUnit.substring(0, newUnit.lastIndexOf("g"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
else if ((temp = newUnit.substring(0, newUnit.lastIndexOf("u"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
}
//do unit convert.
MassUnit mass = new MassUnit( value * mag1, oldUnit);
return new Double( mass.doConvert(newUnit) / mag2);
}
else if (oldType.equals("dist"))
{
if( oldUnit.endsWith("m") || oldUnit.endsWith("ang"))
{
if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("m"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
else if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("ang"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
}
if( newUnit.endsWith("m") || newUnit.endsWith("ang"))
{
if ((temp = newUnit.substring(0, newUnit.lastIndexOf("g"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
else if ((temp = newUnit.substring(0, newUnit.lastIndexOf("u"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
}
//do unit convert.
LengthUnit length = new LengthUnit( value * mag1, oldUnit);
return new Double( length.doConvert(newUnit) / mag2);
}
else if (oldType.equals("time"))
{
if( oldUnit.endsWith("sec") )
{
if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("sec"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
}
if( newUnit.endsWith("sec") )
{
if ((temp = newUnit.substring(0, newUnit.lastIndexOf("sec"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
}
//do unit convert.
TimeUnit time = new TimeUnit( value * mag1, oldUnit);
return new Double( time.doConvert(newUnit) / mag2);
}
else if (oldType.equals("pres"))
{
if( oldUnit.endsWith("p") || oldUnit.endsWith("at") || oldUnit.endsWith("mmHg"))
{
if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("p"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
else if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("at"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
else if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("mmHg"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
}
if( newUnit.endsWith("p") || newUnit.endsWith("at") || oldUnit.endsWith("mmHg"))
{
if ((temp = newUnit.substring(0, newUnit.lastIndexOf("p"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
else if ((temp = newUnit.substring(0, newUnit.lastIndexOf("at"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
else if ((temp = newUnit.substring(0, newUnit.lastIndexOf("mmHg"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
}
//do prefix expand for output
PressUnit press = new PressUnit( value * mag1, oldUnit);
return new Double( press.doConvert(newUnit) / mag2);
}
else if (oldType.equals("forc"))
{
if( oldUnit.endsWith("N") || oldUnit.endsWith("dy") )
{
if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("N"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
else if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("dy"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
}
if( newUnit.endsWith("N") || newUnit.endsWith("dy"))
{
if ((temp = newUnit.substring(0, newUnit.lastIndexOf("N"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
else if ((temp = newUnit.substring(0, newUnit.lastIndexOf("dy"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
}
//do unit convert.
ForceUnit force = new ForceUnit( value * mag1, oldUnit);
return new Double( force.doConvert(newUnit) / mag2);
}
else if (oldType.equals("ener"))
{
if( oldUnit.endsWith("J") || oldUnit.endsWith("e") || oldUnit.endsWith("c") || oldUnit.endsWith("cal") || oldUnit.endsWith("ev") || oldUnit.endsWith("wh"))
{
if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("J"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
else if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("e"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
else if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("c"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
else if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("cal"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
else if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("ev"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
else if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("wh"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
}
if( newUnit.endsWith("J") || newUnit.endsWith("e") || newUnit.endsWith("c") || newUnit.endsWith("cal") || newUnit.endsWith("ev") || newUnit.endsWith("wh"))
{
if ((temp = newUnit.substring(0, newUnit.lastIndexOf("J"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
else if ((temp = newUnit.substring(0, newUnit.lastIndexOf("e"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
else if ((temp = newUnit.substring(0, newUnit.lastIndexOf("c"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
else if ((temp = newUnit.substring(0, newUnit.lastIndexOf("cal"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
else if ((temp = newUnit.substring(0, newUnit.lastIndexOf("ev"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
else if ((temp = newUnit.substring(0, newUnit.lastIndexOf("wh"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
}
//do unit convert.
EnergyUnit energy = new EnergyUnit( value * mag1, oldUnit);
return new Double( energy.doConvert(newUnit) / mag2);
}
else if (oldType.equals("powe"))
{
if( oldUnit.endsWith("w") )
{
if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("w"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
}
if( newUnit.endsWith("w") )
{
if ((temp = newUnit.substring(0, newUnit.lastIndexOf("w"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
}
//do unit convert.
PowerUnit power = new PowerUnit( value * mag1, oldUnit);
return new Double( power.doConvert(newUnit) / mag2);
}
else if (oldType.equals("magn"))
{
if( oldUnit.endsWith("T") || oldUnit.endsWith("ga") )
{
if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("T"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
else if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("ga"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
}
if( newUnit.endsWith("T") || newUnit.endsWith("ga"))
{
if ((temp = newUnit.substring(0, newUnit.lastIndexOf("T"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
else if ((temp = newUnit.substring(0, newUnit.lastIndexOf("ga"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
}
//do unit convert.
MagnetUnit magnet = new MagnetUnit( value * mag1, oldUnit);
return new Double( magnet.doConvert(newUnit) / mag2);
}
else if (oldType.equals("temp"))
{
if( oldUnit.endsWith("K") )
{
if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("T"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
}
if( newUnit.endsWith("K") )
{
if ((temp = newUnit.substring(0, newUnit.lastIndexOf("T"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
}
//do unit convert.
TempUnit temperatrue = new TempUnit( value * mag1, oldUnit);
return new Double( temperatrue.doConvert(newUnit) / mag2);
}
else if (oldType.equals("liqu"))
{
if( oldUnit.endsWith("l") )
{
if ((temp = oldUnit.substring(0, oldUnit.lastIndexOf("T"))) != null)
{
//do prefix expand
mag1 = expandPrefix(temp);
}
}
if( newUnit.endsWith("l") )
{
if ((temp = newUnit.substring(0, newUnit.lastIndexOf("T"))) != null)
{
//do prefix expand for output
mag2 = expandPrefix(temp);
}
}
//do unit convert.
FluidUnit fluid = new FluidUnit( value * mag1, oldUnit);
return new Double( fluid.doConvert(newUnit) / mag2);
}
else
throw new SSErrorXelException(SSError.VALUE);
}
catch ( NullPointerException e) {
throw new SSErrorXelException(SSError.NA);
}
catch ( IndexOutOfBoundsException e) {
throw new SSErrorXelException(SSError.NA);
}
catch ( ConvertUnitException e) {
throw new SSErrorXelException(SSError.VALUE);
}
}