From 9ac4acae7ba20b132cffb2855b861b7a53ba3ef5 Mon Sep 17 00:00:00 2001 From: Willem Date: Fri, 23 Oct 2015 21:32:01 +0200 Subject: [PATCH] Added builders --- .../net/forwardfire/unitxc/UnitXCFactory.java | 25 --- .../unitxc/config/UnitXCConfig.java | 2 +- .../unitxc/config/UnitXCConfigManager.java | 4 +- .../unitxc/config/UnitXCConfigModule.java | 6 +- .../config/UnitXCConfigModuleBuilder.java | 164 ------------------ .../config/builder/AbstractUnitXCBuilder.java | 86 +++++++++ .../builder/AbstractUnitXCTypeBuilder.java | 87 ++++++++++ .../config/builder/UnitXCConfigBuilder.java | 49 ++++++ .../config/builder/UnitXCTypeBuilder.java | 40 +++++ .../builder/UnitXCTypeGroupBuilder.java | 62 +++++++ .../config/builder/UnitXCTypeSIPrefix.java | 113 ++++++++++++ .../builder/UnitXCTypeSIPrefixBuilder.java | 82 +++++++++ .../builder/UnitXConverterStepBuilder.java | 73 ++++++++ .../step/UnitXCTypeMultiplyConverterStep.java | 2 +- ...itXCTypeMultiplyFractionConverterStep.java | 21 ++- .../unitxc/model/UnitXCTypeGroupModel.java | 8 +- .../unitxc/model/UnitXCTypeModel.java | 6 +- .../module/UnitXCElectricCurrentModule.java | 24 ++- .../module/UnitXCTemperatureModule.java | 96 ++++++---- .../forwardfire/unitxc/UnitXCManagerTest.java | 129 ++++++++++++++ .../module/UnitXCTemperatureModuleTest.java | 29 ++-- 21 files changed, 843 insertions(+), 265 deletions(-) delete mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/UnitXCConfigModuleBuilder.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/AbstractUnitXCBuilder.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/AbstractUnitXCTypeBuilder.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCConfigBuilder.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeBuilder.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeGroupBuilder.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeSIPrefix.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeSIPrefixBuilder.java create mode 100644 ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXConverterStepBuilder.java create mode 100644 ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/UnitXCManagerTest.java 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 fb1c0a8..8b7479d 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 @@ -30,10 +30,6 @@ import java.util.List; import net.forwardfire.unitxc.config.UnitXCConfig; import net.forwardfire.unitxc.config.UnitXCConfigManager; import net.forwardfire.unitxc.config.UnitXCConfigModule; -import net.forwardfire.unitxc.converter.step.UnitXCTypeDevideConverterStep; -import net.forwardfire.unitxc.converter.step.UnitXCTypeMultiplyConverterStep; -import net.forwardfire.unitxc.converter.step.UnitXCTypeMultiplyFractionConverterStep; -import net.forwardfire.unitxc.converter.step.UnitXCTypeOffsetConverterStep; import net.forwardfire.unitxc.module.UnitXCElectricCurrentModule; import net.forwardfire.unitxc.module.UnitXCTemperatureModule; @@ -64,25 +60,4 @@ public final class UnitXCFactory { public static UnitXCManager createManager(UnitXCConfig config) { return new UnitXCConfigManager(config); } - - public static class MathStep { - public static UnitXCTypeOffsetConverterStep add(double offset) { - return offset(offset,true); - } - public static UnitXCTypeOffsetConverterStep substract(double offset) { - return offset(offset,false); - } - public static UnitXCTypeOffsetConverterStep offset(double offset,boolean offsetPositive) { - return new UnitXCTypeOffsetConverterStep(offset,offsetPositive); - } - public static UnitXCTypeMultiplyFractionConverterStep multiplyFraction(int numerator,int denominator) { - return new UnitXCTypeMultiplyFractionConverterStep(numerator,denominator); - } - public static UnitXCTypeMultiplyConverterStep multiply(double factor) { - return new UnitXCTypeMultiplyConverterStep(factor); - } - public static UnitXCTypeDevideConverterStep divide(double factor) { - return new UnitXCTypeDevideConverterStep(factor); - } - } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/UnitXCConfig.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/UnitXCConfig.java index b6bd23a..f4be4e4 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/UnitXCConfig.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/UnitXCConfig.java @@ -54,7 +54,7 @@ public final class UnitXCConfig { } private UnitXCTypeModel validateGroupId(UnitXCTypeModel unitType) { - Validate.isTrue(unitTypeGroups.containsKey(unitType.getTypeGroupId())); + //Validate.isTrue(unitTypeGroups.containsKey(unitType.getTypeGroupId()),"group is missing"); return unitType; } 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 59a8a45..93d31d9 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 @@ -80,7 +80,7 @@ public class UnitXCConfigManager implements UnitXCManager { @Override public boolean isUnitType(String id) { - return unitTypes.containsKey(id); + return unitTypes.containsKey(Validate.notBlank(id)); } @Override @@ -95,7 +95,7 @@ public class UnitXCConfigManager implements UnitXCManager { @Override public boolean isUnitTypeGroup(String id) { - return unitTypeGroups.containsKey(id); + return unitTypeGroups.containsKey(Validate.notBlank(id)); } @Override 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 5bad728..92b661b 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 @@ -25,12 +25,14 @@ package net.forwardfire.unitxc.config; import java.util.List; +import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; + public interface UnitXCConfigModule { - void configModule(UnitXCConfigModuleBuilder config); + void configModule(UnitXCConfigBuilder config); static UnitXCConfig buildAll(UnitXCConfig config,List configInit) { - UnitXCConfigModuleBuilder builder = new UnitXCConfigModuleBuilder(config); + 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/UnitXCConfigModuleBuilder.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/UnitXCConfigModuleBuilder.java deleted file mode 100644 index 266695d..0000000 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/UnitXCConfigModuleBuilder.java +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Copyright (c) 2013-2015, Willem Cazander - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted provided - * that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, this list of conditions and the - * following disclaimer. - * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and - * the following disclaimer in the documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL - * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR - * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package net.forwardfire.unitxc.config; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import org.apache.commons.lang3.Validate; - -import net.forwardfire.unitxc.converter.step.UnitXCTypeExponentConverterStep; -import net.forwardfire.unitxc.converter.step.UnitXConverterStep; -import net.forwardfire.unitxc.model.UnitXCTypeModel; -import net.forwardfire.unitxc.model.UnitXCTypeGroupModel; - -public class UnitXCConfigModuleBuilder { - - private final UnitXCConfig config; - - public UnitXCConfigModuleBuilder(UnitXCConfig config) { - this.config = Validate.notNull(config); - } - - public void addUnitTypeGroup(String id,String baseTypeId,String name,String description) { - config.addUnitTypeGroup(new UnitXCTypeGroupModel(id,name,description,baseTypeId)); - } - - //private void addUnitType(String groupId,String id,int factorExp,String name) { - // addUnitType(conv,groupId, id, factorExp, name,null); - //} - private void addUnitType(List toConverters,List fromConverters,String groupId,String id,int factorExp,String name,List typeFlags) { - UnitXCTypeModel result = new UnitXCTypeModel(); - result.setId(id); - result.setName(name); - if (factorExp != 0) { - result.addToBaseConverterStep(new UnitXCTypeExponentConverterStep(factorExp,false)); - } - toConverters.forEach((conv) ->result.addToBaseConverterStep(conv)); - fromConverters.forEach((conv) ->result.addFromBaseConverterStep(conv)); - if (factorExp != 0) { - result.addFromBaseConverterStep(new UnitXCTypeExponentConverterStep(factorExp,true)); - } - result.setTypeGroupId(groupId); - typeFlags.forEach((flag) -> result.addTypeFlag(flag)); - config.addUnitType(result); - } - - - public enum SiMultiple { - YOTTA ("Y", 24), - ZETTA ("Z", 21), - EXA ("E", 18), - PETA ("P", 15), - TERA ("T", 12), - GIGA ("G", 9), - MEGA ("M", 6), - KILO ("k", 3), - HECTO ("h", 2), - DECA ("da",1), - DECI ("d", -1), - CENTI ("c", -2), - MILLI ("m", -3), - MICRO ("µ", -6), - NANO ("n", -9), - PICO ("p", -12), - FEMTO ("f", -15), - ATTO ("a", -18), - ZEPTO ("z", -21), - YOCTO ("y", -24), - ; - - public static final String TYPE_FLAG_SI_UNIT = "SI_UNIT"; - public static final String TYPE_FLAG_SI_UNIT_BIGGER = "SI_UNIT_BIGGER"; - public static final String TYPE_FLAG_SI_UNIT_BIGGEST = "SI_UNIT_BIGGEST"; - - private final String id; - private final String name; - private final int exponent; - private final List flags; - - private SiMultiple(String id,int exponent) { - this.id=id; - this.name = this.name().toLowerCase(); - this.exponent=exponent; - List f = new ArrayList<>(3); - f.add(TYPE_FLAG_SI_UNIT); - if (exponent > 3 || exponent < -3) { - f.add(TYPE_FLAG_SI_UNIT_BIGGER); - } - if (exponent > 12 || exponent < -12) { - f.add(TYPE_FLAG_SI_UNIT_BIGGEST); - } - this.flags = Collections.unmodifiableList(f); - } - - public String getPrefixId() { - return id; - } - - public String getPrefixName() { - return name; - } - - public int getExponent() { - return exponent; - } - - public List getFlags() { - return flags; - } - } - - public static final String TYPE_FLAG_SI_UNIT_SILLY = "SI_UNIT_SILLY"; - - public void addSIMultiples(String groupId,String type,String typeName,String flag) { - List flags = new ArrayList<>(); - flags.add(flag); - createTypesSIMultiples(groupId,type,typeName,flags,new ArrayList<>(),new ArrayList<>(),false); - } - - public void addSIMultiplesSilly(String groupId,UnitXCConfigBuildTypeMultiples builder) { - List flags = new ArrayList<>(); - flags.add(builder.getFlag()); - createTypesSIMultiples(groupId,builder.getType(),builder.getTypeName(),flags,Arrays.asList(builder.getToConverters()),Arrays.asList(builder.getFromConverters()),true); - } - - - private void createTypesSIMultiples(String groupId,String type,String typeName,List flags,List toConverters,List fromConverters,boolean addSilly) { - // Create base unit - addUnitType(toConverters,fromConverters,groupId,type,0,typeName,flags); - // Create si values - for (SiMultiple sim:SiMultiple.values()) { - List typeFlags = new ArrayList<>(); - typeFlags.addAll(flags); - typeFlags.addAll(sim.getFlags()); - if (addSilly) { - typeFlags.add(TYPE_FLAG_SI_UNIT_SILLY); - } - addUnitType(toConverters,fromConverters, groupId, sim.getPrefixId()+type, sim.getExponent(), sim.getPrefixName()+typeName, typeFlags); - } - } -} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/AbstractUnitXCBuilder.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/AbstractUnitXCBuilder.java new file mode 100644 index 0000000..875e6bc --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/AbstractUnitXCBuilder.java @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2013-2015, Willem Cazander + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.forwardfire.unitxc.config.builder; + +import java.util.function.BiConsumer; +import java.util.function.Consumer; + +import org.apache.commons.lang3.Validate; +import org.apache.commons.lang3.builder.Builder; + +/** + * Abstract object builder with parent builder. + * + * + * @param

The parent builder. + * @param The object to build. + * @param This builder. + * @author Willem Cazander + * @version 1.0 May 29, 2015 + */ +public abstract class AbstractUnitXCBuilder implements Builder

{ + + private final P parent; + private final T value; + private final BiConsumer parentBuilder; + + /** + * Creates the builder. + * @param parent The parent builder. + * @param value The object to build. + */ + public AbstractUnitXCBuilder(P parent,T value,BiConsumer parentBuilder) { + this.parent = Validate.notNull(parent); + this.value = Validate.notNull(value); + this.parentBuilder = Validate.notNull(parentBuilder); + } + + protected P getParent() { + return parent; + } + + protected T getValue() { + return value; + } + + protected abstract B getBuilder(); + + /** + * Builds the result. + * @return The result. + */ + @Override + public final P build() { + buildPreValue(getValue()); + parentBuilder.accept(getParent(), getValue()); + return getParent(); + } + + protected void buildPreValue(T value) { + } + + protected B make(Consumer mixer) { + mixer.accept(getValue()); + return getBuilder(); + } +} 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 new file mode 100644 index 0000000..fb2db08 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/AbstractUnitXCTypeBuilder.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.config.builder; + +import java.util.Arrays; +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.UnitXCTypeModel; + +public abstract class AbstractUnitXCTypeBuilder extends AbstractUnitXCBuilder { + + public AbstractUnitXCTypeBuilder(P parent,BiConsumer parentBuilder) { + super(parent, new UnitXCTypeModel(), parentBuilder); + } + + public B setId(String id) { + return make((v) -> v.setId(id)); + } + + public B setName(String name) { + return make((v) -> v.setName(name)); + } + + public B setTypeGroupId(String typeGroupId) { + return make((v) -> v.setTypeGroupId(typeGroupId)); + } + + public B addTypeFlag(String flag) { + return make((v) -> v.addTypeFlag(flag)); + } + + public B addTypeFlags(Collection flags) { + return make(v -> flags.forEach(flag -> v.addTypeFlag(flag))); + } + + public B addTypeFlags(String[] flags) { + return addTypeFlags(Arrays.asList(flags)); + } + + public UnitXConverterStepBuilder> createFromBaseConverterSteps() { + return new UnitXConverterStepBuilder<>(this,(p,v) -> p.addFromBaseConverterSteps(v)); + } + + public UnitXConverterStepBuilder> createToBaseConverterSteps() { + return new UnitXConverterStepBuilder<>(this,(p,v) -> p.addToBaseConverterSteps(v)); + } + + public B addFromBaseConverterStep(UnitXConverterStep unitTypeConverter) { + return make((v) -> v.addFromBaseConverterStep(unitTypeConverter)); + } + + public B addFromBaseConverterSteps(List unitTypeConverters) { + return make((v) -> unitTypeConverters.forEach(c -> v.addFromBaseConverterStep(c))); + } + + public B addToBaseConverterStep(UnitXConverterStep unitTypeConverter) { + return make((v) -> v.addFromBaseConverterStep(unitTypeConverter)); + } + + public B addToBaseConverterSteps(List unitTypeConverters) { + return make((v) -> unitTypeConverters.forEach(c -> v.addToBaseConverterStep(c))); + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCConfigBuilder.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCConfigBuilder.java new file mode 100644 index 0000000..ce9c056 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCConfigBuilder.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2013-2015, Willem Cazander + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package net.forwardfire.unitxc.config.builder; + +import org.apache.commons.lang3.Validate; + +import net.forwardfire.unitxc.config.UnitXCConfig; + +public class UnitXCConfigBuilder { + + private final UnitXCConfig config; + + public UnitXCConfigBuilder(UnitXCConfig config) { + this.config = Validate.notNull(config); + } + + protected UnitXCConfig getConfig() { + return config; + } + + public UnitXCTypeGroupBuilder createUnitTypeGroup() { + return new UnitXCTypeGroupBuilder(this); + } + + public UnitXCTypeBuilder createUnitType() { + return new UnitXCTypeBuilder<>(this,(p,v) -> p.getConfig().addUnitType(v)); + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeBuilder.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeBuilder.java new file mode 100644 index 0000000..ec2170e --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeBuilder.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2013-2015, Willem Cazander + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package net.forwardfire.unitxc.config.builder; + +import java.util.function.BiConsumer; + +import net.forwardfire.unitxc.model.UnitXCTypeModel; + +public class UnitXCTypeBuilder

extends AbstractUnitXCTypeBuilder> { + + public UnitXCTypeBuilder(P parent, BiConsumer parentBuilder) { + super(parent, parentBuilder); + } + + @Override + protected UnitXCTypeBuilder

getBuilder() { + return this; + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeGroupBuilder.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeGroupBuilder.java new file mode 100644 index 0000000..bf5f07e --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeGroupBuilder.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.config.builder; + +import net.forwardfire.unitxc.model.UnitXCTypeGroupModel; + +public class UnitXCTypeGroupBuilder extends AbstractUnitXCBuilder { + + public UnitXCTypeGroupBuilder(UnitXCConfigBuilder parent) { + super(parent, new UnitXCTypeGroupModel(), (p,v) -> p.getConfig().addUnitTypeGroup(v)); + } + + @Override + protected UnitXCTypeGroupBuilder getBuilder() { + return this; + } + + public UnitXCTypeBuilder createUnitType() { + return new UnitXCTypeBuilder<>(this,(p,v) -> p.getParent().getConfig().addUnitType(v)).setTypeGroupId(getValue().getId()); + } + + public UnitXCTypeSIPrefixBuilder createSIUnitTypes() { + return new UnitXCTypeSIPrefixBuilder<>(this,getParent()).setTypeGroupId(getValue().getId()); + } + + public UnitXCTypeGroupBuilder setId(String id) { + return make((v) -> v.setId(id)); + } + + public UnitXCTypeGroupBuilder setName(String name) { + return make((v) -> v.setName(name)); + } + + public UnitXCTypeGroupBuilder setDescription(String description) { + return make((v) -> v.setDescription(description)); + } + + public UnitXCTypeGroupBuilder setBaseTypeId(String baseTypeId) { + return make((v) -> v.setBaseTypeId(baseTypeId)); + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeSIPrefix.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeSIPrefix.java new file mode 100644 index 0000000..ebdd602 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeSIPrefix.java @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2013-2015, Willem Cazander + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package net.forwardfire.unitxc.config.builder; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public enum UnitXCTypeSIPrefix { + + YOTTA ("Y", 24), + ZETTA ("Z", 21), + EXA ("E", 18), + PETA ("P", 15), + TERA ("T", 12), + GIGA ("G", 9), + MEGA ("M", 6), + KILO ("k", 3), + HECTO ("h", 2), + DECA ("da",1), + DECI ("d", -1), + CENTI ("c", -2), + MILLI ("m", -3), + MICRO ("µ", -6), + NANO ("n", -9), + PICO ("p", -12), + FEMTO ("f", -15), + ATTO ("a", -18), + ZEPTO ("z", -21), + YOCTO ("y", -24), + + EXBI ("Ei", 60, UnitXCTypeSIPrefix.TYPE_FLAG_SI_UNIT_BINARY), + PEBI ("Pi", 50, UnitXCTypeSIPrefix.TYPE_FLAG_SI_UNIT_BINARY), + TEBI ("Ti", 40, UnitXCTypeSIPrefix.TYPE_FLAG_SI_UNIT_BINARY), + GIBI ("Gi", 30, UnitXCTypeSIPrefix.TYPE_FLAG_SI_UNIT_BINARY), + MEBI ("Mi", 20, UnitXCTypeSIPrefix.TYPE_FLAG_SI_UNIT_BINARY), + KIBI ("Ki", 10, UnitXCTypeSIPrefix.TYPE_FLAG_SI_UNIT_BINARY), + + HECTOKILO ("hk", 5, UnitXCTypeSIPrefix.TYPE_FLAG_SI_UNIT_OBSOLETE), + MYRIA ("my", 4, UnitXCTypeSIPrefix.TYPE_FLAG_SI_UNIT_OBSOLETE), + ; + + public static final String TYPE_FLAG_SI_UNIT = "SI_UNIT"; + public static final String TYPE_FLAG_SI_UNIT_BIGGER = "SI_UNIT_BIGGER"; + public static final String TYPE_FLAG_SI_UNIT_BIGGEST = "SI_UNIT_BIGGEST"; + public static final String TYPE_FLAG_SI_UNIT_BINARY = "SI_UNIT_BINARY"; + public static final String TYPE_FLAG_SI_UNIT_OBSOLETE = "SI_UNIT_OBSOLETE"; + public static final String TYPE_FLAG_SI_UNIT_COMMON = "SI_UNIT_COMMON"; + + private final String id; + private final String name; + private final int exponent; + private final List flags; + + private UnitXCTypeSIPrefix(String id,int exponent) { + this(id,exponent,null); + } + + private UnitXCTypeSIPrefix(String id,int exponent,String flag) { + this.id=id; + this.name = this.name().toLowerCase(); + this.exponent=exponent; + List f = new ArrayList<>(3); + f.add(TYPE_FLAG_SI_UNIT); + if (exponent > 3 || exponent < -3) { + f.add(TYPE_FLAG_SI_UNIT_BIGGER); + } + if (exponent > 12 || exponent < -12) { + f.add(TYPE_FLAG_SI_UNIT_BIGGEST); + } + if (flag != null) { + f.add(flag); + } + this.flags = Collections.unmodifiableList(f); + } + + public String getPrefixId() { + return id; + } + + public String getPrefixName() { + return name; + } + + public int getExponent() { + return exponent; + } + + public List getFlags() { + return flags; + } +} 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 new file mode 100644 index 0000000..048a75f --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXCTypeSIPrefixBuilder.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2013-2015, Willem Cazander + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package net.forwardfire.unitxc.config.builder; + +import java.util.ArrayList; +import java.util.List; + +import net.forwardfire.unitxc.converter.step.UnitXCTypeExponentConverterStep; +import net.forwardfire.unitxc.model.UnitXCTypeModel; + +public class UnitXCTypeSIPrefixBuilder

extends AbstractUnitXCTypeBuilder> { + + private final UnitXCConfigBuilder builder; + private final List commonSIPrefixes; + + public UnitXCTypeSIPrefixBuilder(P parent, UnitXCConfigBuilder builder) { + super(parent, (p,v) -> {}); + this.builder = builder; + this.commonSIPrefixes = new ArrayList<>(); + } + + @Override + protected UnitXCTypeSIPrefixBuilder

getBuilder() { + return this; + } + + @Override + protected void buildPreValue(UnitXCTypeModel v) { + builder.createUnitType() + .setId(v.getId()) + .setName(v.getName()) + .setTypeGroupId(v.getTypeGroupId()) + .addTypeFlags(v.getTypeFlags()) + .addFromBaseConverterSteps(v.getFromBaseConverterSteps()) + .addToBaseConverterSteps(v.getToBaseConverterSteps()) + .build(); + + for (UnitXCTypeSIPrefix sim:UnitXCTypeSIPrefix.values()) { + List typeFlags = new ArrayList<>(); + typeFlags.addAll(v.getTypeFlags()); + typeFlags.addAll(sim.getFlags()); + if (commonSIPrefixes.contains(sim)) { + typeFlags.add(UnitXCTypeSIPrefix.TYPE_FLAG_SI_UNIT_COMMON); + } + builder.createUnitType() + .setId(sim.getPrefixId()+v.getId()) + .setName(sim.getPrefixName()+v.getName()) + .setTypeGroupId(v.getTypeGroupId()) + .addTypeFlags(typeFlags) + .addToBaseConverterStep(new UnitXCTypeExponentConverterStep(sim.getExponent(),false)) + .addToBaseConverterSteps(v.getToBaseConverterSteps()) + .addFromBaseConverterSteps(v.getFromBaseConverterSteps()) + .addFromBaseConverterStep(new UnitXCTypeExponentConverterStep(sim.getExponent(),true)) + .build(); + } + } + + public UnitXCTypeSIPrefixBuilder

addCommonPrefix(UnitXCTypeSIPrefix prefix) { + return make((v) -> commonSIPrefixes.add(prefix)); + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXConverterStepBuilder.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXConverterStepBuilder.java new file mode 100644 index 0000000..14192c9 --- /dev/null +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/config/builder/UnitXConverterStepBuilder.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2013-2015, Willem Cazander + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package net.forwardfire.unitxc.config.builder; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.BiConsumer; + +import org.apache.commons.lang3.math.Fraction; + +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.UnitXConverterStep; + +public class UnitXConverterStepBuilder

extends AbstractUnitXCBuilder,UnitXConverterStepBuilder

> { + + public UnitXConverterStepBuilder(P parent, BiConsumer> parentBuilder) { + super(parent, new ArrayList<>(), parentBuilder); + } + + @Override + protected UnitXConverterStepBuilder

getBuilder() { + return this; + } + + public UnitXConverterStepBuilder

add(double offset) { + return offset(offset,true); + } + public UnitXConverterStepBuilder

sub(double offset) { + return offset(offset,false); + } + public UnitXConverterStepBuilder

substract(double offset) { + return offset(offset,false); + } + public UnitXConverterStepBuilder

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

multiplyFraction(Fraction fraction) { + return make(v -> v.add(new UnitXCTypeMultiplyFractionConverterStep(fraction))); + } + public UnitXConverterStepBuilder

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

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

divide(double factor) { + return make(v -> v.add(new UnitXCTypeDevideConverterStep(factor))); + } +} diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeMultiplyConverterStep.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeMultiplyConverterStep.java index 1be60be..e695cbd 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeMultiplyConverterStep.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeMultiplyConverterStep.java @@ -25,7 +25,7 @@ package net.forwardfire.unitxc.converter.step; public class UnitXCTypeMultiplyConverterStep extends AbstractUnitXConverterStep { - private final static String STEP_NAME = "Factor Converter"; + private final static String STEP_NAME = "Multiply"; private final double factor; public UnitXCTypeMultiplyConverterStep(double factor) { diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeMultiplyFractionConverterStep.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeMultiplyFractionConverterStep.java index 3b42c5f..3389326 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeMultiplyFractionConverterStep.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/converter/step/UnitXCTypeMultiplyFractionConverterStep.java @@ -23,25 +23,32 @@ package net.forwardfire.unitxc.converter.step; +import org.apache.commons.lang3.Validate; +import org.apache.commons.lang3.math.Fraction; + public class UnitXCTypeMultiplyFractionConverterStep extends AbstractUnitXConverterStep { - private final static String STEP_NAME = "FactorFaction"; - private final int numerator; - private final int denominator; + private final static String STEP_NAME = "MultiplyFaction"; + private final Fraction fraction; + private final String fractionMath; public UnitXCTypeMultiplyFractionConverterStep(int numerator,int denominator) { + this(Fraction.getFraction(numerator, denominator)); + } + + public UnitXCTypeMultiplyFractionConverterStep(Fraction fraction) { super(STEP_NAME); - this.numerator = numerator; - this.denominator = denominator; + this.fraction = Validate.notNull(fraction); + this.fractionMath = String.format("*(%1s/%2s)", fraction.getNumerator(),fraction.getDenominator()); } @Override public double convert(double value) { - return value*((double)numerator/(double)denominator); + return value*fraction.doubleValue(); } @Override public String getMathExpression() { - return "*("+numerator+"/"+denominator+")"; + return fractionMath; } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCTypeGroupModel.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCTypeGroupModel.java index 53d9660..61503cf 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCTypeGroupModel.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCTypeGroupModel.java @@ -43,10 +43,10 @@ public class UnitXCTypeGroupModel implements UnitXCTypeGroup { } public UnitXCTypeGroupModel validate() { - Validate.notBlank(id); - Validate.notBlank(name); - Validate.notBlank(description); - Validate.notBlank(baseTypeId); + Validate.notBlank(id,"The id is blank"); + Validate.notBlank(name,"The name is blank"); + Validate.notBlank(description,"The description is blank"); + Validate.notBlank(baseTypeId,"The baseTypeId is blank"); return this; } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCTypeModel.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCTypeModel.java index d5fbf31..5da925f 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCTypeModel.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/model/UnitXCTypeModel.java @@ -47,9 +47,9 @@ public class UnitXCTypeModel implements UnitXCType { } public UnitXCTypeModel validate() { - Validate.notBlank(id); - Validate.notBlank(name); - Validate.notBlank(typeGroupId); + Validate.notBlank(id,"The id is blank"); + Validate.notBlank(name,"The name is blank"); + Validate.notBlank(typeGroupId,"The typeGroupId is blank"); return this; } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCElectricCurrentModule.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCElectricCurrentModule.java index da04f45..02d57ce 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCElectricCurrentModule.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCElectricCurrentModule.java @@ -24,7 +24,8 @@ package net.forwardfire.unitxc.module; import net.forwardfire.unitxc.config.UnitXCConfigModule; -import net.forwardfire.unitxc.config.UnitXCConfigModuleBuilder; +import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; +import net.forwardfire.unitxc.config.builder.UnitXCTypeSIPrefix; public class UnitXCElectricCurrentModule implements UnitXCConfigModule { @@ -35,8 +36,23 @@ public class UnitXCElectricCurrentModule implements UnitXCConfigModule { public static final String TYPE_AMPERE_FLAG = (GROUP_ID+"_"+TYPE_AMPERE_NAME).toUpperCase(); @Override - public void configModule(UnitXCConfigModuleBuilder builder) { - builder.addUnitTypeGroup( GROUP_ID, TYPE_AMPERE_ID, TYPE_AMPERE_NAME, GROUP_DESCRIPTION ); - builder.addSIMultiples( GROUP_ID, TYPE_AMPERE_ID, TYPE_AMPERE_NAME, TYPE_AMPERE_FLAG ); + public void configModule(UnitXCConfigBuilder builder) { + builder.createUnitTypeGroup() + .setId( GROUP_ID) + .setName( TYPE_AMPERE_NAME) + .setDescription ( GROUP_DESCRIPTION) + .setBaseTypeId( TYPE_AMPERE_ID) + .createSIUnitTypes() + .setId( TYPE_AMPERE_ID) + .setName( TYPE_AMPERE_NAME) + .addTypeFlag( TYPE_AMPERE_FLAG) + .addCommonPrefix(UnitXCTypeSIPrefix.MEGA) + .addCommonPrefix(UnitXCTypeSIPrefix.KILO) + .addCommonPrefix(UnitXCTypeSIPrefix.MILLI) + .addCommonPrefix(UnitXCTypeSIPrefix.MICRO) + .addCommonPrefix(UnitXCTypeSIPrefix.NANO) + .build() + .build() + ; } } diff --git a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCTemperatureModule.java b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCTemperatureModule.java index 918c684..4361445 100644 --- a/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCTemperatureModule.java +++ b/ff-unitxc-converter/src/main/java/net/forwardfire/unitxc/module/UnitXCTemperatureModule.java @@ -23,13 +23,10 @@ package net.forwardfire.unitxc.module; -import net.forwardfire.unitxc.config.UnitXCConfigBuildTypeMultiples; import net.forwardfire.unitxc.config.UnitXCConfigModule; -import net.forwardfire.unitxc.config.UnitXCConfigModuleBuilder; -import net.forwardfire.unitxc.converter.step.UnitXConverterStep; -import static net.forwardfire.unitxc.UnitXCFactory.MathStep.add; -import static net.forwardfire.unitxc.UnitXCFactory.MathStep.substract; -import static net.forwardfire.unitxc.UnitXCFactory.MathStep.multiplyFraction; +import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; + +import org.apache.commons.lang3.math.Fraction; public class UnitXCTemperatureModule implements UnitXCConfigModule { @@ -43,46 +40,79 @@ public class UnitXCTemperatureModule implements UnitXCConfigModule { public static final String TYPE_CELSIUS_ID = "°C"; public static final String TYPE_CELSIUS_NAME = "celsius"; public static final String TYPE_CELSIUS_FLAG = (GROUP_ID+"_"+TYPE_CELSIUS_NAME).toUpperCase(); - private static final UnitXCConfigBuildTypeMultiples TYPE_CELSIUS_CONFIG = new UnitXCConfigBuildTypeMultiples( - TYPE_CELSIUS_ID,TYPE_CELSIUS_NAME,TYPE_CELSIUS_FLAG, - new UnitXConverterStep[]{add(273.15)}, - new UnitXConverterStep[]{substract(273.15)} - ); + private static final double TYPE_CELSIUS_OFFSET = 273.15; public static final String TYPE_FAHRENHEIT_ID = "°F"; public static final String TYPE_FAHRENHEIT_NAME = "fahrenheit"; public static final String TYPE_FAHRENHEIT_FLAG = (GROUP_ID+"_"+TYPE_FAHRENHEIT_NAME).toUpperCase(); - private static final UnitXCConfigBuildTypeMultiples TYPE_FAHRENHEIT_CONFIG = new UnitXCConfigBuildTypeMultiples( - TYPE_FAHRENHEIT_ID,TYPE_FAHRENHEIT_NAME,TYPE_FAHRENHEIT_FLAG, - new UnitXConverterStep[]{add(459.67),multiplyFraction(5,9)}, - new UnitXConverterStep[]{multiplyFraction(9,5),substract(459.67)} - ); + private static final double TYPE_FAHRENHEIT_OFFSET = 459.67; + private static final Fraction TYPE_FAHRENHEIT_FRACTION = Fraction.getFraction(5,9); public static final String TYPE_RANKINE_ID = "°R"; public static final String TYPE_RANKINE_NAME = "rankine"; public static final String TYPE_RANKINE_FLAG = (GROUP_ID+"_"+TYPE_RANKINE_NAME).toUpperCase(); - private static final UnitXCConfigBuildTypeMultiples TYPE_RANKINE_CONFIG = new UnitXCConfigBuildTypeMultiples( - TYPE_RANKINE_ID,TYPE_RANKINE_NAME,TYPE_RANKINE_FLAG, - new UnitXConverterStep[]{multiplyFraction(5,9)}, - new UnitXConverterStep[]{multiplyFraction(9,5)} - ); public static final String TYPE_ROMER_ID = "Rø"; public static final String TYPE_ROMER_NAME = "rømer"; public static final String TYPE_ROMER_FLAG = GROUP_ID.toUpperCase()+"_ROMER"; - private static final UnitXCConfigBuildTypeMultiples TYPE_ROMER_CONFIG = new UnitXCConfigBuildTypeMultiples( - TYPE_ROMER_ID,TYPE_ROMER_NAME,TYPE_ROMER_FLAG, - new UnitXConverterStep[]{add(273.15),multiplyFraction(40,21),add(7.5)}, - new UnitXConverterStep[]{substract(7.5),multiplyFraction(21,40),substract(273.15)} - ); + private static final Fraction TYPE_ROMER_FRACTION = Fraction.getFraction(40,21); + private static final double TYPE_ROMER_FRACTION_OFFSET = 7.5; @Override - public void configModule(UnitXCConfigModuleBuilder builder) { - builder.addUnitTypeGroup( GROUP_ID, TYPE_KELVIN_ID, TYPE_KELVIN_NAME, GROUP_DESCRIPTION); - builder.addSIMultiples( GROUP_ID, TYPE_KELVIN_ID, TYPE_KELVIN_NAME, TYPE_KELVIN_FLAG); - builder.addSIMultiplesSilly(GROUP_ID, TYPE_CELSIUS_CONFIG); - builder.addSIMultiplesSilly(GROUP_ID, TYPE_FAHRENHEIT_CONFIG); - builder.addSIMultiplesSilly(GROUP_ID, TYPE_RANKINE_CONFIG); - builder.addSIMultiplesSilly(GROUP_ID, TYPE_ROMER_CONFIG); + public void configModule(UnitXCConfigBuilder builder) { + builder.createUnitTypeGroup() + .setId( GROUP_ID) + .setName( TYPE_KELVIN_NAME) + .setDescription( GROUP_DESCRIPTION) + .setBaseTypeId( TYPE_KELVIN_ID) + .createSIUnitTypes() + .setId( TYPE_KELVIN_ID) + .setName( TYPE_KELVIN_NAME) + .addTypeFlag( TYPE_KELVIN_FLAG) + .build() + .createSIUnitTypes() + .setId( TYPE_CELSIUS_ID) + .setName( TYPE_CELSIUS_NAME) + .addTypeFlag( TYPE_CELSIUS_FLAG) + .createToBaseConverterSteps().add(TYPE_CELSIUS_OFFSET).build() + .createFromBaseConverterSteps().sub(TYPE_CELSIUS_OFFSET).build() + .build() + .createSIUnitTypes() + .setId( TYPE_FAHRENHEIT_ID) + .setName( TYPE_FAHRENHEIT_NAME) + .addTypeFlag( TYPE_FAHRENHEIT_FLAG) + .createToBaseConverterSteps() + .add(TYPE_FAHRENHEIT_OFFSET) + .multiplyFraction(TYPE_FAHRENHEIT_FRACTION) + .build() + .createFromBaseConverterSteps() + .multiplyFraction(TYPE_FAHRENHEIT_FRACTION.invert()) + .substract(TYPE_FAHRENHEIT_OFFSET) + .build() + .build() + .createSIUnitTypes() + .setId( TYPE_RANKINE_ID) + .setName( TYPE_RANKINE_NAME) + .addTypeFlag( TYPE_RANKINE_FLAG) + .createToBaseConverterSteps().multiplyFraction(TYPE_FAHRENHEIT_FRACTION).build() + .createFromBaseConverterSteps().multiplyFraction(TYPE_FAHRENHEIT_FRACTION.invert()).build() + .build() + .createSIUnitTypes() + .setId( TYPE_ROMER_ID) + .setName( TYPE_ROMER_NAME) + .addTypeFlag( TYPE_ROMER_FLAG) + .createToBaseConverterSteps() + .add(TYPE_CELSIUS_OFFSET) + .multiplyFraction(TYPE_ROMER_FRACTION) + .add(TYPE_ROMER_FRACTION_OFFSET) + .build() + .createFromBaseConverterSteps() + .sub(TYPE_ROMER_FRACTION_OFFSET) + .multiplyFraction(TYPE_ROMER_FRACTION.invert()) + .substract(TYPE_CELSIUS_OFFSET) + .build() + .build() + .build() + ; } } 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 new file mode 100644 index 0000000..c7d9844 --- /dev/null +++ b/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/UnitXCManagerTest.java @@ -0,0 +1,129 @@ +package net.forwardfire.unitxc; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; + +import org.junit.Test; + +import net.forwardfire.unitxc.module.UnitXCTemperatureModule; + +public class UnitXCManagerTest { + + private UnitXCManager test = UnitXCFactory.createManager(); + + @Test + public void testGetConverter() { + assertNotNull(test.getConverter()); + } + + @Test + public void testGetUnitType() { + assertNotNull(test.getUnitType(UnitXCTemperatureModule.TYPE_KELVIN_ID)); + } + + @Test(expected=NullPointerException.class) + public void testGetUnitTypeNull() { + assertNotNull(test.getUnitType(null)); + } + + @Test(expected=NullPointerException.class) + public void testGetUnitTypeInvalid() { + assertNotNull(test.getUnitType(getClass().getName())); + } + + @Test + public void testGetUnitTypeResultId() { + assertNotNull(test.getUnitType(UnitXCTemperatureModule.TYPE_KELVIN_ID).getId()); + } + + @Test + public void testGetUnitTypeResultName() { + assertNotNull(test.getUnitType(UnitXCTemperatureModule.TYPE_KELVIN_ID).getName()); + } + + @Test + public void testGetUnitTypeResultTypeFlags() { + assertNotNull(test.getUnitType(UnitXCTemperatureModule.TYPE_KELVIN_ID).getTypeFlags()); + } + + @Test + public void testGetUnitTypeResultTypeGroupId() { + assertNotNull(test.getUnitType(UnitXCTemperatureModule.TYPE_KELVIN_ID).getTypeGroupId()); + } + + @Test + public void testGetUnitTypeGroup() { + assertNotNull(test.getUnitTypeGroup(UnitXCTemperatureModule.GROUP_ID)); + } + + @Test(expected=NullPointerException.class) + public void testGetUnitTypeGroupNull() { + assertNotNull(test.getUnitTypeGroup(null)); + } + + @Test(expected=NullPointerException.class) + public void testGetUnitTypeGroupInvalid() { + assertNotNull(test.getUnitTypeGroup(getClass().getName())); + } + + @Test + public void testGetUnitTypeGroupResultId() { + assertNotNull(test.getUnitTypeGroup(UnitXCTemperatureModule.GROUP_ID).getId()); + } + + @Test + public void testGetUnitTypeGroupResultName() { + assertNotNull(test.getUnitTypeGroup(UnitXCTemperatureModule.GROUP_ID).getName()); + } + + @Test + public void testGetUnitTypeGroupResultDescription() { + assertNotNull(test.getUnitTypeGroup(UnitXCTemperatureModule.GROUP_ID).getDescription()); + } + + @Test + public void testGetUnitTypeResultTypeBaseTypeID() { + assertNotNull(test.getUnitTypeGroup(UnitXCTemperatureModule.GROUP_ID).getBaseTypeId()); + } + + @Test + public void testIsUnitTypeGroupTrue() { + assertTrue(test.isUnitTypeGroup(UnitXCTemperatureModule.GROUP_ID)); + } + + @Test + public void testIsUnitTypeGroupFalse() { + assertFalse(test.isUnitTypeGroup(getClass().getName())); + } + + @Test(expected=NullPointerException.class) + public void testIsUnitTypeGroupNull() { + assertTrue(test.isUnitTypeGroup(null)); + } + + @Test(expected=IllegalArgumentException.class) + public void testIsUnitTypeGroupEmpty() { + assertTrue(test.isUnitTypeGroup("")); + } + + @Test + public void testIsUnitTypeTrue() { + assertTrue(test.isUnitType(UnitXCTemperatureModule.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() { + assertTrue(test.isUnitTypeGroup("")); + } +} diff --git a/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/module/UnitXCTemperatureModuleTest.java b/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/module/UnitXCTemperatureModuleTest.java index 999bfb3..433591b 100644 --- a/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/module/UnitXCTemperatureModuleTest.java +++ b/ff-unitxc-converter/src/test/java/net/forwardfire/unitxc/module/UnitXCTemperatureModuleTest.java @@ -29,25 +29,16 @@ import static org.junit.Assert.assertNotNull; import org.junit.Test; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertEquals; import static net.forwardfire.unitxc.UnitXCAssert.convertEquals; import net.forwardfire.unitxc.UnitXCFactory; import net.forwardfire.unitxc.UnitXCManager; -import net.forwardfire.unitxc.converter.UnitXConverter; import net.forwardfire.unitxc.model.UnitXCType; import net.forwardfire.unitxc.model.UnitXConverterResult; import net.forwardfire.unitxc.model.UnitXConverterResultStep; public class UnitXCTemperatureModuleTest { - - private final UnitXConverter um; - private final double VALUE_DELTA = 0; - - public UnitXCTemperatureModuleTest() { - um = UnitXCFactory.createManager().getConverter(); - } @Test public void testFac() { @@ -64,9 +55,11 @@ public class UnitXCTemperatureModuleTest { for (UnitXCType type:unitManager.getUnitTypes()) { System.out.println("type: "+type.getId()+"\t name: "+type.getName()+"\t\t flags: "+type.getTypeFlags()); } - System.out.println("totalTypes: "+unitManager.getUnitTypes().size()); + int ts = unitManager.getUnitTypes().size(); + System.out.println("totalTypes: "+ts); + System.out.println("totalConv: "+(ts*ts-ts)); - UnitXConverterResult result = um.convertStepped(211.0, "k°F", "m°C"); + UnitXConverterResult result = unitManager.getConverter().convertStepped(211.0, "k°F", "m°C"); System.out.println("Convert from: "+result.getStartType().getName()+" to: "+result.getResultType().getName()); System.out.println("startValue: "+result.getStartValue()); System.out.println("resultValue: "+result.getResultValue()); @@ -77,7 +70,7 @@ public class UnitXCTemperatureModuleTest { System.out.println(""); - result = um.convertStepped(100.0, "K", "°C"); + result = unitManager.getConverter().convertStepped(100.0, "K", "°C"); System.out.println("Convert from: "+result.getStartType().getName()+" to: "+result.getResultType().getName()); System.out.println("startValue: "+result.getStartValue()); System.out.println("resultValue: "+result.getResultValue()); @@ -89,21 +82,19 @@ public class UnitXCTemperatureModuleTest { @Test public void testConverterTempKvsC() throws Exception { - assertEquals(26.85,um.convert(300.0, "K", "°C"),VALUE_DELTA); - assertEquals(310.65,um.convert(37.5, "°C", "K"),VALUE_DELTA); + convertEquals(26.85,"°C",300.0, "K"); + convertEquals(310.65,"K",37.5, "°C"); } @Test public void testConverterTempKvsF() throws Exception { - assertEquals(80.32,um.convert(300.0, "K", "°F"),VALUE_DELTA); - assertEquals(276.2,um.convert(37.5, "°F", "K"),VALUE_DELTA); - assertEquals(276.208,um.convert(37.505, "°F", "K"),VALUE_DELTA); + convertEquals(80.32,"°F",300.0, "K"); + convertEquals(276.2,"K",37.5, "°F"); + convertEquals(276.208,"K",37.505, "°F"); } @Test public void testConverterTempFvsC() throws Exception { - //assertEquals(74.937,um.convert(166.888, "°F", "°C"),VALUE_DELTA); - //assertEquals(211.99,um.convert(100.0, "°C", "°F"),VALUE_DELTA); convertEquals(74.937,"°C",166.888, "°F"); convertEquals(211.99,"°F",100.0, "°C"); }