Added area and volume groups.

This commit is contained in:
Willem 2015-11-03 15:44:56 +01:00
parent 7e521b18d9
commit ca7c918541
42 changed files with 1326 additions and 217 deletions

View file

@ -31,10 +31,14 @@ 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.module.UnitXCModuleAmountOfSubstance; import net.forwardfire.unitxc.module.UnitXCModuleAmountOfSubstance;
import net.forwardfire.unitxc.module.UnitXCModuleArea;
import net.forwardfire.unitxc.module.UnitXCModuleElectricCurrent; import net.forwardfire.unitxc.module.UnitXCModuleElectricCurrent;
import net.forwardfire.unitxc.module.UnitXCModuleLength; import net.forwardfire.unitxc.module.UnitXCModuleLength;
import net.forwardfire.unitxc.module.UnitXCModuleLuminousIntensity; import net.forwardfire.unitxc.module.UnitXCModuleLuminousIntensity;
import net.forwardfire.unitxc.module.UnitXCModuleMass;
import net.forwardfire.unitxc.module.UnitXCModuleTemperature; import net.forwardfire.unitxc.module.UnitXCModuleTemperature;
import net.forwardfire.unitxc.module.UnitXCModuleTime;
import net.forwardfire.unitxc.module.UnitXCModuleVolume;
public final class UnitXCFactory { public final class UnitXCFactory {
@ -43,7 +47,11 @@ public final class UnitXCFactory {
new UnitXCModuleElectricCurrent(), new UnitXCModuleElectricCurrent(),
new UnitXCModuleLuminousIntensity(), new UnitXCModuleLuminousIntensity(),
new UnitXCModuleAmountOfSubstance(), new UnitXCModuleAmountOfSubstance(),
new UnitXCModuleLength() new UnitXCModuleLength(),
new UnitXCModuleMass(),
new UnitXCModuleTime(),
new UnitXCModuleArea(),
new UnitXCModuleVolume()
)); ));

View file

@ -27,7 +27,7 @@ import java.util.Collection;
import net.forwardfire.unitxc.converter.UnitXConverter; import net.forwardfire.unitxc.converter.UnitXConverter;
import net.forwardfire.unitxc.model.UnitXCType; import net.forwardfire.unitxc.model.UnitXCType;
import net.forwardfire.unitxc.model.UnitXCTypeGroup; import net.forwardfire.unitxc.model.UnitXCGroup;
public interface UnitXCManager { public interface UnitXCManager {
@ -37,11 +37,11 @@ public interface UnitXCManager {
boolean isUnitType(String id); boolean isUnitType(String id);
Collection<UnitXCTypeGroup> getUnitTypeGroups(); Collection<UnitXCGroup> getUnitGroups();
UnitXCTypeGroup getUnitTypeGroup(String id); UnitXCGroup getUnitGroup(String id);
boolean isUnitTypeGroup(String id); boolean isUnitGroup(String id);
UnitXConverter getConverter(); UnitXConverter getConverter();
} }

View file

@ -30,15 +30,15 @@ import java.util.Map;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
import net.forwardfire.unitxc.model.UnitXCTypeModel; import net.forwardfire.unitxc.model.UnitXCTypeModel;
import net.forwardfire.unitxc.model.UnitXCTypeGroupModel; import net.forwardfire.unitxc.model.UnitXCGroupModel;
public final class UnitXCConfig { public final class UnitXCConfig {
private final Map<String,UnitXCTypeModel> unitTypes; private final Map<String,UnitXCTypeModel> unitTypes;
private final Map<String,UnitXCTypeGroupModel> unitTypeGroups; private final Map<String,UnitXCGroupModel> unitGroups;
public UnitXCConfig() { public UnitXCConfig() {
unitTypeGroups = new HashMap<>(); unitGroups = new HashMap<>();
unitTypes = new HashMap<>(); unitTypes = new HashMap<>();
} }
@ -63,17 +63,17 @@ public final class UnitXCConfig {
} }
/** /**
* @return the unitTypeGroups * @return the unitGroups
*/ */
public Collection<UnitXCTypeGroupModel> getUnitTypeGroups() { public Collection<UnitXCGroupModel> getUnitroups() {
return unitTypeGroups.values(); return unitGroups.values();
} }
public void addUnitTypeGroup(UnitXCTypeGroupModel unitTypeGroup) { public void addUnitGroup(UnitXCGroupModel unitTypeGroup) {
putUnitTypeGroup(Validate.notNull(unitTypeGroup).validate()); putUnitGroup(Validate.notNull(unitTypeGroup).validate());
} }
private void putUnitTypeGroup(UnitXCTypeGroupModel unitTypeGroup) { private void putUnitGroup(UnitXCGroupModel unitTypeGroup) {
unitTypeGroups.put(unitTypeGroup.getId(),unitTypeGroup); unitGroups.put(unitTypeGroup.getId(),unitTypeGroup);
} }
} }

View file

@ -34,23 +34,23 @@ import net.forwardfire.unitxc.UnitXCManager;
import net.forwardfire.unitxc.converter.UnitXConverter; import net.forwardfire.unitxc.converter.UnitXConverter;
import net.forwardfire.unitxc.converter.UnitXConverterEngine; import net.forwardfire.unitxc.converter.UnitXConverterEngine;
import net.forwardfire.unitxc.model.UnitXCTypeModel; import net.forwardfire.unitxc.model.UnitXCTypeModel;
import net.forwardfire.unitxc.model.UnitXCTypeGroupModel; import net.forwardfire.unitxc.model.UnitXCGroupModel;
import net.forwardfire.unitxc.model.UnitXCType; import net.forwardfire.unitxc.model.UnitXCType;
import net.forwardfire.unitxc.model.UnitXCTypeGroup; import net.forwardfire.unitxc.model.UnitXCGroup;
import net.forwardfire.unitxc.model.UnitXConverterResult; import net.forwardfire.unitxc.model.UnitXConverterResult;
public class UnitXCConfigManager implements UnitXCManager { public class UnitXCConfigManager implements UnitXCManager {
private final UnitXCConfig config; private final UnitXCConfig config;
private final Map<String,UnitXCType> unitTypes; private final Map<String,UnitXCType> unitTypes;
private final Map<String,UnitXCTypeGroup> unitTypeGroups; private final Map<String,UnitXCGroup> unitGroups;
private final UnitXConverter converter; private final UnitXConverter converter;
private final UnitXConverterEngine convertEngine; private final UnitXConverterEngine convertEngine;
public UnitXCConfigManager(UnitXCConfig config) { public UnitXCConfigManager(UnitXCConfig config) {
this.config = Validate.notNull(config); this.config = Validate.notNull(config);
this.unitTypes = Collections.unmodifiableMap(createUnitTypes(config.getUnitTypes())); this.unitTypes = Collections.unmodifiableMap(createUnitTypes(config.getUnitTypes()));
this.unitTypeGroups = Collections.unmodifiableMap(createUnitTypeGroups(config.getUnitTypeGroups())); this.unitGroups = Collections.unmodifiableMap(createUnitGroups(config.getUnitroups()));
this.convertEngine = new UnitXConverterEngine(this); this.convertEngine = new UnitXConverterEngine(this);
this.converter = new UnitXConverterImpl(); this.converter = new UnitXConverterImpl();
@ -62,8 +62,8 @@ public class UnitXCConfigManager implements UnitXCManager {
return result; return result;
} }
private static Map<String,UnitXCTypeGroup> createUnitTypeGroups(Collection<UnitXCTypeGroupModel> values) { private static Map<String,UnitXCGroup> createUnitGroups(Collection<UnitXCGroupModel> values) {
Map<String,UnitXCTypeGroup> result = new HashMap<>(); Map<String,UnitXCGroup> result = new HashMap<>();
values.forEach((value) -> result.put(value.getId(), value)); values.forEach((value) -> result.put(value.getId(), value));
return result; return result;
} }
@ -84,18 +84,18 @@ public class UnitXCConfigManager implements UnitXCManager {
} }
@Override @Override
public Collection<UnitXCTypeGroup> getUnitTypeGroups() { public Collection<UnitXCGroup> getUnitGroups() {
return unitTypeGroups.values(); return unitGroups.values();
} }
@Override @Override
public UnitXCTypeGroup getUnitTypeGroup(String id) { public UnitXCGroup getUnitGroup(String id) {
return Validate.notNull(unitTypeGroups.get(Validate.notNull(id,"Null is not a validate id.")),"No group for: "+id); return Validate.notNull(unitGroups.get(Validate.notNull(id,"Null is not a validate id.")),"No group for: "+id);
} }
@Override @Override
public boolean isUnitTypeGroup(String id) { public boolean isUnitGroup(String id) {
return unitTypeGroups.containsKey(Validate.notBlank(id)); return unitGroups.containsKey(Validate.notBlank(id));
} }
@Override @Override

View file

@ -23,6 +23,7 @@
package net.forwardfire.unitxc.config; package net.forwardfire.unitxc.config;
import java.util.Arrays;
import java.util.List; import java.util.List;
import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
@ -31,6 +32,12 @@ public interface UnitXCConfigModule {
void configModule(UnitXCConfigBuilder config); void configModule(UnitXCConfigBuilder config);
static String buildFlag(String...flag) {
StringBuilder buf = new StringBuilder();
Arrays.asList(flag).forEach(f -> buf.append(f.toUpperCase()).append("_"));
return buf.deleteCharAt(buf.length()-1).toString();
}
static UnitXCConfig buildAll(UnitXCConfig config,List<UnitXCConfigModule> configInit) { static UnitXCConfig buildAll(UnitXCConfig config,List<UnitXCConfigModule> configInit) {
UnitXCConfigBuilder builder = new UnitXCConfigBuilder(config); UnitXCConfigBuilder builder = new UnitXCConfigBuilder(config);
configInit.forEach(ci -> ci.configModule(builder)); configInit.forEach(ci -> ci.configModule(builder));

View file

@ -45,8 +45,20 @@ public abstract class AbstractUnitXCTypeBuilder<P,B> extends AbstractUnitXCBuild
return make((v) -> v.setName(name)); return make((v) -> v.setName(name));
} }
public B addNameAlias(String nameAlias) {
return make((v) -> v.addNameAlias(nameAlias));
}
public B addNameAliases(Collection<String> nameAliases) {
return make((v) -> nameAliases.forEach(a -> v.addNameAlias(a)));
}
public B addNameAliases(String[] nameAliases) {
return addNameAliases(Arrays.asList(nameAliases));
}
public B setTypeGroupId(String typeGroupId) { public B setTypeGroupId(String typeGroupId) {
return make((v) -> v.setTypeGroupId(typeGroupId)); return make((v) -> v.setUnitGroupId(typeGroupId));
} }
public B addTypeFlag(String flag) { public B addTypeFlag(String flag) {

View file

@ -29,6 +29,9 @@ import net.forwardfire.unitxc.config.UnitXCConfig;
public class UnitXCConfigBuilder { public class UnitXCConfigBuilder {
public static final String TYPE_FLAG_IMPERIAL = "IMPERIAL"; // todo move to ...
public static final String TYPE_FLAG_IMPERIAL_EXTRA = "IMPERIAL_EXTRA";
private final UnitXCConfig config; private final UnitXCConfig config;
public UnitXCConfigBuilder(UnitXCConfig config) { public UnitXCConfigBuilder(UnitXCConfig config) {
@ -39,8 +42,8 @@ public class UnitXCConfigBuilder {
return config; return config;
} }
public UnitXCTypeGroupBuilder createUnitTypeGroup() { public UnitXCGroupBuilder createUnitGroup() {
return new UnitXCTypeGroupBuilder(this); return new UnitXCGroupBuilder(this);
} }
public UnitXCTypeBuilder<UnitXCConfigBuilder> createUnitType() { public UnitXCTypeBuilder<UnitXCConfigBuilder> createUnitType() {

View file

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

View file

@ -23,40 +23,54 @@
package net.forwardfire.unitxc.config.builder; package net.forwardfire.unitxc.config.builder;
import net.forwardfire.unitxc.model.UnitXCTypeGroupModel; import net.forwardfire.unitxc.model.UnitXCGroupModel;
import net.forwardfire.unitxc.model.UnitXCGroupType;
public class UnitXCTypeGroupBuilder extends AbstractUnitXCBuilder<UnitXCConfigBuilder,UnitXCTypeGroupModel,UnitXCTypeGroupBuilder> { public class UnitXCGroupBuilder extends AbstractUnitXCBuilder<UnitXCConfigBuilder,UnitXCGroupModel,UnitXCGroupBuilder> {
public UnitXCTypeGroupBuilder(UnitXCConfigBuilder parent) { public UnitXCGroupBuilder(UnitXCConfigBuilder parent) {
super(parent, new UnitXCTypeGroupModel(), (p,v) -> p.getConfig().addUnitTypeGroup(v)); super(parent, new UnitXCGroupModel(), (p,v) -> p.getConfig().addUnitGroup(v));
} }
@Override @Override
protected UnitXCTypeGroupBuilder getBuilder() { protected UnitXCGroupBuilder getBuilder() {
return this; return this;
} }
public UnitXCTypeBuilder<UnitXCTypeGroupBuilder> createUnitType() { public UnitXCTypeBuilder<UnitXCGroupBuilder> createUnitType() {
return new UnitXCTypeBuilder<>(this,(p,v) -> p.getParent().getConfig().addUnitType(v)).setTypeGroupId(getValue().getId()); return new UnitXCTypeBuilder<>(this,(p,v) -> p.getParent().getConfig().addUnitType(v)).setTypeGroupId(getValue().getId());
} }
public UnitXCTypeSIPrefixBuilder<UnitXCTypeGroupBuilder> createSIUnitTypes() { public UnitXCTypeSIPrefixBuilder<UnitXCGroupBuilder> createSIUnitTypes() {
return new UnitXCTypeSIPrefixBuilder<>(this,getParent()).setTypeGroupId(getValue().getId()); return new UnitXCTypeSIPrefixBuilder<>(this,getParent()).setTypeGroupId(getValue().getId());
} }
public UnitXCTypeGroupBuilder setId(String id) {
public UnitXCTypeCompoundExponentBuilder createCompoundExponentUnitTypes(String unitTypeGroupId,int exponent) {
return new UnitXCTypeCompoundExponentBuilder(this,getParent(),unitTypeGroupId,exponent);
}
public UnitXCGroupBuilder setId(String id) {
return make((v) -> v.setId(id)); return make((v) -> v.setId(id));
} }
public UnitXCTypeGroupBuilder setName(String name) { public UnitXCGroupBuilder setName(String name) {
return make((v) -> v.setName(name)); return make((v) -> v.setName(name));
} }
public UnitXCTypeGroupBuilder setDescription(String description) { public UnitXCGroupBuilder setDescription(String description) {
return make((v) -> v.setDescription(description)); return make((v) -> v.setDescription(description));
} }
public UnitXCTypeGroupBuilder setBaseTypeId(String baseTypeId) { public UnitXCGroupBuilder setBaseTypeId(String baseTypeId) {
return make((v) -> v.setBaseTypeId(baseTypeId)); return make((v) -> v.setBaseTypeId(baseTypeId));
} }
public UnitXCGroupBuilder setType(UnitXCGroupType type) {
return make((v) -> v.setType(type));
}
// public UnitXCGroupBuilder addDerivedFrom(String fromId) {
// return make((v) -> v.addDerivedFrom(fromId));
// }
} }

View file

@ -0,0 +1,29 @@
package net.forwardfire.unitxc.config.builder;
public class UnitXCNumberTypeSetting {
private static final char ZERO_CHAR_NORMAL = '0';
private static final char ZERO_CHAR_SUB = '₀';
private static final char ZERO_CHAR_SUPER = '⁰';
public static String toSuperScript(int value) {
return printScript(value, ZERO_CHAR_SUPER, new StringBuilder()).toString();
}
public static String toSubScript(int value) {
return printScript(value, ZERO_CHAR_SUB, new StringBuilder()).toString();
}
private static StringBuilder printScript(int value,char zeroChar,StringBuilder buf) {
String number = Integer.toString(value);
for (char c:number.toCharArray()) {
int offset = c-ZERO_CHAR_NORMAL;
int scriptChar = zeroChar+offset;
if (scriptChar==0x2072 || scriptChar==0x2073) { // TODO: rewrite to lookup table
scriptChar-=(0x2070-0x00B0); // utf-8 is iso-8859-1 compatible :(
}
buf.append((char)scriptChar);
}
return buf;
}
}

View file

@ -0,0 +1,102 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.config.builder;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.Validate;
import net.forwardfire.unitxc.converter.step.UnitXConverterStep;
import net.forwardfire.unitxc.model.UnitXCGroupModel;
import net.forwardfire.unitxc.model.UnitXCTypeModel;
public class UnitXCTypeCompoundExponentBuilder extends AbstractUnitXCBuilder<UnitXCGroupBuilder,Object,UnitXCTypeCompoundExponentBuilder> {
private final UnitXCConfigBuilder builder;
private final String unitGroupId;
private final int exponent;
private String unitIdPostfix;
private String unitNamePrefix;
public UnitXCTypeCompoundExponentBuilder(UnitXCGroupBuilder parent, UnitXCConfigBuilder builder,String unitGroupId,int exponent) {
super(parent,new Object(), (p,v) -> {});
this.builder = builder;
this.unitGroupId = unitGroupId;
this.exponent = exponent;
}
@Override
protected UnitXCTypeCompoundExponentBuilder getBuilder() {
return this;
}
@Override
protected void buildPreValue(Object v) {
UnitXCGroupModel group = null;
for (UnitXCGroupModel unitGroup:builder.getConfig().getUnitroups()) {
if (unitGroup.getId().equals(unitGroupId)) {
group = unitGroup;
break;
}
}
Validate.notNull(group,"Could not find group: "+unitGroupId);
getParent().getValue().addDerivedFrom(unitGroupId);
for (UnitXCTypeModel unitType:new ArrayList<>(builder.getConfig().getUnitTypes())) {
if (!unitType.getUnitGroupId().equals(group.getId())) {
continue;
}
UnitXCTypeBuilder<UnitXCConfigBuilder> typeBuilder = builder.createUnitType()
.setId(unitType.getId()+unitIdPostfix)
.setName(unitNamePrefix+unitType.getName())
.setTypeGroupId(group.getId())
.addTypeFlags(unitType.getTypeFlags());
for (int i=0;i<exponent;i++) {
typeBuilder.addToBaseConverterSteps(addStepReason(exponent,unitType.getToBaseConverterSteps()));
typeBuilder.addFromBaseConverterSteps(addStepReason(exponent,unitType.getFromBaseConverterSteps()));
}
typeBuilder.build();
}
}
private List<UnitXConverterStep> addStepReason(int exponent,List<UnitXConverterStep> steps) {
if (exponent == 0) {
return steps;
}
steps.forEach(step -> step.addStepReason("CompoundeExponent: "+exponent));
return steps;
}
public UnitXCTypeCompoundExponentBuilder setUnitIdPostfix(String unitIdPostfix) {
return make(v -> this.unitIdPostfix = unitIdPostfix);
}
public UnitXCTypeCompoundExponentBuilder setUnitNamePrefix(String unitNamePrefix) {
return make(v -> this.unitNamePrefix = unitNamePrefix);
}
}

View file

@ -24,6 +24,7 @@
package net.forwardfire.unitxc.config.builder; package net.forwardfire.unitxc.config.builder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -42,9 +43,9 @@ public enum UnitXCTypeSIPrefix {
DECI ("d", -1), DECI ("d", -1),
CENTI ("c", -2), CENTI ("c", -2),
MILLI ("m", -3), MILLI ("m", -3),
MICRO ("µ", -6), MICRO ("µ", -6, null, UnitXCTypeSIPrefix.NAME_ALIAS_MILLIMILLI),
NANO ("n", -9), NANO ("n", -9, null, UnitXCTypeSIPrefix.NAME_ALIAS_MILLIMICRO),
PICO ("p", -12), PICO ("p", -12, null, UnitXCTypeSIPrefix.NAME_ALIAS_MICROMICRO),
FEMTO ("f", -15), FEMTO ("f", -15),
ATTO ("a", -18), ATTO ("a", -18),
ZEPTO ("z", -21), ZEPTO ("z", -21),
@ -68,16 +69,21 @@ public enum UnitXCTypeSIPrefix {
public static final String TYPE_FLAG_SI_UNIT_OBSOLETE = "SI_UNIT_OBSOLETE"; public static final String TYPE_FLAG_SI_UNIT_OBSOLETE = "SI_UNIT_OBSOLETE";
public static final String TYPE_FLAG_SI_UNIT_COMMON = "SI_UNIT_COMMON"; public static final String TYPE_FLAG_SI_UNIT_COMMON = "SI_UNIT_COMMON";
public static final String NAME_ALIAS_MILLIMILLI = "millimilli";
public static final String NAME_ALIAS_MILLIMICRO = "millimicro";
public static final String NAME_ALIAS_MICROMICRO = "micromicro";
private final String id; private final String id;
private final String name; private final String name;
private final int exponent; private final int exponent;
private final List<String> flags; private final List<String> flags;
private final List<String> nameAliases;
private UnitXCTypeSIPrefix(String id,int exponent) { private UnitXCTypeSIPrefix(String id,int exponent) {
this(id,exponent,null); this(id,exponent,null);
} }
private UnitXCTypeSIPrefix(String id,int exponent,String flag) { private UnitXCTypeSIPrefix(String id,int exponent,String flag,String...names) {
this.id=id; this.id=id;
this.name = this.name().toLowerCase(); this.name = this.name().toLowerCase();
this.exponent=exponent; this.exponent=exponent;
@ -93,6 +99,9 @@ public enum UnitXCTypeSIPrefix {
f.add(flag); f.add(flag);
} }
this.flags = Collections.unmodifiableList(f); this.flags = Collections.unmodifiableList(f);
List<String> n = new ArrayList<>(2);
n.addAll(Arrays.asList(names));
this.nameAliases = Collections.unmodifiableList(n);
} }
public String getPrefixId() { public String getPrefixId() {
@ -110,4 +119,8 @@ public enum UnitXCTypeSIPrefix {
public List<String> getFlags() { public List<String> getFlags() {
return flags; return flags;
} }
public List<String> getNameAliases() {
return nameAliases;
}
} }

View file

@ -24,9 +24,11 @@
package net.forwardfire.unitxc.config.builder; package net.forwardfire.unitxc.config.builder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List; import java.util.List;
import net.forwardfire.unitxc.converter.step.UnitXCTypeExponentConverterStep; import net.forwardfire.unitxc.converter.step.UnitXCTypePowerOfTenConverterStep;
import net.forwardfire.unitxc.model.UnitXCTypeModel; import net.forwardfire.unitxc.model.UnitXCTypeModel;
public class UnitXCTypeSIPrefixBuilder<P> extends AbstractUnitXCTypeBuilder<P,UnitXCTypeSIPrefixBuilder<P>> { public class UnitXCTypeSIPrefixBuilder<P> extends AbstractUnitXCTypeBuilder<P,UnitXCTypeSIPrefixBuilder<P>> {
@ -50,7 +52,8 @@ public class UnitXCTypeSIPrefixBuilder<P> extends AbstractUnitXCTypeBuilder<P,Un
builder.createUnitType() builder.createUnitType()
.setId(v.getId()) .setId(v.getId())
.setName(v.getName()) .setName(v.getName())
.setTypeGroupId(v.getTypeGroupId()) .addNameAliases(v.getNameAliases())
.setTypeGroupId(v.getUnitGroupId())
.addTypeFlags(v.getTypeFlags()) .addTypeFlags(v.getTypeFlags())
.addFromBaseConverterSteps(v.getFromBaseConverterSteps()) .addFromBaseConverterSteps(v.getFromBaseConverterSteps())
.addToBaseConverterSteps(v.getToBaseConverterSteps()) .addToBaseConverterSteps(v.getToBaseConverterSteps())
@ -66,17 +69,26 @@ public class UnitXCTypeSIPrefixBuilder<P> extends AbstractUnitXCTypeBuilder<P,Un
builder.createUnitType() builder.createUnitType()
.setId(sim.getPrefixId()+v.getId()) .setId(sim.getPrefixId()+v.getId())
.setName(sim.getPrefixName()+v.getName()) .setName(sim.getPrefixName()+v.getName())
.setTypeGroupId(v.getTypeGroupId()) .addNameAliases(sim.getNameAliases())
.setTypeGroupId(v.getUnitGroupId())
.addTypeFlags(typeFlags) .addTypeFlags(typeFlags)
.addToBaseConverterStep(new UnitXCTypeExponentConverterStep(sim.getExponent(),false)) .addToBaseConverterStep(new UnitXCTypePowerOfTenConverterStep(sim.getExponent(),false,"Move dot for SI"))
.addToBaseConverterSteps(v.getToBaseConverterSteps()) .addToBaseConverterSteps(v.getToBaseConverterSteps())
.addFromBaseConverterSteps(v.getFromBaseConverterSteps()) .addFromBaseConverterSteps(v.getFromBaseConverterSteps())
.addFromBaseConverterStep(new UnitXCTypeExponentConverterStep(sim.getExponent(),true)) .addFromBaseConverterStep(new UnitXCTypePowerOfTenConverterStep(sim.getExponent(),true,"Move dot for SI"))
.build(); .build();
} }
} }
public UnitXCTypeSIPrefixBuilder<P> addCommonPrefix(UnitXCTypeSIPrefix prefix) { public UnitXCTypeSIPrefixBuilder<P> addCommonSIPrefixes(UnitXCTypeSIPrefix[] prefixes) {
return make(t -> addCommonSIPrefixes(Arrays.asList(prefixes)));
}
public UnitXCTypeSIPrefixBuilder<P> addCommonSIPrefixes(Collection<UnitXCTypeSIPrefix> prefixes) {
return make(t -> prefixes.forEach(prefix -> addCommonSIPrefix(prefix)));
}
public UnitXCTypeSIPrefixBuilder<P> addCommonSIPrefix(UnitXCTypeSIPrefix prefix) {
return make((v) -> commonSIPrefixes.add(prefix)); return make((v) -> commonSIPrefixes.add(prefix));
} }
} }

View file

@ -30,16 +30,18 @@ import java.util.function.BiConsumer;
import org.apache.commons.lang3.math.Fraction; import org.apache.commons.lang3.math.Fraction;
import net.forwardfire.unitxc.converter.step.UnitXCTypeDevideConverterStep; import net.forwardfire.unitxc.converter.step.UnitXCTypeDevideConverterStep;
import net.forwardfire.unitxc.converter.step.UnitXCTypeExponentConverterStep; import net.forwardfire.unitxc.converter.step.UnitXCTypePowerOfTenConverterStep;
import net.forwardfire.unitxc.converter.step.UnitXCTypeMultiplyConverterStep; import net.forwardfire.unitxc.converter.step.UnitXCTypeMultiplyConverterStep;
import net.forwardfire.unitxc.converter.step.UnitXCTypeMultiplyFractionConverterStep; import net.forwardfire.unitxc.converter.step.UnitXCTypeMultiplyFractionConverterStep;
import net.forwardfire.unitxc.converter.step.UnitXCTypeOffsetConverterStep; import net.forwardfire.unitxc.converter.step.UnitXCTypeOffsetConverterStep;
import net.forwardfire.unitxc.converter.step.UnitXCTypePowerConverterStep;
import net.forwardfire.unitxc.converter.step.UnitXConverterStep; import net.forwardfire.unitxc.converter.step.UnitXConverterStep;
public class UnitXConverterStepBuilder<P> extends AbstractUnitXCBuilder<P,List<UnitXConverterStep>,UnitXConverterStepBuilder<P>> { public class UnitXConverterStepBuilder<P extends AbstractUnitXCTypeBuilder<?,?>> extends AbstractUnitXCBuilder<P,List<UnitXConverterStep>,UnitXConverterStepBuilder<P>> {
public UnitXConverterStepBuilder(P parent, BiConsumer<P, List<UnitXConverterStep>> parentBuilder) { public UnitXConverterStepBuilder(P parent, BiConsumer<P, List<UnitXConverterStep>> parentBuilder) {
super(parent, new ArrayList<>(), parentBuilder); super(parent, new ArrayList<>(), parentBuilder);
this.getParent().getValue().getId();
} }
@Override @Override
@ -47,36 +49,74 @@ public class UnitXConverterStepBuilder<P> extends AbstractUnitXCBuilder<P,List<U
return this; return this;
} }
private String createStepReason() {
return "convert "+getParent().getValue().getId();
}
private UnitXConverterStepBuilder<P> offset(double offset,boolean offsetPositive,String stepReason) {
return make(v -> v.add(new UnitXCTypeOffsetConverterStep(offset,offsetPositive,stepReason)));
}
public UnitXConverterStepBuilder<P> offsetUp(double offset,String stepReason) {
return offset(offset,true,stepReason);
}
public UnitXConverterStepBuilder<P> offsetDown(double offset,String stepReason) {
return offset(offset,false,stepReason);
}
public UnitXConverterStepBuilder<P> offsetUp(double offset) { public UnitXConverterStepBuilder<P> offsetUp(double offset) {
return offset(offset,true); return offsetUp(offset,createStepReason());
} }
public UnitXConverterStepBuilder<P> offsetDown(double offset) { public UnitXConverterStepBuilder<P> offsetDown(double offset) {
return offset(offset,false); return offsetDown(offset,createStepReason());
}
private UnitXConverterStepBuilder<P> offset(double offset,boolean offsetPositive) {
return make(v -> v.add(new UnitXCTypeOffsetConverterStep(offset,offsetPositive)));
} }
public UnitXConverterStepBuilder<P> multiply(Fraction fraction,String stepReason) {
return make(v -> v.add(new UnitXCTypeMultiplyFractionConverterStep(fraction,stepReason)));
}
public UnitXConverterStepBuilder<P> multiply(int numerator,int denominator,String stepReason) {
return make(v -> v.add(new UnitXCTypeMultiplyFractionConverterStep(numerator,denominator,stepReason)));
}
public UnitXConverterStepBuilder<P> multiply(Fraction fraction) { public UnitXConverterStepBuilder<P> multiply(Fraction fraction) {
return make(v -> v.add(new UnitXCTypeMultiplyFractionConverterStep(fraction))); return multiply(fraction,createStepReason());
} }
public UnitXConverterStepBuilder<P> multiply(int numerator,int denominator) { public UnitXConverterStepBuilder<P> multiply(int numerator,int denominator) {
return make(v -> v.add(new UnitXCTypeMultiplyFractionConverterStep(numerator,denominator))); return multiply(numerator,denominator,createStepReason());
}
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)));
} }
private UnitXConverterStepBuilder<P> exponent(int exponent,boolean rev) { public UnitXConverterStepBuilder<P> multiply(double factor,String stepReason) {
return make(v -> v.add(new UnitXCTypeExponentConverterStep(exponent,rev))); return make(v -> v.add(new UnitXCTypeMultiplyConverterStep(factor,stepReason)));
} }
public UnitXConverterStepBuilder<P> exponentUp(UnitXCTypeSIPrefix exponent) { public UnitXConverterStepBuilder<P> divide(double factor,String stepReason) {
return exponent(exponent.getExponent(), false); return make(v -> v.add(new UnitXCTypeDevideConverterStep(factor,stepReason)));
} }
public UnitXConverterStepBuilder<P> exponentDown(UnitXCTypeSIPrefix exponent) { public UnitXConverterStepBuilder<P> multiply(double factor) {
return exponent(exponent.getExponent(), true); return multiply(factor,createStepReason());
}
public UnitXConverterStepBuilder<P> divide(double factor) {
return divide(factor,createStepReason());
}
public UnitXConverterStepBuilder<P> power(int exponent,String stepReason) {
return make(v -> v.add(new UnitXCTypePowerConverterStep(exponent,stepReason)));
}
public UnitXConverterStepBuilder<P> power(int exponent) {
return power(exponent,createStepReason());
}
private UnitXConverterStepBuilder<P> powerOfTen(int exponent,boolean rev,String stepReason) {
return make(v -> v.add(new UnitXCTypePowerOfTenConverterStep(exponent,rev,stepReason)));
}
public UnitXConverterStepBuilder<P> power10Up(UnitXCTypeSIPrefix exponent,String stepReason) {
return powerOfTen(exponent.getExponent(), true,stepReason);
}
public UnitXConverterStepBuilder<P> power10Down(UnitXCTypeSIPrefix exponent,String stepReason) {
return powerOfTen(exponent.getExponent(), false,stepReason);
}
public UnitXConverterStepBuilder<P> power10Up(UnitXCTypeSIPrefix exponent) {
return power10Up(exponent,createStepReason());
}
public UnitXConverterStepBuilder<P> power10Down(UnitXCTypeSIPrefix exponent) {
return power10Down(exponent,createStepReason());
} }
} }

View file

@ -33,7 +33,7 @@ import net.forwardfire.unitxc.UnitXCManager;
import net.forwardfire.unitxc.config.UnitXCConfig; import net.forwardfire.unitxc.config.UnitXCConfig;
import net.forwardfire.unitxc.converter.step.UnitXConverterStep; import net.forwardfire.unitxc.converter.step.UnitXConverterStep;
import net.forwardfire.unitxc.model.UnitXCTypeModel; import net.forwardfire.unitxc.model.UnitXCTypeModel;
import net.forwardfire.unitxc.model.UnitXCTypeGroup; import net.forwardfire.unitxc.model.UnitXCGroup;
import net.forwardfire.unitxc.model.UnitXConverterResult; import net.forwardfire.unitxc.model.UnitXConverterResult;
import net.forwardfire.unitxc.model.UnitXConverterResultModel; import net.forwardfire.unitxc.model.UnitXConverterResultModel;
import net.forwardfire.unitxc.model.UnitXConverterResultStep; import net.forwardfire.unitxc.model.UnitXConverterResultStep;
@ -53,8 +53,8 @@ public final class UnitXConverterEngine {
double startValue = value; double startValue = value;
List<UnitXConverterResultStep> resultSteps = new ArrayList<>(); List<UnitXConverterResultStep> resultSteps = new ArrayList<>();
UnitXCTypeGroup fromTypeGroup = manager.getUnitTypeGroup(fromType.getTypeGroupId()); UnitXCGroup fromTypeGroup = manager.getUnitGroup(fromType.getUnitGroupId());
UnitXCTypeGroup toTypeGroup = manager.getUnitTypeGroup(toType.getTypeGroupId()); UnitXCGroup toTypeGroup = manager.getUnitGroup(toType.getUnitGroupId());
boolean fromTypeBase = fromTypeGroup.getBaseTypeId().equals(fromType.getId()); boolean fromTypeBase = fromTypeGroup.getBaseTypeId().equals(fromType.getId());
if (!fromTypeBase) { if (!fromTypeBase) {
@ -76,15 +76,19 @@ public final class UnitXConverterEngine {
List<UnitXConverterStep> steps = toBase?type.getToBaseConverterSteps():type.getFromBaseConverterSteps(); List<UnitXConverterStep> steps = toBase?type.getToBaseConverterSteps():type.getFromBaseConverterSteps();
for (UnitXConverterStep step:steps) { for (UnitXConverterStep step:steps) {
valueOld = value; valueOld = value;
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
value = step.convert(value); value = step.convert(value);
value = runRounding(resultSteps,value,valueOld);
long convertTime = System.currentTimeMillis()-startTime; long convertTime = System.currentTimeMillis()-startTime;
resultSteps.add(new UnitXConverterResultStepModel(step.getName()+(toBase?"-toBase":"-fromBase")+" = "+step.getMathExpression(),valueOld,value,convertTime)); resultSteps.add(new UnitXConverterResultStepModel(step.getStepName()+(toBase?"-toBase":"-fromBase")+" = "+step.getMathExpression(),valueOld,value,convertTime));
value = runRounding(resultSteps,value,valueOld);
} }
return value; return value;
} }
// TODO: redo this
private double runRounding(List<UnitXConverterResultStep> resultSteps,double value,double valueOrg) { private double runRounding(List<UnitXConverterResultStep> resultSteps,double value,double valueOrg) {
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
String valueStr = Double.toString(value); String valueStr = Double.toString(value);

View file

@ -23,17 +23,34 @@
package net.forwardfire.unitxc.converter.step; package net.forwardfire.unitxc.converter.step;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
public abstract class AbstractUnitXConverterStep implements UnitXConverterStep { public abstract class AbstractUnitXConverterStep implements UnitXConverterStep {
private final String name; private final String name;
private final List<String> stepReasons;
protected AbstractUnitXConverterStep(String name) { protected AbstractUnitXConverterStep(String name,String stepReason) {
this.name = Validate.notBlank(name); this.name = Validate.notBlank(name);
this.stepReasons = new ArrayList<>();
this.addStepReason(stepReason);
} }
public final String getName() { @Override
public final String getStepName() {
return name; return name;
} }
@Override
public final List<String> getStepReasons() {
return stepReasons;
}
@Override
public final void addStepReason(String stepReason) {
stepReasons.add(Validate.notBlank(stepReason));
}
} }

View file

@ -30,8 +30,8 @@ public class UnitXCTypeDevideConverterStep extends AbstractUnitXConverterStep {
private final static String STEP_NAME = "Devide"; private final static String STEP_NAME = "Devide";
private final double factor; private final double factor;
public UnitXCTypeDevideConverterStep(double factor) { public UnitXCTypeDevideConverterStep(double factor,String stepReason) {
super(STEP_NAME); super(STEP_NAME,stepReason);
this.factor = factor; this.factor = factor;
Validate.isTrue(factor != 0); Validate.isTrue(factor != 0);
} }

View file

@ -28,8 +28,8 @@ public class UnitXCTypeMultiplyConverterStep extends AbstractUnitXConverterStep
private final static String STEP_NAME = "Multiply"; private final static String STEP_NAME = "Multiply";
private final double factor; private final double factor;
public UnitXCTypeMultiplyConverterStep(double factor) { public UnitXCTypeMultiplyConverterStep(double factor,String stepReason) {
super(STEP_NAME); super(STEP_NAME,stepReason);
this.factor = factor; this.factor = factor;
} }

View file

@ -32,12 +32,12 @@ public class UnitXCTypeMultiplyFractionConverterStep extends AbstractUnitXConver
private final Fraction fraction; private final Fraction fraction;
private final String fractionMath; private final String fractionMath;
public UnitXCTypeMultiplyFractionConverterStep(int numerator,int denominator) { public UnitXCTypeMultiplyFractionConverterStep(int numerator,int denominator,String stepReason) {
this(Fraction.getFraction(numerator, denominator)); this(Fraction.getFraction(numerator, denominator),stepReason);
} }
public UnitXCTypeMultiplyFractionConverterStep(Fraction fraction) { public UnitXCTypeMultiplyFractionConverterStep(Fraction fraction,String stepReason) {
super(STEP_NAME); super(STEP_NAME,stepReason);
this.fraction = Validate.notNull(fraction); this.fraction = Validate.notNull(fraction);
this.fractionMath = String.format("*(%1s/%2s)", fraction.getNumerator(),fraction.getDenominator()); this.fractionMath = String.format("*(%1s/%2s)", fraction.getNumerator(),fraction.getDenominator());
} }

View file

@ -29,8 +29,8 @@ public class UnitXCTypeOffsetConverterStep extends AbstractUnitXConverterStep {
private final double offset; private final double offset;
private final boolean offsetPositive; private final boolean offsetPositive;
public UnitXCTypeOffsetConverterStep(double offset,boolean offsetPositive) { public UnitXCTypeOffsetConverterStep(double offset,boolean offsetPositive,String stepReason) {
super(STEP_NAME); super(STEP_NAME,stepReason);
this.offset = offset; this.offset = offset;
this.offsetPositive = offsetPositive; this.offsetPositive = offsetPositive;
} }

View file

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

View file

@ -23,29 +23,29 @@
package net.forwardfire.unitxc.converter.step; package net.forwardfire.unitxc.converter.step;
public class UnitXCTypeExponentConverterStep extends AbstractUnitXConverterStep { public class UnitXCTypePowerOfTenConverterStep extends AbstractUnitXConverterStep {
private final static String STEP_NAME = "Exponent"; private final static String STEP_NAME = "power(10)";
private final int factor; private final int exponent;
private final int factorShift; private final int exponentShift;
private final boolean factorReverse; private final boolean exponentReverse;
public UnitXCTypeExponentConverterStep(int factor,boolean factorReverse) { public UnitXCTypePowerOfTenConverterStep(int factor,boolean factorReverse,String stepReason) {
super(factor+STEP_NAME); super(STEP_NAME,stepReason);
this.factor = factor; this.exponent = factor;
this.factorReverse = factorReverse; this.exponentReverse = factorReverse;
this.factorShift = factor<0?0-factor:factor; this.exponentShift = factor<0?0-factor:factor;
} }
@Override @Override
public String getMathExpression() { public String getMathExpression() {
return "*10^"+factor; return "*10^"+exponent;
} }
@Override @Override
public double convert(double value) { public double convert(double value) {
for (int i=0;i<factorShift;i++) { for (int i=0;i<exponentShift;i++) {
value = shiftDotStatic(value,factorReverse?!(factor>0):factor>0); value = shiftDotStatic(value,exponentReverse?!(exponent>0):exponent>0);
} }
return value; return value;
} }

View file

@ -23,9 +23,15 @@
package net.forwardfire.unitxc.converter.step; package net.forwardfire.unitxc.converter.step;
import java.util.List;
public interface UnitXConverterStep { public interface UnitXConverterStep {
String getName(); String getStepName();
List<String> getStepReasons();
void addStepReason(String stepReason);
String getMathExpression(); String getMathExpression();

View file

@ -23,7 +23,9 @@
package net.forwardfire.unitxc.model; package net.forwardfire.unitxc.model;
public interface UnitXCTypeGroup { import java.util.Collection;
public interface UnitXCGroup {
String getId(); String getId();
@ -31,5 +33,9 @@ public interface UnitXCTypeGroup {
String getDescription(); String getDescription();
UnitXCGroupType getType();
String getBaseTypeId(); String getBaseTypeId();
Collection<String> getDerivedFromIds();
} }

View file

@ -23,36 +23,46 @@
package net.forwardfire.unitxc.model; package net.forwardfire.unitxc.model;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
public class UnitXCTypeGroupModel implements UnitXCTypeGroup { public class UnitXCGroupModel implements UnitXCGroup {
private String id; private String id;
private String name; private String name;
private String description; private String description;
private String baseTypeId; private String baseTypeId;
private UnitXCGroupType type;
private final List<String> derivedFrom;
public UnitXCTypeGroupModel() { public UnitXCGroupModel() {
derivedFrom = new ArrayList<>();
} }
public UnitXCTypeGroupModel(String id,String name,String description,String baseTypeId) { public UnitXCGroupModel(String id,String name,String description,String baseTypeId) {
this();
setId(id); setId(id);
setName(name); setName(name);
setDescription(description); setDescription(description);
setBaseTypeId(baseTypeId); setBaseTypeId(baseTypeId);
} }
public UnitXCTypeGroupModel validate() { public UnitXCGroupModel validate() {
Validate.notBlank(id,"The id is blank"); Validate.notBlank(id,"The id is blank");
Validate.notBlank(name,"The name is blank"); Validate.notBlank(name,"The name is blank");
Validate.notBlank(description,"The description is blank"); Validate.notBlank(description,"The description is blank");
Validate.notBlank(baseTypeId,"The baseTypeId is blank"); Validate.notBlank(baseTypeId,"The baseTypeId is blank");
Validate.notNull(type,"The type is null");
return this; return this;
} }
/** /**
* @return the id * @return the id
*/ */
@Override
public String getId() { public String getId() {
return id; return id;
} }
@ -67,6 +77,7 @@ public class UnitXCTypeGroupModel implements UnitXCTypeGroup {
/** /**
* @return the name * @return the name
*/ */
@Override
public String getName() { public String getName() {
return name; return name;
} }
@ -81,6 +92,7 @@ public class UnitXCTypeGroupModel implements UnitXCTypeGroup {
/** /**
* @return the description * @return the description
*/ */
@Override
public String getDescription() { public String getDescription() {
return description; return description;
} }
@ -95,6 +107,7 @@ public class UnitXCTypeGroupModel implements UnitXCTypeGroup {
/** /**
* @return the baseTypeId * @return the baseTypeId
*/ */
@Override
public String getBaseTypeId() { public String getBaseTypeId() {
return baseTypeId; return baseTypeId;
} }
@ -105,4 +118,28 @@ public class UnitXCTypeGroupModel implements UnitXCTypeGroup {
public void setBaseTypeId(String baseTypeId) { public void setBaseTypeId(String baseTypeId) {
this.baseTypeId = baseTypeId; this.baseTypeId = baseTypeId;
} }
/**
* @return the type
*/
@Override
public UnitXCGroupType getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(UnitXCGroupType type) {
this.type = type;
}
@Override
public Collection<String> getDerivedFromIds() {
return derivedFrom;
}
public void addDerivedFrom(String id) {
derivedFrom.add(id);
}
} }

View file

@ -0,0 +1,7 @@
package net.forwardfire.unitxc.model;
public enum UnitXCGroupType {
SI_BASE,
SI_DERIVED
}

View file

@ -31,7 +31,9 @@ public interface UnitXCType {
String getName(); String getName();
String getTypeGroupId(); Collection<String> getNameAliases();
String getUnitGroupId();
Collection<String> getTypeFlags(); Collection<String> getTypeFlags();
} }

View file

@ -35,13 +35,15 @@ public class UnitXCTypeModel implements UnitXCType {
private String id; private String id;
private String name; private String name;
private String typeGroupId; private String unitGroupId;
private final List<UnitXConverterStep> toBaseConverterSteps; private final List<UnitXConverterStep> toBaseConverterSteps;
private final List<UnitXConverterStep> fromBaseConverterSteps; private final List<UnitXConverterStep> fromBaseConverterSteps;
private final List<String> typeFlags; private final List<String> typeFlags;
private final List<String> nameAliases;
public UnitXCTypeModel() { public UnitXCTypeModel() {
typeFlags = new ArrayList<>(); typeFlags = new ArrayList<>();
nameAliases = new ArrayList<>();
toBaseConverterSteps = new ArrayList<>(); toBaseConverterSteps = new ArrayList<>();
fromBaseConverterSteps = new ArrayList<>(); fromBaseConverterSteps = new ArrayList<>();
} }
@ -49,13 +51,14 @@ public class UnitXCTypeModel implements UnitXCType {
public UnitXCTypeModel validate() { public UnitXCTypeModel validate() {
Validate.notBlank(id,"The id is blank"); Validate.notBlank(id,"The id is blank");
Validate.notBlank(name,"The name is blank"); Validate.notBlank(name,"The name is blank");
Validate.notBlank(typeGroupId,"The typeGroupId is blank"); Validate.notBlank(unitGroupId,"The unitGroupId is blank");
return this; return this;
} }
/** /**
* @return the id * @return the id
*/ */
@Override
public String getId() { public String getId() {
return id; return id;
} }
@ -70,6 +73,7 @@ public class UnitXCTypeModel implements UnitXCType {
/** /**
* @return the name * @return the name
*/ */
@Override
public String getName() { public String getName() {
return name; return name;
} }
@ -82,17 +86,18 @@ public class UnitXCTypeModel implements UnitXCType {
} }
/** /**
* @return the typeGroupId * @return the unitGroupId
*/ */
public String getTypeGroupId() { @Override
return typeGroupId; public String getUnitGroupId() {
return unitGroupId;
} }
/** /**
* @param typeGroupId the typeGroupId to set * @param unitGroupId the unitGroupId to set
*/ */
public void setTypeGroupId(String typeGroupId) { public void setUnitGroupId(String unitGroupId) {
this.typeGroupId = typeGroupId; this.unitGroupId = unitGroupId;
} }
public List<UnitXConverterStep> getToBaseConverterSteps() { public List<UnitXConverterStep> getToBaseConverterSteps() {
@ -119,4 +124,13 @@ public class UnitXCTypeModel implements UnitXCType {
public void addTypeFlag(String flag) { public void addTypeFlag(String flag) {
typeFlags.add(flag); typeFlags.add(flag);
} }
@Override
public Collection<String> getNameAliases() {
return nameAliases;
}
public void addNameAlias(String nameAlias) {
nameAliases.add(nameAlias);
}
} }

View file

@ -23,6 +23,9 @@
package net.forwardfire.unitxc.module; package net.forwardfire.unitxc.module;
import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag;
import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE;
import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
import net.forwardfire.unitxc.config.builder.UnitXCTypeSIPrefix; import net.forwardfire.unitxc.config.builder.UnitXCTypeSIPrefix;
@ -33,16 +36,17 @@ public class UnitXCModuleAmountOfSubstance implements UnitXCConfigModule {
public static final String GROUP_DESCRIPTION = "The mole is the amount of substance of a system which contains as many elementary entities as there are atoms in 0.012 kilogram of carbon 12."; public static final String GROUP_DESCRIPTION = "The mole is the amount of substance of a system which contains as many elementary entities as there are atoms in 0.012 kilogram of carbon 12.";
public static final String TYPE_MOLE_ID = "mol"; public static final String TYPE_MOLE_ID = "mol";
public static final String TYPE_MOLE_NAME = "mole"; public static final String TYPE_MOLE_NAME = "mole";
public static final String TYPE_MOLE_FLAG = (GROUP_ID+"_"+TYPE_MOLE_NAME).toUpperCase(); public static final String TYPE_MOLE_FLAG = buildFlag(GROUP_ID,TYPE_MOLE_NAME);
public static final String TYPE_POUND_MOLE_ID = "lbmol"; public static final String TYPE_POUND_MOLE_ID = "lbmol";
public static final String TYPE_POUND_MOLE_NAME = "pound-mole"; public static final String TYPE_POUND_MOLE_NAME = "pound-mole";
public static final String TYPE_POUND_MOLE_FLAG = (GROUP_ID+"_"+TYPE_POUND_MOLE_NAME).toUpperCase(); public static final String TYPE_POUND_MOLE_FLAG = buildFlag(GROUP_ID,TYPE_POUND_MOLE_NAME);
private static final double TYPE_POUND_MOLE_FACTOR = 453.59237; private static final double TYPE_POUND_MOLE_FACTOR = 453.59237;
@Override @Override
public void configModule(UnitXCConfigBuilder builder) { public void configModule(UnitXCConfigBuilder builder) {
builder.createUnitTypeGroup() builder.createUnitGroup()
.setType( SI_BASE)
.setId( GROUP_ID) .setId( GROUP_ID)
.setName( TYPE_MOLE_NAME) .setName( TYPE_MOLE_NAME)
.setDescription ( GROUP_DESCRIPTION) .setDescription ( GROUP_DESCRIPTION)
@ -51,15 +55,16 @@ public class UnitXCModuleAmountOfSubstance implements UnitXCConfigModule {
.setId( TYPE_MOLE_ID) .setId( TYPE_MOLE_ID)
.setName( TYPE_MOLE_NAME) .setName( TYPE_MOLE_NAME)
.addTypeFlag( TYPE_MOLE_FLAG) .addTypeFlag( TYPE_MOLE_FLAG)
.addCommonPrefix(UnitXCTypeSIPrefix.MILLI) .addCommonSIPrefix(UnitXCTypeSIPrefix.MILLI)
.addCommonPrefix(UnitXCTypeSIPrefix.MICRO) .addCommonSIPrefix(UnitXCTypeSIPrefix.MICRO)
.addCommonPrefix(UnitXCTypeSIPrefix.NANO) .addCommonSIPrefix(UnitXCTypeSIPrefix.NANO)
.addCommonPrefix(UnitXCTypeSIPrefix.PICO) .addCommonSIPrefix(UnitXCTypeSIPrefix.PICO)
.addCommonPrefix(UnitXCTypeSIPrefix.FEMTO) .addCommonSIPrefix(UnitXCTypeSIPrefix.FEMTO)
.build() .build()
.createUnitType() .createUnitType()
.setId( TYPE_POUND_MOLE_ID) .setId( TYPE_POUND_MOLE_ID)
.setName( TYPE_POUND_MOLE_NAME) .setName( TYPE_POUND_MOLE_NAME)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.createToBaseConverterSteps().multiply(TYPE_POUND_MOLE_FACTOR).build() .createToBaseConverterSteps().multiply(TYPE_POUND_MOLE_FACTOR).build()
.createFromBaseConverterSteps().divide(TYPE_POUND_MOLE_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_POUND_MOLE_FACTOR).build()
.build() .build()

View file

@ -0,0 +1,58 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.module;
import static net.forwardfire.unitxc.config.builder.UnitXCNumberTypeSetting.toSuperScript;
import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_DERIVED;
import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
public class UnitXCModuleArea implements UnitXCConfigModule {
private static final int UNIT_COMPOUND_EXPONENT = 2;
private static final String UNIT_ID_POSTFIX = toSuperScript(UNIT_COMPOUND_EXPONENT);
private static final String UNIT_NAME_PREFIX = "square ";
public static final String GROUP_ID = "area";
public static final String GROUP_NAME = UNIT_NAME_PREFIX+UnitXCModuleLength.TYPE_METRE_NAME;
public static final String GROUP_DESCRIPTION = "It is defined as the area of a square whose sides measure exactly one metre. The square metre is derived from the SI base unit of the metre,";
public static final String GROUP_BASE_TYPE_ID = UnitXCModuleLength.TYPE_METRE_ID+UNIT_ID_POSTFIX;
@Override
public void configModule(UnitXCConfigBuilder builder) {
builder.createUnitGroup()
.setType( SI_DERIVED)
.setId( GROUP_ID)
.setName( GROUP_NAME)
.setDescription ( GROUP_DESCRIPTION)
.setBaseTypeId( GROUP_BASE_TYPE_ID)
.createCompoundExponentUnitTypes(UnitXCModuleLength.GROUP_ID, UNIT_COMPOUND_EXPONENT)
.setUnitIdPostfix(UNIT_ID_POSTFIX)
.setUnitNamePrefix(UNIT_NAME_PREFIX)
.build()
.build()
;
}
}

View file

@ -23,6 +23,9 @@
package net.forwardfire.unitxc.module; package net.forwardfire.unitxc.module;
import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag;
import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE;
import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
import net.forwardfire.unitxc.config.builder.UnitXCTypeSIPrefix; import net.forwardfire.unitxc.config.builder.UnitXCTypeSIPrefix;
@ -33,11 +36,12 @@ public class UnitXCModuleElectricCurrent implements UnitXCConfigModule {
public static final String GROUP_DESCRIPTION = "The ampere is a measure of the amount of electric charge passing a point in an electric circuit per unit time, with 6.241×1018 electrons (or one coulomb) per second constituting one ampere."; public static final String GROUP_DESCRIPTION = "The ampere is a measure of the amount of electric charge passing a point in an electric circuit per unit time, with 6.241×1018 electrons (or one coulomb) per second constituting one ampere.";
public static final String TYPE_AMPERE_ID = "A"; public static final String TYPE_AMPERE_ID = "A";
public static final String TYPE_AMPERE_NAME = "ampere"; public static final String TYPE_AMPERE_NAME = "ampere";
public static final String TYPE_AMPERE_FLAG = (GROUP_ID+"_"+TYPE_AMPERE_NAME).toUpperCase(); public static final String TYPE_AMPERE_FLAG = buildFlag(GROUP_ID,TYPE_AMPERE_NAME);
@Override @Override
public void configModule(UnitXCConfigBuilder builder) { public void configModule(UnitXCConfigBuilder builder) {
builder.createUnitTypeGroup() builder.createUnitGroup()
.setType( SI_BASE)
.setId( GROUP_ID) .setId( GROUP_ID)
.setName( TYPE_AMPERE_NAME) .setName( TYPE_AMPERE_NAME)
.setDescription ( GROUP_DESCRIPTION) .setDescription ( GROUP_DESCRIPTION)
@ -46,11 +50,11 @@ public class UnitXCModuleElectricCurrent implements UnitXCConfigModule {
.setId( TYPE_AMPERE_ID) .setId( TYPE_AMPERE_ID)
.setName( TYPE_AMPERE_NAME) .setName( TYPE_AMPERE_NAME)
.addTypeFlag( TYPE_AMPERE_FLAG) .addTypeFlag( TYPE_AMPERE_FLAG)
.addCommonPrefix(UnitXCTypeSIPrefix.MEGA) .addCommonSIPrefix(UnitXCTypeSIPrefix.MEGA)
.addCommonPrefix(UnitXCTypeSIPrefix.KILO) .addCommonSIPrefix(UnitXCTypeSIPrefix.KILO)
.addCommonPrefix(UnitXCTypeSIPrefix.MILLI) .addCommonSIPrefix(UnitXCTypeSIPrefix.MILLI)
.addCommonPrefix(UnitXCTypeSIPrefix.MICRO) .addCommonSIPrefix(UnitXCTypeSIPrefix.MICRO)
.addCommonPrefix(UnitXCTypeSIPrefix.NANO) .addCommonSIPrefix(UnitXCTypeSIPrefix.NANO)
.build() .build()
.build() .build()
; ;

View file

@ -23,6 +23,9 @@
package net.forwardfire.unitxc.module; package net.forwardfire.unitxc.module;
import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag;
import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE;
import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
import net.forwardfire.unitxc.config.builder.UnitXCTypeSIPrefix; import net.forwardfire.unitxc.config.builder.UnitXCTypeSIPrefix;
@ -31,82 +34,170 @@ public class UnitXCModuleLength implements UnitXCConfigModule {
public static final String GROUP_ID = "length"; public static final String GROUP_ID = "length";
public static final String GROUP_DESCRIPTION = "The metre is the length of the path travelled by light in vacuum during a time interval of 1/299792458 of a second."; public static final String GROUP_DESCRIPTION = "The metre is the length of the path travelled by light in vacuum during a time interval of 1/299792458 of a second.";
public static final String TYPE_METER_ID = "m"; public static final String TYPE_METRE_ID = "m";
public static final String TYPE_METER_NAME = "meter"; public static final String TYPE_METRE_NAME = "metre";
public static final String TYPE_METER_FLAG = (GROUP_ID+"_"+TYPE_METER_NAME).toUpperCase(); public static final String TYPE_METRE_NAME_METER = "meter";
//public static final String TYPE_METER_ALIAS = "metre"; public static final String TYPE_METRE_FLAG = buildFlag(GROUP_ID,TYPE_METRE_NAME);
public static final String TYPE_INCH_ID = "in"; public static final String TYPE_INCH_ID = "in";
public static final String TYPE_INCH_NAME = "inch"; public static final String TYPE_INCH_NAME = "inch";
public static final String TYPE_INCH_FLAG = (GROUP_ID+"_"+TYPE_INCH_NAME).toUpperCase(); public static final String TYPE_INCH_FLAG = buildFlag(GROUP_ID,TYPE_INCH_NAME);
private static final double TYPE_INCH_FACTOR = 25.4; private static final double TYPE_INCH_FACTOR = 25.4;
public static final String TYPE_LINK_ID = "li"; public static final String TYPE_LINK_ID = "li";
public static final String TYPE_LINK_NAME = "link"; public static final String TYPE_LINK_NAME = "link";
public static final String TYPE_LINK_FLAG = (GROUP_ID+"_"+TYPE_LINK_NAME).toUpperCase(); public static final String TYPE_LINK_FLAG = buildFlag(GROUP_ID,TYPE_LINK_NAME);
private static final double TYPE_LINK_FACTOR = 201.1; private static final double TYPE_LINK_FACTOR = 201.1;
public static final String TYPE_FOOT_ID = "ft"; public static final String TYPE_FOOT_ID = "ft";
public static final String TYPE_FOOT_NAME = "foot"; public static final String TYPE_FOOT_NAME = "foot";
public static final String TYPE_FOOT_FLAG = (GROUP_ID+"_"+TYPE_FOOT_NAME).toUpperCase(); public static final String TYPE_FOOT_FLAG = buildFlag(GROUP_ID,TYPE_FOOT_NAME);
private static final double TYPE_FOOT_FACTOR = 304.8; private static final double TYPE_FOOT_FACTOR = 304.8;
public static final String TYPE_YARD_ID = "yd"; public static final String TYPE_YARD_ID = "yd";
public static final String TYPE_YARD_NAME = "yard"; public static final String TYPE_YARD_NAME = "yard";
public static final String TYPE_YARD_FLAG = (GROUP_ID+"_"+TYPE_YARD_NAME).toUpperCase(); public static final String TYPE_YARD_FLAG = buildFlag(GROUP_ID,TYPE_YARD_NAME);
private static final double TYPE_YARD_FACTOR = 0.9144; private static final double TYPE_YARD_FACTOR = 0.9144;
public static final String TYPE_ROD_ID = "rd";
public static final String TYPE_ROD_NAME = "rod";
public static final String TYPE_ROD_FLAG = buildFlag(GROUP_ID,TYPE_ROD_NAME);
private static final double TYPE_ROD_FACTOR = 5.029;
public static final String TYPE_CHAIN_ID = "ch";
public static final String TYPE_CHAIN_NAME = "chain";
public static final String TYPE_CHAIN_FLAG = buildFlag(GROUP_ID,TYPE_CHAIN_NAME);
private static final double TYPE_CHAIN_FACTOR = 20.117;
public static final String TYPE_FURLONG_ID = "fur";
public static final String TYPE_FURLONG_NAME = "furlong";
public static final String TYPE_FURLONG_FLAG = buildFlag(GROUP_ID,TYPE_FURLONG_NAME);
private static final double TYPE_FURLONG_FACTOR = 201.17;
public static final String TYPE_MILE_ID = "mi";
public static final String TYPE_MILE_NAME = "mile";
public static final String TYPE_MILE_FLAG = buildFlag(GROUP_ID,TYPE_MILE_NAME);
private static final double TYPE_MILE_FACTOR = 1609.344;
public static final String TYPE_MILE_US_ID = "us-mi";
public static final String TYPE_MILE_US_NAME = "us-mile";
public static final String TYPE_MILE_US_FLAG = buildFlag(GROUP_ID,TYPE_MILE_US_NAME);
private static final double TYPE_MILE_US_FACTOR = 1609.34721869;
public static final String TYPE_MILE_NAUTICAL_ID = "nau-mi";
public static final String TYPE_MILE_NAUTICAL_NAME = "nautical-mile";
public static final String TYPE_MILE_NAUTICAL_FLAG = buildFlag(GROUP_ID,TYPE_MILE_NAUTICAL_NAME);
private static final double TYPE_MILE_NAUTICAL_FACTOR = 1852;
@Override @Override
public void configModule(UnitXCConfigBuilder builder) { public void configModule(UnitXCConfigBuilder builder) {
builder.createUnitTypeGroup() builder.createUnitGroup()
.setType( SI_BASE)
.setId( GROUP_ID) .setId( GROUP_ID)
.setName( TYPE_METER_NAME) .setName( TYPE_METRE_NAME)
.setDescription ( GROUP_DESCRIPTION) .setDescription ( GROUP_DESCRIPTION)
.setBaseTypeId( TYPE_METER_ID) .setBaseTypeId( TYPE_METRE_ID)
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_METER_ID) .setId( TYPE_METRE_ID)
.setName( TYPE_METER_NAME) .setName( TYPE_METRE_NAME)
.addTypeFlag( TYPE_METER_FLAG) .addNameAlias(TYPE_METRE_NAME_METER)
.addTypeFlag( TYPE_METRE_FLAG)
.addCommonSIPrefix(UnitXCTypeSIPrefix.KILO)
.addCommonSIPrefix(UnitXCTypeSIPrefix.HECTO)
.addCommonSIPrefix(UnitXCTypeSIPrefix.DECA)
.addCommonSIPrefix(UnitXCTypeSIPrefix.DECI)
.addCommonSIPrefix(UnitXCTypeSIPrefix.CENTI)
.addCommonSIPrefix(UnitXCTypeSIPrefix.MILLI)
.addCommonSIPrefix(UnitXCTypeSIPrefix.MICRO)
.addCommonSIPrefix(UnitXCTypeSIPrefix.NANO)
.addCommonSIPrefix(UnitXCTypeSIPrefix.PICO)
.addCommonSIPrefix(UnitXCTypeSIPrefix.FEMTO)
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_INCH_ID) .setId( TYPE_INCH_ID)
.setName( TYPE_INCH_NAME) .setName( TYPE_INCH_NAME)
.addTypeFlag( TYPE_INCH_FLAG) .addTypeFlag( TYPE_INCH_FLAG)
.createToBaseConverterSteps().multiply(TYPE_INCH_FACTOR).exponentDown(UnitXCTypeSIPrefix.MILLI).build() .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.createFromBaseConverterSteps().exponentUp(UnitXCTypeSIPrefix.MILLI).divide(TYPE_INCH_FACTOR).build() .createToBaseConverterSteps().multiply(TYPE_INCH_FACTOR).power10Down(UnitXCTypeSIPrefix.MILLI).build()
.createFromBaseConverterSteps().power10Up(UnitXCTypeSIPrefix.MILLI).divide(TYPE_INCH_FACTOR).build()
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_LINK_ID) .setId( TYPE_LINK_ID)
.setName( TYPE_LINK_NAME) .setName( TYPE_LINK_NAME)
.addTypeFlag( TYPE_LINK_FLAG) .addTypeFlag( TYPE_LINK_FLAG)
.createToBaseConverterSteps().multiply(TYPE_LINK_FACTOR).exponentDown(UnitXCTypeSIPrefix.MILLI).build() .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.createFromBaseConverterSteps().exponentUp(UnitXCTypeSIPrefix.MILLI).divide(TYPE_LINK_FACTOR).build() .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA)
.createToBaseConverterSteps().multiply(TYPE_LINK_FACTOR).power10Down(UnitXCTypeSIPrefix.MILLI).build()
.createFromBaseConverterSteps().power10Up(UnitXCTypeSIPrefix.MILLI).divide(TYPE_LINK_FACTOR).build()
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_FOOT_ID) .setId( TYPE_FOOT_ID)
.setName( TYPE_FOOT_NAME) .setName( TYPE_FOOT_NAME)
.addTypeFlag( TYPE_FOOT_FLAG) .addTypeFlag( TYPE_FOOT_FLAG)
.createToBaseConverterSteps().multiply(TYPE_FOOT_FACTOR).exponentDown(UnitXCTypeSIPrefix.MILLI).build() .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.createFromBaseConverterSteps().exponentUp(UnitXCTypeSIPrefix.MILLI).divide(TYPE_FOOT_FACTOR).build() .createToBaseConverterSteps().multiply(TYPE_FOOT_FACTOR).power10Down(UnitXCTypeSIPrefix.MILLI).build()
.createFromBaseConverterSteps().power10Up(UnitXCTypeSIPrefix.MILLI).divide(TYPE_FOOT_FACTOR).build()
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_YARD_ID) .setId( TYPE_YARD_ID)
.setName( TYPE_YARD_NAME) .setName( TYPE_YARD_NAME)
.addTypeFlag( TYPE_YARD_FLAG) .addTypeFlag( TYPE_YARD_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.createToBaseConverterSteps().multiply(TYPE_YARD_FACTOR).build() .createToBaseConverterSteps().multiply(TYPE_YARD_FACTOR).build()
.createFromBaseConverterSteps().divide(TYPE_YARD_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_YARD_FACTOR).build()
.build() .build()
.createSIUnitTypes()
.setId( TYPE_ROD_ID)
.setName( TYPE_ROD_NAME)
.addTypeFlag( TYPE_ROD_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA)
.createToBaseConverterSteps().multiply(TYPE_ROD_FACTOR).build()
.createFromBaseConverterSteps().divide(TYPE_ROD_FACTOR).build()
.build()
.createSIUnitTypes()
.setId( TYPE_CHAIN_ID)
.setName( TYPE_CHAIN_NAME)
.addTypeFlag( TYPE_CHAIN_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA)
.createToBaseConverterSteps().multiply(TYPE_CHAIN_FACTOR).build()
.createFromBaseConverterSteps().divide(TYPE_CHAIN_FACTOR).build()
.build()
.createSIUnitTypes()
.setId( TYPE_FURLONG_ID)
.setName( TYPE_FURLONG_NAME)
.addTypeFlag( TYPE_FURLONG_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA)
.createToBaseConverterSteps().multiply(TYPE_FURLONG_FACTOR).build()
.createFromBaseConverterSteps().divide(TYPE_FURLONG_FACTOR).build()
.build()
.createSIUnitTypes()
.setId( TYPE_MILE_ID)
.setName( TYPE_MILE_NAME)
.addTypeFlag( TYPE_MILE_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.createToBaseConverterSteps().multiply(TYPE_MILE_FACTOR).build()
.createFromBaseConverterSteps().divide(TYPE_MILE_FACTOR).build()
.build()
.createSIUnitTypes()
.setId( TYPE_MILE_US_ID)
.setName( TYPE_MILE_US_NAME)
.addTypeFlag( TYPE_MILE_US_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.createToBaseConverterSteps().multiply(TYPE_MILE_US_FACTOR).build()
.createFromBaseConverterSteps().divide(TYPE_MILE_US_FACTOR).build()
.build()
.createSIUnitTypes()
.setId( TYPE_MILE_NAUTICAL_ID)
.setName( TYPE_MILE_NAUTICAL_NAME)
.addTypeFlag( TYPE_MILE_NAUTICAL_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.createToBaseConverterSteps().multiply(TYPE_MILE_NAUTICAL_FACTOR).build()
.createFromBaseConverterSteps().divide(TYPE_MILE_NAUTICAL_FACTOR).build()
.build()
.build() .build()
; ;
// um.addUnitType(createType(UNIT_GROUP_LENGTH,"in", -3, 25.4 , "inch", OPT_TYPE_IMPERIAL));
// um.addUnitType(createType(UNIT_GROUP_LENGTH,"li", -3, 201.1 , "link", OPT_TYPE_IMPERIAL_EXTRA));
// um.addUnitType(createType(UNIT_GROUP_LENGTH,"ft", -3, 304.8 , "foot", OPT_TYPE_IMPERIAL));
// um.addUnitType(createType(UNIT_GROUP_LENGTH,"yd", 0, 0.9144 , "yard", OPT_TYPE_IMPERIAL));
// um.addUnitType(createType(UNIT_GROUP_LENGTH,"rd", 0, 5.029 , "rod", OPT_TYPE_IMPERIAL_EXTRA));
// um.addUnitType(createType(UNIT_GROUP_LENGTH,"ch", 0, 20.117 , "chain", OPT_TYPE_IMPERIAL_EXTRA));
// um.addUnitType(createType(UNIT_GROUP_LENGTH,"fur", 0, 201.17 , "furlong", OPT_TYPE_IMPERIAL_EXTRA));
// um.addUnitType(createType(UNIT_GROUP_LENGTH,"mi", 0, 1609.3 , "mile", OPT_TYPE_IMPERIAL));
} }
} }

View file

@ -23,6 +23,9 @@
package net.forwardfire.unitxc.module; package net.forwardfire.unitxc.module;
import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag;
import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE;
import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
@ -32,11 +35,12 @@ public class UnitXCModuleLuminousIntensity implements UnitXCConfigModule {
public static final String GROUP_DESCRIPTION = "The candela is the luminous intensity, in a given direction, of a source that emits monochromatic radiation of frequency 540×1012 hertz and that has a radiant intensity in that direction of 1/683 watt per steradian."; public static final String GROUP_DESCRIPTION = "The candela is the luminous intensity, in a given direction, of a source that emits monochromatic radiation of frequency 540×1012 hertz and that has a radiant intensity in that direction of 1/683 watt per steradian.";
public static final String TYPE_CANDELA_ID = "cd"; public static final String TYPE_CANDELA_ID = "cd";
public static final String TYPE_CANDELA_NAME = "candela"; public static final String TYPE_CANDELA_NAME = "candela";
public static final String TYPE_CANDELA_FLAG = (GROUP_ID+"_"+TYPE_CANDELA_NAME).toUpperCase(); public static final String TYPE_CANDELA_FLAG = buildFlag(GROUP_ID,TYPE_CANDELA_NAME);
@Override @Override
public void configModule(UnitXCConfigBuilder builder) { public void configModule(UnitXCConfigBuilder builder) {
builder.createUnitTypeGroup() builder.createUnitGroup()
.setType( SI_BASE)
.setId( GROUP_ID) .setId( GROUP_ID)
.setName( TYPE_CANDELA_NAME) .setName( TYPE_CANDELA_NAME)
.setDescription ( GROUP_DESCRIPTION) .setDescription ( GROUP_DESCRIPTION)

View file

@ -0,0 +1,133 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.module;
import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag;
import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE;
import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
import net.forwardfire.unitxc.config.builder.UnitXCTypeSIPrefix;
public class UnitXCModuleMass implements UnitXCConfigModule {
public static final String GROUP_ID = "mass";
public static final String GROUP_NAME = "kilogram";
public static final String GROUP_DESCRIPTION = "The kilogram is the unit of mass; it is equal to the mass of the international prototype of the kilogram.";
public static final String TYPE_GRAM_ID = "g";
public static final String TYPE_GRAM_NAME = "gram";
public static final String TYPE_GRAM_FLAG = buildFlag(GROUP_ID,TYPE_GRAM_NAME);
public static final String TYPE_GRAIN_ID = "gr";
public static final String TYPE_GRAIN_NAME = "grain";
public static final String TYPE_GRAIN_FLAG = buildFlag(GROUP_ID,TYPE_GRAIN_NAME);
private static final double TYPE_GRAIN_FACTOR = 64.80;
public static final String TYPE_DRAM_ID = "dr";
public static final String TYPE_DRAM_NAME = "dram";
public static final String TYPE_DRAM_FLAG = buildFlag(GROUP_ID,TYPE_DRAM_NAME);
private static final double TYPE_DRAM_FACTOR = 1.772;
public static final String TYPE_OUNCE_ID = "oz";
public static final String TYPE_OUNCE_NAME = "ounce";
public static final String TYPE_OUNCE_FLAG = buildFlag(GROUP_ID,TYPE_OUNCE_NAME);
private static final double TYPE_OUNCE_FACTOR = 28.35;
public static final String TYPE_POUND_ID = "lb";
public static final String TYPE_POUND_NAME = "pound";
public static final String TYPE_POUND_FLAG = buildFlag(GROUP_ID,TYPE_POUND_NAME);
private static final double TYPE_POUND_FACTOR = 453.6;
public static final String TYPE_STONE_ID = "st";
public static final String TYPE_STONE_NAME = "stone";
public static final String TYPE_STONE_FLAG = buildFlag(GROUP_ID,TYPE_STONE_NAME);
private static final double TYPE_STONE_FACTOR = 6.350;
@Override
public void configModule(UnitXCConfigBuilder builder) {
builder.createUnitGroup()
.setType( SI_BASE)
.setId( GROUP_ID)
.setName( GROUP_NAME)
.setDescription ( GROUP_DESCRIPTION)
.setBaseTypeId( TYPE_GRAM_ID)
.createSIUnitTypes()
.setId( TYPE_GRAM_ID)
.setName( TYPE_GRAM_NAME)
.addTypeFlag( TYPE_GRAM_FLAG)
.addCommonSIPrefix(UnitXCTypeSIPrefix.MEGA)
.addCommonSIPrefix(UnitXCTypeSIPrefix.KILO)
.addCommonSIPrefix(UnitXCTypeSIPrefix.HECTO)
.addCommonSIPrefix(UnitXCTypeSIPrefix.DECI)
.addCommonSIPrefix(UnitXCTypeSIPrefix.DECA)
.addCommonSIPrefix(UnitXCTypeSIPrefix.CENTI)
.addCommonSIPrefix(UnitXCTypeSIPrefix.MILLI)
.addCommonSIPrefix(UnitXCTypeSIPrefix.MICRO)
.build()
.createSIUnitTypes()
.setId( TYPE_GRAIN_ID)
.setName( TYPE_GRAIN_NAME)
.addTypeFlag( TYPE_GRAIN_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.createToBaseConverterSteps().multiply(TYPE_GRAIN_FACTOR).power10Down(UnitXCTypeSIPrefix.MILLI).build()
.createFromBaseConverterSteps().power10Up(UnitXCTypeSIPrefix.MILLI).divide(TYPE_GRAIN_FACTOR).build()
.build()
.createSIUnitTypes()
.setId( TYPE_DRAM_ID)
.setName( TYPE_DRAM_NAME)
.addTypeFlag( TYPE_DRAM_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA)
.createToBaseConverterSteps().multiply(TYPE_DRAM_FACTOR).build()
.createFromBaseConverterSteps().divide(TYPE_DRAM_FACTOR).build()
.build()
.createSIUnitTypes()
.setId( TYPE_OUNCE_ID)
.setName( TYPE_OUNCE_NAME)
.addTypeFlag( TYPE_OUNCE_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.createToBaseConverterSteps().multiply(TYPE_OUNCE_FACTOR).build()
.createFromBaseConverterSteps().divide(TYPE_OUNCE_FACTOR).build()
.build()
.createSIUnitTypes()
.setId( TYPE_POUND_ID)
.setName( TYPE_POUND_NAME)
.addTypeFlag( TYPE_POUND_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.createToBaseConverterSteps().multiply(TYPE_POUND_FACTOR).build()
.createFromBaseConverterSteps().divide(TYPE_POUND_FACTOR).build()
.build()
.createSIUnitTypes()
.setId( TYPE_STONE_ID)
.setName( TYPE_STONE_NAME)
.addTypeFlag( TYPE_STONE_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA)
.createToBaseConverterSteps().multiply(TYPE_STONE_FACTOR).power10Up(UnitXCTypeSIPrefix.MILLI).build()
.createFromBaseConverterSteps().power10Down(UnitXCTypeSIPrefix.MILLI).divide(TYPE_STONE_FACTOR).build()
.build()
.build()
;
}
}

View file

@ -26,6 +26,9 @@ package net.forwardfire.unitxc.module;
import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag;
import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE;
import org.apache.commons.lang3.math.Fraction; import org.apache.commons.lang3.math.Fraction;
public class UnitXCModuleTemperature implements UnitXCConfigModule { public class UnitXCModuleTemperature implements UnitXCConfigModule {
@ -35,32 +38,34 @@ public class UnitXCModuleTemperature implements UnitXCConfigModule {
public static final String TYPE_KELVIN_ID = "K"; public static final String TYPE_KELVIN_ID = "K";
public static final String TYPE_KELVIN_NAME = "kelvin"; public static final String TYPE_KELVIN_NAME = "kelvin";
public static final String TYPE_KELVIN_FLAG = (GROUP_ID+"_"+TYPE_KELVIN_NAME).toUpperCase(); public static final String TYPE_KELVIN_FLAG = buildFlag(GROUP_ID,TYPE_KELVIN_NAME);
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 = buildFlag(GROUP_ID,TYPE_CELSIUS_NAME);
private static final double TYPE_CELSIUS_OFFSET = 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_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 = buildFlag(GROUP_ID,TYPE_FAHRENHEIT_NAME);
private static final double TYPE_FAHRENHEIT_OFFSET = 459.67; private static final double TYPE_FAHRENHEIT_OFFSET = 459.67;
private static final Fraction TYPE_FAHRENHEIT_FRACTION = Fraction.getFraction(5,9); 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_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 = buildFlag(GROUP_ID,TYPE_RANKINE_NAME);
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_NAME_ALIAS = "romer";
public static final String TYPE_ROMER_FLAG = buildFlag(GROUP_ID,TYPE_ROMER_NAME_ALIAS);
private static final Fraction TYPE_ROMER_FRACTION = Fraction.getFraction(40,21); private static final Fraction TYPE_ROMER_FRACTION = Fraction.getFraction(40,21);
private static final double TYPE_ROMER_FRACTION_OFFSET = 7.5; private static final double TYPE_ROMER_FRACTION_OFFSET = 7.5;
@Override @Override
public void configModule(UnitXCConfigBuilder builder) { public void configModule(UnitXCConfigBuilder builder) {
builder.createUnitTypeGroup() builder.createUnitGroup()
.setType( SI_BASE)
.setId( GROUP_ID) .setId( GROUP_ID)
.setName( TYPE_KELVIN_NAME) .setName( TYPE_KELVIN_NAME)
.setDescription( GROUP_DESCRIPTION) .setDescription( GROUP_DESCRIPTION)
@ -81,6 +86,7 @@ public class UnitXCModuleTemperature implements UnitXCConfigModule {
.setId( TYPE_FAHRENHEIT_ID) .setId( TYPE_FAHRENHEIT_ID)
.setName( TYPE_FAHRENHEIT_NAME) .setName( TYPE_FAHRENHEIT_NAME)
.addTypeFlag( TYPE_FAHRENHEIT_FLAG) .addTypeFlag( TYPE_FAHRENHEIT_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.createToBaseConverterSteps() .createToBaseConverterSteps()
.offsetUp(TYPE_FAHRENHEIT_OFFSET) .offsetUp(TYPE_FAHRENHEIT_OFFSET)
.multiply(TYPE_FAHRENHEIT_FRACTION) .multiply(TYPE_FAHRENHEIT_FRACTION)
@ -100,6 +106,7 @@ public class UnitXCModuleTemperature implements UnitXCConfigModule {
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_ROMER_ID) .setId( TYPE_ROMER_ID)
.setName( TYPE_ROMER_NAME) .setName( TYPE_ROMER_NAME)
.addNameAlias(TYPE_ROMER_NAME_ALIAS)
.addTypeFlag( TYPE_ROMER_FLAG) .addTypeFlag( TYPE_ROMER_FLAG)
.createToBaseConverterSteps() .createToBaseConverterSteps()
.offsetUp(TYPE_CELSIUS_OFFSET) .offsetUp(TYPE_CELSIUS_OFFSET)

View file

@ -0,0 +1,117 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.module;
import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag;
import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE;
import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
import net.forwardfire.unitxc.config.builder.UnitXCTypeSIPrefix;
public class UnitXCModuleTime implements UnitXCConfigModule {
public static final String GROUP_ID = "time";
public static final String GROUP_DESCRIPTION = "The second is the duration of 9192631770 periods of the radiation corresponding to the transition between the two hyperfine levels of the ground state of the caesium 133 atom.";
public static final String TYPE_SECOND_ID = "s";
public static final String TYPE_SECOND_NAME = "second";
public static final String TYPE_SECOND_NAMES = TYPE_SECOND_NAME+TYPE_SECOND_ID;
public static final String TYPE_SECOND_FLAG = buildFlag(GROUP_ID,TYPE_SECOND_NAME);
private static final double TIME_EARTH_MINUTE = 60;
private static final double TIME_EARTH_HOUR = TIME_EARTH_MINUTE*60;
private static final double TIME_EARTH_DAY = TIME_EARTH_HOUR*24;
private static final double TIME_EARTH_WEEK = TIME_EARTH_DAY*7;
private static final double TIME_EARTH_YEAR = 31557600;
// related to the rotating earth, or change to be 794 243 384 928 000 hyperfine oscillations
public static final String TYPE_MINUTE_NAME = "minute";
public static final String TYPE_MINUTE_FLAG = buildFlag(GROUP_ID,TYPE_MINUTE_NAME);
public static final String TYPE_HOUR_NAME = "hour";
public static final String TYPE_HOUR_FLAG = buildFlag(GROUP_ID,TYPE_HOUR_NAME);
public static final String TYPE_DAY_NAME = "day";
public static final String TYPE_DAY_FLAG = buildFlag(GROUP_ID,TYPE_DAY_NAME);
public static final String TYPE_WEEK_NAME = "week";
public static final String TYPE_WEEK_FLAG = buildFlag(GROUP_ID,TYPE_WEEK_NAME);
public static final String TYPE_YEAR_NAME = "year";
public static final String TYPE_YEAR_FLAG = buildFlag(GROUP_ID,TYPE_YEAR_NAME);
@Override
public void configModule(UnitXCConfigBuilder builder) {
builder.createUnitGroup()
.setType( SI_BASE)
.setId( GROUP_ID)
.setName( TYPE_SECOND_NAME)
.setDescription ( GROUP_DESCRIPTION)
.setBaseTypeId( TYPE_SECOND_ID)
.createSIUnitTypes()
.setId( TYPE_SECOND_ID)
.setName( TYPE_SECOND_NAME)
.addTypeFlag( TYPE_SECOND_FLAG)
.addCommonSIPrefix(UnitXCTypeSIPrefix.MILLI)
.addCommonSIPrefix(UnitXCTypeSIPrefix.MICRO)
.addCommonSIPrefix(UnitXCTypeSIPrefix.NANO)
.addCommonSIPrefix(UnitXCTypeSIPrefix.PICO)
.addCommonSIPrefix(UnitXCTypeSIPrefix.FEMTO)
.addCommonSIPrefix(UnitXCTypeSIPrefix.ATTO)
.build()
.createSIUnitTypes()
.setId( TYPE_MINUTE_NAME)
.setName( TYPE_MINUTE_NAME)
.addTypeFlag( TYPE_MINUTE_FLAG)
.createToBaseConverterSteps().multiply(TIME_EARTH_MINUTE).build()
.createFromBaseConverterSteps().divide(TIME_EARTH_MINUTE).build()
.build()
.createSIUnitTypes()
.setId( TYPE_HOUR_NAME)
.setName( TYPE_HOUR_NAME)
.addTypeFlag( TYPE_HOUR_FLAG)
.createToBaseConverterSteps().multiply(TIME_EARTH_HOUR).build()
.createFromBaseConverterSteps().divide(TIME_EARTH_HOUR).build()
.build()
.createSIUnitTypes()
.setId( TYPE_DAY_NAME)
.setName( TYPE_DAY_NAME)
.addTypeFlag( TYPE_DAY_FLAG)
.createToBaseConverterSteps().multiply(TIME_EARTH_DAY).build()
.createFromBaseConverterSteps().divide(TIME_EARTH_DAY).build()
.build()
.createSIUnitTypes()
.setId( TYPE_WEEK_NAME)
.setName( TYPE_WEEK_NAME)
.addTypeFlag( TYPE_WEEK_FLAG)
.createToBaseConverterSteps().multiply(TIME_EARTH_WEEK).build()
.createFromBaseConverterSteps().divide(TIME_EARTH_WEEK).build()
.build()
.createSIUnitTypes()
.setId( TYPE_YEAR_NAME)
.setName( TYPE_YEAR_NAME)
.addTypeFlag( TYPE_YEAR_FLAG)
.createToBaseConverterSteps().multiply(TIME_EARTH_YEAR).build()
.createFromBaseConverterSteps().divide(TIME_EARTH_YEAR).build()
.build()
.build()
;
}
}

View file

@ -0,0 +1,58 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.module;
import static net.forwardfire.unitxc.config.builder.UnitXCNumberTypeSetting.toSuperScript;
import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_DERIVED;
import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
public class UnitXCModuleVolume implements UnitXCConfigModule {
private static final int UNIT_COMPOUND_EXPONENT = 3;
private static final String UNIT_ID_POSTFIX = toSuperScript(UNIT_COMPOUND_EXPONENT);
private static final String UNIT_NAME_PREFIX = "cubic ";
public static final String GROUP_ID = "volume";
public static final String GROUP_NAME = UNIT_NAME_PREFIX+UnitXCModuleLength.TYPE_METRE_NAME;
public static final String GROUP_DESCRIPTION = "It is the volume of a cube with edges one metre in length.";
public static final String GROUP_BASE_TYPE_ID = UnitXCModuleLength.TYPE_METRE_ID+UNIT_ID_POSTFIX;
@Override
public void configModule(UnitXCConfigBuilder builder) {
builder.createUnitGroup()
.setType( SI_DERIVED)
.setId( GROUP_ID)
.setName( GROUP_NAME)
.setDescription ( GROUP_DESCRIPTION)
.setBaseTypeId( GROUP_BASE_TYPE_ID)
.createCompoundExponentUnitTypes(UnitXCModuleLength.GROUP_ID, UNIT_COMPOUND_EXPONENT)
.setUnitIdPostfix(UNIT_ID_POSTFIX)
.setUnitNamePrefix(UNIT_NAME_PREFIX)
.build()
.build()
;
}
}

View file

@ -21,7 +21,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package net.forwardfire.unitxc.module; package net.forwardfire.unitxc;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
@ -35,11 +35,11 @@ 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.model.UnitXCType; import net.forwardfire.unitxc.model.UnitXCType;
import net.forwardfire.unitxc.model.UnitXCTypeGroup; import net.forwardfire.unitxc.model.UnitXCGroup;
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 UnitXCConverterStepTest {
@Test @Test
public void testFac() { public void testFac() {
@ -47,18 +47,19 @@ public class UnitXCTemperatureModuleTest {
assertNotNull(unitManager); assertNotNull(unitManager);
assertNotNull(unitManager.getUnitTypes()); assertNotNull(unitManager.getUnitTypes());
assertNotNull(unitManager.getUnitTypeGroups()); assertNotNull(unitManager.getUnitGroups());
assertFalse(unitManager.getUnitTypes().isEmpty()); assertFalse(unitManager.getUnitTypes().isEmpty());
assertFalse(unitManager.getUnitTypeGroups().isEmpty()); assertFalse(unitManager.getUnitGroups().isEmpty());
assertNotNull(unitManager.getUnitTypes()); assertNotNull(unitManager.getUnitTypes());
for (UnitXCTypeGroup typeGroup:unitManager.getUnitTypeGroups()) { for (UnitXCGroup typeGroup:unitManager.getUnitGroups()) {
System.out.println("TypeGroup: "+typeGroup.getId()+"\t name: "+typeGroup.getName()+"\t\t baseType: "+typeGroup.getBaseTypeId()); UnitXCType type = unitManager.getUnitType(typeGroup.getBaseTypeId());
System.out.println("TypeGroup: "+typeGroup.getId()+"\t name: "+typeGroup.getName()+"\t\t baseType: "+typeGroup.getBaseTypeId()+" id: "+type.getId());
int total = 0; int total = 0;
for (UnitXCType type:unitManager.getUnitTypes()) { for (UnitXCType t:unitManager.getUnitTypes()) {
if (!type.getTypeGroupId().equals(typeGroup.getId())) { if (!t.getUnitGroupId().equals(typeGroup.getId())) {
continue; continue;
} }
total++; total++;
@ -80,7 +81,18 @@ public class UnitXCTemperatureModuleTest {
System.out.println(""); System.out.println("");
result = unitManager.getConverter().convertStepped(640.0, "nK", "µK"); result = unitManager.getConverter().convertStepped(10.763, "ft²", "in²");
System.out.println("Convert from: "+result.getStartType().getName()+" to: "+result.getResultType().getName());
System.out.println("startValue: "+result.getStartValue());
System.out.println("resultValue: "+result.getResultValue());
System.out.println("convertTime: "+result.getConvertTime());
for (UnitXConverterResultStep step:result.getSteps()) {
System.out.println("step: "+step.getName()+" in: "+step.getInputValue()+" out: "+step.getOutputValue()+" time: "+step.getConvertTime());
}
System.out.println("");
result = unitManager.getConverter().convertStepped(10.763, "ft³", "in³");
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());
@ -90,28 +102,4 @@ public class UnitXCTemperatureModuleTest {
} }
} }
@Test
public void testConverterTempKvsC() throws Exception {
convertEquals(26.85,"°C",300.0, "K");
convertEquals(310.65,"K",37.5, "°C");
}
@Test
public void testConverterTempKvsF() throws Exception {
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 {
convertEquals(74.937,"°C",166.888, "°F");
convertEquals(211.99,"°F",100.0, "°C");
}
@Test
public void testConverterTempRvsC() throws Exception {
convertEquals(-143.64,"°C",233.12,"°R");
convertEquals(1691.06,"°R",666.333,"°C");
}
} }

View file

@ -49,62 +49,62 @@ public class UnitXCManagerTest {
@Test @Test
public void testGetUnitTypeResultTypeGroupId() { public void testGetUnitTypeResultTypeGroupId() {
assertNotNull(test.getUnitType(UnitXCModuleTemperature.TYPE_KELVIN_ID).getTypeGroupId()); assertNotNull(test.getUnitType(UnitXCModuleTemperature.TYPE_KELVIN_ID).getUnitGroupId());
} }
@Test @Test
public void testGetUnitTypeGroup() { public void testGetUnitTypeGroup() {
assertNotNull(test.getUnitTypeGroup(UnitXCModuleTemperature.GROUP_ID)); assertNotNull(test.getUnitGroup(UnitXCModuleTemperature.GROUP_ID));
} }
@Test(expected=NullPointerException.class) @Test(expected=NullPointerException.class)
public void testGetUnitTypeGroupNull() { public void testGetUnitTypeGroupNull() {
assertNotNull(test.getUnitTypeGroup(null)); assertNotNull(test.getUnitGroup(null));
} }
@Test(expected=NullPointerException.class) @Test(expected=NullPointerException.class)
public void testGetUnitTypeGroupInvalid() { public void testGetUnitTypeGroupInvalid() {
assertNotNull(test.getUnitTypeGroup(getClass().getName())); assertNotNull(test.getUnitGroup(getClass().getName()));
} }
@Test @Test
public void testGetUnitTypeGroupResultId() { public void testGetUnitTypeGroupResultId() {
assertNotNull(test.getUnitTypeGroup(UnitXCModuleTemperature.GROUP_ID).getId()); assertNotNull(test.getUnitGroup(UnitXCModuleTemperature.GROUP_ID).getId());
} }
@Test @Test
public void testGetUnitTypeGroupResultName() { public void testGetUnitTypeGroupResultName() {
assertNotNull(test.getUnitTypeGroup(UnitXCModuleTemperature.GROUP_ID).getName()); assertNotNull(test.getUnitGroup(UnitXCModuleTemperature.GROUP_ID).getName());
} }
@Test @Test
public void testGetUnitTypeGroupResultDescription() { public void testGetUnitTypeGroupResultDescription() {
assertNotNull(test.getUnitTypeGroup(UnitXCModuleTemperature.GROUP_ID).getDescription()); assertNotNull(test.getUnitGroup(UnitXCModuleTemperature.GROUP_ID).getDescription());
} }
@Test @Test
public void testGetUnitTypeResultTypeBaseTypeID() { public void testGetUnitTypeResultTypeBaseTypeID() {
assertNotNull(test.getUnitTypeGroup(UnitXCModuleTemperature.GROUP_ID).getBaseTypeId()); assertNotNull(test.getUnitGroup(UnitXCModuleTemperature.GROUP_ID).getBaseTypeId());
} }
@Test @Test
public void testIsUnitTypeGroupTrue() { public void testIsUnitTypeGroupTrue() {
assertTrue(test.isUnitTypeGroup(UnitXCModuleTemperature.GROUP_ID)); assertTrue(test.isUnitGroup(UnitXCModuleTemperature.GROUP_ID));
} }
@Test @Test
public void testIsUnitTypeGroupFalse() { public void testIsUnitTypeGroupFalse() {
assertFalse(test.isUnitTypeGroup(getClass().getName())); assertFalse(test.isUnitGroup(getClass().getName()));
} }
@Test(expected=NullPointerException.class) @Test(expected=NullPointerException.class)
public void testIsUnitTypeGroupNull() { public void testIsUnitTypeGroupNull() {
assertTrue(test.isUnitTypeGroup(null)); assertTrue(test.isUnitGroup(null));
} }
@Test(expected=IllegalArgumentException.class) @Test(expected=IllegalArgumentException.class)
public void testIsUnitTypeGroupEmpty() { public void testIsUnitTypeGroupEmpty() {
assertTrue(test.isUnitTypeGroup("")); assertTrue(test.isUnitGroup(""));
} }
@Test @Test
@ -124,6 +124,6 @@ public class UnitXCManagerTest {
@Test(expected=IllegalArgumentException.class) @Test(expected=IllegalArgumentException.class)
public void testIsUnitTypeEmpty() { public void testIsUnitTypeEmpty() {
assertTrue(test.isUnitTypeGroup("")); assertTrue(test.isUnitGroup(""));
} }
} }

View file

@ -0,0 +1,96 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.module;
import org.junit.Test;
import static net.forwardfire.unitxc.UnitXCAssert.convertEquals;
public class UnitXCModuleLengthTest {
@Test
public void test10cm2mm() {
convertEquals(100.0,"mm",10.0, "cm");
}
@Test
public void test2mm2cm() {
convertEquals(1.0,"cm",10.0,"mm");
}
@Test
public void test004567km2cm() {
convertEquals(4567,"cm",0.04567,"km");
}
@Test
public void test2cm2mm() {
convertEquals(232.0,"mm",23.2, "cm");
}
@Test
public void test2345mm2cm() {
convertEquals(234.56,"cm",2345.6, "mm");
}
@Test
public void test1in2mm() {
convertEquals(25.4,"mm",1.0, "in");
}
@Test
public void test25mm2in() {
convertEquals(1.0,"in",25.4,"mm");
}
@Test
public void test2ft2mm() {
convertEquals(304.8*2,"mm",2.0,"ft");
}
@Test
public void test2ft2m() {
convertEquals(0.6096,"m",2.0, "ft");
}
@Test
public void test06m2in() {
convertEquals(24.0,"in",0.6096, "m");
}
@Test
public void test2ft2in() {
convertEquals(24.0,"in",2.0, "ft");
}
@Test
public void test2ft2cm() {
convertEquals(73.152,"cm",2.4, "ft");
}
@Test
public void test24ft2m() {
convertEquals(7.315,"m",24, "ft");
}
}

View file

@ -0,0 +1,76 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.module;
import org.junit.Test;
import static net.forwardfire.unitxc.UnitXCAssert.convertEquals;
public class UnitXCModuleTemperatureTest {
@Test
public void test300K2C() throws Exception {
convertEquals(26.85,"°C",300.0, "K");
}
@Test
public void test37C2K() throws Exception {
convertEquals(310.65,"K",37.5, "°C");
}
@Test
public void test300K2F() throws Exception {
convertEquals(80.32,"°F",300.0, "K");
}
@Test
public void test37C2F() throws Exception {
convertEquals(276.2,"K",37.5, "°F");
}
@Test
public void test37F2K() throws Exception {
convertEquals(276.208,"K",37.505, "°F");
}
@Test
public void test166F2C() throws Exception {
convertEquals(74.937,"°C",166.888, "°F");
}
@Test
public void test100C2F() throws Exception {
convertEquals(211.99,"°F",100.0, "°C");
}
@Test
public void test233R2C() throws Exception {
convertEquals(-143.64,"°C",233.12,"°R");
}
@Test
public void test666C2R() throws Exception {
convertEquals(1691.06,"°R",666.333,"°C");
}
}

View file

@ -0,0 +1,61 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.module;
import org.junit.Test;
import static net.forwardfire.unitxc.UnitXCAssert.convertEquals;
public class UnitXCModuleTimeTest {
@Test
public void test30SecondToMinute() throws Exception {
convertEquals(0.5,"minute",30.0, "s");
}
@Test
public void test300SecondToMinute() throws Exception {
convertEquals(5,"minute",300.0, "s");
}
@Test
public void test7200SecondToHour() throws Exception {
convertEquals(2,"hour",7200.0, "s");
}
@Test
public void test120MinuteToHour() throws Exception {
convertEquals(2,"hour",120.0, "minute");
}
@Test
public void test300MinuteToHour() throws Exception {
convertEquals(5,"hour",300.0, "minute");
}
@Test
public void test48HourToDay() throws Exception {
convertEquals(2,"day",48, "hour");
}
}