added next java imp

This commit is contained in:
Willem 2015-10-22 21:53:48 +02:00
parent dbd8a2e8fd
commit 34c339eee7
37 changed files with 2139 additions and 5 deletions

2
.gitignore vendored
View file

@ -1,5 +1,5 @@
# #
# Ignore some files in the ff-unit-converter project. # Ignore some files in the ff-unitxc project.
# #
# Ignore all maven target directories # Ignore all maven target directories

View file

@ -6,4 +6,16 @@
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
</parent> </parent>
<artifactId>ff-unitxc-converter</artifactId> <artifactId>ff-unitxc-converter</artifactId>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
</project> </project>

View file

@ -0,0 +1,88 @@
/*
* Copyright (c) 2014-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import net.forwardfire.unitxc.config.UnitXCConfig;
import net.forwardfire.unitxc.config.UnitXCConfigManager;
import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.converter.step.UnitXCTypeDevideConverterStep;
import net.forwardfire.unitxc.converter.step.UnitXCTypeMultiplyConverterStep;
import net.forwardfire.unitxc.converter.step.UnitXCTypeMultiplyFractionConverterStep;
import net.forwardfire.unitxc.converter.step.UnitXCTypeOffsetConverterStep;
import net.forwardfire.unitxc.module.UnitXCElectricCurrentModule;
import net.forwardfire.unitxc.module.UnitXCTemperatureModule;
public final class UnitXCFactory {
private static final List<UnitXCConfigModule> DEFAULT_CONFIG_MODULES = Collections.unmodifiableList(Arrays.asList(
new UnitXCTemperatureModule(),
new UnitXCElectricCurrentModule()
));
protected UnitXCFactory() {
}
public static UnitXCConfig createConfig() {
return createConfig(DEFAULT_CONFIG_MODULES);
}
public static UnitXCConfig createConfig(List<UnitXCConfigModule> configInit) {
return UnitXCConfigModule.buildAll(new UnitXCConfig(), configInit);
}
public static UnitXCManager createManager() {
return createManager(createConfig());
}
public static UnitXCManager createManager(UnitXCConfig config) {
return new UnitXCConfigManager(config);
}
public static class MathStep {
public static UnitXCTypeOffsetConverterStep add(double offset) {
return offset(offset,true);
}
public static UnitXCTypeOffsetConverterStep substract(double offset) {
return offset(offset,false);
}
public static UnitXCTypeOffsetConverterStep offset(double offset,boolean offsetPositive) {
return new UnitXCTypeOffsetConverterStep(offset,offsetPositive);
}
public static UnitXCTypeMultiplyFractionConverterStep multiplyFraction(int numerator,int denominator) {
return new UnitXCTypeMultiplyFractionConverterStep(numerator,denominator);
}
public static UnitXCTypeMultiplyConverterStep multiply(double factor) {
return new UnitXCTypeMultiplyConverterStep(factor);
}
public static UnitXCTypeDevideConverterStep divide(double factor) {
return new UnitXCTypeDevideConverterStep(factor);
}
}
}

View file

@ -0,0 +1,47 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc;
import java.util.Collection;
import net.forwardfire.unitxc.converter.UnitXConverter;
import net.forwardfire.unitxc.model.UnitXCType;
import net.forwardfire.unitxc.model.UnitXCTypeGroup;
public interface UnitXCManager {
Collection<UnitXCType> getUnitTypes();
UnitXCType getUnitType(String id);
boolean isUnitType(String id);
Collection<UnitXCTypeGroup> getUnitTypeGroups();
UnitXCTypeGroup getUnitTypeGroup(String id);
boolean isUnitTypeGroup(String id);
UnitXConverter getConverter();
}

View file

@ -0,0 +1,79 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.config;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.Validate;
import net.forwardfire.unitxc.model.UnitXCTypeModel;
import net.forwardfire.unitxc.model.UnitXCTypeGroupModel;
public final class UnitXCConfig {
private final Map<String,UnitXCTypeModel> unitTypes;
private final Map<String,UnitXCTypeGroupModel> unitTypeGroups;
public UnitXCConfig() {
unitTypeGroups = new HashMap<>();
unitTypes = new HashMap<>();
}
/**
* @return the unitTypes
*/
public Collection<UnitXCTypeModel> getUnitTypes() {
return unitTypes.values();
}
public void addUnitType(UnitXCTypeModel unitType) {
putUnitType(validateGroupId(Validate.notNull(unitType).validate()));
}
private UnitXCTypeModel validateGroupId(UnitXCTypeModel unitType) {
Validate.isTrue(unitTypeGroups.containsKey(unitType.getTypeGroupId()));
return unitType;
}
private void putUnitType(UnitXCTypeModel unitType) {
unitTypes.put(unitType.getId(), unitType);
}
/**
* @return the unitTypeGroups
*/
public Collection<UnitXCTypeGroupModel> getUnitTypeGroups() {
return unitTypeGroups.values();
}
public void addUnitTypeGroup(UnitXCTypeGroupModel unitTypeGroup) {
putUnitTypeGroup(Validate.notNull(unitTypeGroup).validate());
}
private void putUnitTypeGroup(UnitXCTypeGroupModel unitTypeGroup) {
unitTypeGroups.put(unitTypeGroup.getId(),unitTypeGroup);
}
}

View file

@ -0,0 +1,84 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.config;
import org.apache.commons.lang3.Validate;
import net.forwardfire.unitxc.converter.step.UnitXConverterStep;
public final class UnitXCConfigBuildTypeMultiples {
private final String type;
private final String typeName;
private final String flag;
private final UnitXConverterStep[] toConverters;
private final UnitXConverterStep[] fromConverters;
public UnitXCConfigBuildTypeMultiples(String type,String typeName,String flag) {
this(type,typeName,flag,new UnitXConverterStep[]{},new UnitXConverterStep[]{});
}
public UnitXCConfigBuildTypeMultiples(String type,String typeName,String flag,UnitXConverterStep[] toConverters,UnitXConverterStep[] fromConverters) {
this.type = Validate.notNull(type);
this.typeName = Validate.notNull(typeName);
this.flag = Validate.notNull(flag);
this.toConverters = Validate.notNull(toConverters);
this.fromConverters = Validate.notNull(fromConverters);
}
/**
* @return the type
*/
public String getType() {
return type;
}
/**
* @return the typeName
*/
public String getTypeName() {
return typeName;
}
/**
* @return the flag
*/
public String getFlag() {
return flag;
}
/**
* @return the toConverters
*/
public UnitXConverterStep[] getToConverters() {
return toConverters;
}
/**
* @return the fromConverters
*/
public UnitXConverterStep[] getFromConverters() {
return fromConverters;
}
}

View file

@ -0,0 +1,130 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.config;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.Validate;
import net.forwardfire.unitxc.UnitXCManager;
import net.forwardfire.unitxc.converter.UnitXConverter;
import net.forwardfire.unitxc.converter.UnitXConverterEngine;
import net.forwardfire.unitxc.model.UnitXCTypeModel;
import net.forwardfire.unitxc.model.UnitXCTypeGroupModel;
import net.forwardfire.unitxc.model.UnitXCType;
import net.forwardfire.unitxc.model.UnitXCTypeGroup;
import net.forwardfire.unitxc.model.UnitXConverterResult;
public class UnitXCConfigManager implements UnitXCManager {
private final UnitXCConfig config;
private final Map<String,UnitXCType> unitTypes;
private final Map<String,UnitXCTypeGroup> unitTypeGroups;
private final UnitXConverter converter;
private final UnitXConverterEngine convertEngine;
public UnitXCConfigManager(UnitXCConfig config) {
this.config = Validate.notNull(config);
this.unitTypes = Collections.unmodifiableMap(createUnitTypes(config.getUnitTypes()));
this.unitTypeGroups = Collections.unmodifiableMap(createUnitTypeGroups(config.getUnitTypeGroups()));
this.convertEngine = new UnitXConverterEngine(this);
this.converter = new UnitXConverterImpl();
}
private static Map<String,UnitXCType> createUnitTypes(Collection<UnitXCTypeModel> values) {
Map<String,UnitXCType> result = new HashMap<>();
values.forEach((value) -> result.put(value.getId(), value));
return result;
}
private static Map<String,UnitXCTypeGroup> createUnitTypeGroups(Collection<UnitXCTypeGroupModel> values) {
Map<String,UnitXCTypeGroup> result = new HashMap<>();
values.forEach((value) -> result.put(value.getId(), value));
return result;
}
@Override
public Collection<UnitXCType> getUnitTypes() {
return unitTypes.values();
}
@Override
public UnitXCType getUnitType(String id) {
return Validate.notNull(unitTypes.get(Validate.notNull(id,"Null is not a validate id.")),"No type for: "+id);
}
@Override
public boolean isUnitType(String id) {
return unitTypes.containsKey(id);
}
@Override
public Collection<UnitXCTypeGroup> getUnitTypeGroups() {
return unitTypeGroups.values();
}
@Override
public UnitXCTypeGroup getUnitTypeGroup(String id) {
return Validate.notNull(unitTypeGroups.get(Validate.notNull(id,"Null is not a validate id.")),"No group for: "+id);
}
@Override
public boolean isUnitTypeGroup(String id) {
return unitTypeGroups.containsKey(id);
}
@Override
public UnitXConverter getConverter() {
return converter;
}
private class UnitXConverterImpl implements UnitXConverter {
@Override
public double convert(double value, String fromTypeId, String toTypeId) {
return convert(value, getUnitType(fromTypeId), getUnitType(toTypeId));
}
@Override
public double convert(double value, UnitXCType fromType, UnitXCType toType) {
return convertStepped(value,fromType,toType).getResultValue();
}
@Override
public UnitXConverterResult convertStepped(double value, String fromTypeId, String toTypeId) {
return convertStepped(value, getUnitType(fromTypeId), getUnitType(toTypeId));
}
@Override
public UnitXConverterResult convertStepped(double value, UnitXCType fromType, UnitXCType toType) {
// TODO: rm casting
return convertEngine.convertStepped(value, (UnitXCTypeModel)fromType, (UnitXCTypeModel)toType);
}
}
}

View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.config;
import java.util.List;
public interface UnitXCConfigModule {
void configModule(UnitXCConfigModuleBuilder config);
static UnitXCConfig buildAll(UnitXCConfig config,List<UnitXCConfigModule> configInit) {
UnitXCConfigModuleBuilder builder = new UnitXCConfigModuleBuilder(config);
configInit.forEach(ci -> ci.configModule(builder));
return config;
}
}

View file

@ -0,0 +1,164 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.config;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang3.Validate;
import net.forwardfire.unitxc.converter.step.UnitXCTypeExponentConverterStep;
import net.forwardfire.unitxc.converter.step.UnitXConverterStep;
import net.forwardfire.unitxc.model.UnitXCTypeModel;
import net.forwardfire.unitxc.model.UnitXCTypeGroupModel;
public class UnitXCConfigModuleBuilder {
private final UnitXCConfig config;
public UnitXCConfigModuleBuilder(UnitXCConfig config) {
this.config = Validate.notNull(config);
}
public void addUnitTypeGroup(String id,String baseTypeId,String name,String description) {
config.addUnitTypeGroup(new UnitXCTypeGroupModel(id,name,description,baseTypeId));
}
//private void addUnitType(String groupId,String id,int factorExp,String name) {
// addUnitType(conv,groupId, id, factorExp, name,null);
//}
private void addUnitType(List<UnitXConverterStep> toConverters,List<UnitXConverterStep> fromConverters,String groupId,String id,int factorExp,String name,List<String> typeFlags) {
UnitXCTypeModel result = new UnitXCTypeModel();
result.setId(id);
result.setName(name);
if (factorExp != 0) {
result.addToBaseConverterStep(new UnitXCTypeExponentConverterStep(factorExp,false));
}
toConverters.forEach((conv) ->result.addToBaseConverterStep(conv));
fromConverters.forEach((conv) ->result.addFromBaseConverterStep(conv));
if (factorExp != 0) {
result.addFromBaseConverterStep(new UnitXCTypeExponentConverterStep(factorExp,true));
}
result.setTypeGroupId(groupId);
typeFlags.forEach((flag) -> result.addTypeFlag(flag));
config.addUnitType(result);
}
public enum SiMultiple {
YOTTA ("Y", 24),
ZETTA ("Z", 21),
EXA ("E", 18),
PETA ("P", 15),
TERA ("T", 12),
GIGA ("G", 9),
MEGA ("M", 6),
KILO ("k", 3),
HECTO ("h", 2),
DECA ("da",1),
DECI ("d", -1),
CENTI ("c", -2),
MILLI ("m", -3),
MICRO ("µ", -6),
NANO ("n", -9),
PICO ("p", -12),
FEMTO ("f", -15),
ATTO ("a", -18),
ZEPTO ("z", -21),
YOCTO ("y", -24),
;
public static final String TYPE_FLAG_SI_UNIT = "SI_UNIT";
public static final String TYPE_FLAG_SI_UNIT_BIGGER = "SI_UNIT_BIGGER";
public static final String TYPE_FLAG_SI_UNIT_BIGGEST = "SI_UNIT_BIGGEST";
private final String id;
private final String name;
private final int exponent;
private final List<String> flags;
private SiMultiple(String id,int exponent) {
this.id=id;
this.name = this.name().toLowerCase();
this.exponent=exponent;
List<String> f = new ArrayList<>(3);
f.add(TYPE_FLAG_SI_UNIT);
if (exponent > 3 || exponent < -3) {
f.add(TYPE_FLAG_SI_UNIT_BIGGER);
}
if (exponent > 12 || exponent < -12) {
f.add(TYPE_FLAG_SI_UNIT_BIGGEST);
}
this.flags = Collections.unmodifiableList(f);
}
public String getPrefixId() {
return id;
}
public String getPrefixName() {
return name;
}
public int getExponent() {
return exponent;
}
public List<String> getFlags() {
return flags;
}
}
public static final String TYPE_FLAG_SI_UNIT_SILLY = "SI_UNIT_SILLY";
public void addSIMultiples(String groupId,String type,String typeName,String flag) {
List<String> flags = new ArrayList<>();
flags.add(flag);
createTypesSIMultiples(groupId,type,typeName,flags,new ArrayList<>(),new ArrayList<>(),false);
}
public void addSIMultiplesSilly(String groupId,UnitXCConfigBuildTypeMultiples builder) {
List<String> flags = new ArrayList<>();
flags.add(builder.getFlag());
createTypesSIMultiples(groupId,builder.getType(),builder.getTypeName(),flags,Arrays.asList(builder.getToConverters()),Arrays.asList(builder.getFromConverters()),true);
}
private void createTypesSIMultiples(String groupId,String type,String typeName,List<String> flags,List<UnitXConverterStep> toConverters,List<UnitXConverterStep> fromConverters,boolean addSilly) {
// Create base unit
addUnitType(toConverters,fromConverters,groupId,type,0,typeName,flags);
// Create si values
for (SiMultiple sim:SiMultiple.values()) {
List<String> typeFlags = new ArrayList<>();
typeFlags.addAll(flags);
typeFlags.addAll(sim.getFlags());
if (addSilly) {
typeFlags.add(TYPE_FLAG_SI_UNIT_SILLY);
}
addUnitType(toConverters,fromConverters, groupId, sim.getPrefixId()+type, sim.getExponent(), sim.getPrefixName()+typeName, typeFlags);
}
}
}

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @author willemc
*
*/
package net.forwardfire.unitxc.config.builder;

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @author willemc
*
*/
package net.forwardfire.unitxc.config;

View file

@ -1,8 +1,38 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.converter; package net.forwardfire.unitxc.converter;
import net.forwardfire.unitxc.model.UnitXCType;
import net.forwardfire.unitxc.model.UnitXConverterResult;
public interface UnitXConverter { public interface UnitXConverter {
double convert(double value,String fromTypeId,String toTypeId); double convert(double value,String fromTypeId,String toTypeId);
double convert(double value/*,UnitXType fromType,UnitXType toType*/); double convert(double value,UnitXCType fromType,UnitXCType toType);
UnitXConverterResult convertStepped(double value,String fromTypeId,String toTypeId);
UnitXConverterResult convertStepped(double value,UnitXCType fromType,UnitXCType toType);
} }

View file

@ -0,0 +1,121 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.converter;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.forwardfire.unitxc.UnitXCManager;
import net.forwardfire.unitxc.config.UnitXCConfig;
import net.forwardfire.unitxc.converter.step.UnitXConverterStep;
import net.forwardfire.unitxc.model.UnitXCTypeModel;
import net.forwardfire.unitxc.model.UnitXCTypeGroup;
import net.forwardfire.unitxc.model.UnitXConverterResult;
import net.forwardfire.unitxc.model.UnitXConverterResultModel;
import net.forwardfire.unitxc.model.UnitXConverterResultStep;
import net.forwardfire.unitxc.model.UnitXConverterResultStepModel;
public final class UnitXConverterEngine {
private final static Logger LOG = LoggerFactory.getLogger(UnitXConverterEngine.class);
private final UnitXCManager manager;
public UnitXConverterEngine(UnitXCManager manager) {
this.manager = manager;
}
public UnitXConverterResult convertStepped(double value,UnitXCTypeModel fromType,UnitXCTypeModel toType) {
long startTime = System.currentTimeMillis();
double startValue = value;
List<UnitXConverterResultStep> resultSteps = new ArrayList<>();
UnitXCTypeGroup fromTypeGroup = manager.getUnitTypeGroup(fromType.getTypeGroupId());
UnitXCTypeGroup toTypeGroup = manager.getUnitTypeGroup(toType.getTypeGroupId());
boolean fromTypeBase = fromTypeGroup.getBaseTypeId().equals(fromType.getId());
if (!fromTypeBase) {
value = runConverter(resultSteps,fromType, value, true);
}
boolean toTypeBase = toTypeGroup.getBaseTypeId().equals(toType.getId());
if (!toTypeBase) {
value = runConverter(resultSteps, toType, value, false);
}
long convertTime = System.currentTimeMillis()-startTime;
UnitXConverterResultModel result = new UnitXConverterResultModel(startValue,value,resultSteps,convertTime, fromType, toType);
return result;
}
private double runConverter(List<UnitXConverterResultStep> resultSteps,UnitXCTypeModel type,double value,boolean toBase) {
double valueOld = value;
List<UnitXConverterStep> steps = toBase?type.getToBaseConverterSteps():type.getFromBaseConverterSteps();
for (UnitXConverterStep step:steps) {
valueOld = value;
long startTime = System.currentTimeMillis();
value = step.convert(value);
value = runRounding(resultSteps,value,valueOld);
long convertTime = System.currentTimeMillis()-startTime;
resultSteps.add(new UnitXConverterResultStepModel(step.getName()+(toBase?"-toBase":"-fromBase")+" = "+step.getMathExpression(),valueOld,value,convertTime));
}
return value;
}
private double runRounding(List<UnitXConverterResultStep> resultSteps,double value,double valueOrg) {
long startTime = System.currentTimeMillis();
String valueStr = Double.toString(value);
String valueOrgStr = Double.toString(valueOrg);
int valueDotIndex = valueStr.indexOf(".");
int valueOrgDotIndex = valueOrgStr.indexOf(".");
int valueDotSize = valueStr.length() - valueDotIndex;
int valueOrgDotSize = valueOrgStr.length() - valueOrgDotIndex;
if (valueDotSize<10) {
return value;
}
if (valueDotSize<=valueOrgDotSize) {
return value;
}
int valueEIndex = valueStr.indexOf("E");
String valueShortStr = null;
if (valueEIndex>0) {
int sub = valueOrgStr.length();
if (sub > valueEIndex) {
sub = valueEIndex;
}
valueShortStr = valueStr.substring(0, sub)+ valueStr.substring(valueEIndex);
} else {
valueShortStr = valueStr.substring(0, valueOrgStr.length());
}
Double result = new Double(valueShortStr);
long roundTime = System.currentTimeMillis()-startTime;
resultSteps.add(new UnitXConverterResultStepModel("rouding",value,result,roundTime));
return result.doubleValue();
}
}

View file

@ -1,5 +1,24 @@
/** /*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
/** /**
* @author willemc * @author willemc

View file

@ -0,0 +1,39 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.converter.step;
import org.apache.commons.lang3.Validate;
public abstract class AbstractUnitXConverterStep implements UnitXConverterStep {
private final String name;
protected AbstractUnitXConverterStep(String name) {
this.name = Validate.notBlank(name);
}
public final String getName() {
return name;
}
}

View file

@ -0,0 +1,48 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.converter.step;
import org.apache.commons.lang3.Validate;
public class UnitXCTypeDevideConverterStep extends AbstractUnitXConverterStep {
private final static String STEP_NAME = "Devide";
private final double factor;
public UnitXCTypeDevideConverterStep(double factor) {
super(STEP_NAME);
this.factor = factor;
Validate.isTrue(factor != 0);
}
@Override
public double convert(double value) {
return value/factor;
}
@Override
public String getMathExpression() {
return "/"+factor;
}
}

View file

@ -0,0 +1,68 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.converter.step;
public class UnitXCTypeExponentConverterStep extends AbstractUnitXConverterStep {
private final static String STEP_NAME = "Exponent";
private final int factor;
private final int factorShift;
private final boolean factorReverse;
public UnitXCTypeExponentConverterStep(int factor,boolean factorReverse) {
super(factor+STEP_NAME);
this.factor = factor;
this.factorReverse = factorReverse;
this.factorShift = factor<0?0-factor:factor;
}
@Override
public String getMathExpression() {
return "*10^"+factor;
}
@Override
public double convert(double value) {
for (int i=0;i<factorShift;i++) {
value = shiftDotStatic(value,factorReverse?!(factor>0):factor>0);
}
return value;
}
private double shiftDotStatic(double value,boolean up) {
String valueStr = ""+value;
int dotIdx = valueStr.indexOf('.');
StringBuilder buf = new StringBuilder();
buf.append(valueStr);
buf.deleteCharAt(dotIdx);
if (up) {
buf.insert(dotIdx+1, '.');
} else {
buf.insert(dotIdx-1, '.');
}
Double result = new Double(buf.toString());
return result.doubleValue();
}
}

View file

@ -0,0 +1,45 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.converter.step;
public class UnitXCTypeMultiplyConverterStep extends AbstractUnitXConverterStep {
private final static String STEP_NAME = "Factor Converter";
private final double factor;
public UnitXCTypeMultiplyConverterStep(double factor) {
super(STEP_NAME);
this.factor = factor;
}
@Override
public double convert(double value) {
return value*factor;
}
@Override
public String getMathExpression() {
return "*"+factor;
}
}

View file

@ -0,0 +1,47 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.converter.step;
public class UnitXCTypeMultiplyFractionConverterStep extends AbstractUnitXConverterStep {
private final static String STEP_NAME = "FactorFaction";
private final int numerator;
private final int denominator;
public UnitXCTypeMultiplyFractionConverterStep(int numerator,int denominator) {
super(STEP_NAME);
this.numerator = numerator;
this.denominator = denominator;
}
@Override
public double convert(double value) {
return value*((double)numerator/(double)denominator);
}
@Override
public String getMathExpression() {
return "*("+numerator+"/"+denominator+")";
}
}

View file

@ -0,0 +1,55 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.converter.step;
public class UnitXCTypeOffsetConverterStep extends AbstractUnitXConverterStep {
private final static String STEP_NAME = "Offset";
private final double offset;
private final boolean offsetPositive;
public UnitXCTypeOffsetConverterStep(double offset,boolean offsetPositive) {
super(STEP_NAME);
this.offset = offset;
this.offsetPositive = offsetPositive;
}
@Override
public double convert(double value) {
if (offsetPositive) {
return value+offset;
} else {
return value-offset;
}
}
@Override
public String getMathExpression() {
if (offsetPositive) {
return "+"+offset;
} else {
return "-"+offset;
}
}
}

View file

@ -0,0 +1,33 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.converter.step;
public interface UnitXConverterStep {
String getName();
String getMathExpression();
double convert(double value);
}

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @author willemc
*
*/
package net.forwardfire.unitxc.converter.step;

View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.model;
import java.util.Collection;
public interface UnitXCType {
String getId();
String getName();
String getTypeGroupId();
Collection<String> getTypeFlags();
}

View file

@ -0,0 +1,35 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.model;
public interface UnitXCTypeGroup {
String getId();
String getName();
String getDescription();
String getBaseTypeId();
}

View file

@ -0,0 +1,108 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.model;
import org.apache.commons.lang3.Validate;
public class UnitXCTypeGroupModel implements UnitXCTypeGroup {
private String id;
private String name;
private String description;
private String baseTypeId;
public UnitXCTypeGroupModel() {
}
public UnitXCTypeGroupModel(String id,String name,String description,String baseTypeId) {
setId(id);
setName(name);
setDescription(description);
setBaseTypeId(baseTypeId);
}
public UnitXCTypeGroupModel validate() {
Validate.notBlank(id);
Validate.notBlank(name);
Validate.notBlank(description);
Validate.notBlank(baseTypeId);
return this;
}
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the description
*/
public String getDescription() {
return description;
}
/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @return the baseTypeId
*/
public String getBaseTypeId() {
return baseTypeId;
}
/**
* @param baseTypeId the baseTypeId to set
*/
public void setBaseTypeId(String baseTypeId) {
this.baseTypeId = baseTypeId;
}
}

View file

@ -0,0 +1,122 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.model;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.lang3.Validate;
import net.forwardfire.unitxc.converter.step.UnitXConverterStep;
public class UnitXCTypeModel implements UnitXCType {
private String id;
private String name;
private String typeGroupId;
private final List<UnitXConverterStep> toBaseConverterSteps;
private final List<UnitXConverterStep> fromBaseConverterSteps;
private final List<String> typeFlags;
public UnitXCTypeModel() {
typeFlags = new ArrayList<>();
toBaseConverterSteps = new ArrayList<>();
fromBaseConverterSteps = new ArrayList<>();
}
public UnitXCTypeModel validate() {
Validate.notBlank(id);
Validate.notBlank(name);
Validate.notBlank(typeGroupId);
return this;
}
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the typeGroupId
*/
public String getTypeGroupId() {
return typeGroupId;
}
/**
* @param typeGroupId the typeGroupId to set
*/
public void setTypeGroupId(String typeGroupId) {
this.typeGroupId = typeGroupId;
}
public List<UnitXConverterStep> getToBaseConverterSteps() {
return toBaseConverterSteps;
}
public void addToBaseConverterStep(UnitXConverterStep unitTypeConverter) {
toBaseConverterSteps.add(Validate.notNull(unitTypeConverter));
}
public List<UnitXConverterStep> getFromBaseConverterSteps() {
return fromBaseConverterSteps;
}
public void addFromBaseConverterStep(UnitXConverterStep unitTypeConverter) {
fromBaseConverterSteps.add(Validate.notNull(unitTypeConverter));
}
@Override
public Collection<String> getTypeFlags() {
return typeFlags;
}
public void addTypeFlag(String flag) {
typeFlags.add(flag);
}
}

View file

@ -0,0 +1,41 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.model;
import java.util.List;
public interface UnitXConverterResult {
Double getStartValue();
UnitXCType getStartType();
Double getResultValue();
UnitXCType getResultType();
List<UnitXConverterResultStep> getSteps();
Long getConvertTime();
}

View file

@ -0,0 +1,77 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.model;
import java.util.List;
import org.apache.commons.lang3.Validate;
public class UnitXConverterResultModel implements UnitXConverterResult {
private final Double startValue;
private final List<UnitXConverterResultStep> resultSteps;
private final Double resultValue;
private final Long convertTime;
private final UnitXCType startType;
private final UnitXCType resultType;
public UnitXConverterResultModel(Double startValue,Double resultValue,List<UnitXConverterResultStep> resultSteps,Long convertTime,UnitXCType startType,UnitXCType resultType) {
this.startValue = Validate.notNull(startValue);
this.resultSteps = Validate.noNullElements(resultSteps);
this.resultValue = Validate.notNull(resultValue);
this.convertTime = convertTime;
this.startType = Validate.notNull(startType);
this.resultType = Validate.notNull(resultType);
}
@Override
public Double getStartValue() {
return startValue;
}
@Override
public Double getResultValue() {
return resultValue;
}
@Override
public List<UnitXConverterResultStep> getSteps() {
return resultSteps;
}
@Override
public Long getConvertTime() {
return convertTime;
}
@Override
public UnitXCType getStartType() {
return startType;
}
@Override
public UnitXCType getResultType() {
return resultType;
}
}

View file

@ -0,0 +1,35 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.model;
public interface UnitXConverterResultStep {
String getName();
Double getInputValue();
Double getOutputValue();
Long getConvertTime();
}

View file

@ -0,0 +1,61 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.model;
import org.apache.commons.lang3.Validate;
public class UnitXConverterResultStepModel implements UnitXConverterResultStep {
private final String name;
private final Double inputValue;
private final Double outputValue;
private final Long convertTime;
public UnitXConverterResultStepModel(String name,Double inputValue,Double outputValue,Long convertTime) {
this.name = Validate.notBlank(name);
this.inputValue = Validate.notNull(inputValue);
this.outputValue = Validate.notNull(outputValue);
this.convertTime = Validate.notNull(convertTime);
}
@Override
public String getName() {
return name;
}
@Override
public Double getInputValue() {
return inputValue;
}
@Override
public Double getOutputValue() {
return outputValue;
}
@Override
public Long getConvertTime() {
return convertTime;
}
}

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @author willemc
*
*/
package net.forwardfire.unitxc.model;

View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.module;
import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.UnitXCConfigModuleBuilder;
public class UnitXCElectricCurrentModule implements UnitXCConfigModule {
public static final String GROUP_ID = "current";
public static final String GROUP_DESCRIPTION = "The ampere is a measure of the amount of electric charge passing a point in an electric circuit per unit time, with 6.241×1018 electrons (or one coulomb) per second constituting one ampere.";
public static final String TYPE_AMPERE_ID = "A";
public static final String TYPE_AMPERE_NAME = "ampere";
public static final String TYPE_AMPERE_FLAG = (GROUP_ID+"_"+TYPE_AMPERE_NAME).toUpperCase();
@Override
public void configModule(UnitXCConfigModuleBuilder builder) {
builder.addUnitTypeGroup( GROUP_ID, TYPE_AMPERE_ID, TYPE_AMPERE_NAME, GROUP_DESCRIPTION );
builder.addSIMultiples( GROUP_ID, TYPE_AMPERE_ID, TYPE_AMPERE_NAME, TYPE_AMPERE_FLAG );
}
}

View file

@ -0,0 +1,88 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.module;
import net.forwardfire.unitxc.config.UnitXCConfigBuildTypeMultiples;
import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.UnitXCConfigModuleBuilder;
import net.forwardfire.unitxc.converter.step.UnitXConverterStep;
import static net.forwardfire.unitxc.UnitXCFactory.MathStep.add;
import static net.forwardfire.unitxc.UnitXCFactory.MathStep.substract;
import static net.forwardfire.unitxc.UnitXCFactory.MathStep.multiplyFraction;
public class UnitXCTemperatureModule implements UnitXCConfigModule {
public static final String GROUP_ID = "temperature";
public static final String GROUP_DESCRIPTION = "The kelvin, unit of thermodynamic temperature, is the fraction 1/273.16 of the thermodynamic temperature of the triple point of water.";
public static final String TYPE_KELVIN_ID = "K";
public static final String TYPE_KELVIN_NAME = "kelvin";
public static final String TYPE_KELVIN_FLAG = (GROUP_ID+"_"+TYPE_KELVIN_NAME).toUpperCase();
public static final String TYPE_CELSIUS_ID = "°C";
public static final String TYPE_CELSIUS_NAME = "celsius";
public static final String TYPE_CELSIUS_FLAG = (GROUP_ID+"_"+TYPE_CELSIUS_NAME).toUpperCase();
private static final UnitXCConfigBuildTypeMultiples TYPE_CELSIUS_CONFIG = new UnitXCConfigBuildTypeMultiples(
TYPE_CELSIUS_ID,TYPE_CELSIUS_NAME,TYPE_CELSIUS_FLAG,
new UnitXConverterStep[]{add(273.15)},
new UnitXConverterStep[]{substract(273.15)}
);
public static final String TYPE_FAHRENHEIT_ID = "°F";
public static final String TYPE_FAHRENHEIT_NAME = "fahrenheit";
public static final String TYPE_FAHRENHEIT_FLAG = (GROUP_ID+"_"+TYPE_FAHRENHEIT_NAME).toUpperCase();
private static final UnitXCConfigBuildTypeMultiples TYPE_FAHRENHEIT_CONFIG = new UnitXCConfigBuildTypeMultiples(
TYPE_FAHRENHEIT_ID,TYPE_FAHRENHEIT_NAME,TYPE_FAHRENHEIT_FLAG,
new UnitXConverterStep[]{add(459.67),multiplyFraction(5,9)},
new UnitXConverterStep[]{multiplyFraction(9,5),substract(459.67)}
);
public static final String TYPE_RANKINE_ID = "°R";
public static final String TYPE_RANKINE_NAME = "rankine";
public static final String TYPE_RANKINE_FLAG = (GROUP_ID+"_"+TYPE_RANKINE_NAME).toUpperCase();
private static final UnitXCConfigBuildTypeMultiples TYPE_RANKINE_CONFIG = new UnitXCConfigBuildTypeMultiples(
TYPE_RANKINE_ID,TYPE_RANKINE_NAME,TYPE_RANKINE_FLAG,
new UnitXConverterStep[]{multiplyFraction(5,9)},
new UnitXConverterStep[]{multiplyFraction(9,5)}
);
public static final String TYPE_ROMER_ID = "";
public static final String TYPE_ROMER_NAME = "rømer";
public static final String TYPE_ROMER_FLAG = GROUP_ID.toUpperCase()+"_ROMER";
private static final UnitXCConfigBuildTypeMultiples TYPE_ROMER_CONFIG = new UnitXCConfigBuildTypeMultiples(
TYPE_ROMER_ID,TYPE_ROMER_NAME,TYPE_ROMER_FLAG,
new UnitXConverterStep[]{add(273.15),multiplyFraction(40,21),add(7.5)},
new UnitXConverterStep[]{substract(7.5),multiplyFraction(21,40),substract(273.15)}
);
@Override
public void configModule(UnitXCConfigModuleBuilder builder) {
builder.addUnitTypeGroup( GROUP_ID, TYPE_KELVIN_ID, TYPE_KELVIN_NAME, GROUP_DESCRIPTION);
builder.addSIMultiples( GROUP_ID, TYPE_KELVIN_ID, TYPE_KELVIN_NAME, TYPE_KELVIN_FLAG);
builder.addSIMultiplesSilly(GROUP_ID, TYPE_CELSIUS_CONFIG);
builder.addSIMultiplesSilly(GROUP_ID, TYPE_FAHRENHEIT_CONFIG);
builder.addSIMultiplesSilly(GROUP_ID, TYPE_RANKINE_CONFIG);
builder.addSIMultiplesSilly(GROUP_ID, TYPE_ROMER_CONFIG);
}
}

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @author willemc
*
*/
package net.forwardfire.unitxc.module;

View file

@ -0,0 +1,49 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import net.forwardfire.unitxc.UnitXCFactory;
import net.forwardfire.unitxc.converter.UnitXConverter;
public class UnitXCAssert {
private static final UnitXConverter DEFAULT_CONVERTER = UnitXCFactory.createManager().getConverter();
private static final double DEFAULT_VALUE_DELTA = 0;
public static void convertEquals(double expected,String toType,double fromValue,String fromType) {
convertEquals(DEFAULT_CONVERTER,expected, toType, fromValue, fromType);
}
public static void convertEquals(UnitXConverter converter,double expected,String toType,double fromValue,String fromType) {
convertEquals(converter, expected, toType, fromValue, fromType, DEFAULT_VALUE_DELTA);
}
public static void convertEquals(UnitXConverter converter,double expected,String toType,double fromValue,String fromType,double valueDelta) {
assertNotNull(converter);
assertEquals(expected,converter.convert(fromValue, fromType, toType),valueDelta);
}
}

View file

@ -0,0 +1,42 @@
package net.forwardfire.unitxc;
import org.junit.Test;
import net.forwardfire.unitxc.config.UnitXCConfig;
import static org.junit.Assert.assertNotNull;
import java.util.ArrayList;
public class UnitXCFactoryTest {
@Test
public void testCreateConfig() {
assertNotNull(UnitXCFactory.createConfig());
}
@Test
public void testCreateConfigList() {
assertNotNull(UnitXCFactory.createConfig(new ArrayList<>()));
}
@Test(expected=NullPointerException.class)
public void testCreateConfigListNull() {
assertNotNull(UnitXCFactory.createConfig(null));
}
@Test
public void testCreateManager() {
assertNotNull(UnitXCFactory.createManager());
}
@Test
public void testCreateManagerConfig() {
assertNotNull(UnitXCFactory.createManager(new UnitXCConfig()));
}
@Test(expected=NullPointerException.class)
public void testCreateManagerConfigNull() {
assertNotNull(UnitXCFactory.createManager(null));
}
}

View file

@ -0,0 +1,116 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.module;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertEquals;
import static net.forwardfire.unitxc.UnitXCAssert.convertEquals;
import net.forwardfire.unitxc.UnitXCFactory;
import net.forwardfire.unitxc.UnitXCManager;
import net.forwardfire.unitxc.converter.UnitXConverter;
import net.forwardfire.unitxc.model.UnitXCType;
import net.forwardfire.unitxc.model.UnitXConverterResult;
import net.forwardfire.unitxc.model.UnitXConverterResultStep;
public class UnitXCTemperatureModuleTest {
private final UnitXConverter um;
private final double VALUE_DELTA = 0;
public UnitXCTemperatureModuleTest() {
um = UnitXCFactory.createManager().getConverter();
}
@Test
public void testFac() {
UnitXCManager unitManager = UnitXCFactory.createManager();
assertNotNull(unitManager);
assertNotNull(unitManager.getUnitTypes());
assertNotNull(unitManager.getUnitTypeGroups());
assertFalse(unitManager.getUnitTypes().isEmpty());
assertFalse(unitManager.getUnitTypeGroups().isEmpty());
assertNotNull(unitManager.getUnitTypes());
for (UnitXCType type:unitManager.getUnitTypes()) {
System.out.println("type: "+type.getId()+"\t name: "+type.getName()+"\t\t flags: "+type.getTypeFlags());
}
System.out.println("totalTypes: "+unitManager.getUnitTypes().size());
UnitXConverterResult result = um.convertStepped(211.0, "k°F", "m°C");
System.out.println("Convert from: "+result.getStartType().getName()+" to: "+result.getResultType().getName());
System.out.println("startValue: "+result.getStartValue());
System.out.println("resultValue: "+result.getResultValue());
System.out.println("convertTime: "+result.getConvertTime());
for (UnitXConverterResultStep step:result.getSteps()) {
System.out.println("step: "+step.getName()+" in: "+step.getInputValue()+" out: "+step.getOutputValue()+" time: "+step.getConvertTime());
}
System.out.println("");
result = um.convertStepped(100.0, "K", "°C");
System.out.println("Convert from: "+result.getStartType().getName()+" to: "+result.getResultType().getName());
System.out.println("startValue: "+result.getStartValue());
System.out.println("resultValue: "+result.getResultValue());
System.out.println("convertTime: "+result.getConvertTime());
for (UnitXConverterResultStep step:result.getSteps()) {
System.out.println("step: "+step.getName()+" in: "+step.getInputValue()+" out: "+step.getOutputValue()+" time: "+step.getConvertTime());
}
}
@Test
public void testConverterTempKvsC() throws Exception {
assertEquals(26.85,um.convert(300.0, "K", "°C"),VALUE_DELTA);
assertEquals(310.65,um.convert(37.5, "°C", "K"),VALUE_DELTA);
}
@Test
public void testConverterTempKvsF() throws Exception {
assertEquals(80.32,um.convert(300.0, "K", "°F"),VALUE_DELTA);
assertEquals(276.2,um.convert(37.5, "°F", "K"),VALUE_DELTA);
assertEquals(276.208,um.convert(37.505, "°F", "K"),VALUE_DELTA);
}
@Test
public void testConverterTempFvsC() throws Exception {
//assertEquals(74.937,um.convert(166.888, "°F", "°C"),VALUE_DELTA);
//assertEquals(211.99,um.convert(100.0, "°C", "°F"),VALUE_DELTA);
convertEquals(74.937,"°C",166.888, "°F");
convertEquals(211.99,"°F",100.0, "°C");
}
@Test
public void testConverterTempRvsC() throws Exception {
convertEquals(-143.64,"°C",233.12,"°R");
convertEquals(1691.06,"°R",666.333,"°C");
}
}