Added builders

This commit is contained in:
Willem 2015-10-23 21:32:01 +02:00
parent 34c339eee7
commit 9ac4acae7b
21 changed files with 843 additions and 265 deletions

View file

@ -30,10 +30,6 @@ import java.util.List;
import net.forwardfire.unitxc.config.UnitXCConfig; import net.forwardfire.unitxc.config.UnitXCConfig;
import net.forwardfire.unitxc.config.UnitXCConfigManager; import net.forwardfire.unitxc.config.UnitXCConfigManager;
import net.forwardfire.unitxc.config.UnitXCConfigModule; 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.UnitXCElectricCurrentModule;
import net.forwardfire.unitxc.module.UnitXCTemperatureModule; import net.forwardfire.unitxc.module.UnitXCTemperatureModule;
@ -64,25 +60,4 @@ public final class UnitXCFactory {
public static UnitXCManager createManager(UnitXCConfig config) { public static UnitXCManager createManager(UnitXCConfig config) {
return new UnitXCConfigManager(config); return new UnitXCConfigManager(config);
} }
public static class MathStep {
public static UnitXCTypeOffsetConverterStep add(double offset) {
return offset(offset,true);
}
public static UnitXCTypeOffsetConverterStep substract(double offset) {
return offset(offset,false);
}
public static UnitXCTypeOffsetConverterStep offset(double offset,boolean offsetPositive) {
return new UnitXCTypeOffsetConverterStep(offset,offsetPositive);
}
public static UnitXCTypeMultiplyFractionConverterStep multiplyFraction(int numerator,int denominator) {
return new UnitXCTypeMultiplyFractionConverterStep(numerator,denominator);
}
public static UnitXCTypeMultiplyConverterStep multiply(double factor) {
return new UnitXCTypeMultiplyConverterStep(factor);
}
public static UnitXCTypeDevideConverterStep divide(double factor) {
return new UnitXCTypeDevideConverterStep(factor);
}
}
} }

View file

@ -54,7 +54,7 @@ public final class UnitXCConfig {
} }
private UnitXCTypeModel validateGroupId(UnitXCTypeModel unitType) { private UnitXCTypeModel validateGroupId(UnitXCTypeModel unitType) {
Validate.isTrue(unitTypeGroups.containsKey(unitType.getTypeGroupId())); //Validate.isTrue(unitTypeGroups.containsKey(unitType.getTypeGroupId()),"group is missing");
return unitType; return unitType;
} }

View file

@ -80,7 +80,7 @@ public class UnitXCConfigManager implements UnitXCManager {
@Override @Override
public boolean isUnitType(String id) { public boolean isUnitType(String id) {
return unitTypes.containsKey(id); return unitTypes.containsKey(Validate.notBlank(id));
} }
@Override @Override
@ -95,7 +95,7 @@ public class UnitXCConfigManager implements UnitXCManager {
@Override @Override
public boolean isUnitTypeGroup(String id) { public boolean isUnitTypeGroup(String id) {
return unitTypeGroups.containsKey(id); return unitTypeGroups.containsKey(Validate.notBlank(id));
} }
@Override @Override

View file

@ -25,12 +25,14 @@ package net.forwardfire.unitxc.config;
import java.util.List; import java.util.List;
import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
public interface UnitXCConfigModule { public interface UnitXCConfigModule {
void configModule(UnitXCConfigModuleBuilder config); void configModule(UnitXCConfigBuilder config);
static UnitXCConfig buildAll(UnitXCConfig config,List<UnitXCConfigModule> configInit) { static UnitXCConfig buildAll(UnitXCConfig config,List<UnitXCConfigModule> configInit) {
UnitXCConfigModuleBuilder builder = new UnitXCConfigModuleBuilder(config); UnitXCConfigBuilder builder = new UnitXCConfigBuilder(config);
configInit.forEach(ci -> ci.configModule(builder)); configInit.forEach(ci -> ci.configModule(builder));
return config; return config;
} }

View file

@ -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<UnitXConverterStep> toConverters,List<UnitXConverterStep> fromConverters,String groupId,String id,int factorExp,String name,List<String> typeFlags) {
UnitXCTypeModel result = new UnitXCTypeModel();
result.setId(id);
result.setName(name);
if (factorExp != 0) {
result.addToBaseConverterStep(new UnitXCTypeExponentConverterStep(factorExp,false));
}
toConverters.forEach((conv) ->result.addToBaseConverterStep(conv));
fromConverters.forEach((conv) ->result.addFromBaseConverterStep(conv));
if (factorExp != 0) {
result.addFromBaseConverterStep(new UnitXCTypeExponentConverterStep(factorExp,true));
}
result.setTypeGroupId(groupId);
typeFlags.forEach((flag) -> result.addTypeFlag(flag));
config.addUnitType(result);
}
public enum SiMultiple {
YOTTA ("Y", 24),
ZETTA ("Z", 21),
EXA ("E", 18),
PETA ("P", 15),
TERA ("T", 12),
GIGA ("G", 9),
MEGA ("M", 6),
KILO ("k", 3),
HECTO ("h", 2),
DECA ("da",1),
DECI ("d", -1),
CENTI ("c", -2),
MILLI ("m", -3),
MICRO ("µ", -6),
NANO ("n", -9),
PICO ("p", -12),
FEMTO ("f", -15),
ATTO ("a", -18),
ZEPTO ("z", -21),
YOCTO ("y", -24),
;
public static final String TYPE_FLAG_SI_UNIT = "SI_UNIT";
public static final String TYPE_FLAG_SI_UNIT_BIGGER = "SI_UNIT_BIGGER";
public static final String TYPE_FLAG_SI_UNIT_BIGGEST = "SI_UNIT_BIGGEST";
private final String id;
private final String name;
private final int exponent;
private final List<String> flags;
private SiMultiple(String id,int exponent) {
this.id=id;
this.name = this.name().toLowerCase();
this.exponent=exponent;
List<String> f = new ArrayList<>(3);
f.add(TYPE_FLAG_SI_UNIT);
if (exponent > 3 || exponent < -3) {
f.add(TYPE_FLAG_SI_UNIT_BIGGER);
}
if (exponent > 12 || exponent < -12) {
f.add(TYPE_FLAG_SI_UNIT_BIGGEST);
}
this.flags = Collections.unmodifiableList(f);
}
public String getPrefixId() {
return id;
}
public String getPrefixName() {
return name;
}
public int getExponent() {
return exponent;
}
public List<String> getFlags() {
return flags;
}
}
public static final String TYPE_FLAG_SI_UNIT_SILLY = "SI_UNIT_SILLY";
public void addSIMultiples(String groupId,String type,String typeName,String flag) {
List<String> flags = new ArrayList<>();
flags.add(flag);
createTypesSIMultiples(groupId,type,typeName,flags,new ArrayList<>(),new ArrayList<>(),false);
}
public void addSIMultiplesSilly(String groupId,UnitXCConfigBuildTypeMultiples builder) {
List<String> flags = new ArrayList<>();
flags.add(builder.getFlag());
createTypesSIMultiples(groupId,builder.getType(),builder.getTypeName(),flags,Arrays.asList(builder.getToConverters()),Arrays.asList(builder.getFromConverters()),true);
}
private void createTypesSIMultiples(String groupId,String type,String typeName,List<String> flags,List<UnitXConverterStep> toConverters,List<UnitXConverterStep> fromConverters,boolean addSilly) {
// Create base unit
addUnitType(toConverters,fromConverters,groupId,type,0,typeName,flags);
// Create si values
for (SiMultiple sim:SiMultiple.values()) {
List<String> typeFlags = new ArrayList<>();
typeFlags.addAll(flags);
typeFlags.addAll(sim.getFlags());
if (addSilly) {
typeFlags.add(TYPE_FLAG_SI_UNIT_SILLY);
}
addUnitType(toConverters,fromConverters, groupId, sim.getPrefixId()+type, sim.getExponent(), sim.getPrefixName()+typeName, typeFlags);
}
}
}

View file

@ -0,0 +1,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 <P> The parent builder.
* @param <T> The object to build.
* @param <B> This builder.
* @author Willem Cazander
* @version 1.0 May 29, 2015
*/
public abstract class AbstractUnitXCBuilder<P,T,B> implements Builder<P> {
private final P parent;
private final T value;
private final BiConsumer<P, T> parentBuilder;
/**
* Creates the builder.
* @param parent The parent builder.
* @param value The object to build.
*/
public AbstractUnitXCBuilder(P parent,T value,BiConsumer<P, T> 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<T> mixer) {
mixer.accept(getValue());
return getBuilder();
}
}

View file

@ -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<P,B> extends AbstractUnitXCBuilder<P,UnitXCTypeModel,B> {
public AbstractUnitXCTypeBuilder(P parent,BiConsumer<P, UnitXCTypeModel> 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<String> flags) {
return make(v -> flags.forEach(flag -> v.addTypeFlag(flag)));
}
public B addTypeFlags(String[] flags) {
return addTypeFlags(Arrays.asList(flags));
}
public UnitXConverterStepBuilder<AbstractUnitXCTypeBuilder<P,B>> createFromBaseConverterSteps() {
return new UnitXConverterStepBuilder<>(this,(p,v) -> p.addFromBaseConverterSteps(v));
}
public UnitXConverterStepBuilder<AbstractUnitXCTypeBuilder<P,B>> 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<UnitXConverterStep> 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<UnitXConverterStep> unitTypeConverters) {
return make((v) -> unitTypeConverters.forEach(c -> v.addToBaseConverterStep(c)));
}
}

View file

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

View file

@ -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<P> extends AbstractUnitXCTypeBuilder<P,UnitXCTypeBuilder<P>> {
public UnitXCTypeBuilder(P parent, BiConsumer<P, UnitXCTypeModel> parentBuilder) {
super(parent, parentBuilder);
}
@Override
protected UnitXCTypeBuilder<P> getBuilder() {
return this;
}
}

View file

@ -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<UnitXCConfigBuilder,UnitXCTypeGroupModel,UnitXCTypeGroupBuilder> {
public UnitXCTypeGroupBuilder(UnitXCConfigBuilder parent) {
super(parent, new UnitXCTypeGroupModel(), (p,v) -> p.getConfig().addUnitTypeGroup(v));
}
@Override
protected UnitXCTypeGroupBuilder getBuilder() {
return this;
}
public UnitXCTypeBuilder<UnitXCTypeGroupBuilder> createUnitType() {
return new UnitXCTypeBuilder<>(this,(p,v) -> p.getParent().getConfig().addUnitType(v)).setTypeGroupId(getValue().getId());
}
public UnitXCTypeSIPrefixBuilder<UnitXCTypeGroupBuilder> 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));
}
}

View file

@ -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<String> 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<String> f = new ArrayList<>(3);
f.add(TYPE_FLAG_SI_UNIT);
if (exponent > 3 || exponent < -3) {
f.add(TYPE_FLAG_SI_UNIT_BIGGER);
}
if (exponent > 12 || exponent < -12) {
f.add(TYPE_FLAG_SI_UNIT_BIGGEST);
}
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<String> getFlags() {
return flags;
}
}

View file

@ -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<P> extends AbstractUnitXCTypeBuilder<P,UnitXCTypeSIPrefixBuilder<P>> {
private final UnitXCConfigBuilder builder;
private final List<UnitXCTypeSIPrefix> commonSIPrefixes;
public UnitXCTypeSIPrefixBuilder(P parent, UnitXCConfigBuilder builder) {
super(parent, (p,v) -> {});
this.builder = builder;
this.commonSIPrefixes = new ArrayList<>();
}
@Override
protected UnitXCTypeSIPrefixBuilder<P> 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<String> 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<P> addCommonPrefix(UnitXCTypeSIPrefix prefix) {
return make((v) -> commonSIPrefixes.add(prefix));
}
}

View file

@ -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<P> extends AbstractUnitXCBuilder<P,List<UnitXConverterStep>,UnitXConverterStepBuilder<P>> {
public UnitXConverterStepBuilder(P parent, BiConsumer<P, List<UnitXConverterStep>> parentBuilder) {
super(parent, new ArrayList<>(), parentBuilder);
}
@Override
protected UnitXConverterStepBuilder<P> getBuilder() {
return this;
}
public UnitXConverterStepBuilder<P> add(double offset) {
return offset(offset,true);
}
public UnitXConverterStepBuilder<P> sub(double offset) {
return offset(offset,false);
}
public UnitXConverterStepBuilder<P> substract(double offset) {
return offset(offset,false);
}
public UnitXConverterStepBuilder<P> offset(double offset,boolean offsetPositive) {
return make(v -> v.add(new UnitXCTypeOffsetConverterStep(offset,offsetPositive)));
}
public UnitXConverterStepBuilder<P> multiplyFraction(Fraction fraction) {
return make(v -> v.add(new UnitXCTypeMultiplyFractionConverterStep(fraction)));
}
public UnitXConverterStepBuilder<P> multiplyFraction(int numerator,int denominator) {
return make(v -> v.add(new UnitXCTypeMultiplyFractionConverterStep(numerator,denominator)));
}
public UnitXConverterStepBuilder<P> multiply(double factor) {
return make(v -> v.add(new UnitXCTypeMultiplyConverterStep(factor)));
}
public UnitXConverterStepBuilder<P> divide(double factor) {
return make(v -> v.add(new UnitXCTypeDevideConverterStep(factor)));
}
}

View file

@ -25,7 +25,7 @@ package net.forwardfire.unitxc.converter.step;
public class UnitXCTypeMultiplyConverterStep extends AbstractUnitXConverterStep { public class UnitXCTypeMultiplyConverterStep extends AbstractUnitXConverterStep {
private final static String STEP_NAME = "Factor Converter"; private final static String STEP_NAME = "Multiply";
private final double factor; private final double factor;
public UnitXCTypeMultiplyConverterStep(double factor) { public UnitXCTypeMultiplyConverterStep(double factor) {

View file

@ -23,25 +23,32 @@
package net.forwardfire.unitxc.converter.step; package net.forwardfire.unitxc.converter.step;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.math.Fraction;
public class UnitXCTypeMultiplyFractionConverterStep extends AbstractUnitXConverterStep { public class UnitXCTypeMultiplyFractionConverterStep extends AbstractUnitXConverterStep {
private final static String STEP_NAME = "FactorFaction"; private final static String STEP_NAME = "MultiplyFaction";
private final int numerator; private final Fraction fraction;
private final int denominator; private final String fractionMath;
public UnitXCTypeMultiplyFractionConverterStep(int numerator,int denominator) { public UnitXCTypeMultiplyFractionConverterStep(int numerator,int denominator) {
this(Fraction.getFraction(numerator, denominator));
}
public UnitXCTypeMultiplyFractionConverterStep(Fraction fraction) {
super(STEP_NAME); super(STEP_NAME);
this.numerator = numerator; this.fraction = Validate.notNull(fraction);
this.denominator = denominator; this.fractionMath = String.format("*(%1s/%2s)", fraction.getNumerator(),fraction.getDenominator());
} }
@Override @Override
public double convert(double value) { public double convert(double value) {
return value*((double)numerator/(double)denominator); return value*fraction.doubleValue();
} }
@Override @Override
public String getMathExpression() { public String getMathExpression() {
return "*("+numerator+"/"+denominator+")"; return fractionMath;
} }
} }

View file

@ -43,10 +43,10 @@ public class UnitXCTypeGroupModel implements UnitXCTypeGroup {
} }
public UnitXCTypeGroupModel validate() { public UnitXCTypeGroupModel validate() {
Validate.notBlank(id); Validate.notBlank(id,"The id is blank");
Validate.notBlank(name); Validate.notBlank(name,"The name is blank");
Validate.notBlank(description); Validate.notBlank(description,"The description is blank");
Validate.notBlank(baseTypeId); Validate.notBlank(baseTypeId,"The baseTypeId is blank");
return this; return this;
} }

View file

@ -47,9 +47,9 @@ public class UnitXCTypeModel implements UnitXCType {
} }
public UnitXCTypeModel validate() { public UnitXCTypeModel validate() {
Validate.notBlank(id); Validate.notBlank(id,"The id is blank");
Validate.notBlank(name); Validate.notBlank(name,"The name is blank");
Validate.notBlank(typeGroupId); Validate.notBlank(typeGroupId,"The typeGroupId is blank");
return this; return this;
} }

View file

@ -24,7 +24,8 @@
package net.forwardfire.unitxc.module; package net.forwardfire.unitxc.module;
import net.forwardfire.unitxc.config.UnitXCConfigModule; 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 { 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(); public static final String TYPE_AMPERE_FLAG = (GROUP_ID+"_"+TYPE_AMPERE_NAME).toUpperCase();
@Override @Override
public void configModule(UnitXCConfigModuleBuilder builder) { public void configModule(UnitXCConfigBuilder builder) {
builder.addUnitTypeGroup( GROUP_ID, TYPE_AMPERE_ID, TYPE_AMPERE_NAME, GROUP_DESCRIPTION ); builder.createUnitTypeGroup()
builder.addSIMultiples( GROUP_ID, TYPE_AMPERE_ID, TYPE_AMPERE_NAME, TYPE_AMPERE_FLAG ); .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()
;
} }
} }

View file

@ -23,13 +23,10 @@
package net.forwardfire.unitxc.module; package net.forwardfire.unitxc.module;
import net.forwardfire.unitxc.config.UnitXCConfigBuildTypeMultiples;
import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.UnitXCConfigModuleBuilder; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
import net.forwardfire.unitxc.converter.step.UnitXConverterStep;
import static net.forwardfire.unitxc.UnitXCFactory.MathStep.add; import org.apache.commons.lang3.math.Fraction;
import static net.forwardfire.unitxc.UnitXCFactory.MathStep.substract;
import static net.forwardfire.unitxc.UnitXCFactory.MathStep.multiplyFraction;
public class UnitXCTemperatureModule implements UnitXCConfigModule { 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_ID = "°C";
public static final String TYPE_CELSIUS_NAME = "celsius"; public static final String TYPE_CELSIUS_NAME = "celsius";
public static final String TYPE_CELSIUS_FLAG = (GROUP_ID+"_"+TYPE_CELSIUS_NAME).toUpperCase(); public static final String TYPE_CELSIUS_FLAG = (GROUP_ID+"_"+TYPE_CELSIUS_NAME).toUpperCase();
private static final UnitXCConfigBuildTypeMultiples TYPE_CELSIUS_CONFIG = new UnitXCConfigBuildTypeMultiples( private static final double TYPE_CELSIUS_OFFSET = 273.15;
TYPE_CELSIUS_ID,TYPE_CELSIUS_NAME,TYPE_CELSIUS_FLAG,
new UnitXConverterStep[]{add(273.15)},
new UnitXConverterStep[]{substract(273.15)}
);
public static final String TYPE_FAHRENHEIT_ID = "°F"; public static final String TYPE_FAHRENHEIT_ID = "°F";
public static final String TYPE_FAHRENHEIT_NAME = "fahrenheit"; public static final String TYPE_FAHRENHEIT_NAME = "fahrenheit";
public static final String TYPE_FAHRENHEIT_FLAG = (GROUP_ID+"_"+TYPE_FAHRENHEIT_NAME).toUpperCase(); public static final String TYPE_FAHRENHEIT_FLAG = (GROUP_ID+"_"+TYPE_FAHRENHEIT_NAME).toUpperCase();
private static final UnitXCConfigBuildTypeMultiples TYPE_FAHRENHEIT_CONFIG = new UnitXCConfigBuildTypeMultiples( private static final double TYPE_FAHRENHEIT_OFFSET = 459.67;
TYPE_FAHRENHEIT_ID,TYPE_FAHRENHEIT_NAME,TYPE_FAHRENHEIT_FLAG, private static final Fraction TYPE_FAHRENHEIT_FRACTION = Fraction.getFraction(5,9);
new UnitXConverterStep[]{add(459.67),multiplyFraction(5,9)},
new UnitXConverterStep[]{multiplyFraction(9,5),substract(459.67)}
);
public static final String TYPE_RANKINE_ID = "°R"; public static final String TYPE_RANKINE_ID = "°R";
public static final String TYPE_RANKINE_NAME = "rankine"; public static final String TYPE_RANKINE_NAME = "rankine";
public static final String TYPE_RANKINE_FLAG = (GROUP_ID+"_"+TYPE_RANKINE_NAME).toUpperCase(); public static final String TYPE_RANKINE_FLAG = (GROUP_ID+"_"+TYPE_RANKINE_NAME).toUpperCase();
private static final UnitXCConfigBuildTypeMultiples TYPE_RANKINE_CONFIG = new UnitXCConfigBuildTypeMultiples(
TYPE_RANKINE_ID,TYPE_RANKINE_NAME,TYPE_RANKINE_FLAG,
new UnitXConverterStep[]{multiplyFraction(5,9)},
new UnitXConverterStep[]{multiplyFraction(9,5)}
);
public static final String TYPE_ROMER_ID = ""; public static final String TYPE_ROMER_ID = "";
public static final String TYPE_ROMER_NAME = "rømer"; public static final String TYPE_ROMER_NAME = "rømer";
public static final String TYPE_ROMER_FLAG = GROUP_ID.toUpperCase()+"_ROMER"; public static final String TYPE_ROMER_FLAG = GROUP_ID.toUpperCase()+"_ROMER";
private static final UnitXCConfigBuildTypeMultiples TYPE_ROMER_CONFIG = new UnitXCConfigBuildTypeMultiples( private static final Fraction TYPE_ROMER_FRACTION = Fraction.getFraction(40,21);
TYPE_ROMER_ID,TYPE_ROMER_NAME,TYPE_ROMER_FLAG, private static final double TYPE_ROMER_FRACTION_OFFSET = 7.5;
new UnitXConverterStep[]{add(273.15),multiplyFraction(40,21),add(7.5)},
new UnitXConverterStep[]{substract(7.5),multiplyFraction(21,40),substract(273.15)}
);
@Override @Override
public void configModule(UnitXCConfigModuleBuilder builder) { public void configModule(UnitXCConfigBuilder builder) {
builder.addUnitTypeGroup( GROUP_ID, TYPE_KELVIN_ID, TYPE_KELVIN_NAME, GROUP_DESCRIPTION); builder.createUnitTypeGroup()
builder.addSIMultiples( GROUP_ID, TYPE_KELVIN_ID, TYPE_KELVIN_NAME, TYPE_KELVIN_FLAG); .setId( GROUP_ID)
builder.addSIMultiplesSilly(GROUP_ID, TYPE_CELSIUS_CONFIG); .setName( TYPE_KELVIN_NAME)
builder.addSIMultiplesSilly(GROUP_ID, TYPE_FAHRENHEIT_CONFIG); .setDescription( GROUP_DESCRIPTION)
builder.addSIMultiplesSilly(GROUP_ID, TYPE_RANKINE_CONFIG); .setBaseTypeId( TYPE_KELVIN_ID)
builder.addSIMultiplesSilly(GROUP_ID, TYPE_ROMER_CONFIG); .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()
;
} }
} }

View file

@ -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(""));
}
}

View file

@ -29,25 +29,16 @@ import static org.junit.Assert.assertNotNull;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertEquals;
import static net.forwardfire.unitxc.UnitXCAssert.convertEquals; import static net.forwardfire.unitxc.UnitXCAssert.convertEquals;
import net.forwardfire.unitxc.UnitXCFactory; import net.forwardfire.unitxc.UnitXCFactory;
import net.forwardfire.unitxc.UnitXCManager; import net.forwardfire.unitxc.UnitXCManager;
import net.forwardfire.unitxc.converter.UnitXConverter;
import net.forwardfire.unitxc.model.UnitXCType; import net.forwardfire.unitxc.model.UnitXCType;
import net.forwardfire.unitxc.model.UnitXConverterResult; import net.forwardfire.unitxc.model.UnitXConverterResult;
import net.forwardfire.unitxc.model.UnitXConverterResultStep; import net.forwardfire.unitxc.model.UnitXConverterResultStep;
public class UnitXCTemperatureModuleTest { public class UnitXCTemperatureModuleTest {
private final UnitXConverter um;
private final double VALUE_DELTA = 0;
public UnitXCTemperatureModuleTest() {
um = UnitXCFactory.createManager().getConverter();
}
@Test @Test
public void testFac() { public void testFac() {
@ -64,9 +55,11 @@ public class UnitXCTemperatureModuleTest {
for (UnitXCType type:unitManager.getUnitTypes()) { for (UnitXCType type:unitManager.getUnitTypes()) {
System.out.println("type: "+type.getId()+"\t name: "+type.getName()+"\t\t flags: "+type.getTypeFlags()); System.out.println("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("Convert from: "+result.getStartType().getName()+" to: "+result.getResultType().getName());
System.out.println("startValue: "+result.getStartValue()); System.out.println("startValue: "+result.getStartValue());
System.out.println("resultValue: "+result.getResultValue()); System.out.println("resultValue: "+result.getResultValue());
@ -77,7 +70,7 @@ public class UnitXCTemperatureModuleTest {
System.out.println(""); 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("Convert from: "+result.getStartType().getName()+" to: "+result.getResultType().getName());
System.out.println("startValue: "+result.getStartValue()); System.out.println("startValue: "+result.getStartValue());
System.out.println("resultValue: "+result.getResultValue()); System.out.println("resultValue: "+result.getResultValue());
@ -89,21 +82,19 @@ public class UnitXCTemperatureModuleTest {
@Test @Test
public void testConverterTempKvsC() throws Exception { public void testConverterTempKvsC() throws Exception {
assertEquals(26.85,um.convert(300.0, "K", "°C"),VALUE_DELTA); convertEquals(26.85,"°C",300.0, "K");
assertEquals(310.65,um.convert(37.5, "°C", "K"),VALUE_DELTA); convertEquals(310.65,"K",37.5, "°C");
} }
@Test @Test
public void testConverterTempKvsF() throws Exception { public void testConverterTempKvsF() throws Exception {
assertEquals(80.32,um.convert(300.0, "K", "°F"),VALUE_DELTA); convertEquals(80.32,"°F",300.0, "K");
assertEquals(276.2,um.convert(37.5, "°F", "K"),VALUE_DELTA); convertEquals(276.2,"K",37.5, "°F");
assertEquals(276.208,um.convert(37.505, "°F", "K"),VALUE_DELTA); convertEquals(276.208,"K",37.505, "°F");
} }
@Test @Test
public void testConverterTempFvsC() throws Exception { 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(74.937,"°C",166.888, "°F");
convertEquals(211.99,"°F",100.0, "°C"); convertEquals(211.99,"°F",100.0, "°C");
} }