diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/UnitXCFactory.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/UnitXCFactory.java index 3a16411..edce40e 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/UnitXCFactory.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/UnitXCFactory.java @@ -36,6 +36,7 @@ import net.forwardfire.unitxc.module.UnitXCModuleElectricCurrent; import net.forwardfire.unitxc.module.UnitXCModuleLength; import net.forwardfire.unitxc.module.UnitXCModuleLuminousIntensity; import net.forwardfire.unitxc.module.UnitXCModuleMass; +import net.forwardfire.unitxc.module.UnitXCModuleSpeed; import net.forwardfire.unitxc.module.UnitXCModuleTemperature; import net.forwardfire.unitxc.module.UnitXCModuleTime; import net.forwardfire.unitxc.module.UnitXCModuleVolume; @@ -57,7 +58,8 @@ public final class UnitXCFactory { new UnitXCModuleMass(), new UnitXCModuleTime(), new UnitXCModuleArea(), - new UnitXCModuleVolume() + new UnitXCModuleVolume(), + new UnitXCModuleSpeed() )); diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/UnitXCConfig.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/UnitXCConfig.java index 14cc799..2ece067 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/UnitXCConfig.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/UnitXCConfig.java @@ -29,8 +29,8 @@ import java.util.Map; import org.apache.commons.lang3.Validate; -import net.forwardfire.unitxc.model.UnitXCTypeModel; -import net.forwardfire.unitxc.model.UnitXCGroupModel; +import net.forwardfire.unitxc.model.UnitXCType; +import net.forwardfire.unitxc.model.UnitXCGroup; /** * Config to build unit types and groups. @@ -40,8 +40,8 @@ import net.forwardfire.unitxc.model.UnitXCGroupModel; */ public final class UnitXCConfig { - private final Map unitTypes; - private final Map unitGroups; + private final Map unitTypes; + private final Map unitGroups; public UnitXCConfig() { unitGroups = new HashMap<>(); @@ -51,35 +51,35 @@ public final class UnitXCConfig { /** * @return the unitTypes */ - public Collection getUnitTypes() { + public Collection getUnitTypes() { return unitTypes.values(); } - public void addUnitType(UnitXCTypeModel unitType) { + public void addUnitType(UnitXCType unitType) { putUnitType(validateGroupId(Validate.notNull(unitType).validate())); } - private UnitXCTypeModel validateGroupId(UnitXCTypeModel unitType) { + private UnitXCType validateGroupId(UnitXCType unitType) { //Validate.isTrue(unitTypeGroups.containsKey(unitType.getTypeGroupId()),"group is missing"); return unitType; } - private void putUnitType(UnitXCTypeModel unitType) { + private void putUnitType(UnitXCType unitType) { unitTypes.put(unitType.getId(), unitType); } /** * @return the unitGroups */ - public Collection getUnitroups() { + public Collection getUnitroups() { return unitGroups.values(); } - public void addUnitGroup(UnitXCGroupModel unitTypeGroup) { + public void addUnitGroup(UnitXCGroup unitTypeGroup) { putUnitGroup(Validate.notNull(unitTypeGroup).validate()); } - private void putUnitGroup(UnitXCGroupModel unitTypeGroup) { + private void putUnitGroup(UnitXCGroup unitTypeGroup) { unitGroups.put(unitTypeGroup.getId(),unitTypeGroup); } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/UnitXCConfigManager.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/UnitXCConfigManager.java index ea31e22..8ef78cf 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/UnitXCConfigManager.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/UnitXCConfigManager.java @@ -33,8 +33,6 @@ 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.UnitXCGroupModel; import net.forwardfire.unitxc.model.UnitXCType; import net.forwardfire.unitxc.model.UnitXCGroup; import net.forwardfire.unitxc.model.UnitXConverterResult; @@ -62,13 +60,29 @@ public class UnitXCConfigManager implements UnitXCManager { this.converter = new UnitXConverterImpl(); } - private static Map createUnitTypes(Collection values) { + private static Map createUnitTypes(Collection values) { Map result = new HashMap<>(); - values.forEach((value) -> result.put(value.getId(), value)); + values.forEach((value) -> { + putValue(result,value.getId(),value); + if (!value.getId().equals(value.getName())) { // fix time group + putValue(result,value.getName(),value); + } + if (!value.getName().equals(value.getNamePlural())) { // mmm normal + putValue(result,value.getNamePlural(),value); + } + }); return result; } - private static Map createUnitGroups(Collection values) { + private static void putValue(Map result,String key,UnitXCType value) { + if (key == null) { + return; + } + Validate.isTrue(!result.containsKey(key),"Duplicate key: "+key); + result.put(key, value); + } + + private static Map createUnitGroups(Collection values) { Map result = new HashMap<>(); values.forEach((value) -> result.put(value.getId(), value)); return result; @@ -130,7 +144,7 @@ public class UnitXCConfigManager implements UnitXCManager { @Override public UnitXConverterResult convertStepped(double value, UnitXCType fromType, UnitXCType toType) { // TODO: rm casting - return convertEngine.convertStepped(value, (UnitXCTypeModel)fromType, (UnitXCTypeModel)toType); + return convertEngine.convertStepped(value, (UnitXCType)fromType, (UnitXCType)toType); } } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/AbstractUnitXCTypeBuilder.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/AbstractUnitXCTypeBuilder.java index 1885729..a6e042f 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/AbstractUnitXCTypeBuilder.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/AbstractUnitXCTypeBuilder.java @@ -29,7 +29,7 @@ import java.util.List; import java.util.function.BiConsumer; import net.forwardfire.unitxc.converter.step.UnitXConverterStep; -import net.forwardfire.unitxc.model.UnitXCTypeModel; +import net.forwardfire.unitxc.model.UnitXCType; /** * @@ -37,10 +37,23 @@ import net.forwardfire.unitxc.model.UnitXCTypeModel; * @author Willem Cazander * @version 1.0 Oct 22, 2015 */ -public abstract class AbstractUnitXCTypeBuilder extends AbstractUnitXCBuilder { +public abstract class AbstractUnitXCTypeBuilder extends AbstractUnitXCBuilder { - public AbstractUnitXCTypeBuilder(P parent,BiConsumer parentBuilder) { - super(parent, new UnitXCTypeModel(), parentBuilder); + public AbstractUnitXCTypeBuilder(P parent,UnitXCType model,BiConsumer parentBuilder) { + super(parent, model, parentBuilder); + } + + protected final void buildPreValue(UnitXCType value) { + if (value.getNamePlural() == null) { + setNamePlurals(); + } + if (value.getAliasOfType() != null && !value.getTypeFlags().contains(UnitXCConfigBuilder.TYPE_FLAG_ALIAS)) { + value.getTypeFlags().add(UnitXCConfigBuilder.TYPE_FLAG_ALIAS); + } + buildModel(value); + } + + protected void buildModel(UnitXCType value) { } public B setId(String id) { @@ -51,28 +64,40 @@ public abstract class AbstractUnitXCTypeBuilder extends AbstractUnitXCBuild return make((v) -> v.setName(name)); } - public B addNameAlias(String nameAlias) { - return make((v) -> v.addNameAlias(nameAlias)); + public B setNamePlurals() { + return setNamePlural(getValue().getName()+"s"); } - public B addNameAliases(Collection nameAliases) { - return make((v) -> nameAliases.forEach(a -> v.addNameAlias(a))); + public B setNamePlural(String name) { + return make((v) -> v.setNamePlural(name)); } - public B addNameAliases(String[] nameAliases) { - return addNameAliases(Arrays.asList(nameAliases)); + public B setAliasOfType(String aliasOfType) { + return make((v) -> v.setAliasOfType(aliasOfType)); } public B setTypeGroupId(String typeGroupId) { return make((v) -> v.setUnitGroupId(typeGroupId)); } + public B addWebsiteWiki(String websiteLink) { + return addWebsiteLink("https://en.wikipedia.org/wiki/"+websiteLink); + } + + public B addWebsiteLink(String websiteLink) { + return make((v) -> v.getWebsiteLinks().add(websiteLink)); + } + + public B addWebsiteLinks(Collection websiteLinks) { + return make((v) -> v.getWebsiteLinks().addAll(websiteLinks)); + } + public B addTypeFlag(String flag) { - return make((v) -> v.addTypeFlag(flag)); + return make((v) -> v.getTypeFlags().add(flag)); } public B addTypeFlags(Collection flags) { - return make(v -> flags.forEach(flag -> v.addTypeFlag(flag))); + return make(v -> flags.forEach(flag -> v.getTypeFlags().add(flag))); } public B addTypeFlags(String[] flags) { @@ -88,18 +113,18 @@ public abstract class AbstractUnitXCTypeBuilder extends AbstractUnitXCBuild } public B addFromBaseConverterStep(UnitXConverterStep unitTypeConverter) { - return make((v) -> v.addFromBaseConverterStep(unitTypeConverter)); + return make((v) -> v.getFromBaseConverterSteps().add(unitTypeConverter)); } public B addFromBaseConverterSteps(List unitTypeConverters) { - return make((v) -> unitTypeConverters.forEach(c -> v.addFromBaseConverterStep(c))); + return make((v) -> unitTypeConverters.forEach(c -> v.getFromBaseConverterSteps().add(c))); } public B addToBaseConverterStep(UnitXConverterStep unitTypeConverter) { - return make((v) -> v.addToBaseConverterStep(unitTypeConverter)); + return make((v) -> v.getToBaseConverterSteps().add(unitTypeConverter)); } public B addToBaseConverterSteps(List unitTypeConverters) { - return make((v) -> unitTypeConverters.forEach(c -> v.addToBaseConverterStep(c))); + return make((v) -> unitTypeConverters.forEach(c -> v.getToBaseConverterSteps().add(c))); } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCConfigBuilder.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCConfigBuilder.java index 1f9a17d..9bb8891 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCConfigBuilder.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCConfigBuilder.java @@ -26,6 +26,7 @@ package net.forwardfire.unitxc.config.builder; import org.apache.commons.lang3.Validate; import net.forwardfire.unitxc.config.UnitXCConfig; +import net.forwardfire.unitxc.model.UnitXCType; /** * @@ -37,6 +38,7 @@ public class UnitXCConfigBuilder { public static final String TYPE_FLAG_IMPERIAL = "IMPERIAL"; // todo move to ... public static final String TYPE_FLAG_IMPERIAL_EXTRA = "IMPERIAL_EXTRA"; + public static final String TYPE_FLAG_ALIAS = "ALIAS"; private final UnitXCConfig config; @@ -53,6 +55,19 @@ public class UnitXCConfigBuilder { } public UnitXCTypeBuilder createUnitType() { - return new UnitXCTypeBuilder<>(this,(p,v) -> p.getConfig().addUnitType(v)); + return new UnitXCTypeBuilder<>(this,new UnitXCType(),(p,v) -> p.getConfig().addUnitType(v)); + } + + private UnitXCType findType(String id) { + for (UnitXCType m:config.getUnitTypes()) { + if (m.getId().equals(id)) { + return m; + } + } + throw new IllegalArgumentException("Could not find: "+id); + } + + public UnitXCTypeBuilder extendUnitType(String id) { + return new UnitXCTypeBuilder<>(this,findType(id),(p,v) -> {}); } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCGroupBuilder.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCGroupBuilder.java index bd9fd25..a36bf20 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCGroupBuilder.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCGroupBuilder.java @@ -23,8 +23,9 @@ package net.forwardfire.unitxc.config.builder; -import net.forwardfire.unitxc.model.UnitXCGroupModel; -import net.forwardfire.unitxc.model.UnitXCGroupType; +import net.forwardfire.unitxc.model.UnitXCGroup; +import net.forwardfire.unitxc.model.UnitXCGroupLevel; +import net.forwardfire.unitxc.model.UnitXCType; /** * @@ -32,10 +33,10 @@ import net.forwardfire.unitxc.model.UnitXCGroupType; * @author Willem Cazander * @version 1.0 Oct 22, 2015 */ -public class UnitXCGroupBuilder extends AbstractUnitXCBuilder { +public class UnitXCGroupBuilder extends AbstractUnitXCBuilder { public UnitXCGroupBuilder(UnitXCConfigBuilder parent) { - super(parent, new UnitXCGroupModel(), (p,v) -> p.getConfig().addUnitGroup(v)); + super(parent, new UnitXCGroup(), (p,v) -> p.getConfig().addUnitGroup(v)); } @Override @@ -44,18 +45,21 @@ public class UnitXCGroupBuilder extends AbstractUnitXCBuilder createUnitType() { - return new UnitXCTypeBuilder<>(this,(p,v) -> p.getParent().getConfig().addUnitType(v)).setTypeGroupId(getValue().getId()); + return new UnitXCTypeBuilder<>(this,new UnitXCType(),(p,v) -> p.getParent().getConfig().addUnitType(v)).setTypeGroupId(getValue().getId()); } public UnitXCTypeSIPrefixBuilder createSIUnitTypes() { return new UnitXCTypeSIPrefixBuilder<>(this,getParent()).setTypeGroupId(getValue().getId()); } - public UnitXCTypeCompoundExponentBuilder createCompoundExponentUnitTypes(String unitTypeGroupId,int exponent) { return new UnitXCTypeCompoundExponentBuilder(this,getParent(),unitTypeGroupId,exponent); } + public UnitXCTypeCompoundPairBuilder createCompoundPairUnitTypes(String groupId,String perGroupId) { + return new UnitXCTypeCompoundPairBuilder(this,getParent(),groupId,perGroupId); + } + public UnitXCGroupBuilder setId(String id) { return make((v) -> v.setId(id)); } @@ -72,8 +76,8 @@ public class UnitXCGroupBuilder extends AbstractUnitXCBuilder v.setBaseTypeId(baseTypeId)); } - public UnitXCGroupBuilder setType(UnitXCGroupType type) { - return make((v) -> v.setType(type)); + public UnitXCGroupBuilder setGroupLevel(UnitXCGroupLevel type) { + return make((v) -> v.setGroupLevel(type)); } // public UnitXCGroupBuilder addDerivedFrom(String fromId) { diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeBuilder.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeBuilder.java index 013cf49..cf9dbca 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeBuilder.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeBuilder.java @@ -25,7 +25,7 @@ package net.forwardfire.unitxc.config.builder; import java.util.function.BiConsumer; -import net.forwardfire.unitxc.model.UnitXCTypeModel; +import net.forwardfire.unitxc.model.UnitXCType; /** * @@ -35,8 +35,8 @@ import net.forwardfire.unitxc.model.UnitXCTypeModel; */ public class UnitXCTypeBuilder

extends AbstractUnitXCTypeBuilder> { - public UnitXCTypeBuilder(P parent, BiConsumer parentBuilder) { - super(parent, parentBuilder); + public UnitXCTypeBuilder(P parent,UnitXCType model, BiConsumer parentBuilder) { + super(parent,model, parentBuilder); } @Override diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeCompoundExponentBuilder.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeCompoundExponentBuilder.java index df4d1a3..2f8c275 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeCompoundExponentBuilder.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeCompoundExponentBuilder.java @@ -29,8 +29,8 @@ import java.util.List; import org.apache.commons.lang3.Validate; import net.forwardfire.unitxc.converter.step.UnitXConverterStep; -import net.forwardfire.unitxc.model.UnitXCGroupModel; -import net.forwardfire.unitxc.model.UnitXCTypeModel; +import net.forwardfire.unitxc.model.UnitXCGroup; +import net.forwardfire.unitxc.model.UnitXCType; /** * @@ -61,8 +61,8 @@ public class UnitXCTypeCompoundExponentBuilder extends AbstractUnitXCBuilder(builder.getConfig().getUnitTypes())) { + for (UnitXCType unitType:new ArrayList<>(builder.getConfig().getUnitTypes())) { if (!unitType.getUnitGroupId().equals(group.getId())) { continue; } UnitXCTypeBuilder typeBuilder = builder.createUnitType() .setId(unitType.getId()+unitIdPostfix) .setName(unitNamePrefix+unitType.getName()) - .setTypeGroupId(group.getId()) + .setTypeGroupId(getParent().getValue().getId()) .addTypeFlags(unitType.getTypeFlags()); for (int i=0;i addStepReason(int exponent,List steps) { - if (exponent == 0) { - return steps; + private List cloneSteps(int exponent,List steps) { + List result = new ArrayList<>(); + for (UnitXConverterStep step:steps) { + UnitXConverterStep clone = step.clone(); + if (exponent != 0) { + clone.addStepReason("CompoundExponent: "+exponent); + } + result.add(clone); } - steps.forEach(step -> step.addStepReason("CompoundeExponent: "+exponent)); - return steps; + return result; } public UnitXCTypeCompoundExponentBuilder setUnitIdPostfix(String unitIdPostfix) { diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeCompoundPairBuilder.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeCompoundPairBuilder.java new file mode 100644 index 0000000..f768499 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeCompoundPairBuilder.java @@ -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.config.builder; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang3.Validate; + +import net.forwardfire.unitxc.converter.step.UnitXConverterStep; +import net.forwardfire.unitxc.model.UnitXCGroup; +import net.forwardfire.unitxc.model.UnitXCType; + +/** + * + * + * @author Willem Cazander + * @version 1.0 Oct 26, 2015 + */ +public class UnitXCTypeCompoundPairBuilder extends AbstractUnitXCBuilder { + + public static final String PER_ID = "/"; + public static final String PER_NAME = " per "; + + private final UnitXCConfigBuilder builder; + private final String groupId; + private final String perGroupId; + + public UnitXCTypeCompoundPairBuilder(UnitXCGroupBuilder parent, UnitXCConfigBuilder builder,String groupId,String perGroupId) { + super(parent,new Object(), (p,v) -> {}); + this.builder = builder; + this.groupId = groupId; + this.perGroupId = perGroupId; + } + + @Override + protected UnitXCTypeCompoundPairBuilder getBuilder() { + return this; + } + + @Override + protected void buildPreValue(Object v) { + + UnitXCGroup group = null; + UnitXCGroup perGroup = null; + for (UnitXCGroup unitGroup:builder.getConfig().getUnitroups()) { + if (unitGroup.getId().equals(groupId)) { + group = unitGroup; + } + if (unitGroup.getId().equals(perGroupId)) { + perGroup = unitGroup; + } + } + Validate.notNull(group,"Could not find group: "+groupId); + Validate.notNull(perGroup,"Could not find group: "+perGroupId); + + getParent().getValue().getDerivedFrom().add(groupId); + getParent().getValue().getDerivedFrom().add(perGroupId); + + List types = new ArrayList<>(builder.getConfig().getUnitTypes()); + + for (UnitXCType unitType:types) { + if (!unitType.getUnitGroupId().equals(group.getId())) { + continue; + } + for (UnitXCType perUnitType:types) { + if (!perUnitType.getUnitGroupId().equals(perGroup.getId())) { + continue; + } + + // TODO: add name/name alias + UnitXCTypeBuilder typeBuilder = builder.createUnitType() + .setId(unitType.getId()+PER_ID+perUnitType.getId()) + .setName(unitType.getName()+PER_NAME+perUnitType.getName()) + .setTypeGroupId(getParent().getValue().getId()) + .addTypeFlags(unitType.getTypeFlags()) + .addTypeFlags(perUnitType.getTypeFlags()); + + typeBuilder.addToBaseConverterSteps(cloneSteps(unitType.getToBaseConverterSteps(),true)); + typeBuilder.addToBaseConverterSteps(cloneSteps(perUnitType.getFromBaseConverterSteps(),false)); + typeBuilder.addFromBaseConverterSteps(cloneSteps(perUnitType.getToBaseConverterSteps(),true)); + typeBuilder.addFromBaseConverterSteps(cloneSteps(unitType.getFromBaseConverterSteps(),false)); + typeBuilder.build(); + } + } + } + + private List cloneSteps(List steps,boolean pairBase) { + List result = new ArrayList<>(); + for (UnitXConverterStep step:steps) { + UnitXConverterStep clone = step.clone(); + if (pairBase) { + clone.addStepReason("CompoundPair base"); + } else { + clone.addStepReason("CompoundPair reverse"); + } + result.add(clone); + } + return result; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeSIPrefixBuilder.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeSIPrefixBuilder.java index cc28f31..98c73e4 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeSIPrefixBuilder.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeSIPrefixBuilder.java @@ -29,7 +29,7 @@ import java.util.Collection; import java.util.List; import net.forwardfire.unitxc.converter.step.UnitXCTypePowerOfTenConverterStep; -import net.forwardfire.unitxc.model.UnitXCTypeModel; +import net.forwardfire.unitxc.model.UnitXCType; /** * @@ -43,7 +43,7 @@ public class UnitXCTypeSIPrefixBuilder

extends AbstractUnitXCTypeBuilder commonSIPrefixes; public UnitXCTypeSIPrefixBuilder(P parent, UnitXCConfigBuilder builder) { - super(parent, (p,v) -> {}); + super(parent,new UnitXCType(), (p,v) -> {}); this.builder = builder; this.commonSIPrefixes = new ArrayList<>(); } @@ -54,11 +54,12 @@ public class UnitXCTypeSIPrefixBuilder

extends AbstractUnitXCTypeBuilder extends AbstractUnitXCTypeBuilder> } private UnitXConverterStepBuilder

offset(double offset,boolean offsetPositive,String stepReason) { - return make(v -> v.add(new UnitXCTypeOffsetConverterStep(offset,offsetPositive,stepReason))); + return make(v -> v.add(new UnitXCTypeOffsetConverterStep(offset,offsetPositive).buildReason(stepReason))); } public UnitXConverterStepBuilder

offsetUp(double offset,String stepReason) { return offset(offset,true,stepReason); @@ -77,10 +77,10 @@ public class UnitXConverterStepBuilder

> public UnitXConverterStepBuilder

multiply(Fraction fraction,String stepReason) { - return make(v -> v.add(new UnitXCTypeMultiplyFractionConverterStep(fraction,stepReason))); + return make(v -> v.add(new UnitXCTypeMultiplyFractionConverterStep(fraction.getNumerator(),fraction.getDenominator()).buildReason(stepReason))); } public UnitXConverterStepBuilder

multiply(int numerator,int denominator,String stepReason) { - return make(v -> v.add(new UnitXCTypeMultiplyFractionConverterStep(numerator,denominator,stepReason))); + return make(v -> v.add(new UnitXCTypeMultiplyFractionConverterStep(numerator,denominator).buildReason(stepReason))); } public UnitXConverterStepBuilder

multiply(Fraction fraction) { return multiply(fraction,createStepReason()); @@ -90,10 +90,10 @@ public class UnitXConverterStepBuilder

> } public UnitXConverterStepBuilder

multiply(double factor,String stepReason) { - return make(v -> v.add(new UnitXCTypeMultiplyConverterStep(factor,stepReason))); + return make(v -> v.add(new UnitXCTypeMultiplyConverterStep(factor).buildReason(stepReason))); } public UnitXConverterStepBuilder

divide(double factor,String stepReason) { - return make(v -> v.add(new UnitXCTypeDevideConverterStep(factor,stepReason))); + return make(v -> v.add(new UnitXCTypeDevideConverterStep(factor).buildReason(stepReason))); } public UnitXConverterStepBuilder

multiply(double factor) { return multiply(factor,createStepReason()); @@ -103,7 +103,7 @@ public class UnitXConverterStepBuilder

> } public UnitXConverterStepBuilder

power(int exponent,String stepReason) { - return make(v -> v.add(new UnitXCTypePowerConverterStep(exponent,stepReason))); + return make(v -> v.add(new UnitXCTypePowerConverterStep(exponent).buildReason(stepReason))); } public UnitXConverterStepBuilder

power(int exponent) { return power(exponent,createStepReason()); @@ -111,7 +111,7 @@ public class UnitXConverterStepBuilder

> private UnitXConverterStepBuilder

powerOfTen(int exponent,boolean rev,String stepReason) { - return make(v -> v.add(new UnitXCTypePowerOfTenConverterStep(exponent,rev,stepReason))); + return make(v -> v.add(new UnitXCTypePowerOfTenConverterStep(exponent,rev).buildReason(stepReason))); } public UnitXConverterStepBuilder

power10Up(UnitXCTypeSIPrefix exponent,String stepReason) { return powerOfTen(exponent.getExponent(), true,stepReason); diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/UnitXConverterEngine.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/UnitXConverterEngine.java index ac2ac32..e296fa8 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/UnitXConverterEngine.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/UnitXConverterEngine.java @@ -30,14 +30,11 @@ 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.UnitXCGroup; +import net.forwardfire.unitxc.model.UnitXCType; import net.forwardfire.unitxc.model.UnitXConverterResult; -import net.forwardfire.unitxc.model.UnitXConverterResultModel; import net.forwardfire.unitxc.model.UnitXConverterResultStep; -import net.forwardfire.unitxc.model.UnitXConverterResultStepModel; /** * Runs requested convert steps and builds result models. @@ -54,7 +51,7 @@ public final class UnitXConverterEngine { this.manager = manager; } - public UnitXConverterResult convertStepped(double value,UnitXCTypeModel fromType,UnitXCTypeModel toType) { + public UnitXConverterResult convertStepped(double value,UnitXCType fromType,UnitXCType toType) { long startTime = System.currentTimeMillis(); double startValue = value; List resultSteps = new ArrayList<>(); @@ -73,11 +70,17 @@ public final class UnitXConverterEngine { long convertTime = System.currentTimeMillis()-startTime; - UnitXConverterResultModel result = new UnitXConverterResultModel(startValue,value,resultSteps,convertTime, fromType, toType); + UnitXConverterResult result = new UnitXConverterResult(); + result.setStartValue(startValue); + result.setStartValueType(fromType); + result.setResultValue(value); + result.setResultValueType(toType); + result.setConvertTime(convertTime); + result.setResultSteps(resultSteps); return result; } - private double runConverter(List resultSteps,UnitXCTypeModel type,double value,boolean toBase) { + private double runConverter(List resultSteps,UnitXCType type,double value,boolean toBase) { double valueOld = value; List steps = toBase?type.getToBaseConverterSteps():type.getFromBaseConverterSteps(); for (UnitXConverterStep step:steps) { @@ -86,7 +89,12 @@ public final class UnitXConverterEngine { long startTime = System.currentTimeMillis(); value = step.convert(value); long convertTime = System.currentTimeMillis()-startTime; - resultSteps.add(new UnitXConverterResultStepModel(step.getStepName()+(toBase?"-toBase":"-fromBase")+" = "+step.getMathExpression(),valueOld,value,convertTime)); + UnitXConverterResultStep resultStep = new UnitXConverterResultStep(); + resultStep.setName(step.getName()+(toBase?"-toBase":"-fromBase")+" = "+step.getMathExpression()); + resultStep.setStartValue(valueOld); + resultStep.setResultValue(value); + resultStep.setConvertTime(convertTime); + resultSteps.add(resultStep); value = runRounding(resultSteps,value,valueOld); @@ -124,7 +132,13 @@ public final class UnitXConverterEngine { Double result = new Double(valueShortStr); long roundTime = System.currentTimeMillis()-startTime; - resultSteps.add(new UnitXConverterResultStepModel("rouding",value,result,roundTime)); + UnitXConverterResultStep resultStep = new UnitXConverterResultStep(); + resultStep.setName("rounding"); + resultStep.setStartValue(value); + resultStep.setResultValue(result); + resultStep.setConvertTime(roundTime); + + resultSteps.add(resultStep); return result.doubleValue(); } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/AbstractUnitXConverterStep.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/AbstractUnitXConverterStep.java index 8d3f825..cb70033 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/AbstractUnitXConverterStep.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/AbstractUnitXConverterStep.java @@ -26,8 +26,14 @@ package net.forwardfire.unitxc.converter.step; import java.util.ArrayList; import java.util.List; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; + import org.apache.commons.lang3.Validate; +import net.forwardfire.unitxc.model.ModelXMLInfoSet; + /** * * @@ -36,27 +42,63 @@ import org.apache.commons.lang3.Validate; */ public abstract class AbstractUnitXConverterStep implements UnitXConverterStep { - private final String name; + private String name; private final List stepReasons; - protected AbstractUnitXConverterStep(String name,String stepReason) { - this.name = Validate.notBlank(name); + protected AbstractUnitXConverterStep(String name) { this.stepReasons = new ArrayList<>(); - this.addStepReason(stepReason); + this.setName(name); + } + + public AbstractUnitXConverterStep buildReason(String reason) { + addStepReason(reason); + return this; } @Override - public final String getStepName() { + @XmlAttribute(name=ModelXMLInfoSet.Attribute.NAME) + public final String getName() { return name; } + public void setName(String name) { + this.name = Validate.notBlank(name); + } + @Override + @XmlElement(name=ModelXMLInfoSet.Element.STEP_REASON) + @XmlElementWrapper(name=ModelXMLInfoSet.Element.STEP_REASONS) public final List getStepReasons() { return stepReasons; } + public final void setStepReasons(List stepReasons) { + this.stepReasons.clear(); + this.stepReasons.addAll(stepReasons); + } + @Override public final void addStepReason(String stepReason) { stepReasons.add(Validate.notBlank(stepReason)); } + + abstract protected String toMath(); + + @Override + public final String getMathExpression() { + return toMath(); + } + + public void setMathExpression(String notUsed) { + } + + @Override + public UnitXConverterStep clone() { + AbstractUnitXConverterStep clone = createClone(); + clone.setName(getName()); + clone.setStepReasons(getStepReasons()); + return clone; + } + + abstract protected AbstractUnitXConverterStep createClone(); } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeDevideConverterStep.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeDevideConverterStep.java index 8309055..261f859 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeDevideConverterStep.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeDevideConverterStep.java @@ -23,6 +23,8 @@ package net.forwardfire.unitxc.converter.step; +import javax.xml.bind.annotation.XmlRootElement; + import org.apache.commons.lang3.Validate; /** @@ -31,15 +33,24 @@ import org.apache.commons.lang3.Validate; * @author Willem Cazander * @version 1.0 Oct 20, 2015 */ +@XmlRootElement public class UnitXCTypeDevideConverterStep extends AbstractUnitXConverterStep { private final static String STEP_NAME = "Devide"; - private final double factor; + private double factor; - public UnitXCTypeDevideConverterStep(double factor,String stepReason) { - super(STEP_NAME,stepReason); - this.factor = factor; - Validate.isTrue(factor != 0); + public UnitXCTypeDevideConverterStep() { + super(STEP_NAME); + } + + public UnitXCTypeDevideConverterStep(double factor) { + this(); + setFactor(factor); + } + + @Override + protected AbstractUnitXConverterStep createClone() { + return new UnitXCTypeDevideConverterStep(getFactor()); } @Override @@ -48,7 +59,22 @@ public class UnitXCTypeDevideConverterStep extends AbstractUnitXConverterStep { } @Override - public String getMathExpression() { + protected String toMath() { return "/"+factor; } + + /** + * @return the factor + */ + public double getFactor() { + return factor; + } + + /** + * @param factor the factor to set + */ + public void setFactor(double factor) { + Validate.isTrue(factor != 0); + this.factor = factor; + } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeMultiplyConverterStep.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeMultiplyConverterStep.java index 19cdd53..dce438b 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeMultiplyConverterStep.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeMultiplyConverterStep.java @@ -23,20 +23,32 @@ package net.forwardfire.unitxc.converter.step; +import javax.xml.bind.annotation.XmlRootElement; + /** * * * @author Willem Cazander * @version 1.0 Oct 17, 2015 */ +@XmlRootElement public class UnitXCTypeMultiplyConverterStep extends AbstractUnitXConverterStep { private final static String STEP_NAME = "Multiply"; - private final double factor; + private double factor; - public UnitXCTypeMultiplyConverterStep(double factor,String stepReason) { - super(STEP_NAME,stepReason); - this.factor = factor; + public UnitXCTypeMultiplyConverterStep() { + super(STEP_NAME); + } + + public UnitXCTypeMultiplyConverterStep(double factor) { + this(); + setFactor(factor); + } + + @Override + protected AbstractUnitXConverterStep createClone() { + return new UnitXCTypeMultiplyConverterStep(getFactor()); } @Override @@ -45,7 +57,21 @@ public class UnitXCTypeMultiplyConverterStep extends AbstractUnitXConverterStep } @Override - public String getMathExpression() { + protected String toMath() { return "*"+factor; } + + /** + * @return the factor + */ + public double getFactor() { + return factor; + } + + /** + * @param factor the factor to set + */ + public void setFactor(double factor) { + this.factor = factor; + } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeMultiplyFractionConverterStep.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeMultiplyFractionConverterStep.java index f2f8573..653b830 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeMultiplyFractionConverterStep.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeMultiplyFractionConverterStep.java @@ -23,6 +23,8 @@ package net.forwardfire.unitxc.converter.step; +import javax.xml.bind.annotation.XmlRootElement; + import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.math.Fraction; @@ -32,12 +34,31 @@ import org.apache.commons.lang3.math.Fraction; * @author Willem Cazander * @version 1.0 Oct 20, 2015 */ +@XmlRootElement public class UnitXCTypeMultiplyFractionConverterStep extends AbstractUnitXConverterStep { private final static String STEP_NAME = "MultiplyFaction"; - private final Fraction fraction; - private final String fractionMath; + //private Fraction fraction; + //private final String fractionMath; + private int numerator; + private int denominator; + public UnitXCTypeMultiplyFractionConverterStep() { + super(STEP_NAME); + } + + public UnitXCTypeMultiplyFractionConverterStep(int numerator,int denominator) { + this(); + setNumerator(numerator); + setDenominator(denominator); + } + + @Override + protected AbstractUnitXConverterStep createClone() { + return new UnitXCTypeMultiplyFractionConverterStep(getNumerator(),getDenominator()); + } + + /* public UnitXCTypeMultiplyFractionConverterStep(int numerator,int denominator,String stepReason) { this(Fraction.getFraction(numerator, denominator),stepReason); } @@ -47,14 +68,44 @@ public class UnitXCTypeMultiplyFractionConverterStep extends AbstractUnitXConver this.fraction = Validate.notNull(fraction); this.fractionMath = String.format("*(%1s/%2s)", fraction.getNumerator(),fraction.getDenominator()); } - + */ @Override public double convert(double value) { - return value*fraction.doubleValue(); + //return value*fraction.doubleValue(); + return value*(numerator/denominator); } @Override - public String getMathExpression() { - return fractionMath; + protected String toMath() { + //return String.format("*(%1s/%2s)", fraction.getNumerator(),fraction.getDenominator()); + return String.format("*(%1s/%2s)", numerator,denominator); + } + + /** + * @return the numerator + */ + public int getNumerator() { + return numerator; + } + + /** + * @param numerator the numerator to set + */ + public void setNumerator(int numerator) { + this.numerator = numerator; + } + + /** + * @return the denominator + */ + public int getDenominator() { + return denominator; + } + + /** + * @param denominator the denominator to set + */ + public void setDenominator(int denominator) { + this.denominator = denominator; } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeOffsetConverterStep.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeOffsetConverterStep.java index 711b3ba..6396689 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeOffsetConverterStep.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeOffsetConverterStep.java @@ -23,22 +23,34 @@ package net.forwardfire.unitxc.converter.step; +import javax.xml.bind.annotation.XmlRootElement; + /** * * * @author Willem Cazander * @version 1.0 Oct 17, 2015 */ +@XmlRootElement public class UnitXCTypeOffsetConverterStep extends AbstractUnitXConverterStep { private final static String STEP_NAME = "Offset"; - private final double offset; - private final boolean offsetPositive; + private double offset; + private boolean offsetPositive; - public UnitXCTypeOffsetConverterStep(double offset,boolean offsetPositive,String stepReason) { - super(STEP_NAME,stepReason); - this.offset = offset; - this.offsetPositive = offsetPositive; + public UnitXCTypeOffsetConverterStep() { + super(STEP_NAME); + } + + public UnitXCTypeOffsetConverterStep(double offset,boolean offsetPositive) { + this(); + setOffset(offset); + setOffsetPositive(offsetPositive); + } + + @Override + protected AbstractUnitXConverterStep createClone() { + return new UnitXCTypeOffsetConverterStep(getOffset(),isOffsetPositive()); } @Override @@ -49,13 +61,44 @@ public class UnitXCTypeOffsetConverterStep extends AbstractUnitXConverterStep { return value-offset; } } - + @Override - public String getMathExpression() { + protected String toMath() { if (offsetPositive) { return "+"+offset; } else { return "-"+offset; } } + + /** + * @return the offset + */ + public double getOffset() { + return offset; + } + + /** + * @param offset the offset to set + */ + public void setOffset(double offset) { + this.offset = offset; + } + + /** + * @return the offsetPositive + */ + public boolean isOffsetPositive() { + return offsetPositive; + } + + /** + * @param offsetPositive the offsetPositive to set + */ + public void setOffsetPositive(boolean offsetPositive) { + this.offsetPositive = offsetPositive; + } + + + } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypePowerConverterStep.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypePowerConverterStep.java index 08b86a3..a295bca 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypePowerConverterStep.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypePowerConverterStep.java @@ -23,29 +23,55 @@ package net.forwardfire.unitxc.converter.step; +import javax.xml.bind.annotation.XmlRootElement; + /** * * * @author Willem Cazander * @version 1.0 Oct 26, 2015 */ +@XmlRootElement public class UnitXCTypePowerConverterStep extends AbstractUnitXConverterStep { private final static String STEP_NAME = "power"; - private final int exponent; + private int exponent; - public UnitXCTypePowerConverterStep(int exponent,String stepReason) { - super(STEP_NAME,stepReason); - this.exponent = exponent; + public UnitXCTypePowerConverterStep() { + super(STEP_NAME); + } + + public UnitXCTypePowerConverterStep(int exponent) { + this(); + setExponent(exponent); } @Override - public String getMathExpression() { - return "^"+exponent; + protected AbstractUnitXConverterStep createClone() { + return new UnitXCTypePowerConverterStep(getExponent()); } @Override public double convert(double value) { return Math.pow(value, exponent); } + + @Override + protected String toMath() { + return "/"+exponent; + } + + /** + * @return the exponent + */ + public int getExponent() { + return exponent; + } + + /** + * @param exponent the exponent to set + */ + public void setExponent(int exponent) { + this.exponent = exponent; + } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypePowerOfTenConverterStep.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypePowerOfTenConverterStep.java index fe174e0..fbc572f 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypePowerOfTenConverterStep.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypePowerOfTenConverterStep.java @@ -23,39 +23,51 @@ package net.forwardfire.unitxc.converter.step; +import javax.xml.bind.annotation.XmlRootElement; + /** * * * @author Willem Cazander * @version 1.0 Oct 17, 2015 */ +@XmlRootElement public class UnitXCTypePowerOfTenConverterStep extends AbstractUnitXConverterStep { private final static String STEP_NAME = "power(10)"; - private final int exponent; - private final int exponentShift; - private final boolean exponentReverse; + private int exponent; + private boolean exponentReverse; - public UnitXCTypePowerOfTenConverterStep(int factor,boolean factorReverse,String stepReason) { - super(STEP_NAME,stepReason); - this.exponent = factor; - this.exponentReverse = factorReverse; - this.exponentShift = factor<0?0-factor:factor; + public UnitXCTypePowerOfTenConverterStep() { + super(STEP_NAME); + } + + public UnitXCTypePowerOfTenConverterStep(int exponent,boolean exponentReverse) { + this(); + setExponent(exponent); + setExponentReverse(exponentReverse); } @Override - public String getMathExpression() { - return "*10^"+exponent; + protected AbstractUnitXConverterStep createClone() { + return new UnitXCTypePowerOfTenConverterStep(getExponent(),isExponentReverse()); } @Override public double convert(double value) { + int exponentShift = exponent<0?0-exponent:exponent; for (int i=0;i0):exponent>0); } return value; } + + @Override + protected String toMath() { + return "*10^"+exponent; + } + private double shiftDotStatic(double value,boolean up) { String valueStr = ""+value; int dotIdx = valueStr.indexOf('.'); @@ -71,4 +83,34 @@ public class UnitXCTypePowerOfTenConverterStep extends AbstractUnitXConverterSte Double result = new Double(buf.toString()); return result.doubleValue(); } + + /** + * @return the exponent + */ + public int getExponent() { + return exponent; + } + + /** + * @param exponent the exponent to set + */ + public void setExponent(int exponent) { + this.exponent = exponent; + } + + /** + * @return the exponentReverse + */ + public boolean isExponentReverse() { + return exponentReverse; + } + + /** + * @param exponentReverse the exponentReverse to set + */ + public void setExponentReverse(boolean exponentReverse) { + this.exponentReverse = exponentReverse; + } + + } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXConverterStep.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXConverterStep.java index b3a2098..d500a00 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXConverterStep.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXConverterStep.java @@ -31,15 +31,17 @@ import java.util.List; * @author Willem Cazander * @version 1.0 Oct 10, 2015 */ -public interface UnitXConverterStep { +public interface UnitXConverterStep extends Cloneable { - String getStepName(); + String getName(); List getStepReasons(); - void addStepReason(String stepReason); + void addStepReason(String stepReason); // TODO: rm : fix builder String getMathExpression(); double convert(double value); + + UnitXConverterStep clone(); } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelXMLInfoSet.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelXMLInfoSet.java new file mode 100644 index 0000000..2a2db21 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelXMLInfoSet.java @@ -0,0 +1,81 @@ +/* + * 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; + +/** + * ModelXMLInfoSet has all jaxb meta info. + * + * @author Willem Cazander + * @version 1.0 Jan 16, 2015 + */ +public class ModelXMLInfoSet { + + protected static final String XML_ENCODING = "UTF-8"; + + protected ModelXMLInfoSet() { + } + + protected static class Meta { + protected static final boolean REQUIRED = true; + protected Meta() { + } + } + + public static class Element { + protected static final String DESCRIPTION = "description"; + protected static final String CONVERTER_RESULT = "converterResult"; + protected static final String CONVERTER_RESULT_STEP = "converterResultStep"; + protected static final String START_VALUE_TYPE = "startValueType"; + protected static final String RESULT_VALUE_TYPE = "resultValueType"; + protected static final String DERIVED_FROM = "derivedFrom"; + protected static final String TYPE_FLAG = "typeFlag"; + protected static final String TYPE_FLAGS = "typeFlags"; + protected static final String WEBSITE_LINK = "websiteLink"; + protected static final String WEBSITE_LINKS = "websiteLinks"; + public static final String STEP_REASON = "stepReason"; + public static final String STEP_REASONS = "stepReasons"; + + protected Element() { + } + } + + public static class Attribute { + protected static final String ID = "id"; + public static final String NAME = "name"; + protected static final String NAME_PLURAL = "namePlural"; + + protected static final String START_VALUE = "startValue"; + protected static final String RESULT_VALUE = "resultValue"; + protected static final String CONVERT_TIME = "convertTime"; + + protected static final String BASE_TYPE_ID = "baseTypeId"; + protected static final String GROUP_LEVEL = "groupLevel"; + + protected static final String ALIAS_OF_TYPE = "aliasOfType"; + protected static final String UNIT_GROUP_ID = "unitGroupId"; + + + protected Attribute() { + } + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelXMLMarshaller.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelXMLMarshaller.java new file mode 100644 index 0000000..30feab2 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelXMLMarshaller.java @@ -0,0 +1,103 @@ +/* + * 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.io.InputStream; +import java.io.OutputStream; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; + +import org.apache.commons.lang3.Validate; + +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.converter.step.UnitXCTypePowerConverterStep; +import net.forwardfire.unitxc.converter.step.UnitXCTypePowerOfTenConverterStep; + +/** + * Jaxb marshaller of the query sets. + * + * @author Willem Cazander + * @version 1.0 Jan 14, 2015 + */ +public class ModelXMLMarshaller { + + private final JAXBContext jaxbContext; + + /** + * Creates a query set xml marshaller. + * @throws JAXBException When context could not be build. + */ + public ModelXMLMarshaller() throws JAXBException { + this(JAXBContext.newInstance(UnitXConverterResult.class, + UnitXCTypeDevideConverterStep.class, + UnitXCTypeMultiplyConverterStep.class, + UnitXCTypeMultiplyFractionConverterStep.class, + UnitXCTypeOffsetConverterStep.class, + UnitXCTypePowerConverterStep.class, + UnitXCTypePowerOfTenConverterStep.class + )); + } + + /** + * Creates a query set xml marshaller. + * @param querySetContext The jaxb query set context. + */ + public ModelXMLMarshaller(JAXBContext querySetContext) { + this.jaxbContext = Validate.notNull(querySetContext,"querySetContext is null."); + } + + /** + * Marshal the UnitXConverterResult to xml. + * @param querySet The UnitXConverterResult to marshal. + * @param output The xml output of the query set. + * @throws JAXBException When error. + */ + public void marshal(UnitXConverterResult converterResult, OutputStream output) throws JAXBException { + Validate.notNull(converterResult,"converterResult is null."); + Validate.notNull(output,"OutputStream is null."); + + Marshaller marshaller = jaxbContext.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + marshaller.setProperty(Marshaller.JAXB_ENCODING, ModelXMLInfoSet.XML_ENCODING); + marshaller.marshal(converterResult, output); + } + + /** + * Unmarshals xml to UnitXConverterResult objects. + * @param input The xml input stream. + * @return The UnitXConverterResult. + * @throws JAXBException When error. + */ + public UnitXConverterResult unmarshal(InputStream input) throws JAXBException { + Validate.notNull(input,"InputStream is null."); + + Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); + return (UnitXConverterResult) unmarshaller.unmarshal(input); + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroup.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroup.java index 69c42d2..a31372f 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroup.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroup.java @@ -23,7 +23,13 @@ package net.forwardfire.unitxc.model; -import java.util.Collection; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; + +import org.apache.commons.lang3.Validate; /** * The unit group. @@ -31,26 +37,123 @@ import java.util.Collection; * @author Willem Cazander * @version 1.0 Oct 10, 2015 */ -public interface UnitXCGroup { +public class UnitXCGroup { + + private String id; + private String name; + private String description; + private String baseTypeId; + private UnitXCGroupLevel groupLevel; + private final List derivedFrom; + + public UnitXCGroup() { + derivedFrom = new ArrayList<>(); + } + + public UnitXCGroup(String id,String name,String description,String baseTypeId) { + this(); + setId(id); + setName(name); + setDescription(description); + setBaseTypeId(baseTypeId); + } + + public UnitXCGroup validate() { + Validate.notBlank(id,"The id is blank"); + Validate.notBlank(name,"The name is blank"); + Validate.notBlank(description,"The description is blank"); + Validate.notBlank(baseTypeId,"The baseTypeId is blank"); + Validate.notNull(groupLevel,"The groupLevel is null"); + return this; + } /** - * @return The unit group id. + * @return the id */ - String getId(); + @XmlAttribute(name=ModelXMLInfoSet.Attribute.ID) + public String getId() { + return id; + } + /** + * @param id the id to set + */ + public void setId(String id) { + this.id = id; + } /** - * @return The unit group name. + * @return the name */ - String getName(); + @XmlAttribute(name=ModelXMLInfoSet.Attribute.NAME) + public String getName() { + return name; + } /** - * @return The unit group description. + * @param name the name to set */ - String getDescription(); + public void setName(String name) { + this.name = name; + } - UnitXCGroupType getType(); + /** + * @return the description + */ + @XmlElement(name=ModelXMLInfoSet.Element.DESCRIPTION) + public String getDescription() { + return description; + } - String getBaseTypeId(); + /** + * @param description the description to set + */ + public void setDescription(String description) { + this.description = description; + } - Collection getDerivedFromIds(); + /** + * @return the baseTypeId + */ + @XmlAttribute(name=ModelXMLInfoSet.Attribute.BASE_TYPE_ID) + public String getBaseTypeId() { + return baseTypeId; + } + + /** + * @param baseTypeId the baseTypeId to set + */ + public void setBaseTypeId(String baseTypeId) { + this.baseTypeId = baseTypeId; + } + + /** + * @return the groupLevel + */ + @XmlAttribute(name=ModelXMLInfoSet.Attribute.GROUP_LEVEL) + public UnitXCGroupLevel getGroupLevel() { + return groupLevel; + } + + /** + * @param groupLevel the groupLevel to set + */ + public void setGroupLevel(UnitXCGroupLevel groupLevel) { + this.groupLevel = groupLevel; + } + + /** + * @return the derivedFrom + */ + @XmlElement(name=ModelXMLInfoSet.Element.DERIVED_FROM) + public List getDerivedFrom() { + return derivedFrom; + } + + /** + * @param derivedFrom the derivedFrom to set + */ + public void setDerivedFrom(List derivedFrom) { + this.derivedFrom.clear(); + this.derivedFrom.addAll(derivedFrom); + } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroupType.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroupLevel.java similarity index 81% rename from ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroupType.java rename to ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroupLevel.java index fb25f72..6b6df92 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroupType.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroupLevel.java @@ -6,7 +6,7 @@ package net.forwardfire.unitxc.model; * @author Willem Cazander * @version 1.0 Oct 26, 2015 */ -public enum UnitXCGroupType { +public enum UnitXCGroupLevel { SI_BASE, SI_DERIVED diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroupModel.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroupModel.java deleted file mode 100644 index 2eb009a..0000000 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroupModel.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * 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; - -/** - * - * - * @author Willem Cazander - * @version 1.0 Oct 17, 2015 - */ -public class UnitXCGroupModel implements UnitXCGroup { - - private String id; - private String name; - private String description; - private String baseTypeId; - private UnitXCGroupType type; - private final List derivedFrom; - - public UnitXCGroupModel() { - derivedFrom = new ArrayList<>(); - } - - public UnitXCGroupModel(String id,String name,String description,String baseTypeId) { - this(); - setId(id); - setName(name); - setDescription(description); - setBaseTypeId(baseTypeId); - } - - public UnitXCGroupModel validate() { - Validate.notBlank(id,"The id is blank"); - Validate.notBlank(name,"The name is blank"); - Validate.notBlank(description,"The description is blank"); - Validate.notBlank(baseTypeId,"The baseTypeId is blank"); - Validate.notNull(type,"The type is null"); - return this; - } - - /** - * @return the id - */ - @Override - public String getId() { - return id; - } - - /** - * @param id the id to set - */ - public void setId(String id) { - this.id = id; - } - - /** - * @return the name - */ - @Override - public String getName() { - return name; - } - - /** - * @param name the name to set - */ - public void setName(String name) { - this.name = name; - } - - /** - * @return the description - */ - @Override - public String getDescription() { - return description; - } - - /** - * @param description the description to set - */ - public void setDescription(String description) { - this.description = description; - } - - /** - * @return the baseTypeId - */ - @Override - public String getBaseTypeId() { - return baseTypeId; - } - - /** - * @param baseTypeId the baseTypeId to set - */ - public void setBaseTypeId(String baseTypeId) { - this.baseTypeId = baseTypeId; - } - - /** - * @return the type - */ - @Override - public UnitXCGroupType getType() { - return type; - } - - /** - * @param type the type to set - */ - public void setType(UnitXCGroupType type) { - this.type = type; - } - - @Override - public Collection getDerivedFromIds() { - return derivedFrom; - } - - public void addDerivedFrom(String id) { - derivedFrom.add(id); - } -} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCType.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCType.java index ace2a35..7e0319f 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCType.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCType.java @@ -23,7 +23,18 @@ package net.forwardfire.unitxc.model; -import java.util.Collection; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.annotation.XmlAnyElement; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlTransient; + +import org.apache.commons.lang3.Validate; + +import net.forwardfire.unitxc.converter.step.UnitXConverterStep; /** * The unit type. @@ -31,24 +42,172 @@ import java.util.Collection; * @author Willem Cazander * @version 1.0 Oct 10, 2015 */ -public interface UnitXCType { +public class UnitXCType { + + private String id; + private String name; + private String namePlural; + private String aliasOfType; + private String unitGroupId; + private final List toBaseConverterSteps; + private final List fromBaseConverterSteps; + private final List typeFlags; + private final List websiteLinks; + + public UnitXCType() { + typeFlags = new ArrayList<>(); + websiteLinks = new ArrayList<>(); + toBaseConverterSteps = new ArrayList<>(); + fromBaseConverterSteps = new ArrayList<>(); + } + + public UnitXCType validate() { + Validate.notBlank(id,"The id is blank"); + Validate.notBlank(name,"The name is blank of: "+id); + Validate.notBlank(namePlural,"The namePlural is blank of: "+id); + Validate.notBlank(unitGroupId,"The unitGroupId is blank of: "+id); + return this; + } /** - * @return The unit type id. + * @return the id */ - String getId(); + @XmlAttribute(name=ModelXMLInfoSet.Attribute.ID) + public String getId() { + return id; + } + /** + * @param id the id to set + */ + public void setId(String id) { + this.id = id; + } /** - * @return The unit type name. + * @return the name */ - String getName(); - - Collection getNameAliases(); - - String getUnitGroupId(); + @XmlAttribute(name=ModelXMLInfoSet.Attribute.NAME) + public String getName() { + return name; + } /** - * @return The unit type flags. + * @param name the name to set */ - Collection getTypeFlags(); + public void setName(String name) { + this.name = name; + } + + /** + * @return the namePlural + */ + @XmlAttribute(name=ModelXMLInfoSet.Attribute.NAME_PLURAL) + public String getNamePlural() { + return namePlural; + } + + /** + * @param namePlural the namePlural to set + */ + public void setNamePlural(String namePlural) { + this.namePlural = namePlural; + } + + /** + * @return the aliasOfType + */ + @XmlAttribute(name=ModelXMLInfoSet.Attribute.ALIAS_OF_TYPE) + public String getAliasOfType() { + return aliasOfType; + } + + /** + * @param aliasOfType the aliasOfType to set + */ + public void setAliasOfType(String aliasOfType) { + this.aliasOfType = aliasOfType; + } + + /** + * @return the unitGroupId + */ + @XmlAttribute(name=ModelXMLInfoSet.Attribute.UNIT_GROUP_ID) + public String getUnitGroupId() { + return unitGroupId; + } + + /** + * @param unitGroupId the unitGroupId to set + */ + public void setUnitGroupId(String unitGroupId) { + this.unitGroupId = unitGroupId; + } + + /** + * @return the toBaseConverterSteps + */ + @XmlElementWrapper + @XmlAnyElement + public List getToBaseConverterSteps() { + return toBaseConverterSteps; + } + + /** + * @param toBaseConverterSteps the toBaseConverterSteps to set + */ + public void setToBaseConverterSteps(List toBaseConverterSteps) { + this.toBaseConverterSteps.clear(); + this.toBaseConverterSteps.addAll(toBaseConverterSteps); + } + + /** + * @return the fromBaseConverterSteps + */ + @XmlElementWrapper + @XmlAnyElement + public List getFromBaseConverterSteps() { + return fromBaseConverterSteps; + } + + /** + * @param fromBaseConverterSteps the fromBaseConverterSteps to set + */ + public void setFromBaseConverterSteps(List fromBaseConverterSteps) { + this.fromBaseConverterSteps.clear(); + this.fromBaseConverterSteps.addAll(fromBaseConverterSteps); + } + + /** + * @return the typeFlags + */ + @XmlElement(name=ModelXMLInfoSet.Element.TYPE_FLAG) + @XmlElementWrapper(name=ModelXMLInfoSet.Element.TYPE_FLAGS) + public List getTypeFlags() { + return typeFlags; + } + + /** + * @param typeFlags the typeFlags to set + */ + public void setTypeFlags(List typeFlags) { + this.typeFlags.clear(); + this.typeFlags.addAll(typeFlags); + } + + /** + * @return the websiteLinks + */ + @XmlElement(name=ModelXMLInfoSet.Element.WEBSITE_LINK) + @XmlElementWrapper(name=ModelXMLInfoSet.Element.WEBSITE_LINKS) + public List getWebsiteLinks() { + return websiteLinks; + } + + /** + * @param websiteLinks the websiteLinks to set + */ + public void setWebsiteLinks(List websiteLinks) { + this.websiteLinks.clear(); + this.websiteLinks.addAll(websiteLinks); + } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCTypeModel.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCTypeModel.java deleted file mode 100644 index 8cf1640..0000000 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCTypeModel.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * 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; - -/** - * - * - * @author Willem Cazander - * @version 1.0 Oct 17, 2015 - */ -public class UnitXCTypeModel implements UnitXCType { - - private String id; - private String name; - private String unitGroupId; - private final List toBaseConverterSteps; - private final List fromBaseConverterSteps; - private final List typeFlags; - private final List nameAliases; - - public UnitXCTypeModel() { - typeFlags = new ArrayList<>(); - nameAliases = new ArrayList<>(); - toBaseConverterSteps = new ArrayList<>(); - fromBaseConverterSteps = new ArrayList<>(); - } - - public UnitXCTypeModel validate() { - Validate.notBlank(id,"The id is blank"); - Validate.notBlank(name,"The name is blank"); - Validate.notBlank(unitGroupId,"The unitGroupId is blank"); - return this; - } - - /** - * @return the id - */ - @Override - public String getId() { - return id; - } - - /** - * @param id the id to set - */ - public void setId(String id) { - this.id = id; - } - - /** - * @return the name - */ - @Override - public String getName() { - return name; - } - - /** - * @param name the name to set - */ - public void setName(String name) { - this.name = name; - } - - /** - * @return the unitGroupId - */ - @Override - public String getUnitGroupId() { - return unitGroupId; - } - - /** - * @param unitGroupId the unitGroupId to set - */ - public void setUnitGroupId(String unitGroupId) { - this.unitGroupId = unitGroupId; - } - - public List getToBaseConverterSteps() { - return toBaseConverterSteps; - } - - public void addToBaseConverterStep(UnitXConverterStep unitTypeConverter) { - toBaseConverterSteps.add(Validate.notNull(unitTypeConverter)); - } - - public List getFromBaseConverterSteps() { - return fromBaseConverterSteps; - } - - public void addFromBaseConverterStep(UnitXConverterStep unitTypeConverter) { - fromBaseConverterSteps.add(Validate.notNull(unitTypeConverter)); - } - - @Override - public Collection getTypeFlags() { - return typeFlags; - } - - public void addTypeFlag(String flag) { - typeFlags.add(flag); - } - - @Override - public Collection getNameAliases() { - return nameAliases; - } - - public void addNameAlias(String nameAlias) { - nameAliases.add(nameAlias); - } -} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXConverterResult.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXConverterResult.java index 2d5705a..37b662a 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXConverterResult.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXConverterResult.java @@ -25,41 +25,113 @@ package net.forwardfire.unitxc.model; import java.util.List; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + /** * Holds the convert result and meta steps how we got there. * * @author Willem Cazander * @version 1.0 Oct 17, 2015 */ -public interface UnitXConverterResult { - - /** - * @return The start value. - */ - Double getStartValue(); +@XmlRootElement(name=ModelXMLInfoSet.Element.CONVERTER_RESULT) +public class UnitXConverterResult { + + private Double startValue; + private List resultSteps; + private Double resultValue; + private Long convertTime; + private UnitXCType startValueType; + private UnitXCType resultValueType; /** - * @return The start value type. + * @return the startValue */ - UnitXCType getStartValueType(); + @XmlAttribute(name=ModelXMLInfoSet.Attribute.START_VALUE) + public Double getStartValue() { + return startValue; + } /** - * @return The result value. + * @param startValue the startValue to set */ - Double getResultValue(); + public void setStartValue(Double startValue) { + this.startValue = startValue; + } /** - * @return The result value type. + * @return the resultSteps */ - UnitXCType getResultValueType(); + @XmlElement(name=ModelXMLInfoSet.Element.CONVERTER_RESULT_STEP) + public List getResultSteps() { + return resultSteps; + } /** - * @return The convert steps performed to get to result value. + * @param resultSteps the resultSteps to set */ - List getSteps(); + public void setResultSteps(List resultSteps) { + this.resultSteps = resultSteps; + } /** - * @return The convert time in ms. + * @return the resultValue */ - Long getConvertTime(); + @XmlAttribute(name=ModelXMLInfoSet.Attribute.RESULT_VALUE) + public Double getResultValue() { + return resultValue; + } + + /** + * @param resultValue the resultValue to set + */ + public void setResultValue(Double resultValue) { + this.resultValue = resultValue; + } + + /** + * @return the convertTime + */ + @XmlAttribute(name=ModelXMLInfoSet.Attribute.CONVERT_TIME) + public Long getConvertTime() { + return convertTime; + } + + /** + * @param convertTime the convertTime to set + */ + public void setConvertTime(Long convertTime) { + this.convertTime = convertTime; + } + + /** + * @return the startValueType + */ + @XmlElement(name=ModelXMLInfoSet.Element.START_VALUE_TYPE) + public UnitXCType getStartValueType() { + return startValueType; + } + + /** + * @param startValueType the startValueType to set + */ + public void setStartValueType(UnitXCType startValueType) { + this.startValueType = startValueType; + } + + /** + * @return the resultValueType + */ + @XmlElement(name=ModelXMLInfoSet.Element.RESULT_VALUE_TYPE) + public UnitXCType getResultValueType() { + return resultValueType; + } + + /** + * @param resultValueType the resultValueType to set + */ + public void setResultValueType(UnitXCType resultValueType) { + this.resultValueType = resultValueType; + } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXConverterResultStep.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXConverterResultStep.java index 0e140e1..23dda36 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXConverterResultStep.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXConverterResultStep.java @@ -23,19 +23,78 @@ package net.forwardfire.unitxc.model; +import javax.xml.bind.annotation.XmlAttribute; + /** * * * @author Willem Cazander * @version 1.0 Oct 17, 2015 */ -public interface UnitXConverterResultStep { - - String getName(); +public class UnitXConverterResultStep { - Double getInputValue(); + private String name; + private Double startValue; + private Double resultValue; + private Long convertTime; - Double getOutputValue(); + /** + * @return the name + */ + @XmlAttribute(name=ModelXMLInfoSet.Attribute.NAME) + public String getName() { + return name; + } - Long getConvertTime(); + /** + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * @return the startValue + */ + @XmlAttribute(name=ModelXMLInfoSet.Attribute.START_VALUE) + public Double getStartValue() { + return startValue; + } + + /** + * @param startValue the startValue to set + */ + public void setStartValue(Double startValue) { + this.startValue = startValue; + } + + /** + * @return the resultValue + */ + @XmlAttribute(name=ModelXMLInfoSet.Attribute.RESULT_VALUE) + public Double getResultValue() { + return resultValue; + } + + /** + * @param resultValue the resultValue to set + */ + public void setResultValue(Double resultValue) { + this.resultValue = resultValue; + } + + /** + * @return the convertTime + */ + @XmlAttribute(name=ModelXMLInfoSet.Attribute.CONVERT_TIME) + public Long getConvertTime() { + return convertTime; + } + + /** + * @param convertTime the convertTime to set + */ + public void setConvertTime(Long convertTime) { + this.convertTime = convertTime; + } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleArea.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleArea.java index 825fc40..b84d7af 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleArea.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleArea.java @@ -23,8 +23,9 @@ package net.forwardfire.unitxc.module; +import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag; import static net.forwardfire.unitxc.config.builder.UnitXCNumberTypeSetting.toSuperScript; -import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_DERIVED; +import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_DERIVED; import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; @@ -46,10 +47,25 @@ public class UnitXCModuleArea implements UnitXCConfigModule { public static final String GROUP_DESCRIPTION = "It is defined as the area of a square whose sides measure exactly one metre. The square metre is derived from the SI base unit of the metre,"; public static final String GROUP_BASE_TYPE_ID = UnitXCModuleLength.TYPE_METRE_ID+UNIT_ID_POSTFIX; + public static final String TYPE_CENTIARE_ALIAS = "m²"; + public static final String TYPE_CENTIARE_ID = "ca"; + public static final String TYPE_CENTIARE_NAME = "centiare"; + public static final String TYPE_CENTIARE_URL = "https://en.wikipedia.org/wiki/Hectare#Centiare"; + + public static final String TYPE_ARE_ALIAS = "dam²"; + public static final String TYPE_ARE_ID = "a"; + public static final String TYPE_ARE_NAME = "are"; + public static final String TYPE_ARE_URL = "https://en.wikipedia.org/wiki/Hectare#Are"; + + public static final String TYPE_HECTARE_ALIAS = "hm²"; + public static final String TYPE_HECTARE_ID = "ha"; + public static final String TYPE_HECTARE_NAME = "hectare"; + public static final String TYPE_HECTARE_URL = "https://en.wikipedia.org/wiki/Hectare"; + @Override public void configModule(UnitXCConfigBuilder builder) { builder.createUnitGroup() - .setType( SI_DERIVED) + .setGroupLevel( SI_DERIVED) .setId( GROUP_ID) .setName( GROUP_NAME) .setDescription ( GROUP_DESCRIPTION) @@ -58,6 +74,24 @@ public class UnitXCModuleArea implements UnitXCConfigModule { .setUnitIdPostfix(UNIT_ID_POSTFIX) .setUnitNamePrefix(UNIT_NAME_PREFIX) .build() + .createUnitType() + .setAliasOfType( TYPE_CENTIARE_ALIAS) + .setId( TYPE_CENTIARE_ID) + .setName( TYPE_CENTIARE_NAME) + .addWebsiteLink( TYPE_CENTIARE_URL) + .build() + .createUnitType() + .setAliasOfType( TYPE_ARE_ALIAS) + .setId( TYPE_ARE_ID) + .setName( TYPE_ARE_NAME) + .addWebsiteLink( TYPE_ARE_URL) + .build() + .createUnitType() + .setAliasOfType( TYPE_HECTARE_ALIAS) + .setId( TYPE_HECTARE_ID) + .setName( TYPE_HECTARE_NAME) + .addWebsiteLink( TYPE_HECTARE_URL) + .build() .build() ; } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleElectricCurrent.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleElectricCurrent.java index f157673..b83c988 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleElectricCurrent.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleElectricCurrent.java @@ -24,7 +24,7 @@ package net.forwardfire.unitxc.module; import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag; -import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE; +import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_BASE; import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; @@ -43,19 +43,21 @@ public class UnitXCModuleElectricCurrent implements UnitXCConfigModule { public static final String TYPE_AMPERE_ID = "A"; public static final String TYPE_AMPERE_NAME = "ampere"; public static final String TYPE_AMPERE_FLAG = buildFlag(GROUP_ID,TYPE_AMPERE_NAME); + private static final String TYPE_AMPERE_WIKI = "Ampere"; @Override public void configModule(UnitXCConfigBuilder builder) { builder.createUnitGroup() - .setType( SI_BASE) - .setId( GROUP_ID) - .setName( TYPE_AMPERE_NAME) - .setDescription ( GROUP_DESCRIPTION) - .setBaseTypeId( TYPE_AMPERE_ID) + .setGroupLevel( SI_BASE) + .setId( GROUP_ID) + .setName( TYPE_AMPERE_NAME) + .setDescription ( GROUP_DESCRIPTION) + .setBaseTypeId( TYPE_AMPERE_ID) .createSIUnitTypes() - .setId( TYPE_AMPERE_ID) - .setName( TYPE_AMPERE_NAME) - .addTypeFlag( TYPE_AMPERE_FLAG) + .setId( TYPE_AMPERE_ID) + .setName( TYPE_AMPERE_NAME) + .addTypeFlag( TYPE_AMPERE_FLAG) + .addWebsiteWiki( TYPE_AMPERE_WIKI) .addCommonSIPrefix(UnitXCTypeSIPrefix.MEGA) .addCommonSIPrefix(UnitXCTypeSIPrefix.KILO) .addCommonSIPrefix(UnitXCTypeSIPrefix.MILLI) diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleLength.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleLength.java index 11d2c89..26b7121 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleLength.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleLength.java @@ -24,7 +24,7 @@ package net.forwardfire.unitxc.module; import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag; -import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE; +import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_BASE; import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; @@ -42,164 +42,200 @@ public class UnitXCModuleLength implements UnitXCConfigModule { public static final String GROUP_DESCRIPTION = "The metre is the length of the path travelled by light in vacuum during a time interval of 1/299792458 of a second."; public static final String TYPE_METRE_ID = "m"; public static final String TYPE_METRE_NAME = "metre"; - public static final String TYPE_METRE_NAME_METER = "meter"; public static final String TYPE_METRE_FLAG = buildFlag(GROUP_ID,TYPE_METRE_NAME); + public static final String TYPE_METRE_ALIAS_METER = "meter"; + public static final String TYPE_METRE_ALIAS_METER_FLAG = buildFlag(GROUP_ID,TYPE_METRE_ALIAS_METER); + private static final String TYPE_METRE_WIKI = "Metre"; + private static final UnitXCTypeSIPrefix[] TYPE_METRE_COMMON_SI = new UnitXCTypeSIPrefix[] { + UnitXCTypeSIPrefix.KILO, + UnitXCTypeSIPrefix.HECTO, + UnitXCTypeSIPrefix.DECA, + UnitXCTypeSIPrefix.DECI, + UnitXCTypeSIPrefix.CENTI, + UnitXCTypeSIPrefix.MILLI, + UnitXCTypeSIPrefix.MICRO, + UnitXCTypeSIPrefix.NANO, + UnitXCTypeSIPrefix.PICO, + UnitXCTypeSIPrefix.FEMTO + }; public static final String TYPE_INCH_ID = "in"; public static final String TYPE_INCH_NAME = "inch"; + public static final String TYPE_INCH_NAME_PLURAL = "inches"; public static final String TYPE_INCH_FLAG = buildFlag(GROUP_ID,TYPE_INCH_NAME); private static final double TYPE_INCH_FACTOR = 25.4; + private static final String TYPE_INCH_WIKI = "Inch"; public static final String TYPE_LINK_ID = "li"; public static final String TYPE_LINK_NAME = "link"; public static final String TYPE_LINK_FLAG = buildFlag(GROUP_ID,TYPE_LINK_NAME); private static final double TYPE_LINK_FACTOR = 201.1; + public static final String TYPE_LINK_WIKI = "Link_(unit)"; public static final String TYPE_FOOT_ID = "ft"; public static final String TYPE_FOOT_NAME = "foot"; + public static final String TYPE_FOOT_NAME_PLURAL = "feet"; public static final String TYPE_FOOT_FLAG = buildFlag(GROUP_ID,TYPE_FOOT_NAME); private static final double TYPE_FOOT_FACTOR = 304.8; + private static final String TYPE_FOOT_WIKI = "Foot_(unit)"; public static final String TYPE_YARD_ID = "yd"; public static final String TYPE_YARD_NAME = "yard"; public static final String TYPE_YARD_FLAG = buildFlag(GROUP_ID,TYPE_YARD_NAME); private static final double TYPE_YARD_FACTOR = 0.9144; + private static final String TYPE_YARD_WIKI = "Yard"; public static final String TYPE_ROD_ID = "rd"; public static final String TYPE_ROD_NAME = "rod"; public static final String TYPE_ROD_FLAG = buildFlag(GROUP_ID,TYPE_ROD_NAME); private static final double TYPE_ROD_FACTOR = 5.029; + private static final String TYPE_ROD_WIKI = "Rod_(unit)"; public static final String TYPE_CHAIN_ID = "ch"; public static final String TYPE_CHAIN_NAME = "chain"; public static final String TYPE_CHAIN_FLAG = buildFlag(GROUP_ID,TYPE_CHAIN_NAME); private static final double TYPE_CHAIN_FACTOR = 20.117; + private static final String TYPE_CHAIN_WIKI = "Chain_(unit)"; public static final String TYPE_FURLONG_ID = "fur"; public static final String TYPE_FURLONG_NAME = "furlong"; public static final String TYPE_FURLONG_FLAG = buildFlag(GROUP_ID,TYPE_FURLONG_NAME); private static final double TYPE_FURLONG_FACTOR = 201.17; + private static final String TYPE_FURLONG_WIKI = "Furlong"; public static final String TYPE_MILE_ID = "mi"; public static final String TYPE_MILE_NAME = "mile"; public static final String TYPE_MILE_FLAG = buildFlag(GROUP_ID,TYPE_MILE_NAME); private static final double TYPE_MILE_FACTOR = 1609.344; + private static final String TYPE_MILE_WIKI = "Mile"; public static final String TYPE_MILE_US_ID = "us-mi"; public static final String TYPE_MILE_US_NAME = "us-mile"; public static final String TYPE_MILE_US_FLAG = buildFlag(GROUP_ID,TYPE_MILE_US_NAME); private static final double TYPE_MILE_US_FACTOR = 1609.34721869; + private static final String TYPE_MILE_US_WIKI = "Mile#US_survey_mile"; public static final String TYPE_MILE_NAUTICAL_ID = "nau-mi"; public static final String TYPE_MILE_NAUTICAL_NAME = "nautical-mile"; public static final String TYPE_MILE_NAUTICAL_FLAG = buildFlag(GROUP_ID,TYPE_MILE_NAUTICAL_NAME); private static final double TYPE_MILE_NAUTICAL_FACTOR = 1852; + private static final String TYPE_MILE_NAUTICAL_WIKI = "Mile#Nautical_mile"; @Override public void configModule(UnitXCConfigBuilder builder) { builder.createUnitGroup() - .setType( SI_BASE) - .setId( GROUP_ID) - .setName( TYPE_METRE_NAME) - .setDescription ( GROUP_DESCRIPTION) - .setBaseTypeId( TYPE_METRE_ID) + .setGroupLevel( SI_BASE) + .setId( GROUP_ID) + .setName( TYPE_METRE_NAME) + .setDescription ( GROUP_DESCRIPTION) + .setBaseTypeId( TYPE_METRE_ID) .createSIUnitTypes() - .setId( TYPE_METRE_ID) - .setName( TYPE_METRE_NAME) - .addNameAlias(TYPE_METRE_NAME_METER) - .addTypeFlag( TYPE_METRE_FLAG) - .addCommonSIPrefix(UnitXCTypeSIPrefix.KILO) - .addCommonSIPrefix(UnitXCTypeSIPrefix.HECTO) - .addCommonSIPrefix(UnitXCTypeSIPrefix.DECA) - .addCommonSIPrefix(UnitXCTypeSIPrefix.DECI) - .addCommonSIPrefix(UnitXCTypeSIPrefix.CENTI) - .addCommonSIPrefix(UnitXCTypeSIPrefix.MILLI) - .addCommonSIPrefix(UnitXCTypeSIPrefix.MICRO) - .addCommonSIPrefix(UnitXCTypeSIPrefix.NANO) - .addCommonSIPrefix(UnitXCTypeSIPrefix.PICO) - .addCommonSIPrefix(UnitXCTypeSIPrefix.FEMTO) + .setId( TYPE_METRE_ID) + .setName( TYPE_METRE_NAME) + .addTypeFlag( TYPE_METRE_FLAG) + .addWebsiteWiki( TYPE_METRE_WIKI) + .addCommonSIPrefixes(TYPE_METRE_COMMON_SI) .build() .createSIUnitTypes() - .setId( TYPE_INCH_ID) - .setName( TYPE_INCH_NAME) - .addTypeFlag( TYPE_INCH_FLAG) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .setAliasOfType(TYPE_METRE_ID) + .setId( TYPE_METRE_ALIAS_METER) + .setName( TYPE_METRE_ALIAS_METER) + .addTypeFlag( TYPE_METRE_ALIAS_METER_FLAG) + .addCommonSIPrefixes(TYPE_METRE_COMMON_SI) + .build() + .createSIUnitTypes() + .setId( TYPE_INCH_ID) + .setName( TYPE_INCH_NAME) + .setNamePlural( TYPE_INCH_NAME_PLURAL) + .addTypeFlag( TYPE_INCH_FLAG) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .addWebsiteWiki( TYPE_INCH_WIKI) .createToBaseConverterSteps().multiply(TYPE_INCH_FACTOR).power10Down(UnitXCTypeSIPrefix.MILLI).build() .createFromBaseConverterSteps().power10Up(UnitXCTypeSIPrefix.MILLI).divide(TYPE_INCH_FACTOR).build() .build() .createSIUnitTypes() - .setId( TYPE_LINK_ID) - .setName( TYPE_LINK_NAME) - .addTypeFlag( TYPE_LINK_FLAG) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA) + .setId( TYPE_LINK_ID) + .setName( TYPE_LINK_NAME) + .addTypeFlag( TYPE_LINK_FLAG) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA) + .addWebsiteWiki( TYPE_LINK_WIKI) .createToBaseConverterSteps().multiply(TYPE_LINK_FACTOR).power10Down(UnitXCTypeSIPrefix.MILLI).build() .createFromBaseConverterSteps().power10Up(UnitXCTypeSIPrefix.MILLI).divide(TYPE_LINK_FACTOR).build() .build() .createSIUnitTypes() - .setId( TYPE_FOOT_ID) - .setName( TYPE_FOOT_NAME) - .addTypeFlag( TYPE_FOOT_FLAG) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .setId( TYPE_FOOT_ID) + .setName( TYPE_FOOT_NAME) + .setNamePlural( TYPE_FOOT_NAME_PLURAL) + .addTypeFlag( TYPE_FOOT_FLAG) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .addWebsiteWiki( TYPE_FOOT_WIKI) .createToBaseConverterSteps().multiply(TYPE_FOOT_FACTOR).power10Down(UnitXCTypeSIPrefix.MILLI).build() .createFromBaseConverterSteps().power10Up(UnitXCTypeSIPrefix.MILLI).divide(TYPE_FOOT_FACTOR).build() .build() .createSIUnitTypes() - .setId( TYPE_YARD_ID) - .setName( TYPE_YARD_NAME) - .addTypeFlag( TYPE_YARD_FLAG) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .setId( TYPE_YARD_ID) + .setName( TYPE_YARD_NAME) + .addTypeFlag( TYPE_YARD_FLAG) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .addWebsiteWiki( TYPE_YARD_WIKI) .createToBaseConverterSteps().multiply(TYPE_YARD_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_YARD_FACTOR).build() .build() .createSIUnitTypes() - .setId( TYPE_ROD_ID) - .setName( TYPE_ROD_NAME) - .addTypeFlag( TYPE_ROD_FLAG) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA) + .setId( TYPE_ROD_ID) + .setName( TYPE_ROD_NAME) + .addTypeFlag( TYPE_ROD_FLAG) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA) + .addWebsiteWiki( TYPE_ROD_WIKI) .createToBaseConverterSteps().multiply(TYPE_ROD_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_ROD_FACTOR).build() .build() .createSIUnitTypes() - .setId( TYPE_CHAIN_ID) - .setName( TYPE_CHAIN_NAME) - .addTypeFlag( TYPE_CHAIN_FLAG) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA) + .setId( TYPE_CHAIN_ID) + .setName( TYPE_CHAIN_NAME) + .addTypeFlag( TYPE_CHAIN_FLAG) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA) + .addWebsiteWiki( TYPE_CHAIN_WIKI) .createToBaseConverterSteps().multiply(TYPE_CHAIN_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_CHAIN_FACTOR).build() .build() .createSIUnitTypes() - .setId( TYPE_FURLONG_ID) - .setName( TYPE_FURLONG_NAME) - .addTypeFlag( TYPE_FURLONG_FLAG) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA) + .setId( TYPE_FURLONG_ID) + .setName( TYPE_FURLONG_NAME) + .addTypeFlag( TYPE_FURLONG_FLAG) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA) + .addWebsiteWiki( TYPE_FURLONG_WIKI) .createToBaseConverterSteps().multiply(TYPE_FURLONG_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_FURLONG_FACTOR).build() .build() .createSIUnitTypes() - .setId( TYPE_MILE_ID) - .setName( TYPE_MILE_NAME) - .addTypeFlag( TYPE_MILE_FLAG) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .setId( TYPE_MILE_ID) + .setName( TYPE_MILE_NAME) + .addTypeFlag( TYPE_MILE_FLAG) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .addWebsiteWiki( TYPE_MILE_WIKI) .createToBaseConverterSteps().multiply(TYPE_MILE_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_MILE_FACTOR).build() .build() .createSIUnitTypes() - .setId( TYPE_MILE_US_ID) - .setName( TYPE_MILE_US_NAME) - .addTypeFlag( TYPE_MILE_US_FLAG) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .setId( TYPE_MILE_US_ID) + .setName( TYPE_MILE_US_NAME) + .addTypeFlag( TYPE_MILE_US_FLAG) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .addWebsiteWiki( TYPE_MILE_US_WIKI) .createToBaseConverterSteps().multiply(TYPE_MILE_US_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_MILE_US_FACTOR).build() .build() .createSIUnitTypes() - .setId( TYPE_MILE_NAUTICAL_ID) - .setName( TYPE_MILE_NAUTICAL_NAME) - .addTypeFlag( TYPE_MILE_NAUTICAL_FLAG) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .setId( TYPE_MILE_NAUTICAL_ID) + .setName( TYPE_MILE_NAUTICAL_NAME) + .addTypeFlag( TYPE_MILE_NAUTICAL_FLAG) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .addWebsiteWiki( TYPE_MILE_NAUTICAL_WIKI) .createToBaseConverterSteps().multiply(TYPE_MILE_NAUTICAL_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_MILE_NAUTICAL_FACTOR).build() .build() diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleLuminousIntensity.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleLuminousIntensity.java index 2b9dda3..96c924c 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleLuminousIntensity.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleLuminousIntensity.java @@ -24,7 +24,7 @@ package net.forwardfire.unitxc.module; import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag; -import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE; +import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_BASE; import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; @@ -42,19 +42,21 @@ public class UnitXCModuleLuminousIntensity implements UnitXCConfigModule { public static final String TYPE_CANDELA_ID = "cd"; public static final String TYPE_CANDELA_NAME = "candela"; public static final String TYPE_CANDELA_FLAG = buildFlag(GROUP_ID,TYPE_CANDELA_NAME); + private static final String TYPE_CANDELA_WIKI = "Candela"; @Override public void configModule(UnitXCConfigBuilder builder) { builder.createUnitGroup() - .setType( SI_BASE) - .setId( GROUP_ID) - .setName( TYPE_CANDELA_NAME) - .setDescription ( GROUP_DESCRIPTION) - .setBaseTypeId( TYPE_CANDELA_ID) + .setGroupLevel( SI_BASE) + .setId( GROUP_ID) + .setName( TYPE_CANDELA_NAME) + .setDescription ( GROUP_DESCRIPTION) + .setBaseTypeId( TYPE_CANDELA_ID) .createSIUnitTypes() - .setId( TYPE_CANDELA_ID) - .setName( TYPE_CANDELA_NAME) - .addTypeFlag( TYPE_CANDELA_FLAG) + .setId( TYPE_CANDELA_ID) + .setName( TYPE_CANDELA_NAME) + .addTypeFlag( TYPE_CANDELA_FLAG) + .addWebsiteWiki( TYPE_CANDELA_WIKI) .build() .build() ; diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleMass.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleMass.java index 1bc4617..3dff7bc 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleMass.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleMass.java @@ -24,7 +24,7 @@ package net.forwardfire.unitxc.module; import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag; -import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE; +import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_BASE; import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; @@ -44,44 +44,51 @@ public class UnitXCModuleMass implements UnitXCConfigModule { public static final String TYPE_GRAM_ID = "g"; public static final String TYPE_GRAM_NAME = "gram"; public static final String TYPE_GRAM_FLAG = buildFlag(GROUP_ID,TYPE_GRAM_NAME); + private static final String TYPE_GRAM_WIKI = "Kilogram"; public static final String TYPE_GRAIN_ID = "gr"; public static final String TYPE_GRAIN_NAME = "grain"; public static final String TYPE_GRAIN_FLAG = buildFlag(GROUP_ID,TYPE_GRAIN_NAME); private static final double TYPE_GRAIN_FACTOR = 64.80; + private static final String TYPE_GRAIN_WIKI = "Grain_(unit)"; public static final String TYPE_DRAM_ID = "dr"; public static final String TYPE_DRAM_NAME = "dram"; public static final String TYPE_DRAM_FLAG = buildFlag(GROUP_ID,TYPE_DRAM_NAME); private static final double TYPE_DRAM_FACTOR = 1.772; + private static final String TYPE_DRAM_WIKI = "Dram_(unit)"; public static final String TYPE_OUNCE_ID = "oz"; public static final String TYPE_OUNCE_NAME = "ounce"; public static final String TYPE_OUNCE_FLAG = buildFlag(GROUP_ID,TYPE_OUNCE_NAME); private static final double TYPE_OUNCE_FACTOR = 28.35; + private static final String TYPE_OUNCE_WIKI = "Ounce"; public static final String TYPE_POUND_ID = "lb"; public static final String TYPE_POUND_NAME = "pound"; public static final String TYPE_POUND_FLAG = buildFlag(GROUP_ID,TYPE_POUND_NAME); private static final double TYPE_POUND_FACTOR = 453.6; + private static final String TYPE_POUND_WIKI = "Pound_(mass)"; public static final String TYPE_STONE_ID = "st"; public static final String TYPE_STONE_NAME = "stone"; public static final String TYPE_STONE_FLAG = buildFlag(GROUP_ID,TYPE_STONE_NAME); private static final double TYPE_STONE_FACTOR = 6.350; + private static final String TYPE_STONE_WIKI = "Stone_(unit)"; @Override public void configModule(UnitXCConfigBuilder builder) { builder.createUnitGroup() - .setType( SI_BASE) - .setId( GROUP_ID) - .setName( GROUP_NAME) - .setDescription ( GROUP_DESCRIPTION) - .setBaseTypeId( TYPE_GRAM_ID) + .setGroupLevel( SI_BASE) + .setId( GROUP_ID) + .setName( GROUP_NAME) + .setDescription ( GROUP_DESCRIPTION) + .setBaseTypeId( TYPE_GRAM_ID) .createSIUnitTypes() - .setId( TYPE_GRAM_ID) - .setName( TYPE_GRAM_NAME) - .addTypeFlag( TYPE_GRAM_FLAG) + .setId( TYPE_GRAM_ID) + .setName( TYPE_GRAM_NAME) + .addTypeFlag( TYPE_GRAM_FLAG) + .addWebsiteWiki( TYPE_GRAM_WIKI) .addCommonSIPrefix(UnitXCTypeSIPrefix.MEGA) .addCommonSIPrefix(UnitXCTypeSIPrefix.KILO) .addCommonSIPrefix(UnitXCTypeSIPrefix.HECTO) @@ -92,44 +99,49 @@ public class UnitXCModuleMass implements UnitXCConfigModule { .addCommonSIPrefix(UnitXCTypeSIPrefix.MICRO) .build() .createSIUnitTypes() - .setId( TYPE_GRAIN_ID) - .setName( TYPE_GRAIN_NAME) - .addTypeFlag( TYPE_GRAIN_FLAG) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .setId( TYPE_GRAIN_ID) + .setName( TYPE_GRAIN_NAME) + .addTypeFlag( TYPE_GRAIN_FLAG) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .addWebsiteWiki( TYPE_GRAIN_WIKI) .createToBaseConverterSteps().multiply(TYPE_GRAIN_FACTOR).power10Down(UnitXCTypeSIPrefix.MILLI).build() .createFromBaseConverterSteps().power10Up(UnitXCTypeSIPrefix.MILLI).divide(TYPE_GRAIN_FACTOR).build() .build() .createSIUnitTypes() - .setId( TYPE_DRAM_ID) - .setName( TYPE_DRAM_NAME) - .addTypeFlag( TYPE_DRAM_FLAG) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA) + .setId( TYPE_DRAM_ID) + .setName( TYPE_DRAM_NAME) + .addTypeFlag( TYPE_DRAM_FLAG) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA) + .addWebsiteWiki( TYPE_DRAM_WIKI) .createToBaseConverterSteps().multiply(TYPE_DRAM_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_DRAM_FACTOR).build() .build() .createSIUnitTypes() - .setId( TYPE_OUNCE_ID) - .setName( TYPE_OUNCE_NAME) - .addTypeFlag( TYPE_OUNCE_FLAG) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .setId( TYPE_OUNCE_ID) + .setName( TYPE_OUNCE_NAME) + .addTypeFlag( TYPE_OUNCE_FLAG) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .addWebsiteWiki( TYPE_OUNCE_WIKI) .createToBaseConverterSteps().multiply(TYPE_OUNCE_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_OUNCE_FACTOR).build() .build() .createSIUnitTypes() - .setId( TYPE_POUND_ID) - .setName( TYPE_POUND_NAME) - .addTypeFlag( TYPE_POUND_FLAG) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .setId( TYPE_POUND_ID) + .setName( TYPE_POUND_NAME) + .addTypeFlag( TYPE_POUND_FLAG) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .addWebsiteWiki( TYPE_POUND_WIKI) .createToBaseConverterSteps().multiply(TYPE_POUND_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_POUND_FACTOR).build() .build() .createSIUnitTypes() - .setId( TYPE_STONE_ID) - .setName( TYPE_STONE_NAME) - .addTypeFlag( TYPE_STONE_FLAG) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA) + .setId( TYPE_STONE_ID) + .setName( TYPE_STONE_NAME) + .addTypeFlag( TYPE_STONE_FLAG) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA) + .addWebsiteWiki( TYPE_STONE_WIKI) .createToBaseConverterSteps().multiply(TYPE_STONE_FACTOR).power10Up(UnitXCTypeSIPrefix.MILLI).build() .createFromBaseConverterSteps().power10Down(UnitXCTypeSIPrefix.MILLI).divide(TYPE_STONE_FACTOR).build() .build() diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXConverterResultModel.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleSpeed.java similarity index 50% rename from ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXConverterResultModel.java rename to ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleSpeed.java index abf6eb8..6de2c76 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXConverterResultModel.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleSpeed.java @@ -21,63 +21,38 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package net.forwardfire.unitxc.model; +package net.forwardfire.unitxc.module; -import java.util.List; +import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_DERIVED; -import org.apache.commons.lang3.Validate; +import net.forwardfire.unitxc.config.UnitXCConfigModule; +import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; +import net.forwardfire.unitxc.config.builder.UnitXCTypeCompoundPairBuilder; /** * * * @author Willem Cazander - * @version 1.0 Oct 22, 2015 + * @version 1.0 Nov 3, 2015 */ -public class UnitXConverterResultModel implements UnitXConverterResult { +public class UnitXCModuleSpeed implements UnitXCConfigModule { - private final Double startValue; - private final List resultSteps; - private final Double resultValue; - private final Long convertTime; - private final UnitXCType startValueType; - private final UnitXCType resultValueType; - - public UnitXConverterResultModel(Double startValue,Double resultValue,List resultSteps,Long convertTime,UnitXCType startValueType,UnitXCType resultValueType) { - this.startValue = Validate.notNull(startValue); - this.resultSteps = Validate.noNullElements(resultSteps); - this.resultValue = Validate.notNull(resultValue); - this.convertTime = convertTime; - this.startValueType = Validate.notNull(startValueType); - this.resultValueType = Validate.notNull(resultValueType); - } + public static final String GROUP_ID = "speed"; + public static final String GROUP_NAME = UnitXCModuleLength.TYPE_METRE_NAME+UnitXCTypeCompoundPairBuilder.PER_NAME+UnitXCModuleTime.TYPE_SECOND_NAME;; + public static final String GROUP_DESCRIPTION = "Speed is the dimensions of a length divided by a time."; + public static final String GROUP_BASE_TYPE_ID = UnitXCModuleLength.TYPE_METRE_ID+UnitXCTypeCompoundPairBuilder.PER_ID+UnitXCModuleTime.TYPE_SECOND_ID; @Override - public Double getStartValue() { - return startValue; - } - - @Override - public Double getResultValue() { - return resultValue; - } - - @Override - public List getSteps() { - return resultSteps; - } - - @Override - public Long getConvertTime() { - return convertTime; - } - - @Override - public UnitXCType getStartValueType() { - return startValueType; - } - - @Override - public UnitXCType getResultValueType() { - return resultValueType; + public void configModule(UnitXCConfigBuilder builder) { + builder.createUnitGroup() + .setGroupLevel( SI_DERIVED) + .setId( GROUP_ID) + .setName( GROUP_NAME) + .setDescription ( GROUP_DESCRIPTION) + .setBaseTypeId( GROUP_BASE_TYPE_ID) + .createCompoundPairUnitTypes(UnitXCModuleLength.GROUP_ID, UnitXCModuleTime.GROUP_ID) + .build() + .build() + ; } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleSubstance.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleSubstance.java index 4f4523e..765e09b 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleSubstance.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleSubstance.java @@ -24,7 +24,7 @@ package net.forwardfire.unitxc.module; import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag; -import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE; +import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_BASE; import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; @@ -43,24 +43,27 @@ public class UnitXCModuleSubstance implements UnitXCConfigModule { public static final String TYPE_MOLE_ID = "mol"; public static final String TYPE_MOLE_NAME = "mole"; public static final String TYPE_MOLE_FLAG = buildFlag(GROUP_ID,TYPE_MOLE_NAME); + private static final String TYPE_MOLE_WIKI = "Mole_(unit)"; public static final String TYPE_POUND_MOLE_ID = "lbmol"; public static final String TYPE_POUND_MOLE_NAME = "pound-mole"; public static final String TYPE_POUND_MOLE_FLAG = buildFlag(GROUP_ID,TYPE_POUND_MOLE_NAME); private static final double TYPE_POUND_MOLE_FACTOR = 453.59237; + private static final String TYPE_POUND_MOLE_WIKI = "Mole_(unit)#Other_units_called_.22mole.22"; @Override public void configModule(UnitXCConfigBuilder builder) { builder.createUnitGroup() - .setType( SI_BASE) - .setId( GROUP_ID) - .setName( TYPE_MOLE_NAME) - .setDescription ( GROUP_DESCRIPTION) - .setBaseTypeId( TYPE_MOLE_ID) + .setGroupLevel( SI_BASE) + .setId( GROUP_ID) + .setName( TYPE_MOLE_NAME) + .setDescription ( GROUP_DESCRIPTION) + .setBaseTypeId( TYPE_MOLE_ID) .createSIUnitTypes() - .setId( TYPE_MOLE_ID) - .setName( TYPE_MOLE_NAME) - .addTypeFlag( TYPE_MOLE_FLAG) + .setId( TYPE_MOLE_ID) + .setName( TYPE_MOLE_NAME) + .addTypeFlag( TYPE_MOLE_FLAG) + .addWebsiteWiki( TYPE_MOLE_WIKI) .addCommonSIPrefix(UnitXCTypeSIPrefix.MILLI) .addCommonSIPrefix(UnitXCTypeSIPrefix.MICRO) .addCommonSIPrefix(UnitXCTypeSIPrefix.NANO) @@ -68,9 +71,10 @@ public class UnitXCModuleSubstance implements UnitXCConfigModule { .addCommonSIPrefix(UnitXCTypeSIPrefix.FEMTO) .build() .createUnitType() - .setId( TYPE_POUND_MOLE_ID) - .setName( TYPE_POUND_MOLE_NAME) - .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .setId( TYPE_POUND_MOLE_ID) + .setName( TYPE_POUND_MOLE_NAME) + .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .addWebsiteWiki( TYPE_POUND_MOLE_WIKI) .createToBaseConverterSteps().multiply(TYPE_POUND_MOLE_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_POUND_MOLE_FACTOR).build() .build() diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleTemperature.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleTemperature.java index 2930460..4f33d16 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleTemperature.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleTemperature.java @@ -27,7 +27,7 @@ import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag; -import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE; +import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_BASE; import org.apache.commons.lang3.math.Fraction; @@ -45,87 +45,104 @@ public class UnitXCModuleTemperature implements UnitXCConfigModule { public static final String TYPE_KELVIN_ID = "K"; public static final String TYPE_KELVIN_NAME = "kelvin"; public static final String TYPE_KELVIN_FLAG = buildFlag(GROUP_ID,TYPE_KELVIN_NAME); + private static final String TYPE_KELVIN_WIKI = "Kelvin"; public static final String TYPE_CELSIUS_ID = "°C"; public static final String TYPE_CELSIUS_NAME = "celsius"; public static final String TYPE_CELSIUS_FLAG = buildFlag(GROUP_ID,TYPE_CELSIUS_NAME); private static final double TYPE_CELSIUS_OFFSET = 273.15; + private static final String TYPE_CELSIUS_WIKI = "Celsius"; public static final String TYPE_FAHRENHEIT_ID = "°F"; public static final String TYPE_FAHRENHEIT_NAME = "fahrenheit"; public static final String TYPE_FAHRENHEIT_FLAG = buildFlag(GROUP_ID,TYPE_FAHRENHEIT_NAME); private static final double TYPE_FAHRENHEIT_OFFSET = 459.67; private static final Fraction TYPE_FAHRENHEIT_FRACTION = Fraction.getFraction(5,9); + private static final String TYPE_FAHRENHEIT_WIKI = "Fahrenheit"; public static final String TYPE_RANKINE_ID = "°R"; public static final String TYPE_RANKINE_NAME = "rankine"; public static final String TYPE_RANKINE_FLAG = buildFlag(GROUP_ID,TYPE_RANKINE_NAME); + private static final String TYPE_RANKINE_WIKI = "Rankine_scale"; public static final String TYPE_ROMER_ID = "Rø"; public static final String TYPE_ROMER_NAME = "rømer"; - public static final String TYPE_ROMER_NAME_ALIAS = "romer"; - public static final String TYPE_ROMER_FLAG = buildFlag(GROUP_ID,TYPE_ROMER_NAME_ALIAS); + public static final String TYPE_ROMER_FLAG = buildFlag(GROUP_ID,TYPE_ROMER_NAME); + public static final String TYPE_ROMER_ALIAS_NAME = "romer"; + public static final String TYPE_ROMER_ALIAS_FLAG = buildFlag(GROUP_ID,TYPE_ROMER_ALIAS_NAME); private static final Fraction TYPE_ROMER_FRACTION = Fraction.getFraction(40,21); private static final double TYPE_ROMER_FRACTION_OFFSET = 7.5; + private static final String TYPE_ROMER_WIKI = "Rømer_scale"; @Override public void configModule(UnitXCConfigBuilder builder) { builder.createUnitGroup() - .setType( SI_BASE) - .setId( GROUP_ID) - .setName( TYPE_KELVIN_NAME) - .setDescription( GROUP_DESCRIPTION) - .setBaseTypeId( TYPE_KELVIN_ID) + .setGroupLevel( SI_BASE) + .setId( GROUP_ID) + .setName( TYPE_KELVIN_NAME) + .setDescription( GROUP_DESCRIPTION) + .setBaseTypeId( TYPE_KELVIN_ID) .createSIUnitTypes() - .setId( TYPE_KELVIN_ID) - .setName( TYPE_KELVIN_NAME) - .addTypeFlag( TYPE_KELVIN_FLAG) + .setId( TYPE_KELVIN_ID) + .setName( TYPE_KELVIN_NAME) + .addTypeFlag( TYPE_KELVIN_FLAG) + .addWebsiteWiki(TYPE_KELVIN_WIKI) .build() .createSIUnitTypes() - .setId( TYPE_CELSIUS_ID) - .setName( TYPE_CELSIUS_NAME) - .addTypeFlag( TYPE_CELSIUS_FLAG) + .setId( TYPE_CELSIUS_ID) + .setName( TYPE_CELSIUS_NAME) + .addTypeFlag( TYPE_CELSIUS_FLAG) + .addWebsiteWiki(TYPE_CELSIUS_WIKI) .createToBaseConverterSteps().offsetUp(TYPE_CELSIUS_OFFSET).build() .createFromBaseConverterSteps().offsetDown(TYPE_CELSIUS_OFFSET).build() .build() .createSIUnitTypes() - .setId( TYPE_FAHRENHEIT_ID) - .setName( TYPE_FAHRENHEIT_NAME) - .addTypeFlag( TYPE_FAHRENHEIT_FLAG) + .setId( TYPE_FAHRENHEIT_ID) + .setName( TYPE_FAHRENHEIT_NAME) + .addTypeFlag( TYPE_FAHRENHEIT_FLAG) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) + .addWebsiteWiki( TYPE_FAHRENHEIT_WIKI) .createToBaseConverterSteps() - .offsetUp(TYPE_FAHRENHEIT_OFFSET) - .multiply(TYPE_FAHRENHEIT_FRACTION) + .offsetUp( TYPE_FAHRENHEIT_OFFSET) + .multiply( TYPE_FAHRENHEIT_FRACTION) .build() .createFromBaseConverterSteps() - .multiply(TYPE_FAHRENHEIT_FRACTION.invert()) - .offsetDown(TYPE_FAHRENHEIT_OFFSET) + .multiply( TYPE_FAHRENHEIT_FRACTION.invert()) + .offsetDown( TYPE_FAHRENHEIT_OFFSET) .build() .build() .createSIUnitTypes() - .setId( TYPE_RANKINE_ID) - .setName( TYPE_RANKINE_NAME) - .addTypeFlag( TYPE_RANKINE_FLAG) + .setId( TYPE_RANKINE_ID) + .setName( TYPE_RANKINE_NAME) + .addTypeFlag ( TYPE_RANKINE_FLAG) + .addWebsiteWiki( TYPE_RANKINE_WIKI) .createToBaseConverterSteps().multiply(TYPE_FAHRENHEIT_FRACTION).build() .createFromBaseConverterSteps().multiply(TYPE_FAHRENHEIT_FRACTION.invert()).build() .build() .createSIUnitTypes() - .setId( TYPE_ROMER_ID) - .setName( TYPE_ROMER_NAME) - .addNameAlias(TYPE_ROMER_NAME_ALIAS) - .addTypeFlag( TYPE_ROMER_FLAG) + .setId( TYPE_ROMER_ID) + .setName( TYPE_ROMER_NAME) + .addTypeFlag( TYPE_ROMER_FLAG) + .addWebsiteWiki( TYPE_ROMER_WIKI) .createToBaseConverterSteps() - .offsetUp(TYPE_CELSIUS_OFFSET) - .multiply(TYPE_ROMER_FRACTION) - .offsetUp(TYPE_ROMER_FRACTION_OFFSET) + .offsetUp( TYPE_CELSIUS_OFFSET) + .multiply( TYPE_ROMER_FRACTION) + .offsetUp( TYPE_ROMER_FRACTION_OFFSET) .build() .createFromBaseConverterSteps() - .offsetDown(TYPE_ROMER_FRACTION_OFFSET) - .multiply(TYPE_ROMER_FRACTION.invert()) - .offsetDown(TYPE_CELSIUS_OFFSET) + .offsetDown( TYPE_ROMER_FRACTION_OFFSET) + .multiply( TYPE_ROMER_FRACTION.invert()) + .offsetDown( TYPE_CELSIUS_OFFSET) .build() .build() + .createSIUnitTypes() + .setAliasOfType( TYPE_ROMER_ID) + .setId( TYPE_ROMER_ALIAS_NAME) + .setName( TYPE_ROMER_ALIAS_NAME) + .addTypeFlag( TYPE_ROMER_ALIAS_FLAG) + .build() .build() ; } } + diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleTime.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleTime.java index 12b6ea9..50f24bb 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleTime.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleTime.java @@ -24,7 +24,7 @@ package net.forwardfire.unitxc.module; import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag; -import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE; +import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_BASE; import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; @@ -52,21 +52,26 @@ public class UnitXCModuleTime implements UnitXCConfigModule { private static final double TIME_EARTH_YEAR = 31557600; // related to the rotating earth, or change to be 794 243 384 928 000 hyperfine oscillations + public static final String TYPE_MINUTE_ID = "min"; public static final String TYPE_MINUTE_NAME = "minute"; public static final String TYPE_MINUTE_FLAG = buildFlag(GROUP_ID,TYPE_MINUTE_NAME); + public static final String TYPE_HOUR_ID = "h"; public static final String TYPE_HOUR_NAME = "hour"; public static final String TYPE_HOUR_FLAG = buildFlag(GROUP_ID,TYPE_HOUR_NAME); + public static final String TYPE_DAY_ID = "d"; public static final String TYPE_DAY_NAME = "day"; public static final String TYPE_DAY_FLAG = buildFlag(GROUP_ID,TYPE_DAY_NAME); + public static final String TYPE_WEEK_ID = "wk"; public static final String TYPE_WEEK_NAME = "week"; public static final String TYPE_WEEK_FLAG = buildFlag(GROUP_ID,TYPE_WEEK_NAME); + public static final String TYPE_YEAR_ID = "yr"; public static final String TYPE_YEAR_NAME = "year"; public static final String TYPE_YEAR_FLAG = buildFlag(GROUP_ID,TYPE_YEAR_NAME); @Override public void configModule(UnitXCConfigBuilder builder) { builder.createUnitGroup() - .setType( SI_BASE) + .setGroupLevel( SI_BASE) .setId( GROUP_ID) .setName( TYPE_SECOND_NAME) .setDescription ( GROUP_DESCRIPTION) @@ -83,35 +88,35 @@ public class UnitXCModuleTime implements UnitXCConfigModule { .addCommonSIPrefix(UnitXCTypeSIPrefix.ATTO) .build() .createSIUnitTypes() - .setId( TYPE_MINUTE_NAME) + .setId( TYPE_MINUTE_ID) .setName( TYPE_MINUTE_NAME) .addTypeFlag( TYPE_MINUTE_FLAG) .createToBaseConverterSteps().multiply(TIME_EARTH_MINUTE).build() .createFromBaseConverterSteps().divide(TIME_EARTH_MINUTE).build() .build() .createSIUnitTypes() - .setId( TYPE_HOUR_NAME) + .setId( TYPE_HOUR_ID) .setName( TYPE_HOUR_NAME) .addTypeFlag( TYPE_HOUR_FLAG) .createToBaseConverterSteps().multiply(TIME_EARTH_HOUR).build() .createFromBaseConverterSteps().divide(TIME_EARTH_HOUR).build() .build() .createSIUnitTypes() - .setId( TYPE_DAY_NAME) + .setId( TYPE_DAY_ID) .setName( TYPE_DAY_NAME) .addTypeFlag( TYPE_DAY_FLAG) .createToBaseConverterSteps().multiply(TIME_EARTH_DAY).build() .createFromBaseConverterSteps().divide(TIME_EARTH_DAY).build() .build() .createSIUnitTypes() - .setId( TYPE_WEEK_NAME) + .setId( TYPE_WEEK_ID) .setName( TYPE_WEEK_NAME) .addTypeFlag( TYPE_WEEK_FLAG) .createToBaseConverterSteps().multiply(TIME_EARTH_WEEK).build() .createFromBaseConverterSteps().divide(TIME_EARTH_WEEK).build() .build() .createSIUnitTypes() - .setId( TYPE_YEAR_NAME) + .setId( TYPE_YEAR_ID) .setName( TYPE_YEAR_NAME) .addTypeFlag( TYPE_YEAR_FLAG) .createToBaseConverterSteps().multiply(TIME_EARTH_YEAR).build() diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleVolume.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleVolume.java index e818f30..c05cded 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleVolume.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCModuleVolume.java @@ -24,7 +24,7 @@ package net.forwardfire.unitxc.module; import static net.forwardfire.unitxc.config.builder.UnitXCNumberTypeSetting.toSuperScript; -import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_DERIVED; +import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_DERIVED; import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; @@ -49,7 +49,7 @@ public class UnitXCModuleVolume implements UnitXCConfigModule { @Override public void configModule(UnitXCConfigBuilder builder) { builder.createUnitGroup() - .setType( SI_DERIVED) + .setGroupLevel( SI_DERIVED) .setId( GROUP_ID) .setName( GROUP_NAME) .setDescription ( GROUP_DESCRIPTION) diff --git a/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/UnitXCConverterStepTest.java b/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/UnitXCConverterStepTest.java index 1ec6a64..afb6b92 100644 --- a/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/UnitXCConverterStepTest.java +++ b/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/UnitXCConverterStepTest.java @@ -35,6 +35,7 @@ import static net.forwardfire.unitxc.UnitXCAssert.convertEquals; import net.forwardfire.unitxc.UnitXCFactory; import net.forwardfire.unitxc.UnitXCManager; import net.forwardfire.unitxc.model.UnitXCType; +import net.forwardfire.unitxc.model.ModelXMLMarshaller; import net.forwardfire.unitxc.model.UnitXCGroup; import net.forwardfire.unitxc.model.UnitXConverterResult; import net.forwardfire.unitxc.model.UnitXConverterResultStep; @@ -42,7 +43,8 @@ import net.forwardfire.unitxc.model.UnitXConverterResultStep; public class UnitXCConverterStepTest { @Test - public void testFac() { + public void testFac() throws Exception { + ModelXMLMarshaller xmlDriver = new ModelXMLMarshaller(); UnitXCManager unitManager = UnitXCFactory.createManager(); assertNotNull(unitManager); @@ -75,10 +77,11 @@ public class UnitXCConverterStepTest { 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()); + for (UnitXConverterResultStep step:result.getResultSteps()) { + System.out.println("step: "+step.getName()+" in: "+step.getStartValue()+" out: "+step.getResultValue()+" time: "+step.getConvertTime()); } + xmlDriver.marshal(result, System.out); System.out.println(""); result = unitManager.getConverter().convertStepped(10.763, "ft²", "in²"); @@ -86,8 +89,8 @@ public class UnitXCConverterStepTest { 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()); + for (UnitXConverterResultStep step:result.getResultSteps()) { + System.out.println("step: "+step.getName()+" in: "+step.getStartValue()+" out: "+step.getResultValue()+" time: "+step.getConvertTime()); } System.out.println(""); @@ -97,9 +100,21 @@ public class UnitXCConverterStepTest { 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()); + for (UnitXConverterResultStep step:result.getResultSteps()) { + System.out.println("step: "+step.getName()+" in: "+step.getStartValue()+" out: "+step.getResultValue()+" time: "+step.getConvertTime()); } + + + result = unitManager.getConverter().convertStepped(1079000000, "km/h", "m/s"); + System.out.println("Convert from: "+result.getStartValueType().getName()+" to: "+result.getResultValueType().getName()); + System.out.println("startValue: "+result.getStartValue()); + System.out.println("resultValue: "+result.getResultValue()); + System.out.println("convertTime: "+result.getConvertTime()); + for (UnitXConverterResultStep step:result.getResultSteps()) { + System.out.println("step: "+step.getName()+" in: "+step.getStartValue()+" out: "+step.getResultValue()+" time: "+step.getConvertTime()); + } + xmlDriver.marshal(result, System.out); + } } diff --git a/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/UnitXCTestUI.java b/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/UnitXCTestUI.java new file mode 100644 index 0000000..e69de29 diff --git a/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/module/UnitXCModuleAreaTest.java b/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/module/UnitXCModuleAreaTest.java new file mode 100644 index 0000000..cd7084c --- /dev/null +++ b/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/module/UnitXCModuleAreaTest.java @@ -0,0 +1,91 @@ +/* + * 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 org.junit.Test; + +import static net.forwardfire.unitxc.UnitXCAssert.convertEquals; + +public class UnitXCModuleAreaTest { + + @Test + public void test1ToSquareKilometre() throws Exception { + convertEquals(0.000001,"km²",1.0, "m²"); + } + + @Test + public void test1ToSquareCentimetre() throws Exception { + convertEquals(10000,"cm²",1.0, "m²"); + } + + @Test + public void test1ToHectares() throws Exception { + convertEquals(0.0001,"ha",1.0, "m²"); + } + + @Test + public void test1ToDecares() throws Exception { + convertEquals(0.001,"daa",1.0, "m²"); + } + + @Test + public void test1ToAres() throws Exception { + convertEquals(0.01,"a",1.0, "m²"); + } + + @Test + public void test1ToDeciares() throws Exception { + convertEquals(0.1,"da",1.0, "m²"); + } + + @Test + public void test1ToCentiare() throws Exception { + convertEquals(1.0,"ca",1.0, "m²"); + } + + @Test + public void test1ToAcres() throws Exception { + convertEquals(0.000247105381,"acres",1.0, "m²"); + } + + @Test + public void test1ToCents() throws Exception { + convertEquals(0.024710538 ,"cents",1.0, "m²"); + } + + @Test + public void test1ToSquareYard() throws Exception { + convertEquals(1.195990 ,"yd²",1.0, "m²"); + } + + @Test + public void test1ToSquareFeet() throws Exception { + convertEquals(10.763911 ,"ft²",1.0, "m²"); + } + + @Test + public void test1ToSquareInch() throws Exception { + convertEquals(1550.0031 ,"in²",1.0, "m²"); + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXConverterResultStepModel.java b/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/module/UnitXCModuleVolumeTest.java similarity index 59% rename from ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXConverterResultStepModel.java rename to ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/module/UnitXCModuleVolumeTest.java index 539f174..990e41e 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXConverterResultStepModel.java +++ b/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/module/UnitXCModuleVolumeTest.java @@ -21,47 +21,26 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package net.forwardfire.unitxc.model; +package net.forwardfire.unitxc.module; -import org.apache.commons.lang3.Validate; +import org.junit.Test; -/** - * - * - * @author Willem Cazander - * @version 1.0 Oct 22, 2015 - */ -public class UnitXConverterResultStepModel implements UnitXConverterResultStep { +import static net.forwardfire.unitxc.UnitXCAssert.convertEquals; + +public class UnitXCModuleVolumeTest { - 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); + @Test + public void test10CubicFootToCubicInch() throws Exception { + convertEquals(18597.989,"in³",10.763, "ft³"); // redo after fix } - @Override - public String getName() { - return name; + @Test + public void test1ToCubicCentimetre() throws Exception { + convertEquals(1000000,"cm³",1.0, "m³"); } - @Override - public Double getInputValue() { - return inputValue; - } - - @Override - public Double getOutputValue() { - return outputValue; - } - - @Override - public Long getConvertTime() { - return convertTime; + @Test + public void test1ToCubicDecimetre() throws Exception { + convertEquals(1000,"dm³",1.0, "m³"); } } diff --git a/readme.md b/readme.md index f4d4334..48d6f57 100644 --- a/readme.md +++ b/readme.md @@ -2,14 +2,16 @@ FF-UnitXC ========= FF-UnitXC is a java8 library providing a cross unit converter. +NOTE: Status is in progress. ## Features * Many units provided. * Unit groups. -* Unit tags. +* Unit flags. * Full metric system. * Imperial values. +* Weird SI units. ## Installation