From 5076d2be5528295f90b6e61cb39894a03bf169ba Mon Sep 17 00:00:00 2001 From: Willem Date: Mon, 24 Oct 2016 17:00:50 +0200 Subject: [PATCH] First version with unit types on request. --- ff-unitxc-converter/pom.xml | 23 ++- .../net/forwardfire/unitxc/UnitXCFactory.java | 45 ++++- .../net/forwardfire/unitxc/UnitXCManager.java | 17 +- .../unitxc/config/UnitXCConfigManager.java | 88 ++------ .../unitxc/config/UnitXCConfigModule.java | 7 - .../builder/AbstractUnitXCTypeBuilder.java | 10 +- .../config/builder/UnitXCConfigBuilder.java | 35 +++- .../config/builder/UnitXCGroupBuilder.java | 53 ++++- .../builder/UnitXCGroupJumpBuilder.java | 61 ++++++ .../UnitXCTypeCompoundExponentBuilder.java | 101 +++++---- .../UnitXCTypeCompoundPairBuilder.java | 181 ++++++++++++----- .../builder/UnitXCTypeSIPrefixBuilder.java | 13 +- .../builder/UnitXConverterStepBuilder.java | 67 +++--- .../unitxc/converter/UnitXConverter.java | 49 ++++- .../converter/UnitXConverterEngine.java | 191 ++++++++++++------ .../UnitXConverterParameterValue.java | 76 +++++++ .../unitxc/model/ModelJAXBInfoSet.java | 32 ++- .../unitxc/model/ModelJAXBMarshaller.java | 134 ++++++------ .../unitxc/model/ModelJAXBObjectWrap.java | 59 ++++++ .../model/ModelJAXBObjectWrapAdapter.java | 44 ++++ .../model/ModelJAXBTypedMarshaller.java | 152 ++++++++++++++ .../unitxc/model/UnitXCConfig.java | 87 ++++++++ .../forwardfire/unitxc/model/UnitXCGroup.java | 99 ++++++++- .../unitxc/model/UnitXCGroupJump.java | 128 ++++++++++++ .../model/UnitXCGroupJumpParameter.java | 84 ++++++++ .../unitxc/model/UnitXCRounding.java | 14 ++ .../forwardfire/unitxc/model/UnitXCType.java | 42 ++-- .../unitxc/model/UnitXCTypeGenerator.java | 12 ++ .../unitxc/model/UnitXConverterResult.java | 42 ++-- .../model/UnitXConverterResultStep.java | 41 ++-- .../step/AbstractUnitXConverterStep.java | 45 +---- .../step/UnitXCConverterStepAutoRounding.java | 91 +++++++++ .../step/UnitXCConverterStepCondition.java | 125 ++++++++++++ .../UnitXCConverterStepConditionEquality.java | 37 ++++ .../step/UnitXCConverterStepOperation.java | 102 +++++++--- .../UnitXCConverterStepOperationOperator.java | 57 ++++++ .../step/UnitXCConverterStepPowerOfTen.java | 72 +++++-- .../step/UnitXCConverterStepReference.java | 90 +++++++++ .../unitxc/model/step/UnitXConverterStep.java | 12 +- .../model/step/UnitXConverterStepContext.java | 60 ++++++ .../unitxc/model/step/package-info.java | 7 + ...UnitXConverterStepValueNamedParameter.java | 76 +++++++ .../UnitXConverterStepValueNamedVariable.java | 99 +++++++++ .../value/UnitXConverterStepValueRead.java | 39 ++++ .../UnitXConverterStepValueReadWrite.java | 39 ++++ ...nitXConverterStepValueReferenceDouble.java | 80 ++++++++ ...erterStepValueReferenceDoubleFraction.java | 94 +++++++++ ...itXConverterStepValueReferenceInteger.java | 79 ++++++++ ...nitXConverterStepValueReferenceString.java | 79 ++++++++ .../unitxc/model/step/value/package-info.java | 5 + .../module/UnitXCModuleElectricCurrent.java | 9 +- .../unitxc/module/UnitXCModuleLength.java | 9 +- .../module/UnitXCModuleLuminousIntensity.java | 13 +- .../unitxc/module/UnitXCModuleMass.java | 4 +- .../unitxc/module/UnitXCModuleSubstance.java | 19 +- .../module/UnitXCModuleTemperature.java | 8 +- .../unitxc/module/UnitXCModuleTime.java | 7 +- .../derived/UnitXCModuleAcceleration.java | 63 ++++++ .../module/derived/UnitXCModuleArea.java | 8 +- .../derived/UnitXCModuleAreaDensity.java | 59 ++++++ .../module/derived/UnitXCModuleJerk.java | 60 ++++++ .../module/derived/UnitXCModuleJounce.java | 60 ++++++ .../derived/UnitXCModuleMassDensity.java | 59 ++++++ .../UnitXCModuleMolarConcentration.java | 59 ++++++ .../derived/UnitXCModuleMolarVolume.java | 59 ++++++ .../derived/UnitXCModuleSpecificVolume.java | 59 ++++++ .../module/derived/UnitXCModuleSpeed.java | 8 +- .../module/derived/UnitXCModuleVolume.java | 7 +- .../derived/UnitXCModuleVolumetricFlow.java | 59 ++++++ .../unitxc/module/derived/package-info.java | 27 +++ .../module/named/UnitXCModuleNewton.java | 62 ++++++ .../unitxc/module/named/package-info.java | 8 + .../unitxc/UnitXCConverterStepTest.java | 174 ++++++++++------ .../forwardfire/unitxc/UnitXCFactoryTest.java | 2 +- .../forwardfire/unitxc/UnitXCManagerTest.java | 33 ++- 75 files changed, 3555 insertions(+), 655 deletions(-) create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCGroupJumpBuilder.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/UnitXConverterParameterValue.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelJAXBObjectWrap.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelJAXBObjectWrapAdapter.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelJAXBTypedMarshaller.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCConfig.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroupJump.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroupJumpParameter.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCRounding.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCTypeGenerator.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXCConverterStepAutoRounding.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXCConverterStepCondition.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXCConverterStepConditionEquality.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXCConverterStepOperationOperator.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXCConverterStepReference.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXConverterStepContext.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/package-info.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueNamedParameter.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueNamedVariable.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueRead.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueReadWrite.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueReferenceDouble.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueReferenceDoubleFraction.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueReferenceInteger.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueReferenceString.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/package-info.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleAcceleration.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleAreaDensity.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleJerk.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleJounce.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleMassDensity.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleMolarConcentration.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleMolarVolume.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleSpecificVolume.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleVolumetricFlow.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/package-info.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/named/UnitXCModuleNewton.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/named/package-info.java diff --git a/ff-unitxc-converter/pom.xml b/ff-unitxc-converter/pom.xml index 0ff8923..fb99502 100644 --- a/ff-unitxc-converter/pom.xml +++ b/ff-unitxc-converter/pom.xml @@ -1,11 +1,12 @@ - - 4.0.0 - - net.forwardfire.unitxc - ff-unitxc - 0.0.1-SNAPSHOT - - ff-unitxc-converter + + 4.0.0 + + net.forwardfire.unitxc + ff-unitxc + 0.0.1-SNAPSHOT + + ff-unitxc-converter org.slf4j @@ -17,5 +18,11 @@ commons-lang3 compile + + org.codehaus.jettison + jettison + 1.3.7 + test + \ No newline at end of file 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 edce40e..e55d34b 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 @@ -27,19 +27,30 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import net.forwardfire.unitxc.config.UnitXCConfig; import net.forwardfire.unitxc.config.UnitXCConfigManager; import net.forwardfire.unitxc.config.UnitXCConfigModule; +import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; +import net.forwardfire.unitxc.model.UnitXCConfig; import net.forwardfire.unitxc.module.UnitXCModuleSubstance; -import net.forwardfire.unitxc.module.UnitXCModuleArea; 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; +import net.forwardfire.unitxc.module.derived.UnitXCModuleAcceleration; +import net.forwardfire.unitxc.module.derived.UnitXCModuleArea; +import net.forwardfire.unitxc.module.derived.UnitXCModuleAreaDensity; +import net.forwardfire.unitxc.module.derived.UnitXCModuleJerk; +import net.forwardfire.unitxc.module.derived.UnitXCModuleJounce; +import net.forwardfire.unitxc.module.derived.UnitXCModuleMassDensity; +import net.forwardfire.unitxc.module.derived.UnitXCModuleMolarConcentration; +import net.forwardfire.unitxc.module.derived.UnitXCModuleMolarVolume; +import net.forwardfire.unitxc.module.derived.UnitXCModuleSpecificVolume; +import net.forwardfire.unitxc.module.derived.UnitXCModuleSpeed; +import net.forwardfire.unitxc.module.derived.UnitXCModuleVolume; +import net.forwardfire.unitxc.module.derived.UnitXCModuleVolumetricFlow; +import net.forwardfire.unitxc.module.named.UnitXCModuleNewton; /** * The unit cross converter factory to create the manager. @@ -57,9 +68,25 @@ public final class UnitXCFactory { new UnitXCModuleLength(), new UnitXCModuleMass(), new UnitXCModuleTime(), + + // currently ordered here new UnitXCModuleArea(), new UnitXCModuleVolume(), - new UnitXCModuleSpeed() + new UnitXCModuleSpeed(), + new UnitXCModuleVolumetricFlow(), + new UnitXCModuleAcceleration(), + new UnitXCModuleJerk(), + new UnitXCModuleJounce(), + + // named + //new UnitXCModuleNewton(), + + // more derived + new UnitXCModuleAreaDensity(), + new UnitXCModuleMassDensity(), + new UnitXCModuleSpecificVolume(), + new UnitXCModuleMolarConcentration(), + new UnitXCModuleMolarVolume() )); @@ -67,12 +94,18 @@ public final class UnitXCFactory { } + private static UnitXCConfig buildAll(UnitXCConfig config,List configInit) { + UnitXCConfigBuilder builder = new UnitXCConfigBuilder(config); + configInit.forEach(ci -> ci.configModule(builder)); + return config; + } + public static UnitXCConfig createConfig() { return createConfig(DEFAULT_CONFIG_MODULES); } public static UnitXCConfig createConfig(List configInit) { - return UnitXCConfigModule.buildAll(new UnitXCConfig(), configInit); + return buildAll(new UnitXCConfig(), configInit); } public static UnitXCManager createManager() { diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/UnitXCManager.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/UnitXCManager.java index f8b3681..8e2eec5 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/UnitXCManager.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/UnitXCManager.java @@ -23,10 +23,9 @@ package net.forwardfire.unitxc; -import java.util.Collection; - import net.forwardfire.unitxc.converter.UnitXConverter; import net.forwardfire.unitxc.model.UnitXCType; +import net.forwardfire.unitxc.model.UnitXCConfig; import net.forwardfire.unitxc.model.UnitXCGroup; /** @@ -37,23 +36,13 @@ import net.forwardfire.unitxc.model.UnitXCGroup; */ public interface UnitXCManager { - /** - * @return Returns all unit types. - */ - Collection getUnitTypes(); - UnitXCType getUnitType(String id); - boolean isUnitType(String id); - - /** - * @return Returns all unit groups. - */ - Collection getUnitGroups(); - UnitXCGroup getUnitGroup(String id); boolean isUnitGroup(String id); UnitXConverter getConverter(); + + UnitXCConfig getConfig(); } 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 8ef78cf..89303db 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 @@ -32,10 +32,9 @@ 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.UnitXCType; +import net.forwardfire.unitxc.model.UnitXCConfig; import net.forwardfire.unitxc.model.UnitXCGroup; -import net.forwardfire.unitxc.model.UnitXConverterResult; /** * @@ -46,66 +45,39 @@ import net.forwardfire.unitxc.model.UnitXConverterResult; public class UnitXCConfigManager implements UnitXCManager { private final UnitXCConfig config; - private final Map unitTypes; private final Map unitGroups; private final UnitXConverter converter; - private final UnitXConverterEngine convertEngine; public UnitXCConfigManager(UnitXCConfig config) { this.config = Validate.notNull(config); - this.unitTypes = Collections.unmodifiableMap(createUnitTypes(config.getUnitTypes())); - this.unitGroups = Collections.unmodifiableMap(createUnitGroups(config.getUnitroups())); - - this.convertEngine = new UnitXConverterEngine(this); - this.converter = new UnitXConverterImpl(); - } - - private static Map createUnitTypes(Collection values) { - Map result = new HashMap<>(); - 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 void putValue(Map result,String key,UnitXCType value) { - if (key == null) { - return; - } - Validate.isTrue(!result.containsKey(key),"Duplicate key: "+key); - result.put(key, value); + this.unitGroups = Collections.unmodifiableMap(createUnitGroups(config.getUnitGroups())); + this.converter = new UnitXConverter(this); } private static Map createUnitGroups(Collection values) { Map result = new HashMap<>(); - values.forEach((value) -> result.put(value.getId(), value)); + values.forEach((value) -> { + value.setTotalUnitTypes(value.getTypeGenerator().size()); + result.put(value.getId(), value); + }); return result; } @Override - public Collection getUnitTypes() { - return unitTypes.values(); + public UnitXCConfig getConfig() { + return config; } @Override public UnitXCType getUnitType(String id) { - return Validate.notNull(unitTypes.get(Validate.notNull(id,"Null is not a validate id.")),"No type for: "+id); - } - - @Override - public boolean isUnitType(String id) { - return unitTypes.containsKey(Validate.notBlank(id)); - } - - @Override - public Collection getUnitGroups() { - return unitGroups.values(); + UnitXCType result = null; + for (UnitXCGroup group:unitGroups.values()) { + result = group.getTypeGenerator().getUnitType(id); + if (result != null) { + break; + } + } + return result; } @Override @@ -122,29 +94,5 @@ public class UnitXCConfigManager implements UnitXCManager { public UnitXConverter getConverter() { return converter; } - - private class UnitXConverterImpl implements UnitXConverter { - - - @Override - public double convert(double value, String fromTypeId, String toTypeId) { - return convert(value, getUnitType(fromTypeId), getUnitType(toTypeId)); - } - - @Override - public double convert(double value, UnitXCType fromType, UnitXCType toType) { - return convertStepped(value,fromType,toType).getResultValue(); - } - - @Override - public UnitXConverterResult convertStepped(double value, String fromTypeId, String toTypeId) { - return convertStepped(value, getUnitType(fromTypeId), getUnitType(toTypeId)); - } - - @Override - public UnitXConverterResult convertStepped(double value, UnitXCType fromType, UnitXCType toType) { - // TODO: rm casting - return convertEngine.convertStepped(value, (UnitXCType)fromType, (UnitXCType)toType); - } - } + } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/UnitXCConfigModule.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/UnitXCConfigModule.java index eee30f4..4a580db 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/UnitXCConfigModule.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/UnitXCConfigModule.java @@ -24,7 +24,6 @@ package net.forwardfire.unitxc.config; import java.util.Arrays; -import java.util.List; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; @@ -43,10 +42,4 @@ public interface UnitXCConfigModule { Arrays.asList(flag).forEach(f -> buf.append(f.toUpperCase()).append("_")); return buf.deleteCharAt(buf.length()-1).toString(); } - - static UnitXCConfig buildAll(UnitXCConfig config,List configInit) { - UnitXCConfigBuilder builder = new UnitXCConfigBuilder(config); - configInit.forEach(ci -> ci.configModule(builder)); - return config; - } } 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 a6e042f..995fa53 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 @@ -28,8 +28,8 @@ import java.util.Collection; import java.util.List; import java.util.function.BiConsumer; -import net.forwardfire.unitxc.converter.step.UnitXConverterStep; import net.forwardfire.unitxc.model.UnitXCType; +import net.forwardfire.unitxc.model.step.UnitXConverterStep; /** * @@ -76,10 +76,6 @@ public abstract class AbstractUnitXCTypeBuilder extends AbstractUnitXCBuild 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); } @@ -105,11 +101,11 @@ public abstract class AbstractUnitXCTypeBuilder extends AbstractUnitXCBuild } public UnitXConverterStepBuilder> createFromBaseConverterSteps() { - return new UnitXConverterStepBuilder<>(this,(p,v) -> p.addFromBaseConverterSteps(v)); + return new UnitXConverterStepBuilder<>(this,(p,v) -> p.addFromBaseConverterSteps(v),() -> "convert "+getValue().getId()); } public UnitXConverterStepBuilder> createToBaseConverterSteps() { - return new UnitXConverterStepBuilder<>(this,(p,v) -> p.addToBaseConverterSteps(v)); + return new UnitXConverterStepBuilder<>(this,(p,v) -> p.addToBaseConverterSteps(v),() -> "convert "+getValue().getId()); } public B addFromBaseConverterStep(UnitXConverterStep unitTypeConverter) { 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 9bb8891..9e71916 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 @@ -25,7 +25,8 @@ package net.forwardfire.unitxc.config.builder; import org.apache.commons.lang3.Validate; -import net.forwardfire.unitxc.config.UnitXCConfig; +import net.forwardfire.unitxc.model.UnitXCConfig; +import net.forwardfire.unitxc.model.UnitXCGroup; import net.forwardfire.unitxc.model.UnitXCType; /** @@ -51,15 +52,22 @@ public class UnitXCConfigBuilder { } public UnitXCGroupBuilder createUnitGroup() { - return new UnitXCGroupBuilder(this); + return new UnitXCGroupBuilder(this, new UnitXCGroup(), (p,v) -> p.getConfig().addUnitGroup(v)); } - public UnitXCTypeBuilder createUnitType() { - return new UnitXCTypeBuilder<>(this,new UnitXCType(),(p,v) -> p.getConfig().addUnitType(v)); + public UnitXCGroupBuilder extendUnitGroup(String groupId) { + UnitXCGroup group = findGroup(groupId); + return new UnitXCGroupBuilder(this, group, (p,v) -> {}); } - private UnitXCType findType(String id) { - for (UnitXCType m:config.getUnitTypes()) { + public UnitXCTypeBuilder createUnitType(String groupId) { + UnitXCGroup group = findGroup(groupId); + return new UnitXCTypeBuilder<>(this,new UnitXCType(),(p,v) -> group.addUnitType(v)); + } + + private UnitXCGroup findGroup(String id) { + Validate.notBlank(id,"Can't search blank id."); + for (UnitXCGroup m:config.getUnitGroups()) { if (m.getId().equals(id)) { return m; } @@ -67,7 +75,16 @@ public class UnitXCConfigBuilder { throw new IllegalArgumentException("Could not find: "+id); } - public UnitXCTypeBuilder extendUnitType(String id) { - return new UnitXCTypeBuilder<>(this,findType(id),(p,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 a36bf20..743aea4 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,9 +23,15 @@ package net.forwardfire.unitxc.config.builder; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.function.BiConsumer; + import net.forwardfire.unitxc.model.UnitXCGroup; import net.forwardfire.unitxc.model.UnitXCGroupLevel; import net.forwardfire.unitxc.model.UnitXCType; +import net.forwardfire.unitxc.model.UnitXCTypeGenerator; /** * @@ -35,8 +41,37 @@ import net.forwardfire.unitxc.model.UnitXCType; */ public class UnitXCGroupBuilder extends AbstractUnitXCBuilder { - public UnitXCGroupBuilder(UnitXCConfigBuilder parent) { - super(parent, new UnitXCGroup(), (p,v) -> p.getConfig().addUnitGroup(v)); + public UnitXCGroupBuilder(UnitXCConfigBuilder parent, UnitXCGroup model, BiConsumer parentBuilder) { + super(parent, model, parentBuilder); + if (model.getTypeGenerator() == null) { + model.setTypeGenerator(new UnitXCTypeGenerator() { + + @Override + public long size() { + return model.getUnitTypes().size(); + } + + @Override + public Iterator getUnitTypes() { + List result = new ArrayList<>(); + model.getUnitTypes().forEach(t -> result.add(t.getId())); + return result.iterator(); + } + + @Override + public UnitXCType getUnitType(String id) { + for (UnitXCType type:model.getUnitTypes()) { + if (type.getId().equals(id)) { + return type; + } + if (type.getName().equals(id)) { + return type; + } + } + return null; + } + }); + } } @Override @@ -44,20 +79,24 @@ public class UnitXCGroupBuilder extends AbstractUnitXCBuilder createUnitType() { - return new UnitXCTypeBuilder<>(this,new UnitXCType(),(p,v) -> p.getParent().getConfig().addUnitType(v)).setTypeGroupId(getValue().getId()); + return new UnitXCTypeBuilder<>(this,new UnitXCType(),(p,v) -> p.getValue().addUnitType(v)); } public UnitXCTypeSIPrefixBuilder createSIUnitTypes() { - return new UnitXCTypeSIPrefixBuilder<>(this,getParent()).setTypeGroupId(getValue().getId()); + return new UnitXCTypeSIPrefixBuilder<>(this,this); } - public UnitXCTypeCompoundExponentBuilder createCompoundExponentUnitTypes(String unitTypeGroupId,int exponent) { - return new UnitXCTypeCompoundExponentBuilder(this,getParent(),unitTypeGroupId,exponent); + public UnitXCTypeCompoundExponentBuilder createCompoundExponentUnitTypes(String unitTypeId, int exponent) { + return new UnitXCTypeCompoundExponentBuilder(this, unitTypeId, exponent); } public UnitXCTypeCompoundPairBuilder createCompoundPairUnitTypes(String groupId,String perGroupId) { - return new UnitXCTypeCompoundPairBuilder(this,getParent(),groupId,perGroupId); + return new UnitXCTypeCompoundPairBuilder(this,groupId,perGroupId); } public UnitXCGroupBuilder setId(String id) { diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCGroupJumpBuilder.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCGroupJumpBuilder.java new file mode 100644 index 0000000..7709316 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCGroupJumpBuilder.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2013-2015, Willem Cazander + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package net.forwardfire.unitxc.config.builder; + + +import net.forwardfire.unitxc.model.UnitXCGroup; +import net.forwardfire.unitxc.model.UnitXCGroupJump; +import net.forwardfire.unitxc.model.UnitXCGroupJumpParameter; + +/** + * + * + * @author Willem Cazander + * @version 1.0 Apr 1, 2016 + */ +public class UnitXCGroupJumpBuilder extends AbstractUnitXCBuilder { + + public UnitXCGroupJumpBuilder(UnitXCConfigBuilder parent, UnitXCGroup forGroup, String unitGroupId) { + super(parent, new UnitXCGroupJump(), (b,v) -> forGroup.getGroupJumps().add(v)); + getValue().setUnitGroupId(unitGroupId); + getValue().setId(forGroup.getId()+"_"+unitGroupId); + } + + @Override + protected UnitXCGroupJumpBuilder getBuilder() { + return this; + } + + public UnitXCGroupJumpBuilder setId(String id) { + return make((v) -> v.setId(id)); + } + + public UnitXCGroupJumpBuilder addJumpParameter(String id,String unitGroupId) { + return make((v) -> v.addJumpParameter(new UnitXCGroupJumpParameter(id, unitGroupId))); + } + + public UnitXConverterStepBuilder createToGroupConverterSteps() { + return new UnitXConverterStepBuilder<>(this,(p,v) -> getValue().getToGroupConverterSteps().addAll(v),() -> "group jump"); + } +} 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 2f8c275..5162a5b 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 @@ -23,14 +23,14 @@ package net.forwardfire.unitxc.config.builder; -import java.util.ArrayList; -import java.util.List; +import java.util.Iterator; 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; +import net.forwardfire.unitxc.model.UnitXCTypeGenerator; +import net.forwardfire.unitxc.model.step.UnitXCConverterStepReference; /** * @@ -40,17 +40,75 @@ import net.forwardfire.unitxc.model.UnitXCType; */ public class UnitXCTypeCompoundExponentBuilder extends AbstractUnitXCBuilder { - private final UnitXCConfigBuilder builder; private final String unitGroupId; private final int exponent; private String unitIdPostfix; private String unitNamePrefix; - public UnitXCTypeCompoundExponentBuilder(UnitXCGroupBuilder parent, UnitXCConfigBuilder builder,String unitGroupId,int exponent) { + public UnitXCTypeCompoundExponentBuilder(UnitXCGroupBuilder parent, String unitGroupId, int exponent) { super(parent,new Object(), (p,v) -> {}); - this.builder = builder; this.unitGroupId = unitGroupId; this.exponent = exponent; + parent.getValue().setTypeGenerator(new UnitXCTypeGenerator() { + + UnitXCGroup getGroup() { + UnitXCGroup group = null; + for (UnitXCGroup unitGroup:getParent().getParent().getConfig().getUnitGroups()) { + if (unitGroup.getId().equals(unitGroupId)) { + group = unitGroup; + break; + } + } + return group; + } + + @Override + public long size() { + return getGroup().getTypeGenerator().size(); + } + + @Override + public Iterator getUnitTypes() { + Iterator g = getGroup().getTypeGenerator().getUnitTypes(); + return new Iterator() { + @Override + public boolean hasNext() { + return g.hasNext(); + } + @Override + public String next() { + return g.next()+unitIdPostfix; + } + }; + } + + @Override + public UnitXCType getUnitType(String id) { + if (!id.endsWith(unitIdPostfix)) { + return null; + } + String idRaw = id.substring(0, id.length()-unitIdPostfix.length()); + UnitXCGroup group = getGroup(); + UnitXCType unitType = group.getTypeGenerator().getUnitType(idRaw); + if (unitType == null) { + return null; + } + + UnitXCTypeBuilder typeBuilder = new UnitXCTypeBuilder<>(this,new UnitXCType(),(p,v) -> {}); + typeBuilder + .setId(unitType.getId()+unitIdPostfix) + .setName(unitNamePrefix+unitType.getName()) + .addTypeFlags(unitType.getTypeFlags()); + + for (int i=0;i(builder.getConfig().getUnitTypes())) { - if (!unitType.getUnitGroupId().equals(group.getId())) { - continue; - } - UnitXCTypeBuilder typeBuilder = builder.createUnitType() - .setId(unitType.getId()+unitIdPostfix) - .setName(unitNamePrefix+unitType.getName()) - .setTypeGroupId(getParent().getValue().getId()) - .addTypeFlags(unitType.getTypeFlags()); - - for (int i=0;i 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); - } - 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 index f768499..cec66f5 100644 --- 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 @@ -23,35 +23,124 @@ package net.forwardfire.unitxc.config.builder; -import java.util.ArrayList; -import java.util.List; +import java.util.Iterator; 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; +import net.forwardfire.unitxc.model.UnitXCTypeGenerator; +import net.forwardfire.unitxc.model.step.UnitXCConverterStepReference; +import net.forwardfire.unitxc.model.step.value.UnitXConverterStepValueNamedParameter; /** - * + * Build compound pair units. * * @author Willem Cazander * @version 1.0 Oct 26, 2015 */ -public class UnitXCTypeCompoundPairBuilder extends AbstractUnitXCBuilder { +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; + public UnitXCTypeCompoundPairBuilder(UnitXCGroupBuilder parent,String groupId,String perGroupId) { + super(parent,parent.getValue(), (p,v) -> {}); this.groupId = groupId; this.perGroupId = perGroupId; + + parent.getValue().setTypeGenerator(new UnitXCTypeGenerator() { + + UnitXCGroup getGroup(String id) { + UnitXCGroup perGroup = null; + for (UnitXCGroup unitGroup:getParent().getParent().getConfig().getUnitGroups()) { + if (unitGroup.getId().equals(id)) { + perGroup = unitGroup; + } + } + return perGroup; + } + + @Override + public long size() { + UnitXCGroup group = getGroup(groupId); + UnitXCGroup perGroup = getGroup(perGroupId); + return group.getTypeGenerator().size()*perGroup.getTypeGenerator().size(); + } + + @Override + public Iterator getUnitTypes() { + UnitXCGroup group = getGroup(groupId); + UnitXCGroup perGroup = getGroup(perGroupId); + Iterator groupIt = group.getTypeGenerator().getUnitTypes(); + return new Iterator() { + + String groupId2; + Iterator perGroupIt; + + @Override + public boolean hasNext() { + if (perGroupIt!=null && perGroupIt.hasNext()) { + return true; + } + if (groupIt.hasNext()) { + groupId2 = groupIt.next(); + perGroupIt = perGroup.getTypeGenerator().getUnitTypes(); + return true; + } + return false; + } + + @Override + public String next() { + return groupId2+PER_ID+perGroupIt.next(); + } + }; + } + + @Override + public UnitXCType getUnitType(String id) { + if (!id.contains(PER_ID)) { + return null; + } + UnitXCGroup group = getGroup(groupId); + UnitXCGroup perGroup = getGroup(perGroupId); + Validate.notNull(group,"Could not find group: "+groupId); + Validate.notNull(perGroup,"Could not find group: "+perGroupId); + + String idGroup = id.substring(0,id.lastIndexOf(PER_ID)); // FIXME parse correctly see newton + String idGroupPer = id.substring(id.lastIndexOf(PER_ID)+1); + + UnitXCType unitType = group.getTypeGenerator().getUnitType(idGroup); + if (unitType == null) { + return null; + } + UnitXCType perUnitType = perGroup.getTypeGenerator().getUnitType(idGroupPer); + if (perUnitType == null) { + return null; + } + + // TODO: add name/name alias + UnitXCTypeBuilder typeBuilder = new UnitXCTypeBuilder<>(this,new UnitXCType(),(p,v) -> {}); + typeBuilder + .setId(id) + .setName(unitType.getName()+PER_NAME+perUnitType.getName()) + .addTypeFlags(unitType.getTypeFlags()) + .addTypeFlags(perUnitType.getTypeFlags()); + + typeBuilder.addToBaseConverterStep(new UnitXCConverterStepReference(unitType.getId(),true)); + typeBuilder.addToBaseConverterStep(new UnitXCConverterStepReference(perUnitType.getId(),false)); + typeBuilder.addFromBaseConverterStep(new UnitXCConverterStepReference(perUnitType.getId(),true)); + typeBuilder.addFromBaseConverterStep(new UnitXCConverterStepReference(unitType.getId(),false)); + typeBuilder.build(); + typeBuilder.getValue().setUnitGroup(parent.getValue()); + return typeBuilder.getValue(); + } + + }); } @Override @@ -60,11 +149,11 @@ public class UnitXCTypeCompoundPairBuilder extends AbstractUnitXCBuilder types = new ArrayList<>(builder.getConfig().getUnitTypes()); + //getValue().getGroupJumps().add(toGroupJump); // m/s -> m + //getValue().getGroupJumps().add(perGroupJump); // m/s -> h - 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; + //group.getGroupJumps().add(toGroupJump); // m -> m/s + //perGroup.getGroupJumps().add(perGroupJump); // s -> m/s + + // m/s -> m + getParent() + .createGroupJump(group.getId()) + .addJumpParameter(value.getId()+"_"+perGroup.getId(),perGroup.getId()) + .createToGroupConverterSteps() + .multiply(new UnitXConverterStepValueNamedParameter(value.getId()+"_"+perGroup.getId())) + .build() + .build(); + + // m -> m/s + getParent().getParent().extendUnitGroup(group.getId()) + .createGroupJump(value.getId()) + .addJumpParameter(value.getId()+"_"+perGroup.getId(),perGroup.getId()) + .createToGroupConverterSteps() + .divide(new UnitXConverterStepValueNamedParameter(value.getId()+"_"+perGroup.getId())) + .build() + .build(); } } 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 98c73e4..ce95cfa 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 @@ -28,8 +28,9 @@ import java.util.Arrays; import java.util.Collection; import java.util.List; -import net.forwardfire.unitxc.converter.step.UnitXCTypePowerOfTenConverterStep; import net.forwardfire.unitxc.model.UnitXCType; +import net.forwardfire.unitxc.model.step.UnitXCConverterStepPowerOfTen; +import net.forwardfire.unitxc.model.step.UnitXConverterStepContext; /** * @@ -39,10 +40,10 @@ import net.forwardfire.unitxc.model.UnitXCType; */ public class UnitXCTypeSIPrefixBuilder

extends AbstractUnitXCTypeBuilder> { - private final UnitXCConfigBuilder builder; + private final UnitXCGroupBuilder builder; private final List commonSIPrefixes; - public UnitXCTypeSIPrefixBuilder(P parent, UnitXCConfigBuilder builder) { + public UnitXCTypeSIPrefixBuilder(P parent, UnitXCGroupBuilder builder) { super(parent,new UnitXCType(), (p,v) -> {}); this.builder = builder; this.commonSIPrefixes = new ArrayList<>(); @@ -60,7 +61,6 @@ public class UnitXCTypeSIPrefixBuilder

extends AbstractUnitXCTypeBuilder extends AbstractUnitXCTypeBuilder> extends AbstractUnitXCBuilder,UnitXConverterStepBuilder

> { +public class UnitXConverterStepBuilder

> extends AbstractUnitXCBuilder,UnitXConverterStepBuilder

> { - public UnitXConverterStepBuilder(P parent, BiConsumer> parentBuilder) { + private final Supplier stepReason; + + public UnitXConverterStepBuilder(P parent, BiConsumer> parentBuilder, Supplier stepReason) { super(parent, new ArrayList<>(), parentBuilder); - this.getParent().getValue().getId(); + this.stepReason = Validate.notNull(stepReason); } @Override @@ -56,17 +61,19 @@ public class UnitXConverterStepBuilder

> } private String createStepReason() { - return "convert "+getParent().getValue().getId(); + return stepReason.get(); + //return "convert "+getParent().getValue().getId(); } - private UnitXConverterStepBuilder

offset(double offset,boolean offsetPositive,String stepReason) { - return make(v -> v.add(new UnitXCTypeOffsetConverterStep(offset,offsetPositive).buildReason(stepReason))); + private UnitXCConverterStepOperation createStepOperation(UnitXCConverterStepOperationOperator op,UnitXConverterStepValueRead opB) { + return new UnitXCConverterStepOperation(op,UnitXConverterStepContext.createStepValue(),opB,UnitXConverterStepContext.createStepValue()); } + public UnitXConverterStepBuilder

offsetUp(double offset,String stepReason) { - return offset(offset,true,stepReason); + return make(v -> v.add(createStepOperation(UnitXCConverterStepOperationOperator.ADD,new UnitXConverterStepValueReferenceDouble(offset)))); } public UnitXConverterStepBuilder

offsetDown(double offset,String stepReason) { - return offset(offset,false,stepReason); + return make(v -> v.add(createStepOperation(UnitXCConverterStepOperationOperator.SUBTRACT,new UnitXConverterStepValueReferenceDouble(offset)))); } public UnitXConverterStepBuilder

offsetUp(double offset) { return offsetUp(offset,createStepReason()); @@ -77,10 +84,10 @@ public class UnitXConverterStepBuilder

> public UnitXConverterStepBuilder

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

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

multiply(Fraction fraction) { return multiply(fraction,createStepReason()); @@ -89,29 +96,41 @@ public class UnitXConverterStepBuilder

> return multiply(numerator,denominator,createStepReason()); } + public UnitXConverterStepBuilder

multiply(UnitXConverterStepValueRead factor,String stepReason) { + return make(v -> v.add(createStepOperation(UnitXCConverterStepOperationOperator.MULTIPLY,factor))); + } public UnitXConverterStepBuilder

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

divide(UnitXConverterStepValueRead factor,String stepReason) { + return make(v -> v.add(createStepOperation(UnitXCConverterStepOperationOperator.DIVIDE,factor))); } public UnitXConverterStepBuilder

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

multiply(double factor) { return multiply(factor,createStepReason()); } - public UnitXConverterStepBuilder

divide(double factor) { + public UnitXConverterStepBuilder

multiply(UnitXConverterStepValueRead factor) { + return multiply(factor,createStepReason()); + } + public UnitXConverterStepBuilder

divide(UnitXConverterStepValueRead factor) { return divide(factor,createStepReason()); } + public UnitXConverterStepBuilder

divide(double factor) { + return divide(factor,createStepReason()); + } - public UnitXConverterStepBuilder

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

power(double exponent,String stepReason) { + return make(v -> v.add(createStepOperation(UnitXCConverterStepOperationOperator.POWER,new UnitXConverterStepValueReferenceDouble(exponent)))); } - public UnitXConverterStepBuilder

power(int exponent) { + public UnitXConverterStepBuilder

power(double exponent) { return power(exponent,createStepReason()); } private UnitXConverterStepBuilder

powerOfTen(int exponent,boolean rev,String stepReason) { - return make(v -> v.add(new UnitXCTypePowerOfTenConverterStep(exponent,rev).buildReason(stepReason))); + return make(v -> v.add(new UnitXCConverterStepPowerOfTen(exponent,rev,UnitXConverterStepContext.createStepValue(),UnitXConverterStepContext.createStepValue()).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/UnitXConverter.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/UnitXConverter.java index 9f894b5..2344e65 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/UnitXConverter.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/UnitXConverter.java @@ -23,6 +23,13 @@ package net.forwardfire.unitxc.converter; +import java.math.BigDecimal; +import java.util.Collections; +import java.util.Map; + +import org.apache.commons.lang3.Validate; + +import net.forwardfire.unitxc.UnitXCManager; import net.forwardfire.unitxc.model.UnitXCType; import net.forwardfire.unitxc.model.UnitXConverterResult; @@ -32,13 +39,45 @@ import net.forwardfire.unitxc.model.UnitXConverterResult; * @author Willem Cazander * @version 1.0 Oct 8, 2015 */ -public interface UnitXConverter { +public class UnitXConverter { - double convert(double value,String fromTypeId,String toTypeId); + private final UnitXCManager unitManager; + private final UnitXConverterEngine convertEngine; - double convert(double value,UnitXCType fromType,UnitXCType toType); + public UnitXConverter(UnitXCManager unitManager) { + this.unitManager = Validate.notNull(unitManager); + this.convertEngine = new UnitXConverterEngine(unitManager); + } - UnitXConverterResult convertStepped(double value,String fromTypeId,String toTypeId); + private UnitXCType getUnitType(String id) { + return unitManager.getUnitType(id); + } - UnitXConverterResult convertStepped(double value,UnitXCType fromType,UnitXCType toType); + public double convert(double value, String fromTypeId, String toTypeId) { + return convert(value, getUnitType(fromTypeId), getUnitType(toTypeId)); + } + + public double convert(double value, UnitXCType fromType, UnitXCType toType) { + return convertStepped(value,fromType,toType).getResultValue(); + } + + public UnitXConverterResult convertStepped(double value, String fromTypeId, String toTypeId) { + return convertStepped(value, getUnitType(fromTypeId), getUnitType(toTypeId)); + } + + public UnitXConverterResult convertStepped(double value, String fromTypeId, String toTypeId,Map parameters) { + return convertStepped(value, getUnitType(fromTypeId), getUnitType(toTypeId), parameters); + } + + public UnitXConverterResult convertStepped(double value, UnitXCType fromType, UnitXCType toType) { + return convertStepped(value, fromType, toType, Collections.emptyMap()); + } + + public UnitXConverterResult convertStepped(double value, UnitXCType fromType, UnitXCType toType, Map parameters) { + return convertStepped(BigDecimal.valueOf(value), fromType, toType, parameters); + } + + public UnitXConverterResult convertStepped(BigDecimal value, UnitXCType fromType, UnitXCType toType, Map parameters) { + return convertEngine.convertStepped(value, fromType, toType, parameters); + } } 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 e296fa8..b5f7718 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 @@ -23,18 +23,30 @@ package net.forwardfire.unitxc.converter; +import java.math.BigDecimal; +import java.math.MathContext; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import org.apache.commons.lang3.Validate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import net.forwardfire.unitxc.UnitXCManager; -import net.forwardfire.unitxc.converter.step.UnitXConverterStep; import net.forwardfire.unitxc.model.UnitXCGroup; +import net.forwardfire.unitxc.model.UnitXCGroupJump; +import net.forwardfire.unitxc.model.UnitXCGroupJumpParameter; import net.forwardfire.unitxc.model.UnitXCType; import net.forwardfire.unitxc.model.UnitXConverterResult; import net.forwardfire.unitxc.model.UnitXConverterResultStep; +import net.forwardfire.unitxc.model.step.UnitXCConverterStepAutoRounding; +import net.forwardfire.unitxc.model.step.UnitXConverterStep; +import net.forwardfire.unitxc.model.step.UnitXConverterStepContext; +import net.forwardfire.unitxc.model.step.value.UnitXConverterStepValueRead; +import net.forwardfire.unitxc.model.step.value.UnitXConverterStepValueReadWrite; +import net.forwardfire.unitxc.model.step.value.UnitXConverterStepValueReferenceDouble; /** * Runs requested convert steps and builds result models. @@ -46,100 +58,149 @@ public final class UnitXConverterEngine { private final static Logger LOG = LoggerFactory.getLogger(UnitXConverterEngine.class); private final UnitXCManager manager; + private final UnitXConverterStep roundingStep; public UnitXConverterEngine(UnitXCManager manager) { this.manager = manager; + this.roundingStep = new UnitXCConverterStepAutoRounding(UnitXConverterStepContext.createStepValue(),UnitXConverterStepContext.createStepValue()); } - public UnitXConverterResult convertStepped(double value,UnitXCType fromType,UnitXCType toType) { + public UnitXConverterResult convertStepped(BigDecimal valueConvert2,UnitXCType fromType,UnitXCType toType, Map parameters) { + double valueConvert = valueConvert2.doubleValue(); long startTime = System.currentTimeMillis(); - double startValue = value; - List resultSteps = new ArrayList<>(); + MathContext mathContext = MathContext.DECIMAL128; + UnitXConverterStepContextImpl ctx = new UnitXConverterStepContextImpl(mathContext,valueConvert); - UnitXCGroup fromTypeGroup = manager.getUnitGroup(fromType.getUnitGroupId()); - UnitXCGroup toTypeGroup = manager.getUnitGroup(toType.getUnitGroupId()); + UnitXCGroup fromTypeGroup = fromType.getUnitGroup(); + UnitXCGroup toTypeGroup = toType.getUnitGroup(); + UnitXCGroupJump crossGroupJump = null; // List<> after multi level search + + if (!fromTypeGroup.getId().equals(toTypeGroup.getId())) { + for (UnitXCGroupJump groupJump:fromTypeGroup.getGroupJumps()) { // FIXME + if (toTypeGroup.getId().equals(groupJump.getUnitGroupId())) { + crossGroupJump = groupJump; + break; + } + } + if (crossGroupJump == null) { + throw new IllegalArgumentException("from and to groups are not equals: "+fromTypeGroup.getId()+" != "+toTypeGroup.getId()); + } + for (UnitXCGroupJumpParameter jumpPara:crossGroupJump.getJumpParameters()) { + if (!parameters.containsKey(jumpPara.getId())) { + throw new IllegalArgumentException("Missing required convert parameter: "+jumpPara.getId()); + } + UnitXConverterParameterValue convPara = parameters.get(jumpPara.getId()); + if (convPara.getValueType() == null) { + throw new IllegalArgumentException("Convert parameter has not type: "+jumpPara.getId()); + } + if (!jumpPara.getUnitGroupId().equals(convPara.getValueType().getUnitGroup().getId())) { + throw new IllegalArgumentException("Convert parameter is wrong group required: "+jumpPara.getUnitGroupId()+" got: "+convPara.getValueType().getUnitGroup().getId()); + } + boolean paraToBase = convPara.getValueType().getUnitGroup().getBaseTypeId().equals(convPara.getValueType().getId()); + double paraValue = convPara.getValue(); + if (!paraToBase) { + System.out.println("--- cont para");; + UnitXConverterResult res = convertStepped(BigDecimal.valueOf(paraValue), convPara.getValueType(), manager.getUnitType(convPara.getValueType().getUnitGroup().getBaseTypeId()), parameters); // FIXME rm parameters ? + paraValue = res.getResultValue(); + //ctx.resultSteps.addAll(res.getResultSteps()); + } + ctx.namedParameter.put(jumpPara.getId(), new UnitXConverterStepValueReferenceDouble(paraValue)); + } + } boolean fromTypeBase = fromTypeGroup.getBaseTypeId().equals(fromType.getId()); if (!fromTypeBase) { - value = runConverter(resultSteps,fromType, value, true); - } - boolean toTypeBase = toTypeGroup.getBaseTypeId().equals(toType.getId()); - if (!toTypeBase) { - value = runConverter(resultSteps, toType, value, false); + ctx.runSteps(fromType.getToBaseConverterSteps()); } + if (crossGroupJump != null) { + ctx.runSteps(crossGroupJump.getToGroupConverterSteps()); + } + + boolean toTypeBase = toTypeGroup.getBaseTypeId().equals(toType.getId()); + if (!toTypeBase) { + ctx.runSteps(toType.getFromBaseConverterSteps()); + } long convertTime = System.currentTimeMillis()-startTime; UnitXConverterResult result = new UnitXConverterResult(); - result.setStartValue(startValue); - result.setStartValueType(fromType); - result.setResultValue(value); - result.setResultValueType(toType); +// result.setStartValue(ctx.getStartValue()); + result.setStartTypeId(fromType.getId()); + result.setResultValue(ctx.getValue()); + result.setResultTypeId(toType.getId()); result.setConvertTime(convertTime); - result.setResultSteps(resultSteps); + result.setResultSteps(ctx.resultSteps); return result; } - 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) { - valueOld = value; + class UnitXConverterStepContextImpl implements UnitXConverterStepContext { + + final MathContext mathContext; + List resultSteps = new ArrayList<>(); + Map namedVariables = new HashMap<>(); + Map namedParameter = new HashMap<>(); + + public UnitXConverterStepContextImpl(MathContext mathContext, double startValue) { + this.mathContext = Validate.notNull(mathContext); + setNamedVariable(VALUE, new UnitXConverterStepValueReferenceDouble(startValue)); + setNamedVariable(VALUE_START, new UnitXConverterStepValueReferenceDouble(startValue)); + } + + @Override + public void setNamedVariable(String name,UnitXConverterStepValueReadWrite variable) { + namedVariables.put(Validate.notBlank(name,"name is blank"),Validate.notNull(variable,"variable is null")); + } + + @Override + public UnitXConverterStepValueReadWrite getNamedVariable(String name) { + return Validate.notNull(namedVariables.get(Validate.notBlank(name,"name is blank")),"named variable not found: "+name); + } + + @Override + public UnitXConverterStepValueRead getNamedParameter(String name) { + return Validate.notNull(namedParameter.get(Validate.notBlank(name,"name is blank")),"named parameter not found: "+name); + } + + @Override + public void runSteps(List steps) { + for (UnitXConverterStep step:steps) { + runStep(step); + if (roundingStep != null) { + runStep(roundingStep); + } + } + } + + private void runStep(UnitXConverterStep step) { long startTime = System.currentTimeMillis(); - value = step.convert(value); + double valueOld = getValue(); + step.runStep(this); long convertTime = System.currentTimeMillis()-startTime; + + System.out.println("runStep: "+step+" res: "+step.getStepReasons()); + UnitXConverterResultStep resultStep = new UnitXConverterResultStep(); - resultStep.setName(step.getName()+(toBase?"-toBase":"-fromBase")+" = "+step.getMathExpression()); resultStep.setStartValue(valueOld); - resultStep.setResultValue(value); + resultStep.setResultValue(getValue()); resultStep.setConvertTime(convertTime); + resultStep.setConvertStep(step); resultSteps.add(resultStep); - - value = runRounding(resultSteps,value,valueOld); - +// previousValue = valueOld; } - return value; - } - - // TODO: redo this - private double runRounding(List resultSteps,double value,double valueOrg) { - long startTime = System.currentTimeMillis(); - String valueStr = Double.toString(value); - String valueOrgStr = Double.toString(valueOrg); - int valueDotIndex = valueStr.indexOf("."); - int valueOrgDotIndex = valueOrgStr.indexOf("."); - int valueDotSize = valueStr.length() - valueDotIndex; - int valueOrgDotSize = valueOrgStr.length() - valueOrgDotIndex; - if (valueDotSize<10) { - return value; + private Double getValue() { + return (Double)(getNamedVariable(VALUE).valueRead(this)); } - if (valueDotSize<=valueOrgDotSize) { - return value; + + @Override + public MathContext getMathContext() { + return mathContext; } - int valueEIndex = valueStr.indexOf("E"); - String valueShortStr = null; - if (valueEIndex>0) { - int sub = valueOrgStr.length(); - if (sub > valueEIndex) { - sub = valueEIndex; - } - valueShortStr = valueStr.substring(0, sub)+ valueStr.substring(valueEIndex); - } else { - valueShortStr = valueStr.substring(0, valueOrgStr.length()); + + @Override + public UnitXCManager getUnitManager() { + return manager; } - Double result = new Double(valueShortStr); - long roundTime = System.currentTimeMillis()-startTime; - - 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/UnitXConverterParameterValue.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/UnitXConverterParameterValue.java new file mode 100644 index 0000000..b7295ca --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/UnitXConverterParameterValue.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2013-2015, Willem Cazander + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package net.forwardfire.unitxc.converter; + +import org.apache.commons.lang3.Validate; + +import net.forwardfire.unitxc.model.UnitXCType; + +/** + * Holds the parameter value and type. + * + * @author Willem Cazander + * @version 1.0 Mar 18, 2016 + */ +public final class UnitXConverterParameterValue { + + private double value; + private UnitXCType valueType; + + public UnitXConverterParameterValue() { + } + + public UnitXConverterParameterValue(double value,UnitXCType valueType) { + setValue(value); + setValueType(Validate.notNull(valueType)); + } + + /** + * @return the value + */ + public double getValue() { + return value; + } + + /** + * @param value the value to set + */ + public void setValue(double value) { + this.value = value; + } + + /** + * @return the valueType + */ + public UnitXCType getValueType() { + return valueType; + } + + /** + * @param valueType the valueType to set + */ + public void setValueType(UnitXCType valueType) { + this.valueType = valueType; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelJAXBInfoSet.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelJAXBInfoSet.java index 2a2db21..b3ab224 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelJAXBInfoSet.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelJAXBInfoSet.java @@ -28,11 +28,9 @@ package net.forwardfire.unitxc.model; * @author Willem Cazander * @version 1.0 Jan 16, 2015 */ -public class ModelXMLInfoSet { +public class ModelJAXBInfoSet { - protected static final String XML_ENCODING = "UTF-8"; - - protected ModelXMLInfoSet() { + protected ModelJAXBInfoSet() { } protected static class Meta { @@ -42,19 +40,36 @@ public class ModelXMLInfoSet { } public static class Element { + protected static final String UNIT_CONFIG = "unitConfig"; + protected static final String UNIT_GROUP = "unitGroup"; + protected static final String UNIT_TYPE = "unitType"; 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"; + protected static final String GROUP_JUMP = "groupJump"; + protected static final String GROUP_JUMP_PARAMETER = "groupJumpParameter"; public static final String STEP_REASON = "stepReason"; public static final String STEP_REASONS = "stepReasons"; + public static final String OPERATION = "operation"; + public static final String CONDITION = "condition"; + public static final String REFERENCE = "reference"; + public static final String POWER_10 = "power10"; + public static final String ROUNDING = "rounding"; + + public static final String NAMED_VARIABLE = "namedVariable"; + public static final String NAMED_PARAMETER = "namedParameter"; + + public static final String VALUE_DOUBLE = "valueDouble"; + public static final String VALUE_DOUBLE_FRACTION = "valueDoubleFraction"; + public static final String VALUE_INTEGER = "valueInteger"; + public static final String VALUE_STRING = "valueString"; + protected Element() { } } @@ -63,17 +78,20 @@ public class ModelXMLInfoSet { protected static final String ID = "id"; public static final String NAME = "name"; protected static final String NAME_PLURAL = "namePlural"; + protected static final String UNIT_GROUP_ID = "unitGroupId"; 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 START_TYPE_ID = "startTypeId"; + protected static final String RESULT_TYPE_ID = "resultTypeId"; 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 static final String ROUNDING = "rounding"; protected Attribute() { } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelJAXBMarshaller.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelJAXBMarshaller.java index 30feab2..a56a4bd 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelJAXBMarshaller.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelJAXBMarshaller.java @@ -22,22 +22,24 @@ */ 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 javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; -import org.apache.commons.lang3.Validate; +import org.apache.commons.lang3.ArrayUtils; -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; +import net.forwardfire.unitxc.model.step.UnitXCConverterStepAutoRounding; +import net.forwardfire.unitxc.model.step.UnitXCConverterStepCondition; +import net.forwardfire.unitxc.model.step.UnitXCConverterStepOperation; +import net.forwardfire.unitxc.model.step.UnitXCConverterStepPowerOfTen; +import net.forwardfire.unitxc.model.step.UnitXCConverterStepReference; +import net.forwardfire.unitxc.model.step.value.UnitXConverterStepValueNamedParameter; +import net.forwardfire.unitxc.model.step.value.UnitXConverterStepValueNamedVariable; +import net.forwardfire.unitxc.model.step.value.UnitXConverterStepValueReferenceDouble; +import net.forwardfire.unitxc.model.step.value.UnitXConverterStepValueReferenceDoubleFraction; +import net.forwardfire.unitxc.model.step.value.UnitXConverterStepValueReferenceInteger; +import net.forwardfire.unitxc.model.step.value.UnitXConverterStepValueReferenceString; /** * Jaxb marshaller of the query sets. @@ -45,59 +47,71 @@ import net.forwardfire.unitxc.converter.step.UnitXCTypePowerOfTenConverterStep; * @author Willem Cazander * @version 1.0 Jan 14, 2015 */ -public class ModelXMLMarshaller { +public class ModelJAXBMarshaller { - private final JAXBContext jaxbContext; + private final ModelJAXBTypedMarshaller configModelMarshaller; + private final ModelJAXBTypedMarshaller groupModelMarshaller; + private final ModelJAXBTypedMarshaller typeModelMarshaller; + private final ModelJAXBTypedMarshaller resultModelMarshaller; - /** - * 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 - )); - } + private final static Class[] JAXB_TYPES = new Class[] { + UnitXCConfig.class, + UnitXCGroup.class, + UnitXConverterResult.class, + UnitXCConverterStepReference.class, + UnitXCConverterStepOperation.class, + UnitXCConverterStepCondition.class, + UnitXCConverterStepPowerOfTen.class, + UnitXCConverterStepAutoRounding.class, + UnitXConverterStepValueNamedParameter.class, + UnitXConverterStepValueNamedVariable.class, + UnitXConverterStepValueReferenceDouble.class, + UnitXConverterStepValueReferenceDoubleFraction.class, + UnitXConverterStepValueReferenceInteger.class, + UnitXConverterStepValueReferenceString.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); + public ModelJAXBMarshaller() throws JAXBException { + this(new Class[]{}); } - /** - * 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); + public ModelJAXBMarshaller(Class[] extraTypes) throws JAXBException { + this(JAXBContext.newInstance(ArrayUtils.addAll(getJAXBTypes(), extraTypes))); + } + + public ModelJAXBMarshaller(JAXBContext jaxbContext) throws JAXBException { + this(XMLInputFactory.newInstance(),XMLOutputFactory.newInstance(),jaxbContext); + } + + public ModelJAXBMarshaller(XMLInputFactory xmlInputFactory,XMLOutputFactory xmlOutputFactory, JAXBContext jaxbContext) { + this.configModelMarshaller = buildTypedMarshaller(UnitXCConfig.class, xmlInputFactory, xmlOutputFactory, jaxbContext); + this.groupModelMarshaller = buildTypedMarshaller(UnitXCGroup.class, xmlInputFactory, xmlOutputFactory, jaxbContext); + this.typeModelMarshaller = buildTypedMarshaller(UnitXCType.class, xmlInputFactory, xmlOutputFactory, jaxbContext); + this.resultModelMarshaller = buildTypedMarshaller(UnitXConverterResult.class, xmlInputFactory, xmlOutputFactory, jaxbContext); + } + + public static Class[] getJAXBTypes() { + return JAXB_TYPES; + } + + public ModelJAXBTypedMarshaller getUnitXCConfig() { + return configModelMarshaller; + } + + public ModelJAXBTypedMarshaller getUnitXCGroup() { + return groupModelMarshaller; + } + + public ModelJAXBTypedMarshaller getUnitXCType() { + return typeModelMarshaller; + } + + public ModelJAXBTypedMarshaller getUnitXConverterResult() { + return resultModelMarshaller; + } + + private static ModelJAXBTypedMarshaller buildTypedMarshaller(Class typeClass,XMLInputFactory xmlInputFactory,XMLOutputFactory xmlOutputFactory, JAXBContext jaxbContext) { + return new ModelJAXBTypedMarshaller(typeClass,xmlInputFactory,xmlOutputFactory,jaxbContext); } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelJAXBObjectWrap.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelJAXBObjectWrap.java new file mode 100644 index 0000000..79d67d4 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelJAXBObjectWrap.java @@ -0,0 +1,59 @@ +/* + * 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 javax.xml.bind.annotation.XmlAnyElement; + +/** + * + * + * @author Willem Cazander + * @version 1.0 Apr 3, 2016 + */ +public class ModelJAXBObjectWrap { + + private T value; + + public ModelJAXBObjectWrap() { + } + + public ModelJAXBObjectWrap(T value) { + setValue(value); + } + + /** + * @return the value + */ + @XmlAnyElement + public T getValue() { + return value; + } + + /** + * @param value the value to set + */ + public void setValue(T value) { + this.value = value; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelJAXBObjectWrapAdapter.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelJAXBObjectWrapAdapter.java new file mode 100644 index 0000000..ac0c029 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelJAXBObjectWrapAdapter.java @@ -0,0 +1,44 @@ +/* + * 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 javax.xml.bind.annotation.adapters.XmlAdapter; + +/** + * Adds extra wrap tag to any object. + * + * @author Willem Cazander + * @version 1.0 Apr 3, 2016 + */ +public class ModelJAXBObjectWrapAdapter extends XmlAdapter, Object> { + + @Override + public Object unmarshal(ModelJAXBObjectWrap v) throws Exception { + return v.getValue(); + } + + @Override + public ModelJAXBObjectWrap marshal(Object v) throws Exception { + return new ModelJAXBObjectWrap(v); + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelJAXBTypedMarshaller.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelJAXBTypedMarshaller.java new file mode 100644 index 0000000..e1ad835 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/ModelJAXBTypedMarshaller.java @@ -0,0 +1,152 @@ +package net.forwardfire.unitxc.model; + +import java.io.InputStream; +import java.io.OutputStream; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.Validate; + +public class ModelJAXBTypedMarshaller { + + private final Class typeClass; + private final XMLInputFactory xmlInputFactory; + private final XMLOutputFactory xmlOutputFactory; + private final JAXBContext jaxbContext; + + public ModelJAXBTypedMarshaller(Class typeClass, XMLInputFactory xmlInputFactory, + XMLOutputFactory xmlOutputFactory, JAXBContext jaxbContext) { + this.typeClass = Validate.notNull(typeClass); + this.xmlInputFactory = Validate.notNull(xmlInputFactory); + this.xmlOutputFactory = Validate.notNull(xmlOutputFactory); + this.jaxbContext = Validate.notNull(jaxbContext); + } + + protected Unmarshaller createUnmarshaller() throws JAXBException { + return jaxbContext.createUnmarshaller(); + } + + protected Marshaller createMarshaller(String encoding) throws JAXBException { + Validate.notNull(encoding, "encoding is null."); + Marshaller marshaller = jaxbContext.createMarshaller(); + //marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // done by stax proxy now + marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding); + return marshaller; + } + + public void marshal(T jaxbElement, XMLStreamWriter writer) throws JAXBException { + marshal(jaxbElement, writer, StandardCharsets.UTF_8.name()); + } + + public void marshal(T jaxbElement, XMLStreamWriter writer, String encoding) throws JAXBException { + Validate.notNull(jaxbElement, "jaxbElement is null."); + Validate.notNull(writer, "reader is null."); + Validate.notNull(encoding, "encoding is null."); + createMarshaller(encoding).marshal(jaxbElement, writer); + } + + public void marshal(T jaxbElement, OutputStream output) throws JAXBException, XMLStreamException { + marshal(jaxbElement, output, StandardCharsets.UTF_8.name()); + } + + public void marshal(T jaxbElement, OutputStream output, String encoding) throws JAXBException, XMLStreamException { + Validate.notNull(jaxbElement, "jaxbElement is null."); + Validate.notNull(output, "ouput is null."); + Validate.notNull(encoding, "encoding is null."); + marshal(jaxbElement, IndentingXMLStreamWriterProxy.wrap(xmlOutputFactory.createXMLStreamWriter(output, encoding)), encoding); + } + + public T unmarshal(XMLStreamReader reader) throws JAXBException { + Validate.notNull(reader, "reader is null."); + return createUnmarshaller().unmarshal(reader, typeClass).getValue(); + } + + public T unmarshal(InputStream input, String encoding) throws JAXBException, XMLStreamException { + Validate.notNull(input, "input is null."); + Validate.notNull(encoding, "encoding is null."); + return unmarshal(xmlInputFactory.createXMLStreamReader(input, encoding)); + } + + public T unmarshal(InputStream input) throws JAXBException, XMLStreamException { + return unmarshal(input, StandardCharsets.UTF_8.name()); + } + + static class IndentingXMLStreamWriterProxy implements InvocationHandler { + + private final XMLStreamWriter target; + private int depth = 0; + private boolean firstLine = true; + private final Map hasChildElement = new HashMap<>(); + + public IndentingXMLStreamWriterProxy(XMLStreamWriter target) { + this.target = Validate.notNull(target); + } + + public static XMLStreamWriter wrap(XMLStreamWriter writer) { + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + if (cl == null) { + IndentingXMLStreamWriterProxy.class.getClassLoader(); + } + return (XMLStreamWriter) Proxy.newProxyInstance(cl, new Class[]{XMLStreamWriter.class}, new IndentingXMLStreamWriterProxy(writer)); + } + + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + String m = method.getName(); + // Needs to be BEFORE the actual event, so that for instance the + // sequence writeStartElem, writeAttr, writeStartElem, writeEndElem,writeEndElem + // is correctly handled + if ("writeStartElement".equals(m)) { + // update state of parent node + if (depth > 0) { + hasChildElement.put(depth - 1, true); + } + // reset state of current node + hasChildElement.put(depth, false); + // indent for current depth + if (!firstLine) { + writeLineWrap(); + } + depth++; + } else if ("writeEndElement".equals(m)) { + depth--; + if (hasChildElement.get(depth) == true) { + writeLineWrap(); + } + } else if ("writeEmptyElement".equals(m)) { + // update state of parent node + if (depth > 0) { + hasChildElement.put(depth - 1, true); + } + writeLineWrap(); + } else if ("writeComment".equals(m)) { + writeLineWrap(); + } else if ("writeEndDocument".equals(m)) { + target.writeCharacters(StringUtils.LF); + } + method.invoke(target, args); + firstLine = false; + return null; + } + + private void writeLineWrap() throws XMLStreamException { + target.writeCharacters(StringUtils.LF); + // indent for current depth + target.writeCharacters(StringUtils.repeat(StringUtils.SPACE, depth)); + } + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCConfig.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCConfig.java new file mode 100644 index 0000000..2bb371e --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCConfig.java @@ -0,0 +1,87 @@ +/* + * 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.List; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +import org.apache.commons.lang3.Validate; + +/** + * The unit config. + * + * @author Willem Cazander + * @version 1.0 Oct 17, 2015 + */ +@XmlRootElement(name=ModelJAXBInfoSet.Element.UNIT_CONFIG) +public class UnitXCConfig { + + private UnitXCRounding rounding; + private final List unitGroups; + + public UnitXCConfig() { + unitGroups = new ArrayList<>(); + } + + /** + * @return the rounding + */ + @XmlAttribute(name=ModelJAXBInfoSet.Attribute.ROUNDING) + public UnitXCRounding getRounding() { + return rounding; + } + + /** + * @param rounding the rounding to set + */ + public void setRounding(UnitXCRounding rounding) { + this.rounding = rounding; + } + + /** + * @return the unit groups. + */ + @XmlElement(name=ModelJAXBInfoSet.Element.UNIT_GROUP) + public List getUnitGroups() { + return unitGroups; + } + + /** + * @param unitGroups the groups to set. + */ + public void setUnitGroups(List groups) { + this.unitGroups.clear(); + groups.forEach(group -> addUnitGroup(group)); + } + + public void addUnitGroup(UnitXCGroup group) { + Validate.notNull(group); + group.validate(); + unitGroups.add(group); + } +} 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 a31372f..1740221 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 @@ -28,8 +28,12 @@ import java.util.List; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlTransient; import org.apache.commons.lang3.Validate; +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; /** * The unit group. @@ -37,17 +41,24 @@ import org.apache.commons.lang3.Validate; * @author Willem Cazander * @version 1.0 Oct 10, 2015 */ +@XmlRootElement(name=ModelJAXBInfoSet.Element.UNIT_GROUP) public class UnitXCGroup { private String id; private String name; private String description; private String baseTypeId; + private long totalUnitTypes = 0L; private UnitXCGroupLevel groupLevel; + private UnitXCTypeGenerator typeGenerator; private final List derivedFrom; + private final List unitTypes; + private final List groupJumps; public UnitXCGroup() { derivedFrom = new ArrayList<>(); + unitTypes = new ArrayList<>(); + groupJumps = new ArrayList<>(); } public UnitXCGroup(String id,String name,String description,String baseTypeId) { @@ -70,7 +81,7 @@ public class UnitXCGroup { /** * @return the id */ - @XmlAttribute(name=ModelXMLInfoSet.Attribute.ID) + @XmlAttribute(name=ModelJAXBInfoSet.Attribute.ID) public String getId() { return id; } @@ -84,7 +95,7 @@ public class UnitXCGroup { /** * @return the name */ - @XmlAttribute(name=ModelXMLInfoSet.Attribute.NAME) + @XmlAttribute(name=ModelJAXBInfoSet.Attribute.NAME) public String getName() { return name; } @@ -99,7 +110,7 @@ public class UnitXCGroup { /** * @return the description */ - @XmlElement(name=ModelXMLInfoSet.Element.DESCRIPTION) + @XmlElement(name=ModelJAXBInfoSet.Element.DESCRIPTION) public String getDescription() { return description; } @@ -114,7 +125,7 @@ public class UnitXCGroup { /** * @return the baseTypeId */ - @XmlAttribute(name=ModelXMLInfoSet.Attribute.BASE_TYPE_ID) + @XmlAttribute(name=ModelJAXBInfoSet.Attribute.BASE_TYPE_ID) public String getBaseTypeId() { return baseTypeId; } @@ -129,7 +140,7 @@ public class UnitXCGroup { /** * @return the groupLevel */ - @XmlAttribute(name=ModelXMLInfoSet.Attribute.GROUP_LEVEL) + @XmlAttribute(name=ModelJAXBInfoSet.Attribute.GROUP_LEVEL) public UnitXCGroupLevel getGroupLevel() { return groupLevel; } @@ -144,7 +155,8 @@ public class UnitXCGroup { /** * @return the derivedFrom */ - @XmlElement(name=ModelXMLInfoSet.Element.DERIVED_FROM) + @XmlElement(name=ModelJAXBInfoSet.Element.DERIVED_FROM) + @XmlElementWrapper public List getDerivedFrom() { return derivedFrom; } @@ -156,4 +168,79 @@ public class UnitXCGroup { this.derivedFrom.clear(); this.derivedFrom.addAll(derivedFrom); } + + /** + * @return the unit types. + */ + @XmlElement(name=ModelJAXBInfoSet.Element.UNIT_TYPE) + @XmlElementWrapper + public List getUnitTypes() { + return unitTypes; + } + + /** + * @param unitTypes the types to set. + */ + public void setUnitTypes(List types) { + this.unitTypes.clear(); + types.forEach(type -> addUnitType(type)); + } + + public void addUnitType(UnitXCType type) { + Validate.notNull(type); + type.setUnitGroup(this); + type.validate(); + unitTypes.add(type); + } + + /** + * @return the groupJumps + */ + @XmlElement(name=ModelJAXBInfoSet.Element.GROUP_JUMP) + @XmlElementWrapper + public List getGroupJumps() { + return groupJumps; + } + + /** + * @param groupJumps the groupJumps to set + */ + public void setGroupJumps(List groupJumps) { + this.groupJumps.clear(); + this.groupJumps.addAll(groupJumps); + } + + /** + * @return the typeGenerator + */ + @XmlTransient + public UnitXCTypeGenerator getTypeGenerator() { + return typeGenerator; + } + + /** + * @param typeGenerator the typeGenerator to set + */ + public void setTypeGenerator(UnitXCTypeGenerator typeGenerator) { + this.typeGenerator = typeGenerator; + } + + /** + * @return the totalUnitTypes + */ + public long getTotalUnitTypes() { + return totalUnitTypes; + } + + /** + * @param totalUnitTypes the totalUnitTypes to set + */ + public void setTotalUnitTypes(long totalUnitTypes) { + this.totalUnitTypes = totalUnitTypes; + } + + @Override + public String toString() { + return ReflectionToStringBuilder.toString(this); + } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroupJump.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroupJump.java new file mode 100644 index 0000000..f5ba59b --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroupJump.java @@ -0,0 +1,128 @@ +/* + * 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.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 org.apache.commons.lang3.Validate; + +import net.forwardfire.unitxc.model.step.UnitXConverterStep; + +/** + * The unit group jump. + * + * @author Willem Cazander + * @version 1.0 Apr 1, 2016 + */ +public class UnitXCGroupJump { + + private String id; + private String unitGroupId; + private final List toGroupConverterSteps; + private final List jumpParameters; + + public UnitXCGroupJump() { + toGroupConverterSteps = new ArrayList<>(); + jumpParameters = new ArrayList<>(); + } + + public UnitXCGroupJump validate() { + Validate.notBlank(id,"The id is blank"); + Validate.notBlank(unitGroupId,"The unitGroupId is blank of: "+id); + return this; + } + + /** + * @return the id + */ + @XmlAttribute(name=ModelJAXBInfoSet.Attribute.ID) + public String getId() { + return id; + } + /** + * @param id the id to set + */ + public void setId(String id) { + this.id = id; + } + + /** + * @return the toGroupId + */ + @XmlAttribute(name=ModelJAXBInfoSet.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 toGroupConverterSteps + */ + @XmlElementWrapper + @XmlAnyElement + public List getToGroupConverterSteps() { + return toGroupConverterSteps; + } + + /** + * @param toGroupConverterSteps the toGroupConverterSteps to set + */ + public void setToBaseConverterSteps(List toGroupConverterSteps) { + this.toGroupConverterSteps.clear(); + this.toGroupConverterSteps.addAll(toGroupConverterSteps); + } + + /** + * @return the jumpParameters + */ + @XmlElementWrapper + @XmlElement(name=ModelJAXBInfoSet.Element.GROUP_JUMP_PARAMETER) + public List getJumpParameters() { + return jumpParameters; + } + + /** + * @param jumpParameters the jumpParameters to set + */ + public void setJumpParameters(List jumpParameters) { + this.jumpParameters.clear(); + this.jumpParameters.addAll(jumpParameters); + } + + public void addJumpParameter(UnitXCGroupJumpParameter jumpParameter) { + this.jumpParameters.add(Validate.notNull(jumpParameter)); + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroupJumpParameter.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroupJumpParameter.java new file mode 100644 index 0000000..26a1c66 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCGroupJumpParameter.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2013-2015, Willem Cazander + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package net.forwardfire.unitxc.model; + +import javax.xml.bind.annotation.XmlAttribute; + +import org.apache.commons.lang3.Validate; + + +/** + * The unit group jump. + * + * @author Willem Cazander + * @version 1.0 Apr 1, 2016 + */ +public class UnitXCGroupJumpParameter { + + private String id; + private String unitGroupId; + + public UnitXCGroupJumpParameter() { + } + + public UnitXCGroupJumpParameter(String id,String unitGroupId) { + setId(id); + setUnitGroupId(unitGroupId); + } + + public UnitXCGroupJumpParameter validate() { + Validate.notBlank(id,"The id is blank"); + Validate.notBlank(unitGroupId,"The unitGroup is blank of: "+id); + return this; + } + + /** + * @return the id + */ + @XmlAttribute(name=ModelJAXBInfoSet.Attribute.ID) + public String getId() { + return id; + } + /** + * @param id the id to set + */ + public void setId(String id) { + this.id = id; + } + + /** + * @return the unitGroupId + */ + @XmlAttribute(name=ModelJAXBInfoSet.Attribute.UNIT_GROUP_ID) + public String getUnitGroupId() { + return unitGroupId; + } + + /** + * @param unitGroupId the unitGroupId to set + */ + public void setUnitGroupId(String unitGroupId) { + this.unitGroupId = unitGroupId; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCRounding.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCRounding.java new file mode 100644 index 0000000..50e1400 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCRounding.java @@ -0,0 +1,14 @@ +package net.forwardfire.unitxc.model; + +/** + * + * + * @author Willem Cazander + * @version 1.0 Mar 23, 2016 + */ +public enum UnitXCRounding { + + OFF, + PRE_STEP; + /* PER_CONVERT */ +} 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 7e0319f..06a61be 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 @@ -33,8 +33,9 @@ import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlTransient; import org.apache.commons.lang3.Validate; +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; -import net.forwardfire.unitxc.converter.step.UnitXConverterStep; +import net.forwardfire.unitxc.model.step.UnitXConverterStep; /** * The unit type. @@ -48,7 +49,7 @@ public class UnitXCType { private String name; private String namePlural; private String aliasOfType; - private String unitGroupId; + private UnitXCGroup unitGroup; private final List toBaseConverterSteps; private final List fromBaseConverterSteps; private final List typeFlags; @@ -65,14 +66,14 @@ public class UnitXCType { 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); + Validate.notNull(unitGroup,"The unitGroup is null of: "+id); return this; } /** * @return the id */ - @XmlAttribute(name=ModelXMLInfoSet.Attribute.ID) + @XmlAttribute(name=ModelJAXBInfoSet.Attribute.ID) public String getId() { return id; } @@ -86,7 +87,7 @@ public class UnitXCType { /** * @return the name */ - @XmlAttribute(name=ModelXMLInfoSet.Attribute.NAME) + @XmlAttribute(name=ModelJAXBInfoSet.Attribute.NAME) public String getName() { return name; } @@ -101,7 +102,7 @@ public class UnitXCType { /** * @return the namePlural */ - @XmlAttribute(name=ModelXMLInfoSet.Attribute.NAME_PLURAL) + @XmlAttribute(name=ModelJAXBInfoSet.Attribute.NAME_PLURAL) public String getNamePlural() { return namePlural; } @@ -116,7 +117,7 @@ public class UnitXCType { /** * @return the aliasOfType */ - @XmlAttribute(name=ModelXMLInfoSet.Attribute.ALIAS_OF_TYPE) + @XmlAttribute(name=ModelJAXBInfoSet.Attribute.ALIAS_OF_TYPE) public String getAliasOfType() { return aliasOfType; } @@ -129,18 +130,18 @@ public class UnitXCType { } /** - * @return the unitGroupId + * @return the unitGroup */ - @XmlAttribute(name=ModelXMLInfoSet.Attribute.UNIT_GROUP_ID) - public String getUnitGroupId() { - return unitGroupId; + @XmlTransient + public UnitXCGroup getUnitGroup() { + return unitGroup; } /** - * @param unitGroupId the unitGroupId to set + * @param unitGroup the unitGroup to set */ - public void setUnitGroupId(String unitGroupId) { - this.unitGroupId = unitGroupId; + public void setUnitGroup(UnitXCGroup unitGroup) { + this.unitGroup = unitGroup; } /** @@ -180,8 +181,8 @@ public class UnitXCType { /** * @return the typeFlags */ - @XmlElement(name=ModelXMLInfoSet.Element.TYPE_FLAG) - @XmlElementWrapper(name=ModelXMLInfoSet.Element.TYPE_FLAGS) + @XmlElement(name=ModelJAXBInfoSet.Element.TYPE_FLAG) + @XmlElementWrapper(name=ModelJAXBInfoSet.Element.TYPE_FLAGS) public List getTypeFlags() { return typeFlags; } @@ -197,8 +198,8 @@ public class UnitXCType { /** * @return the websiteLinks */ - @XmlElement(name=ModelXMLInfoSet.Element.WEBSITE_LINK) - @XmlElementWrapper(name=ModelXMLInfoSet.Element.WEBSITE_LINKS) + @XmlElement(name=ModelJAXBInfoSet.Element.WEBSITE_LINK) + @XmlElementWrapper(name=ModelJAXBInfoSet.Element.WEBSITE_LINKS) public List getWebsiteLinks() { return websiteLinks; } @@ -210,4 +211,9 @@ public class UnitXCType { this.websiteLinks.clear(); this.websiteLinks.addAll(websiteLinks); } + + @Override + public String toString() { + return ReflectionToStringBuilder.toString(this); + } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCTypeGenerator.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCTypeGenerator.java new file mode 100644 index 0000000..2c18697 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCTypeGenerator.java @@ -0,0 +1,12 @@ +package net.forwardfire.unitxc.model; + +import java.util.Iterator; + +public interface UnitXCTypeGenerator { + + long size(); + + Iterator getUnitTypes(); + + UnitXCType getUnitType(String id); +} 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 37b662a..df0b224 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 @@ -35,20 +35,20 @@ import javax.xml.bind.annotation.XmlRootElement; * @author Willem Cazander * @version 1.0 Oct 17, 2015 */ -@XmlRootElement(name=ModelXMLInfoSet.Element.CONVERTER_RESULT) +@XmlRootElement(name=ModelJAXBInfoSet.Element.CONVERTER_RESULT) public class UnitXConverterResult { private Double startValue; private List resultSteps; private Double resultValue; private Long convertTime; - private UnitXCType startValueType; - private UnitXCType resultValueType; + private String startTypeId; + private String resultTypeId; /** * @return the startValue */ - @XmlAttribute(name=ModelXMLInfoSet.Attribute.START_VALUE) + @XmlAttribute(name=ModelJAXBInfoSet.Attribute.START_VALUE) public Double getStartValue() { return startValue; } @@ -63,7 +63,7 @@ public class UnitXConverterResult { /** * @return the resultSteps */ - @XmlElement(name=ModelXMLInfoSet.Element.CONVERTER_RESULT_STEP) + @XmlElement(name=ModelJAXBInfoSet.Element.CONVERTER_RESULT_STEP) public List getResultSteps() { return resultSteps; } @@ -78,7 +78,7 @@ public class UnitXConverterResult { /** * @return the resultValue */ - @XmlAttribute(name=ModelXMLInfoSet.Attribute.RESULT_VALUE) + @XmlAttribute(name=ModelJAXBInfoSet.Attribute.RESULT_VALUE) public Double getResultValue() { return resultValue; } @@ -93,7 +93,7 @@ public class UnitXConverterResult { /** * @return the convertTime */ - @XmlAttribute(name=ModelXMLInfoSet.Attribute.CONVERT_TIME) + @XmlAttribute(name=ModelJAXBInfoSet.Attribute.CONVERT_TIME) public Long getConvertTime() { return convertTime; } @@ -106,32 +106,32 @@ public class UnitXConverterResult { } /** - * @return the startValueType + * @return the startTypeId */ - @XmlElement(name=ModelXMLInfoSet.Element.START_VALUE_TYPE) - public UnitXCType getStartValueType() { - return startValueType; + @XmlAttribute(name=ModelJAXBInfoSet.Attribute.START_TYPE_ID) + public String getStartTypeId() { + return startTypeId; } /** - * @param startValueType the startValueType to set + * @param startTypeId the startTypeId to set */ - public void setStartValueType(UnitXCType startValueType) { - this.startValueType = startValueType; + public void setStartTypeId(String startTypeId) { + this.startTypeId = startTypeId; } /** - * @return the resultValueType + * @return the resultTypeId */ - @XmlElement(name=ModelXMLInfoSet.Element.RESULT_VALUE_TYPE) - public UnitXCType getResultValueType() { - return resultValueType; + @XmlAttribute(name=ModelJAXBInfoSet.Attribute.RESULT_TYPE_ID) + public String getResultTypeId() { + return resultTypeId; } /** - * @param resultValueType the resultValueType to set + * @param resultTypeId the resultTypeId to set */ - public void setResultValueType(UnitXCType resultValueType) { - this.resultValueType = resultValueType; + public void setResultTypeId(String resultTypeId) { + this.resultTypeId = resultTypeId; } } 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 23dda36..35e3c70 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,8 +23,11 @@ package net.forwardfire.unitxc.model; +import javax.xml.bind.annotation.XmlAnyElement; import javax.xml.bind.annotation.XmlAttribute; +import net.forwardfire.unitxc.model.step.UnitXConverterStep; + /** * * @@ -33,30 +36,15 @@ import javax.xml.bind.annotation.XmlAttribute; */ public class UnitXConverterResultStep { - private String name; private Double startValue; private Double resultValue; private Long convertTime; - - /** - * @return the name - */ - @XmlAttribute(name=ModelXMLInfoSet.Attribute.NAME) - public String getName() { - return name; - } - - /** - * @param name the name to set - */ - public void setName(String name) { - this.name = name; - } + private UnitXConverterStep convertStep; /** * @return the startValue */ - @XmlAttribute(name=ModelXMLInfoSet.Attribute.START_VALUE) + @XmlAttribute(name=ModelJAXBInfoSet.Attribute.START_VALUE) public Double getStartValue() { return startValue; } @@ -71,7 +59,7 @@ public class UnitXConverterResultStep { /** * @return the resultValue */ - @XmlAttribute(name=ModelXMLInfoSet.Attribute.RESULT_VALUE) + @XmlAttribute(name=ModelJAXBInfoSet.Attribute.RESULT_VALUE) public Double getResultValue() { return resultValue; } @@ -86,7 +74,7 @@ public class UnitXConverterResultStep { /** * @return the convertTime */ - @XmlAttribute(name=ModelXMLInfoSet.Attribute.CONVERT_TIME) + @XmlAttribute(name=ModelJAXBInfoSet.Attribute.CONVERT_TIME) public Long getConvertTime() { return convertTime; } @@ -97,4 +85,19 @@ public class UnitXConverterResultStep { public void setConvertTime(Long convertTime) { this.convertTime = convertTime; } + + /** + * @return the convertStep + */ + @XmlAnyElement + public UnitXConverterStep getConvertStep() { + return convertStep; + } + + /** + * @param convertStep the convertStep to set + */ + public void setConvertStep(UnitXConverterStep convertStep) { + this.convertStep = convertStep; + } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/AbstractUnitXConverterStep.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/AbstractUnitXConverterStep.java index cb70033..7182824 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/AbstractUnitXConverterStep.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/AbstractUnitXConverterStep.java @@ -21,18 +21,17 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package net.forwardfire.unitxc.converter.step; +package net.forwardfire.unitxc.model.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; +import net.forwardfire.unitxc.model.ModelJAXBInfoSet; /** * @@ -42,12 +41,10 @@ import net.forwardfire.unitxc.model.ModelXMLInfoSet; */ public abstract class AbstractUnitXConverterStep implements UnitXConverterStep { - private String name; private final List stepReasons; - protected AbstractUnitXConverterStep(String name) { + protected AbstractUnitXConverterStep() { this.stepReasons = new ArrayList<>(); - this.setName(name); } public AbstractUnitXConverterStep buildReason(String reason) { @@ -55,19 +52,8 @@ public abstract class AbstractUnitXConverterStep implements UnitXConverterStep { return this; } - @Override - @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) + @XmlElement(name=ModelJAXBInfoSet.Element.STEP_REASON) + @XmlElementWrapper(name=ModelJAXBInfoSet.Element.STEP_REASONS) public final List getStepReasons() { return stepReasons; } @@ -77,28 +63,7 @@ public abstract class AbstractUnitXConverterStep implements UnitXConverterStep { 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/model/step/UnitXCConverterStepAutoRounding.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXCConverterStepAutoRounding.java new file mode 100644 index 0000000..6aac9f9 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXCConverterStepAutoRounding.java @@ -0,0 +1,91 @@ +package net.forwardfire.unitxc.model.step; + +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + +import net.forwardfire.unitxc.model.ModelJAXBInfoSet; +import net.forwardfire.unitxc.model.ModelJAXBObjectWrapAdapter; +import net.forwardfire.unitxc.model.step.value.UnitXConverterStepValueNamedVariable; +import net.forwardfire.unitxc.model.step.value.UnitXConverterStepValueRead; +import net.forwardfire.unitxc.model.step.value.UnitXConverterStepValueReadWrite; + +@XmlRootElement(name=ModelJAXBInfoSet.Element.ROUNDING) +public class UnitXCConverterStepAutoRounding extends AbstractUnitXConverterStep { + + private UnitXConverterStepValueRead operand; + private UnitXConverterStepValueReadWrite result; + + public UnitXCConverterStepAutoRounding() { + } + + public UnitXCConverterStepAutoRounding(UnitXConverterStepValueRead operand,UnitXConverterStepValueReadWrite result) { + setOperand(operand); + setResult(result); + } + + private double round(double value,double prevValue) { + String valueStr = Double.toString(value); + String valueOrgStr = Double.toString(prevValue); + int valueDotIndex = valueStr.indexOf("."); + int valueOrgDotIndex = valueOrgStr.indexOf("."); + int valueDotSize = valueStr.length() - valueDotIndex; + int valueOrgDotSize = valueOrgStr.length() - valueOrgDotIndex; + + if (valueDotSize<10) { + return value; + } + if (valueDotSize<=valueOrgDotSize) { + return value; + } + int valueEIndex = valueStr.indexOf("E"); + String valueShortStr = null; + if (valueEIndex>0) { + int sub = valueOrgStr.length(); + if (sub > valueEIndex) { + sub = valueEIndex; + } + valueShortStr = valueStr.substring(0, sub)+ valueStr.substring(valueEIndex); + } else { + valueShortStr = valueStr.substring(0, valueOrgStr.length()); + } + Double result = new Double(valueShortStr); + return result.doubleValue(); + } + + @Override + public void runStep(UnitXConverterStepContext ctx) { + // FIXME: rm obj and casting + UnitXConverterStepValueReadWrite startValue = new UnitXConverterStepValueNamedVariable(Double.class,UnitXConverterStepContext.VALUE_START); + getResult().valueWrite(ctx, round((Double)getOperand().valueRead(ctx),(Double)startValue.valueRead(ctx))); + } + + /** + * @return the operand + */ + @XmlJavaTypeAdapter(value=ModelJAXBObjectWrapAdapter.class) + public UnitXConverterStepValueRead getOperand() { + return operand; + } + + /** + * @param operand the operand to set + */ + public void setOperand(UnitXConverterStepValueRead operand) { + this.operand = operand; + } + + /** + * @return the result + */ + @XmlJavaTypeAdapter(value=ModelJAXBObjectWrapAdapter.class) + public UnitXConverterStepValueReadWrite getResult() { + return result; + } + + /** + * @param result the result to set + */ + public void setResult(UnitXConverterStepValueReadWrite result) { + this.result = result; + } +} \ No newline at end of file diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXCConverterStepCondition.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXCConverterStepCondition.java new file mode 100644 index 0000000..4e57aa9 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXCConverterStepCondition.java @@ -0,0 +1,125 @@ +/* + * 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.step; + +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.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + +import net.forwardfire.unitxc.model.ModelJAXBInfoSet; +import net.forwardfire.unitxc.model.ModelJAXBObjectWrapAdapter; +import net.forwardfire.unitxc.model.step.value.UnitXConverterStepValueRead; + +/** + * + * + * @author Willem Cazander + * @version 1.0 Oct 17, 2015 + */ +@XmlRootElement(name=ModelJAXBInfoSet.Element.CONDITION) +public class UnitXCConverterStepCondition extends AbstractUnitXConverterStep { + + private UnitXCConverterStepConditionEquality equality; + private UnitXConverterStepValueRead operandA; + private UnitXConverterStepValueRead operandB; + private final List conditionalSteps; + + public UnitXCConverterStepCondition() { + this.conditionalSteps = new ArrayList<>(); + } + + @Override + public void runStep(UnitXConverterStepContext ctx) { + if (equality.execute(operandA.valueRead(ctx),operandB.valueRead(ctx))) { + ctx.runSteps(conditionalSteps); + } + } + + /** + * @return the equality + */ + @XmlAttribute + public UnitXCConverterStepConditionEquality getEquality() { + return equality; + } + + /** + * @param equality the equality to set + */ + public void setEquality(UnitXCConverterStepConditionEquality equality) { + this.equality = equality; + } + + /** + * @return the operandA + */ + @XmlJavaTypeAdapter(value=ModelJAXBObjectWrapAdapter.class) + public UnitXConverterStepValueRead getOperandA() { + return operandA; + } + + /** + * @param operandA the operandA to set + */ + public void setOperandA(UnitXConverterStepValueRead operandA) { + this.operandA = operandA; + } + + /** + * @return the operandB + */ + @XmlJavaTypeAdapter(value=ModelJAXBObjectWrapAdapter.class) + public UnitXConverterStepValueRead getOperandB() { + return operandB; + } + + /** + * @param operandB the operandB to set + */ + public void setOperandB(UnitXConverterStepValueRead operandB) { + this.operandB = operandB; + } + + /** + * @return the conditionalSteps + */ + @XmlElementWrapper + @XmlAnyElement + public List getConditionalSteps() { + return conditionalSteps; + } + + /** + * @param conditionalSteps the conditionalSteps to set + */ + public void setConditionalSteps(List conditionalSteps) { + this.conditionalSteps.clear(); + this.conditionalSteps.addAll(conditionalSteps); + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXCConverterStepConditionEquality.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXCConverterStepConditionEquality.java new file mode 100644 index 0000000..c861516 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXCConverterStepConditionEquality.java @@ -0,0 +1,37 @@ +package net.forwardfire.unitxc.model.step; + +import java.util.function.BiFunction; + +public enum UnitXCConverterStepConditionEquality { + + EQUALS ((l,r) -> l.compareTo(r) == 0, (l,r) -> l.compareTo(r) == 0, (l,r) -> l.compareTo(r) == 0), + NOT_EQUALS ((l,r) -> l.compareTo(r) != 0, (l,r) -> l.compareTo(r) != 0, (l,r) -> l.compareTo(r) != 0), + GREATER_THEN ((l,r) -> l.compareTo(r) > 0, (l,r) -> l.compareTo(r) > 0, (l,r) -> l.compareTo(r) > 0), + LESSER_THEN ((l,r) -> l.compareTo(r) < 0, (l,r) -> l.compareTo(r) < 0, (l,r) -> l.compareTo(r) < 0), + GREATER_OR_EQUALS ((l,r) -> l.compareTo(r) >= 0, (l,r) -> l.compareTo(r) >= 0, (l,r) -> l.compareTo(r) >= 0), + LESSER_OR_EQUALS ((l,r) -> l.compareTo(r) <= 0, (l,r) -> l.compareTo(r) <= 0, (l,r) -> l.compareTo(r) <= 0), + ; + + private final BiFunction opertionDouble; + private final BiFunction opertionInteger; + private final BiFunction opertionString; + + private UnitXCConverterStepConditionEquality(BiFunction opertionDouble,BiFunction opertionInteger,BiFunction opertionString) { + this.opertionDouble = opertionDouble; + this.opertionInteger = opertionInteger; + this.opertionString = opertionString; + } + + public boolean execute(Object left, Object right) { + if (Double.class.equals(left.getClass())) { + return opertionDouble.apply(Double.class.cast(left), Double.class.cast(right)); + } + if (Integer.class.equals(left.getClass())) { + return opertionInteger.apply(Integer.class.cast(left), Integer.class.cast(right)); + } + if (String.class.equals(left.getClass())) { + return opertionString.apply(String.class.cast(left), String.class.cast(right)); + } + throw new IllegalArgumentException("Unknown typeClass: "+left.getClass()); + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXCConverterStepOperation.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXCConverterStepOperation.java index dce438b..0271bf3 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXCConverterStepOperation.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXCConverterStepOperation.java @@ -21,9 +21,16 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package net.forwardfire.unitxc.converter.step; +package net.forwardfire.unitxc.model.step; +import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + +import net.forwardfire.unitxc.model.ModelJAXBInfoSet; +import net.forwardfire.unitxc.model.ModelJAXBObjectWrapAdapter; +import net.forwardfire.unitxc.model.step.value.UnitXConverterStepValueRead; +import net.forwardfire.unitxc.model.step.value.UnitXConverterStepValueReadWrite; /** * @@ -31,47 +38,86 @@ import javax.xml.bind.annotation.XmlRootElement; * @author Willem Cazander * @version 1.0 Oct 17, 2015 */ -@XmlRootElement -public class UnitXCTypeMultiplyConverterStep extends AbstractUnitXConverterStep { +@XmlRootElement(name=ModelJAXBInfoSet.Element.OPERATION) +public class UnitXCConverterStepOperation extends AbstractUnitXConverterStep { - private final static String STEP_NAME = "Multiply"; - private double factor; + private UnitXCConverterStepOperationOperator operator; + private UnitXConverterStepValueRead operandA; + private UnitXConverterStepValueRead operandB; + private UnitXConverterStepValueReadWrite result; - public UnitXCTypeMultiplyConverterStep() { - super(STEP_NAME); + public UnitXCConverterStepOperation() { } - public UnitXCTypeMultiplyConverterStep(double factor) { - this(); - setFactor(factor); + public UnitXCConverterStepOperation(UnitXCConverterStepOperationOperator operator,UnitXConverterStepValueRead operandA,UnitXConverterStepValueRead operandB,UnitXConverterStepValueReadWrite result) { + setOperator(operator); + setOperandA(operandA); + setOperandB(operandB); + setResult(result); } @Override - protected AbstractUnitXConverterStep createClone() { - return new UnitXCTypeMultiplyConverterStep(getFactor()); - } - - @Override - public double convert(double value) { - return value*factor; - } - - @Override - protected String toMath() { - return "*"+factor; + public void runStep(UnitXConverterStepContext ctx) { + result.valueWrite(ctx, operator.execute(operandA.valueRead(ctx),operandB.valueRead(ctx))); } /** - * @return the factor + * @return the operator */ - public double getFactor() { - return factor; + @XmlAttribute + public UnitXCConverterStepOperationOperator getOperator() { + return operator; } /** - * @param factor the factor to set + * @param operator the operator to set */ - public void setFactor(double factor) { - this.factor = factor; + public void setOperator(UnitXCConverterStepOperationOperator operator) { + this.operator = operator; + } + + /** + * @return the operandA + */ + @XmlJavaTypeAdapter(value=ModelJAXBObjectWrapAdapter.class) + public UnitXConverterStepValueRead getOperandA() { + return operandA; + } + + /** + * @param operandA the operandA to set + */ + public void setOperandA(UnitXConverterStepValueRead operandA) { + this.operandA = operandA; + } + + /** + * @return the operandB + */ + @XmlJavaTypeAdapter(value=ModelJAXBObjectWrapAdapter.class) + public UnitXConverterStepValueRead getOperandB() { + return operandB; + } + + /** + * @param operandB the operandB to set + */ + public void setOperandB(UnitXConverterStepValueRead operandB) { + this.operandB = operandB; + } + + /** + * @return the result + */ + @XmlJavaTypeAdapter(value=ModelJAXBObjectWrapAdapter.class) + public UnitXConverterStepValueReadWrite getResult() { + return result; + } + + /** + * @param result the result to set + */ + public void setResult(UnitXConverterStepValueReadWrite result) { + this.result = result; } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXCConverterStepOperationOperator.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXCConverterStepOperationOperator.java new file mode 100644 index 0000000..b3845de --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXCConverterStepOperationOperator.java @@ -0,0 +1,57 @@ +package net.forwardfire.unitxc.model.step; + +import java.math.BigDecimal; +import java.math.MathContext; +import java.util.function.BinaryOperator; + +import org.apache.commons.lang3.StringUtils; + +public enum UnitXCConverterStepOperationOperator { + + ADD ((l,r) -> l+r, (l,r) -> l+r, (l,r) -> l+r), + SUBTRACT ((l,r) -> l-r, (l,r) -> l-r, (l,r) -> l.replaceAll(l, StringUtils.EMPTY)), + MULTIPLY ((l,r) -> l*r, (l,r) -> l*r, null), + DIVIDE ((l,r) -> l/r, (l,r) -> l/r, null), + POWER ((l,r) -> p(l,r), (l,r) -> p(l,r), null), + AND (null, (l,r) -> l & r, null), + OR (null, (l,r) -> l | r, null), + ; + + private final BinaryOperator opertionDouble; + private final BinaryOperator opertionInteger; + private final BinaryOperator opertionString; + + private UnitXCConverterStepOperationOperator(BinaryOperator opertionDouble,BinaryOperator opertionInteger,BinaryOperator opertionString) { + this.opertionDouble = opertionDouble; + this.opertionInteger = opertionInteger; + this.opertionString = opertionString; + } + + private static BigDecimal p(BigDecimal l,BigDecimal r) { + return l.pow(r.intValueExact(), MathContext.UNLIMITED); + } + + private static double p(double l,double r) { + return Math.pow(l, r); + } + + private static int p(int l,int r) { + for (int i=0;i steps = Boolean.TRUE.equals(toBaseSteps)?type.getToBaseConverterSteps():type.getFromBaseConverterSteps(); + ctx.runSteps(steps); + } + + /** + * @return the unitTypeId + */ + @XmlAttribute + public String getUnitTypeId() { + return unitTypeId; + } + + /** + * @param unitTypeId the unitTypeId to set + */ + public void setUnitTypeId(String unitTypeId) { + this.unitTypeId = unitTypeId; + } + + /** + * @return the toBaseSteps + */ + @XmlAttribute + public Boolean getToBaseSteps() { + return toBaseSteps; + } + + /** + * @param toBaseSteps the toBaseSteps to set + */ + public void setToBaseSteps(Boolean toBaseSteps) { + this.toBaseSteps = toBaseSteps; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXConverterStep.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXConverterStep.java index d500a00..85d10a3 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXConverterStep.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXConverterStep.java @@ -21,7 +21,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package net.forwardfire.unitxc.converter.step; +package net.forwardfire.unitxc.model.step; import java.util.List; @@ -31,17 +31,11 @@ import java.util.List; * @author Willem Cazander * @version 1.0 Oct 10, 2015 */ -public interface UnitXConverterStep extends Cloneable { +public interface UnitXConverterStep { - String getName(); + void runStep(UnitXConverterStepContext ctx); List getStepReasons(); 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/step/UnitXConverterStepContext.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXConverterStepContext.java new file mode 100644 index 0000000..a11201d --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/UnitXConverterStepContext.java @@ -0,0 +1,60 @@ +/* + * 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.step; + +import java.math.MathContext; +import java.util.List; + +import net.forwardfire.unitxc.UnitXCManager; +import net.forwardfire.unitxc.model.step.value.UnitXConverterStepValueNamedVariable; +import net.forwardfire.unitxc.model.step.value.UnitXConverterStepValueRead; +import net.forwardfire.unitxc.model.step.value.UnitXConverterStepValueReadWrite; + +/** + * + * + * @author Willem Cazander + * @version 1.0 Oct 10, 2015 + */ +public interface UnitXConverterStepContext { + + static final String VALUE = "value"; + static final String VALUE_START = "valueStart"; + + MathContext getMathContext(); + + void setNamedVariable(String name,UnitXConverterStepValueReadWrite variable); + + UnitXConverterStepValueReadWrite getNamedVariable(String name); + + UnitXConverterStepValueRead getNamedParameter(String name); + + void runSteps(List steps); + + UnitXCManager getUnitManager(); + + public static UnitXConverterStepValueReadWrite createStepValue() { + return new UnitXConverterStepValueNamedVariable(Double.class,VALUE); + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/package-info.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/package-info.java new file mode 100644 index 0000000..2478659 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/package-info.java @@ -0,0 +1,7 @@ + + +/** + * @author willemc + * + */ +package net.forwardfire.unitxc.model.step; \ No newline at end of file diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueNamedParameter.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueNamedParameter.java new file mode 100644 index 0000000..423cf4a --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueNamedParameter.java @@ -0,0 +1,76 @@ +/* + * 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.step.value; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; + +import net.forwardfire.unitxc.model.ModelJAXBInfoSet; +import net.forwardfire.unitxc.model.step.UnitXConverterStepContext; + +/** + * + * + * @author Willem Cazander + * @version 1.0 Apr 1, 2016 + */ +@XmlRootElement(name=ModelJAXBInfoSet.Element.NAMED_PARAMETER) +public class UnitXConverterStepValueNamedParameter implements UnitXConverterStepValueRead { + + private String parameterName; + + public UnitXConverterStepValueNamedParameter() { + } + + public UnitXConverterStepValueNamedParameter(String parameterName) { + setParameterName(parameterName); + } + + @Override + public Object valueRead(UnitXConverterStepContext ctx) { + return ctx.getNamedParameter(parameterName).valueRead(ctx); + } + + @Override + public UnitXConverterStepValueRead clone() { + UnitXConverterStepValueNamedParameter clone = new UnitXConverterStepValueNamedParameter(); + clone.setParameterName(getParameterName()); + return clone; + } + + /** + * @return the parameterName + */ + @XmlAttribute + public String getParameterName() { + return parameterName; + } + + /** + * @param parameterName the parameterName to set + */ + public void setParameterName(String parameterName) { + this.parameterName = parameterName; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueNamedVariable.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueNamedVariable.java new file mode 100644 index 0000000..91e030f --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueNamedVariable.java @@ -0,0 +1,99 @@ +/* + * 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.step.value; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; + +import net.forwardfire.unitxc.model.ModelJAXBInfoSet; +import net.forwardfire.unitxc.model.step.UnitXConverterStepContext; + +/** + * + * + * @author Willem Cazander + * @version 1.0 Apr 1, 2016 + */ +@XmlRootElement(name=ModelJAXBInfoSet.Element.NAMED_VARIABLE) +public class UnitXConverterStepValueNamedVariable implements UnitXConverterStepValueReadWrite { + + private Class valueType; + private String variableName; + + public UnitXConverterStepValueNamedVariable() { + } + + public UnitXConverterStepValueNamedVariable(Class valueType,String variableName) { + setValueType(valueType); + setVariableName(variableName); + } + + @Override + public Object valueRead(UnitXConverterStepContext ctx) { + return ctx.getNamedVariable(variableName).valueRead(ctx); + } + + @Override + public void valueWrite(UnitXConverterStepContext ctx,Object value) { + ctx.getNamedVariable(variableName).valueWrite(ctx,value); + } + + @Override + public UnitXConverterStepValueReadWrite clone() { + UnitXConverterStepValueNamedVariable clone = new UnitXConverterStepValueNamedVariable(); + clone.setValueType(getValueType()); + clone.setVariableName(getVariableName()); + return clone; + } + + /** + * @return the variableName + */ + @XmlAttribute + public String getVariableName() { + return variableName; + } + + /** + * @param variableName the variableName to set + */ + public void setVariableName(String variableName) { + this.variableName = variableName; + } + + /** + * @return the valueType + */ + @XmlAttribute + public Class getValueType() { + return valueType; + } + + /** + * @param valueType the valueType to set + */ + public void setValueType(Class valueType) { + this.valueType = valueType; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueRead.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueRead.java new file mode 100644 index 0000000..9b7ceda --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueRead.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2013-2015, Willem Cazander + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package net.forwardfire.unitxc.model.step.value; + +import net.forwardfire.unitxc.model.step.UnitXConverterStepContext; + +/** + * + * + * @author Willem Cazander + * @version 1.0 Apr 1, 2016 + */ +public interface UnitXConverterStepValueRead extends Cloneable { + + Object valueRead(UnitXConverterStepContext ctx); + + UnitXConverterStepValueRead clone(); +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueReadWrite.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueReadWrite.java new file mode 100644 index 0000000..31270a6 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueReadWrite.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2013-2015, Willem Cazander + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package net.forwardfire.unitxc.model.step.value; + +import net.forwardfire.unitxc.model.step.UnitXConverterStepContext; + +/** + * + * + * @author Willem Cazander + * @version 1.0 Apr 1, 2016 + */ +public interface UnitXConverterStepValueReadWrite extends UnitXConverterStepValueRead { + + void valueWrite(UnitXConverterStepContext ctx,Object value); + + UnitXConverterStepValueReadWrite clone(); +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueReferenceDouble.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueReferenceDouble.java new file mode 100644 index 0000000..a529dfb --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueReferenceDouble.java @@ -0,0 +1,80 @@ +/* + * 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.step.value; + +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlValue; + +import net.forwardfire.unitxc.model.ModelJAXBInfoSet; +import net.forwardfire.unitxc.model.step.UnitXConverterStepContext; + +/** + * Holds an Double value. + * + * @author Willem Cazander + * @version 1.0 Apr 1, 2016 + */ +@XmlRootElement(name=ModelJAXBInfoSet.Element.VALUE_DOUBLE) +public class UnitXConverterStepValueReferenceDouble implements UnitXConverterStepValueReadWrite { + + private Double value; + + public UnitXConverterStepValueReferenceDouble() { + } + + public UnitXConverterStepValueReferenceDouble(Double value) { + setValue(value); + } + + @Override + public Object valueRead(UnitXConverterStepContext ctx) { + return value; + } + + @Override + public void valueWrite(UnitXConverterStepContext ctx,Object value) { + this.value = Double.class.cast(value); + } + + @Override + public UnitXConverterStepValueReadWrite clone() { + return new UnitXConverterStepValueReferenceDouble(getValue()); + } + + /** + * @return the value + */ + //@XmlAttribute + @XmlValue + public Double getValue() { + return value; + } + + /** + * @param value the value to set + */ + public void setValue(Double value) { + this.value = value; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueReferenceDoubleFraction.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueReferenceDoubleFraction.java new file mode 100644 index 0000000..29bd1d0 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueReferenceDoubleFraction.java @@ -0,0 +1,94 @@ +/* + * 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.step.value; + +import java.math.BigDecimal; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; + +import net.forwardfire.unitxc.model.ModelJAXBInfoSet; +import net.forwardfire.unitxc.model.step.UnitXConverterStepContext; + +/** + * Holds an Double value as a Fraction. + * + * @author Willem Cazander + * @version 1.0 Apr 1, 2016 + */ +@XmlRootElement(name=ModelJAXBInfoSet.Element.VALUE_DOUBLE_FRACTION) +public class UnitXConverterStepValueReferenceDoubleFraction implements UnitXConverterStepValueRead { + + private int numerator; + private int denominator; + + public UnitXConverterStepValueReferenceDoubleFraction() { + } + + public UnitXConverterStepValueReferenceDoubleFraction(int numerator,int denominator) { + setNumerator(numerator); + setDenominator(denominator); + } + + @Override + public Object valueRead(UnitXConverterStepContext ctx) { + //new BigDecimal(numerator).divide(new BigDecimal(denominator)); + return (Double)(((Integer)numerator).doubleValue()/((Integer)denominator).doubleValue()); // FIXME + } + + @Override + public UnitXConverterStepValueRead clone() { + return new UnitXConverterStepValueReferenceDoubleFraction(getNumerator(),getDenominator()); + } + + /** + * @return the numerator + */ + @XmlAttribute + public int getNumerator() { + return numerator; + } + + /** + * @param numerator the numerator to set + */ + public void setNumerator(int numerator) { + this.numerator = numerator; + } + + /** + * @return the denominator + */ + @XmlAttribute + 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/model/step/value/UnitXConverterStepValueReferenceInteger.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueReferenceInteger.java new file mode 100644 index 0000000..52f1c8c --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueReferenceInteger.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2013-2015, Willem Cazander + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package net.forwardfire.unitxc.model.step.value; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; + +import net.forwardfire.unitxc.model.ModelJAXBInfoSet; +import net.forwardfire.unitxc.model.step.UnitXConverterStepContext; + +/** + * Holds an Integer value. + * + * @author Willem Cazander + * @version 1.0 Apr 1, 2016 + */ +@XmlRootElement(name=ModelJAXBInfoSet.Element.VALUE_INTEGER) +public class UnitXConverterStepValueReferenceInteger implements UnitXConverterStepValueReadWrite { + + private Integer value; + + public UnitXConverterStepValueReferenceInteger() { + } + + public UnitXConverterStepValueReferenceInteger(Integer value) { + setValue(value); + } + + @Override + public Object valueRead(UnitXConverterStepContext ctx) { + return value; + } + + @Override + public void valueWrite(UnitXConverterStepContext ctx,Object value) { + this.value = Integer.class.cast(value); + } + + @Override + public UnitXConverterStepValueReadWrite clone() { + return new UnitXConverterStepValueReferenceInteger(getValue()); + } + + /** + * @return the value + */ + @XmlAttribute + public Integer getValue() { + return value; + } + + /** + * @param value the value to set + */ + public void setValue(Integer value) { + this.value = value; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueReferenceString.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueReferenceString.java new file mode 100644 index 0000000..d3115a7 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/UnitXConverterStepValueReferenceString.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2013-2015, Willem Cazander + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package net.forwardfire.unitxc.model.step.value; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; + +import net.forwardfire.unitxc.model.ModelJAXBInfoSet; +import net.forwardfire.unitxc.model.step.UnitXConverterStepContext; + +/** + * Holds an String value. + * + * @author Willem Cazander + * @version 1.0 Apr 1, 2016 + */ +@XmlRootElement(name=ModelJAXBInfoSet.Element.VALUE_STRING) +public class UnitXConverterStepValueReferenceString implements UnitXConverterStepValueReadWrite { + + private String value; + + public UnitXConverterStepValueReferenceString() { + } + + public UnitXConverterStepValueReferenceString(String value) { + setValue(value); + } + + @Override + public Object valueRead(UnitXConverterStepContext ctx) { + return value; + } + + @Override + public void valueWrite(UnitXConverterStepContext ctx,Object value) { + this.value = String.class.cast(value); + } + + @Override + public UnitXConverterStepValueReadWrite clone() { + return new UnitXConverterStepValueReferenceString(getValue()); + } + + /** + * @return the value + */ + @XmlAttribute + public String getValue() { + return value; + } + + /** + * @param value the value to set + */ + public void setValue(String value) { + this.value = value; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/package-info.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/package-info.java new file mode 100644 index 0000000..3f175a0 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/step/value/package-info.java @@ -0,0 +1,5 @@ +/** + * @author willemc + * + */ +package net.forwardfire.unitxc.model.step.value; \ No newline at end of file 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 b83c988..870ded8 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 @@ -38,10 +38,13 @@ import net.forwardfire.unitxc.config.builder.UnitXCTypeSIPrefix; */ public class UnitXCModuleElectricCurrent implements UnitXCConfigModule { - public static final String GROUP_ID = "current"; + public static final String GROUP_ID = "current"; + public static final String GROUP_NAME = "ampere"; public static final String GROUP_DESCRIPTION = "The ampere is a measure of the amount of electric charge passing a point in an electric circuit per unit time, with 6.241×1018 electrons (or one coulomb) per second constituting one ampere."; - public static final String TYPE_AMPERE_ID = "A"; - public static final String TYPE_AMPERE_NAME = "ampere"; + public static final String GROUP_BASE_TYPE_ID = "A"; + + public static final String TYPE_AMPERE_ID = GROUP_BASE_TYPE_ID; + public static final String TYPE_AMPERE_NAME = GROUP_NAME; public static final String TYPE_AMPERE_FLAG = buildFlag(GROUP_ID,TYPE_AMPERE_NAME); private static final String TYPE_AMPERE_WIKI = "Ampere"; 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 26b7121..7a78590 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 @@ -38,10 +38,13 @@ import net.forwardfire.unitxc.config.builder.UnitXCTypeSIPrefix; */ public class UnitXCModuleLength implements UnitXCConfigModule { - public static final String GROUP_ID = "length"; + public static final String GROUP_ID = "length"; + public static final String GROUP_NAME = "metre"; 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 GROUP_BASE_TYPE_ID = "m"; + + public static final String TYPE_METRE_ID = GROUP_BASE_TYPE_ID; + public static final String TYPE_METRE_NAME = GROUP_NAME; 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); 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 96c924c..09f94a1 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 @@ -37,11 +37,14 @@ import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; */ public class UnitXCModuleLuminousIntensity implements UnitXCConfigModule { - public static final String GROUP_ID = "light"; - public static final String GROUP_DESCRIPTION = "The candela is the luminous intensity, in a given direction, of a source that emits monochromatic radiation of frequency 540×1012 hertz and that has a radiant intensity in that direction of 1/683 watt per steradian."; - 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); + public static final String GROUP_ID = "light"; + public static final String GROUP_NAME = "candela"; + public static final String GROUP_DESCRIPTION = "The candela is the luminous intensity, in a given direction, of a source that emits monochromatic radiation of frequency 540×1012 hertz and that has a radiant intensity in that direction of 1/683 watt per steradian."; + public static final String GROUP_BASE_TYPE_ID = "cd"; + + public static final String TYPE_CANDELA_ID = GROUP_BASE_TYPE_ID; + public static final String TYPE_CANDELA_NAME = GROUP_NAME; + public static final String TYPE_CANDELA_FLAG = buildFlag(GROUP_ID,TYPE_CANDELA_NAME); private static final String TYPE_CANDELA_WIKI = "Candela"; @Override 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 3dff7bc..ef34d82 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 @@ -41,7 +41,9 @@ public class UnitXCModuleMass implements UnitXCConfigModule { public static final String GROUP_ID = "mass"; public static final String GROUP_NAME = "kilogram"; public static final String GROUP_DESCRIPTION = "The kilogram is the unit of mass; it is equal to the mass of the international prototype of the kilogram."; - public static final String TYPE_GRAM_ID = "g"; + public static final String GROUP_BASE_TYPE_ID = "g"; + + public static final String TYPE_GRAM_ID = GROUP_BASE_TYPE_ID; 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"; 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 765e09b..44d762e 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 @@ -38,18 +38,21 @@ import net.forwardfire.unitxc.config.builder.UnitXCTypeSIPrefix; */ public class UnitXCModuleSubstance implements UnitXCConfigModule { - public static final String GROUP_ID = "substance"; + public static final String GROUP_ID = "substance"; + public static final String GROUP_NAME = "mole"; public static final String GROUP_DESCRIPTION = "The mole is the amount of substance of a system which contains as many elementary entities as there are atoms in 0.012 kilogram of carbon 12."; - 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); + public static final String GROUP_BASE_TYPE_ID = "mol"; + + public static final String TYPE_MOLE_ID = GROUP_BASE_TYPE_ID; + public static final String TYPE_MOLE_NAME = GROUP_NAME; + 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); + 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"; + private static final String TYPE_POUND_MOLE_WIKI = "Mole_(unit)#Other_units_called_.22mole.22"; @Override public void configModule(UnitXCConfigBuilder builder) { 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 4f33d16..dfcc6e9 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 @@ -39,11 +39,13 @@ import org.apache.commons.lang3.math.Fraction; */ public class UnitXCModuleTemperature implements UnitXCConfigModule { - public static final String GROUP_ID = "temperature"; + public static final String GROUP_ID = "temperature"; + public static final String GROUP_NAME = "kelvin"; public static final String GROUP_DESCRIPTION = "The kelvin, unit of thermodynamic temperature, is the fraction 1/273.16 of the thermodynamic temperature of the triple point of water."; + public static final String GROUP_BASE_TYPE_ID = "K"; - public static final String TYPE_KELVIN_ID = "K"; - public static final String TYPE_KELVIN_NAME = "kelvin"; + public static final String TYPE_KELVIN_ID = GROUP_BASE_TYPE_ID; + public static final String TYPE_KELVIN_NAME = GROUP_NAME; public static final String TYPE_KELVIN_FLAG = buildFlag(GROUP_ID,TYPE_KELVIN_NAME); private static final String TYPE_KELVIN_WIKI = "Kelvin"; 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 50f24bb..83c7dd7 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 @@ -39,9 +39,12 @@ import net.forwardfire.unitxc.config.builder.UnitXCTypeSIPrefix; public class UnitXCModuleTime implements UnitXCConfigModule { public static final String GROUP_ID = "time"; + public static final String GROUP_NAME = "second"; public static final String GROUP_DESCRIPTION = "The second is the duration of 9192631770 periods of the radiation corresponding to the transition between the two hyperfine levels of the ground state of the caesium 133 atom."; - public static final String TYPE_SECOND_ID = "s"; - public static final String TYPE_SECOND_NAME = "second"; + public static final String GROUP_BASE_TYPE_ID = "s"; + + public static final String TYPE_SECOND_ID = GROUP_BASE_TYPE_ID; + public static final String TYPE_SECOND_NAME = GROUP_NAME; public static final String TYPE_SECOND_NAMES = TYPE_SECOND_NAME+TYPE_SECOND_ID; public static final String TYPE_SECOND_FLAG = buildFlag(GROUP_ID,TYPE_SECOND_NAME); diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleAcceleration.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleAcceleration.java new file mode 100644 index 0000000..fd72952 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleAcceleration.java @@ -0,0 +1,63 @@ +/* + * 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.derived; + +import static net.forwardfire.unitxc.config.builder.UnitXCNumberTypeSetting.toSuperScript; +import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_DERIVED; + +import net.forwardfire.unitxc.config.UnitXCConfigModule; +import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; +import net.forwardfire.unitxc.config.builder.UnitXCTypeCompoundPairBuilder; +import net.forwardfire.unitxc.module.UnitXCModuleTime; + +/** + * + * + * @author Willem Cazander + * @version 1.0 Oct 1, 2016 + */ +public class UnitXCModuleAcceleration implements UnitXCConfigModule { + + private static final int UNIT_COMPOUND_EXPONENT = 2; + private static final String UNIT_ID_POSTFIX = toSuperScript(UNIT_COMPOUND_EXPONENT); + + public static final String GROUP_ID = "acceleration"; + public static final String GROUP_NAME = UnitXCModuleSpeed.GROUP_NAME+UnitXCTypeCompoundPairBuilder.PER_NAME+UnitXCModuleTime.GROUP_NAME; + public static final String GROUP_DESCRIPTION = "Acceleration, is the rate of change of velocity of an object with respect to time."; + public static final String GROUP_BASE_TYPE_ID = UnitXCModuleSpeed.GROUP_BASE_TYPE_ID+UnitXCTypeCompoundPairBuilder.PER_ID+UnitXCModuleTime.GROUP_BASE_TYPE_ID; + + @Override + 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(UnitXCModuleSpeed.GROUP_ID, UnitXCModuleTime.GROUP_ID) + .build() + .build() + ; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleArea.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleArea.java index b84d7af..7f16f03 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleArea.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleArea.java @@ -21,14 +21,14 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package net.forwardfire.unitxc.module; +package net.forwardfire.unitxc.module.derived; -import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag; import static net.forwardfire.unitxc.config.builder.UnitXCNumberTypeSetting.toSuperScript; import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_DERIVED; import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; +import net.forwardfire.unitxc.module.UnitXCModuleLength; /** * @@ -43,9 +43,9 @@ public class UnitXCModuleArea implements UnitXCConfigModule { private static final String UNIT_NAME_PREFIX = "square "; public static final String GROUP_ID = "area"; - public static final String GROUP_NAME = UNIT_NAME_PREFIX+UnitXCModuleLength.TYPE_METRE_NAME; + public static final String GROUP_NAME = UNIT_NAME_PREFIX+UnitXCModuleLength.GROUP_NAME; 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 GROUP_BASE_TYPE_ID = UnitXCModuleLength.GROUP_BASE_TYPE_ID+UNIT_ID_POSTFIX; public static final String TYPE_CENTIARE_ALIAS = "m²"; public static final String TYPE_CENTIARE_ID = "ca"; diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleAreaDensity.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleAreaDensity.java new file mode 100644 index 0000000..ac379fd --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleAreaDensity.java @@ -0,0 +1,59 @@ +/* + * 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.derived; + +import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_DERIVED; + +import net.forwardfire.unitxc.config.UnitXCConfigModule; +import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; +import net.forwardfire.unitxc.config.builder.UnitXCTypeCompoundPairBuilder; +import net.forwardfire.unitxc.module.UnitXCModuleMass; + +/** + * + * + * @author Willem Cazander + * @version 1.0 Oct 12, 2016 + */ +public class UnitXCModuleAreaDensity implements UnitXCConfigModule { + + public static final String GROUP_ID = "area_density"; + public static final String GROUP_NAME = UnitXCModuleMass.GROUP_NAME+UnitXCTypeCompoundPairBuilder.PER_NAME+UnitXCModuleArea.GROUP_NAME; + public static final String GROUP_DESCRIPTION = "Area density is of a two-dimensional object is calculated as the mass per unit area."; + public static final String GROUP_BASE_TYPE_ID = UnitXCModuleMass.GROUP_BASE_TYPE_ID+UnitXCTypeCompoundPairBuilder.PER_ID+UnitXCModuleArea.GROUP_BASE_TYPE_ID; + + @Override + 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(UnitXCModuleMass.GROUP_ID, UnitXCModuleArea.GROUP_ID) + .build() + .build() + ; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleJerk.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleJerk.java new file mode 100644 index 0000000..f6ba630 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleJerk.java @@ -0,0 +1,60 @@ +/* + * 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.derived; + +import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_DERIVED; + +import net.forwardfire.unitxc.config.UnitXCConfigModule; +import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; +import net.forwardfire.unitxc.config.builder.UnitXCTypeCompoundPairBuilder; +import net.forwardfire.unitxc.module.UnitXCModuleTime; + +/** + * + * + * @author Willem Cazander + * @version 1.0 Oct 1, 2016 + */ +public class UnitXCModuleJerk implements UnitXCConfigModule { + + public static final String GROUP_ID = "jerk"; + public static final String GROUP_NAME = UnitXCModuleAcceleration.GROUP_NAME+UnitXCTypeCompoundPairBuilder.PER_NAME+UnitXCModuleTime.GROUP_NAME; + public static final String GROUP_DESCRIPTION = "Jerk, also known as jolt, surge, or lurch, is the rate of change of acceleration, with respect to time."; + public static final String GROUP_BASE_TYPE_ID = UnitXCModuleAcceleration.GROUP_BASE_TYPE_ID+UnitXCTypeCompoundPairBuilder.PER_ID+UnitXCModuleTime.GROUP_BASE_TYPE_ID; + + // TODO: add aliases + @Override + 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(UnitXCModuleAcceleration.GROUP_ID, UnitXCModuleTime.GROUP_ID) + .build() + .build() + ; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleJounce.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleJounce.java new file mode 100644 index 0000000..9fe4e4d --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleJounce.java @@ -0,0 +1,60 @@ +/* + * 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.derived; + +import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_DERIVED; + +import net.forwardfire.unitxc.config.UnitXCConfigModule; +import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; +import net.forwardfire.unitxc.config.builder.UnitXCTypeCompoundPairBuilder; +import net.forwardfire.unitxc.module.UnitXCModuleTime; + +/** + * + * + * @author Willem Cazander + * @version 1.0 Oct 1, 2016 + */ +public class UnitXCModuleJounce implements UnitXCConfigModule { + + public static final String GROUP_ID = "jounce"; + public static final String GROUP_NAME = UnitXCModuleJerk.GROUP_NAME+UnitXCTypeCompoundPairBuilder.PER_NAME+UnitXCModuleTime.GROUP_NAME; + public static final String GROUP_DESCRIPTION = "Is the fourth derivative of the position vector with respect to time or the rate of change of the jerk with respect to time."; + public static final String GROUP_BASE_TYPE_ID = UnitXCModuleJerk.GROUP_BASE_TYPE_ID+UnitXCTypeCompoundPairBuilder.PER_ID+UnitXCModuleTime.GROUP_BASE_TYPE_ID; + + // TODO: add aliases + @Override + 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(UnitXCModuleJerk.GROUP_ID, UnitXCModuleTime.GROUP_ID) + .build() + .build() + ; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleMassDensity.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleMassDensity.java new file mode 100644 index 0000000..c45749b --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleMassDensity.java @@ -0,0 +1,59 @@ +/* + * 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.derived; + +import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_DERIVED; + +import net.forwardfire.unitxc.config.UnitXCConfigModule; +import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; +import net.forwardfire.unitxc.config.builder.UnitXCTypeCompoundPairBuilder; +import net.forwardfire.unitxc.module.UnitXCModuleMass; + +/** + * + * + * @author Willem Cazander + * @version 1.0 Oct 12, 2016 + */ +public class UnitXCModuleMassDensity implements UnitXCConfigModule { + + public static final String GROUP_ID = "mass_density"; + public static final String GROUP_NAME = UnitXCModuleMass.GROUP_NAME+UnitXCTypeCompoundPairBuilder.PER_NAME+UnitXCModuleVolume.GROUP_NAME; + public static final String GROUP_DESCRIPTION = "The volumetric mass density, of a substance is its mass per unit volume."; + public static final String GROUP_BASE_TYPE_ID = UnitXCModuleMass.GROUP_BASE_TYPE_ID+UnitXCTypeCompoundPairBuilder.PER_ID+UnitXCModuleVolume.GROUP_BASE_TYPE_ID; + + @Override + 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(UnitXCModuleMass.GROUP_ID, UnitXCModuleVolume.GROUP_ID) + .build() + .build() + ; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleMolarConcentration.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleMolarConcentration.java new file mode 100644 index 0000000..4b5fb78 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleMolarConcentration.java @@ -0,0 +1,59 @@ +/* + * 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.derived; + +import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_DERIVED; + +import net.forwardfire.unitxc.config.UnitXCConfigModule; +import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; +import net.forwardfire.unitxc.config.builder.UnitXCTypeCompoundPairBuilder; +import net.forwardfire.unitxc.module.UnitXCModuleSubstance; + +/** + * + * + * @author Willem Cazander + * @version 1.0 Oct 12, 2016 + */ +public class UnitXCModuleMolarConcentration implements UnitXCConfigModule { + + public static final String GROUP_ID = "molar_concentration"; + public static final String GROUP_NAME = UnitXCModuleSubstance.GROUP_NAME+UnitXCTypeCompoundPairBuilder.PER_NAME+UnitXCModuleVolume.GROUP_NAME; + public static final String GROUP_DESCRIPTION = "Molar concentration is a measure of the concentration of a solute in a solution."; + public static final String GROUP_BASE_TYPE_ID = UnitXCModuleSubstance.GROUP_BASE_TYPE_ID+UnitXCTypeCompoundPairBuilder.PER_ID+UnitXCModuleVolume.GROUP_BASE_TYPE_ID; + + @Override + 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(UnitXCModuleSubstance.GROUP_ID, UnitXCModuleVolume.GROUP_ID) + .build() + .build() + ; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleMolarVolume.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleMolarVolume.java new file mode 100644 index 0000000..5197fea --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleMolarVolume.java @@ -0,0 +1,59 @@ +/* + * 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.derived; + +import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_DERIVED; + +import net.forwardfire.unitxc.config.UnitXCConfigModule; +import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; +import net.forwardfire.unitxc.config.builder.UnitXCTypeCompoundPairBuilder; +import net.forwardfire.unitxc.module.UnitXCModuleSubstance; + +/** + * + * + * @author Willem Cazander + * @version 1.0 Oct 12, 2016 + */ +public class UnitXCModuleMolarVolume implements UnitXCConfigModule { + + public static final String GROUP_ID = "molar_volume"; + public static final String GROUP_NAME = UnitXCModuleVolume.GROUP_NAME+UnitXCTypeCompoundPairBuilder.PER_NAME+UnitXCModuleSubstance.GROUP_NAME; + public static final String GROUP_DESCRIPTION = "Molar volume is the volume occupied by one mole of a substance."; + public static final String GROUP_BASE_TYPE_ID = UnitXCModuleVolume.GROUP_BASE_TYPE_ID+UnitXCTypeCompoundPairBuilder.PER_ID+UnitXCModuleSubstance.GROUP_BASE_TYPE_ID; + + @Override + 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(UnitXCModuleVolume.GROUP_ID, UnitXCModuleSubstance.GROUP_ID) + .build() + .build() + ; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleSpecificVolume.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleSpecificVolume.java new file mode 100644 index 0000000..38404ba --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleSpecificVolume.java @@ -0,0 +1,59 @@ +/* + * 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.derived; + +import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_DERIVED; + +import net.forwardfire.unitxc.config.UnitXCConfigModule; +import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; +import net.forwardfire.unitxc.config.builder.UnitXCTypeCompoundPairBuilder; +import net.forwardfire.unitxc.module.UnitXCModuleMass; + +/** + * + * + * @author Willem Cazander + * @version 1.0 Oct 12, 2016 + */ +public class UnitXCModuleSpecificVolume implements UnitXCConfigModule { + + public static final String GROUP_ID = "specific_volume"; + public static final String GROUP_NAME = UnitXCModuleVolume.GROUP_NAME+UnitXCTypeCompoundPairBuilder.PER_NAME+UnitXCModuleMass.GROUP_NAME; + public static final String GROUP_DESCRIPTION = "In thermodynamics, the specific volume of a substance is the ratio of the substance's volume to its mass."; + public static final String GROUP_BASE_TYPE_ID = UnitXCModuleVolume.GROUP_BASE_TYPE_ID+UnitXCTypeCompoundPairBuilder.PER_ID+UnitXCModuleMass.GROUP_BASE_TYPE_ID; + + @Override + 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(UnitXCModuleVolume.GROUP_ID, UnitXCModuleMass.GROUP_ID) + .build() + .build() + ; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleSpeed.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleSpeed.java index 6de2c76..2a7ef87 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleSpeed.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleSpeed.java @@ -21,13 +21,15 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package net.forwardfire.unitxc.module; +package net.forwardfire.unitxc.module.derived; import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_DERIVED; import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; import net.forwardfire.unitxc.config.builder.UnitXCTypeCompoundPairBuilder; +import net.forwardfire.unitxc.module.UnitXCModuleLength; +import net.forwardfire.unitxc.module.UnitXCModuleTime; /** * @@ -38,9 +40,9 @@ import net.forwardfire.unitxc.config.builder.UnitXCTypeCompoundPairBuilder; public class UnitXCModuleSpeed implements UnitXCConfigModule { 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_NAME = UnitXCModuleLength.GROUP_NAME+UnitXCTypeCompoundPairBuilder.PER_NAME+UnitXCModuleTime.GROUP_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; + public static final String GROUP_BASE_TYPE_ID = UnitXCModuleLength.GROUP_BASE_TYPE_ID+UnitXCTypeCompoundPairBuilder.PER_ID+UnitXCModuleTime.GROUP_BASE_TYPE_ID; @Override public void configModule(UnitXCConfigBuilder builder) { diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleVolume.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleVolume.java index c05cded..b2cd847 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleVolume.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleVolume.java @@ -21,13 +21,14 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package net.forwardfire.unitxc.module; +package net.forwardfire.unitxc.module.derived; import static net.forwardfire.unitxc.config.builder.UnitXCNumberTypeSetting.toSuperScript; import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_DERIVED; import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; +import net.forwardfire.unitxc.module.UnitXCModuleLength; /** * @@ -42,9 +43,9 @@ public class UnitXCModuleVolume implements UnitXCConfigModule { private static final String UNIT_NAME_PREFIX = "cubic "; public static final String GROUP_ID = "volume"; - public static final String GROUP_NAME = UNIT_NAME_PREFIX+UnitXCModuleLength.TYPE_METRE_NAME; + public static final String GROUP_NAME = UNIT_NAME_PREFIX+UnitXCModuleLength.GROUP_NAME; public static final String GROUP_DESCRIPTION = "It is the volume of a cube with edges one metre in length."; - public static final String GROUP_BASE_TYPE_ID = UnitXCModuleLength.TYPE_METRE_ID+UNIT_ID_POSTFIX; + public static final String GROUP_BASE_TYPE_ID = UnitXCModuleLength.GROUP_BASE_TYPE_ID+UNIT_ID_POSTFIX; @Override public void configModule(UnitXCConfigBuilder builder) { diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleVolumetricFlow.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleVolumetricFlow.java new file mode 100644 index 0000000..7254cb3 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/UnitXCModuleVolumetricFlow.java @@ -0,0 +1,59 @@ +/* + * 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.derived; + +import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_DERIVED; + +import net.forwardfire.unitxc.config.UnitXCConfigModule; +import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; +import net.forwardfire.unitxc.config.builder.UnitXCTypeCompoundPairBuilder; +import net.forwardfire.unitxc.module.UnitXCModuleTime; + +/** + * + * + * @author Willem Cazander + * @version 1.0 Oct 1, 2016 + */ +public class UnitXCModuleVolumetricFlow implements UnitXCConfigModule { + + public static final String GROUP_ID = "volumetric_flow"; + public static final String GROUP_NAME = UnitXCModuleVolume.GROUP_NAME+UnitXCTypeCompoundPairBuilder.PER_NAME+UnitXCModuleTime.GROUP_NAME; + public static final String GROUP_DESCRIPTION = "Volumetric flow is the volume of fluid which passes per unit time."; + public static final String GROUP_BASE_TYPE_ID = UnitXCModuleVolume.GROUP_BASE_TYPE_ID+UnitXCTypeCompoundPairBuilder.PER_ID+UnitXCModuleTime.GROUP_BASE_TYPE_ID; + + @Override + 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(UnitXCModuleVolume.GROUP_ID, UnitXCModuleTime.GROUP_ID) + .build() + .build() + ; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/package-info.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/package-info.java new file mode 100644 index 0000000..424063a --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/derived/package-info.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2013-2015, Willem Cazander + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + * @author willemc + * + */ +package net.forwardfire.unitxc.module.derived; \ No newline at end of file diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/named/UnitXCModuleNewton.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/named/UnitXCModuleNewton.java new file mode 100644 index 0000000..07ccce8 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/named/UnitXCModuleNewton.java @@ -0,0 +1,62 @@ +/* + * 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.named; + +import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_DERIVED; + +import net.forwardfire.unitxc.config.UnitXCConfigModule; +import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; +import net.forwardfire.unitxc.config.builder.UnitXCTypeCompoundPairBuilder; +import net.forwardfire.unitxc.module.UnitXCModuleMass; +import net.forwardfire.unitxc.module.derived.UnitXCModuleAcceleration; + +/** + * + * + * @author Willem Cazander + * @version 1.0 Oct 12, 2016 + */ +public class UnitXCModuleNewton implements UnitXCConfigModule { + + public static final String GROUP_ID = "newton"; + public static final String GROUP_NAME = UnitXCModuleMass.GROUP_NAME+UnitXCTypeCompoundPairBuilder.PER_NAME+UnitXCModuleAcceleration.GROUP_NAME; + public static final String GROUP_DESCRIPTION = "One newton is the force needed to accelerate one kilogram of mass at the rate of one metre per second squared in direction of the applied force."; + public static final String GROUP_BASE_TYPE_ID = "kg"+UnitXCTypeCompoundPairBuilder.PER_ID+UnitXCModuleAcceleration.GROUP_BASE_TYPE_ID; + + // TODO: symbol N + fix builder + + @Override + 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(UnitXCModuleMass.GROUP_ID, UnitXCModuleAcceleration.GROUP_ID) + .build() + .build() + ; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/named/package-info.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/named/package-info.java new file mode 100644 index 0000000..b6a0785 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/named/package-info.java @@ -0,0 +1,8 @@ +/** + * + */ +/** + * @author willemc + * + */ +package net.forwardfire.unitxc.module.named; \ No newline at end of file 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 afb6b92..30ebd30 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 @@ -26,6 +26,18 @@ package net.forwardfire.unitxc; import static org.junit.Assert.assertNotNull; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.stream.XMLStreamWriter; + +import org.apache.commons.lang3.StringUtils; +import org.codehaus.jettison.mapped.Configuration; +import org.codehaus.jettison.mapped.MappedNamespaceConvention; +import org.codehaus.jettison.mapped.MappedXMLStreamWriter; import org.junit.Test; import static org.junit.Assert.assertFalse; @@ -34,8 +46,9 @@ import static net.forwardfire.unitxc.UnitXCAssert.convertEquals; import net.forwardfire.unitxc.UnitXCFactory; import net.forwardfire.unitxc.UnitXCManager; +import net.forwardfire.unitxc.converter.UnitXConverterParameterValue; import net.forwardfire.unitxc.model.UnitXCType; -import net.forwardfire.unitxc.model.ModelXMLMarshaller; +import net.forwardfire.unitxc.model.ModelJAXBMarshaller; import net.forwardfire.unitxc.model.UnitXCGroup; import net.forwardfire.unitxc.model.UnitXConverterResult; import net.forwardfire.unitxc.model.UnitXConverterResultStep; @@ -44,77 +57,124 @@ public class UnitXCConverterStepTest { @Test public void testFac() throws Exception { - ModelXMLMarshaller xmlDriver = new ModelXMLMarshaller(); + ModelJAXBMarshaller jaxbDriver = new ModelJAXBMarshaller(); UnitXCManager unitManager = UnitXCFactory.createManager(); assertNotNull(unitManager); - assertNotNull(unitManager.getUnitTypes()); - assertNotNull(unitManager.getUnitGroups()); - assertFalse(unitManager.getUnitTypes().isEmpty()); - assertFalse(unitManager.getUnitGroups().isEmpty()); - - assertNotNull(unitManager.getUnitTypes()); - - for (UnitXCGroup typeGroup:unitManager.getUnitGroups()) { + assertNotNull(unitManager.getConfig()); + assertNotNull(unitManager.getConfig().getUnitGroups()); + assertFalse(unitManager.getConfig().getUnitGroups().isEmpty()); + + long ts = 0l; + for (UnitXCGroup typeGroup:unitManager.getConfig().getUnitGroups()) { UnitXCType type = unitManager.getUnitType(typeGroup.getBaseTypeId()); - System.out.println("TypeGroup: "+typeGroup.getId()+"\t name: "+typeGroup.getName()+"\t\t baseType: "+typeGroup.getBaseTypeId()+" id: "+type.getId()); - - int total = 0; - for (UnitXCType t:unitManager.getUnitTypes()) { - if (!t.getUnitGroupId().equals(typeGroup.getId())) { - continue; - } - total++; + if (type == null) { + throw new IllegalStateException("No base type for: "+typeGroup); } - System.out.println("typeInGroup: "+total); + long tc = typeGroup.getTotalUnitTypes(); + ts += tc; + System.out.println("TypeGroup: "+typeGroup.getId()+"\tunits:"+tc+"\t name: "+typeGroup.getName()+"\t\t baseType: "+type.getId()); } - int ts = unitManager.getUnitTypes().size(); System.out.println("\ntotalTypes: "+ts); - System.out.println("totalConv: "+(ts*ts-ts)); + System.out.println(""); + + UnitXConverterResult result2 = unitManager.getConverter().convertStepped(20.0, "m/s/s", "km/s/s"); + System.out.println("20m/s = "+result2.getResultValue()+" km/h"); UnitXConverterResult result = unitManager.getConverter().convertStepped(211.0, "k°F", "m°C"); - 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); + jaxbDriver.getUnitXConverterResult().marshal(result, System.out); System.out.println(""); - result = unitManager.getConverter().convertStepped(10.763, "ft²", "in²"); - 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()); - } + //result = unitManager.getConverter().convertStepped(10.763, "ft²", "in²"); + //xmlDriver.marshal(result, System.out); + //System.out.println(""); - System.out.println(""); - - result = unitManager.getConverter().convertStepped(10.763, "ft³", "in³"); - 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()); - } + //result = unitManager.getConverter().convertStepped(10.763, "ft³", "in³"); + //xmlDriver.marshal(result, System.out); - 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); + //result = unitManager.getConverter().convertStepped(1079000000, "km/h", "m/s"); + //xmlDriver.marshal(result, System.out); + + //try (OutputStream out = new FileOutputStream("/tmp/unitxc.conf.xml")) { + // jaxbDriver.getUnitXCConfig().marshal(unitManager.getConfig(), out); + //} + + //System.out.println("GROUP:"); + //jaxbDriver.getUnitXCGroup().marshal(unitManager.getUnitGroup("length"), System.out); + + Map para = new HashMap<>(); + para.put("speed_time", new UnitXConverterParameterValue(2, unitManager.getUnitType("minute"))); + result = unitManager.getConverter().convertStepped(50, "km/h", "mm",para); + System.out.println("CONVERT-RESULT:"); + jaxbDriver.getUnitXConverterResult().marshal(result, System.out); + + + Configuration config = new Configuration(); + + MappedNamespaceConvention con = new MappedNamespaceConvention(config); + Writer writer = new OutputStreamWriter(System.out); + + XMLStreamWriter xmlStreamWriter = new MappedXMLStreamWriter(con, new IndentedJSONWriter(writer)); + + //jaxbDriver.getUnitXConverterResult().marshal(result, xmlStreamWriter); } + class IndentedJSONWriter extends Writer { + + private final Writer writer; + private int depth = 0; + private boolean firstLine = true; + + public IndentedJSONWriter(Writer writer) { + this.writer = writer; + } + + private void writeLineWrap() throws IOException { + writer.write(StringUtils.LF); + writer.write(StringUtils.repeat(StringUtils.SPACE, depth)); + } + + @Override + public void write(int c) throws IOException { + if (c == '{') { + if (!firstLine) { + writer.write(StringUtils.SPACE); + } + writer.write(c); + depth++; + writeLineWrap(); + } else if (c == '}') { + writer.write(c); + depth--; + writeLineWrap(); + } else if (c == ',') { + writer.write(c); + writeLineWrap(); + } else if (c == '[') { + writer.write(StringUtils.SPACE); + writer.write(c); + writeLineWrap(); + } else { + writer.write(c); + } + firstLine = false; + } + + @Override + public void write(char[] cbuf, int off, int len) throws IOException { + writer.write(cbuf, off, len); + } + + @Override + public void flush() throws IOException { + writer.flush(); + } + + @Override + public void close() throws IOException { + writer.close(); + } + } } diff --git a/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/UnitXCFactoryTest.java b/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/UnitXCFactoryTest.java index b100347..a494a44 100644 --- a/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/UnitXCFactoryTest.java +++ b/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/UnitXCFactoryTest.java @@ -2,7 +2,7 @@ package net.forwardfire.unitxc; import org.junit.Test; -import net.forwardfire.unitxc.config.UnitXCConfig; +import net.forwardfire.unitxc.model.UnitXCConfig; import static org.junit.Assert.assertNotNull; diff --git a/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/UnitXCManagerTest.java b/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/UnitXCManagerTest.java index 1ebd20f..387a751 100644 --- a/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/UnitXCManagerTest.java +++ b/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/UnitXCManagerTest.java @@ -47,11 +47,6 @@ public class UnitXCManagerTest { assertNotNull(test.getUnitType(UnitXCModuleTemperature.TYPE_KELVIN_ID).getTypeFlags()); } - @Test - public void testGetUnitTypeResultTypeGroupId() { - assertNotNull(test.getUnitType(UnitXCModuleTemperature.TYPE_KELVIN_ID).getUnitGroupId()); - } - @Test public void testGetUnitTypeGroup() { assertNotNull(test.getUnitGroup(UnitXCModuleTemperature.GROUP_ID)); @@ -107,20 +102,20 @@ public class UnitXCManagerTest { assertTrue(test.isUnitGroup("")); } - @Test - public void testIsUnitTypeTrue() { - assertTrue(test.isUnitType(UnitXCModuleTemperature.TYPE_KELVIN_ID)); - } - - @Test - public void testIsUnitTypeFalse() { - assertFalse(test.isUnitType(getClass().getName())); - } - - @Test(expected=NullPointerException.class) - public void testIsUnitTypeNull() { - assertTrue(test.isUnitType(null)); - } +// @Test +// public void testIsUnitTypeTrue() { +// assertTrue(test.isUnitType(UnitXCModuleTemperature.TYPE_KELVIN_ID)); +// } +// +// @Test +// public void testIsUnitTypeFalse() { +// assertFalse(test.isUnitType(getClass().getName())); +// } +// +// @Test(expected=NullPointerException.class) +// public void testIsUnitTypeNull() { +// assertTrue(test.isUnitType(null)); +// } @Test(expected=IllegalArgumentException.class) public void testIsUnitTypeEmpty() {