WIP made compond units working and a bit of xml

This commit is contained in:
Willem 2015-12-27 15:46:36 +01:00
parent 1324e19c7a
commit 01cdb0b478
44 changed files with 1654 additions and 732 deletions

View file

@ -36,6 +36,7 @@ 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.UnitXCModuleMass;
import net.forwardfire.unitxc.module.UnitXCModuleSpeed;
import net.forwardfire.unitxc.module.UnitXCModuleTemperature; import net.forwardfire.unitxc.module.UnitXCModuleTemperature;
import net.forwardfire.unitxc.module.UnitXCModuleTime; import net.forwardfire.unitxc.module.UnitXCModuleTime;
import net.forwardfire.unitxc.module.UnitXCModuleVolume; import net.forwardfire.unitxc.module.UnitXCModuleVolume;
@ -57,7 +58,8 @@ public final class UnitXCFactory {
new UnitXCModuleMass(), new UnitXCModuleMass(),
new UnitXCModuleTime(), new UnitXCModuleTime(),
new UnitXCModuleArea(), new UnitXCModuleArea(),
new UnitXCModuleVolume() new UnitXCModuleVolume(),
new UnitXCModuleSpeed()
)); ));

View file

@ -29,8 +29,8 @@ 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.UnitXCType;
import net.forwardfire.unitxc.model.UnitXCGroupModel; import net.forwardfire.unitxc.model.UnitXCGroup;
/** /**
* Config to build unit types and groups. * Config to build unit types and groups.
@ -40,8 +40,8 @@ import net.forwardfire.unitxc.model.UnitXCGroupModel;
*/ */
public final class UnitXCConfig { public final class UnitXCConfig {
private final Map<String,UnitXCTypeModel> unitTypes; private final Map<String,UnitXCType> unitTypes;
private final Map<String,UnitXCGroupModel> unitGroups; private final Map<String,UnitXCGroup> unitGroups;
public UnitXCConfig() { public UnitXCConfig() {
unitGroups = new HashMap<>(); unitGroups = new HashMap<>();
@ -51,35 +51,35 @@ public final class UnitXCConfig {
/** /**
* @return the unitTypes * @return the unitTypes
*/ */
public Collection<UnitXCTypeModel> getUnitTypes() { public Collection<UnitXCType> getUnitTypes() {
return unitTypes.values(); return unitTypes.values();
} }
public void addUnitType(UnitXCTypeModel unitType) { public void addUnitType(UnitXCType unitType) {
putUnitType(validateGroupId(Validate.notNull(unitType).validate())); putUnitType(validateGroupId(Validate.notNull(unitType).validate()));
} }
private UnitXCTypeModel validateGroupId(UnitXCTypeModel unitType) { private UnitXCType validateGroupId(UnitXCType unitType) {
//Validate.isTrue(unitTypeGroups.containsKey(unitType.getTypeGroupId()),"group is missing"); //Validate.isTrue(unitTypeGroups.containsKey(unitType.getTypeGroupId()),"group is missing");
return unitType; return unitType;
} }
private void putUnitType(UnitXCTypeModel unitType) { private void putUnitType(UnitXCType unitType) {
unitTypes.put(unitType.getId(), unitType); unitTypes.put(unitType.getId(), unitType);
} }
/** /**
* @return the unitGroups * @return the unitGroups
*/ */
public Collection<UnitXCGroupModel> getUnitroups() { public Collection<UnitXCGroup> getUnitroups() {
return unitGroups.values(); return unitGroups.values();
} }
public void addUnitGroup(UnitXCGroupModel unitTypeGroup) { public void addUnitGroup(UnitXCGroup unitTypeGroup) {
putUnitGroup(Validate.notNull(unitTypeGroup).validate()); putUnitGroup(Validate.notNull(unitTypeGroup).validate());
} }
private void putUnitGroup(UnitXCGroupModel unitTypeGroup) { private void putUnitGroup(UnitXCGroup unitTypeGroup) {
unitGroups.put(unitTypeGroup.getId(),unitTypeGroup); unitGroups.put(unitTypeGroup.getId(),unitTypeGroup);
} }
} }

View file

@ -33,8 +33,6 @@ import org.apache.commons.lang3.Validate;
import net.forwardfire.unitxc.UnitXCManager; 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.UnitXCGroupModel;
import net.forwardfire.unitxc.model.UnitXCType; import net.forwardfire.unitxc.model.UnitXCType;
import net.forwardfire.unitxc.model.UnitXCGroup; import net.forwardfire.unitxc.model.UnitXCGroup;
import net.forwardfire.unitxc.model.UnitXConverterResult; import net.forwardfire.unitxc.model.UnitXConverterResult;
@ -62,13 +60,29 @@ public class UnitXCConfigManager implements UnitXCManager {
this.converter = new UnitXConverterImpl(); this.converter = new UnitXConverterImpl();
} }
private static Map<String,UnitXCType> createUnitTypes(Collection<UnitXCTypeModel> values) { private static Map<String,UnitXCType> createUnitTypes(Collection<UnitXCType> values) {
Map<String,UnitXCType> result = new HashMap<>(); Map<String,UnitXCType> result = new HashMap<>();
values.forEach((value) -> result.put(value.getId(), value)); values.forEach((value) -> {
putValue(result,value.getId(),value);
if (!value.getId().equals(value.getName())) { // fix time group
putValue(result,value.getName(),value);
}
if (!value.getName().equals(value.getNamePlural())) { // mmm normal
putValue(result,value.getNamePlural(),value);
}
});
return result; return result;
} }
private static Map<String,UnitXCGroup> createUnitGroups(Collection<UnitXCGroupModel> values) { private static void putValue(Map<String,UnitXCType> result,String key,UnitXCType value) {
if (key == null) {
return;
}
Validate.isTrue(!result.containsKey(key),"Duplicate key: "+key);
result.put(key, value);
}
private static Map<String,UnitXCGroup> createUnitGroups(Collection<UnitXCGroup> values) {
Map<String,UnitXCGroup> 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;
@ -130,7 +144,7 @@ public class UnitXCConfigManager implements UnitXCManager {
@Override @Override
public UnitXConverterResult convertStepped(double value, UnitXCType fromType, UnitXCType toType) { public UnitXConverterResult convertStepped(double value, UnitXCType fromType, UnitXCType toType) {
// TODO: rm casting // TODO: rm casting
return convertEngine.convertStepped(value, (UnitXCTypeModel)fromType, (UnitXCTypeModel)toType); return convertEngine.convertStepped(value, (UnitXCType)fromType, (UnitXCType)toType);
} }
} }
} }

View file

@ -29,7 +29,7 @@ import java.util.List;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
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.UnitXCType;
/** /**
* *
@ -37,10 +37,23 @@ import net.forwardfire.unitxc.model.UnitXCTypeModel;
* @author Willem Cazander * @author Willem Cazander
* @version 1.0 Oct 22, 2015 * @version 1.0 Oct 22, 2015
*/ */
public abstract class AbstractUnitXCTypeBuilder<P,B> extends AbstractUnitXCBuilder<P,UnitXCTypeModel,B> { public abstract class AbstractUnitXCTypeBuilder<P,B> extends AbstractUnitXCBuilder<P,UnitXCType,B> {
public AbstractUnitXCTypeBuilder(P parent,BiConsumer<P, UnitXCTypeModel> parentBuilder) { public AbstractUnitXCTypeBuilder(P parent,UnitXCType model,BiConsumer<P, UnitXCType> parentBuilder) {
super(parent, new UnitXCTypeModel(), parentBuilder); super(parent, model, parentBuilder);
}
protected final void buildPreValue(UnitXCType value) {
if (value.getNamePlural() == null) {
setNamePlurals();
}
if (value.getAliasOfType() != null && !value.getTypeFlags().contains(UnitXCConfigBuilder.TYPE_FLAG_ALIAS)) {
value.getTypeFlags().add(UnitXCConfigBuilder.TYPE_FLAG_ALIAS);
}
buildModel(value);
}
protected void buildModel(UnitXCType value) {
} }
public B setId(String id) { public B setId(String id) {
@ -51,28 +64,40 @@ 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) { public B setNamePlurals() {
return make((v) -> v.addNameAlias(nameAlias)); return setNamePlural(getValue().getName()+"s");
} }
public B addNameAliases(Collection<String> nameAliases) { public B setNamePlural(String name) {
return make((v) -> nameAliases.forEach(a -> v.addNameAlias(a))); return make((v) -> v.setNamePlural(name));
} }
public B addNameAliases(String[] nameAliases) { public B setAliasOfType(String aliasOfType) {
return addNameAliases(Arrays.asList(nameAliases)); return make((v) -> v.setAliasOfType(aliasOfType));
} }
public B setTypeGroupId(String typeGroupId) { public B setTypeGroupId(String typeGroupId) {
return make((v) -> v.setUnitGroupId(typeGroupId)); return make((v) -> v.setUnitGroupId(typeGroupId));
} }
public B addWebsiteWiki(String websiteLink) {
return addWebsiteLink("https://en.wikipedia.org/wiki/"+websiteLink);
}
public B addWebsiteLink(String websiteLink) {
return make((v) -> v.getWebsiteLinks().add(websiteLink));
}
public B addWebsiteLinks(Collection<String> websiteLinks) {
return make((v) -> v.getWebsiteLinks().addAll(websiteLinks));
}
public B addTypeFlag(String flag) { public B addTypeFlag(String flag) {
return make((v) -> v.addTypeFlag(flag)); return make((v) -> v.getTypeFlags().add(flag));
} }
public B addTypeFlags(Collection<String> flags) { public B addTypeFlags(Collection<String> flags) {
return make(v -> flags.forEach(flag -> v.addTypeFlag(flag))); return make(v -> flags.forEach(flag -> v.getTypeFlags().add(flag)));
} }
public B addTypeFlags(String[] flags) { public B addTypeFlags(String[] flags) {
@ -88,18 +113,18 @@ public abstract class AbstractUnitXCTypeBuilder<P,B> extends AbstractUnitXCBuild
} }
public B addFromBaseConverterStep(UnitXConverterStep unitTypeConverter) { public B addFromBaseConverterStep(UnitXConverterStep unitTypeConverter) {
return make((v) -> v.addFromBaseConverterStep(unitTypeConverter)); return make((v) -> v.getFromBaseConverterSteps().add(unitTypeConverter));
} }
public B addFromBaseConverterSteps(List<UnitXConverterStep> unitTypeConverters) { public B addFromBaseConverterSteps(List<UnitXConverterStep> unitTypeConverters) {
return make((v) -> unitTypeConverters.forEach(c -> v.addFromBaseConverterStep(c))); return make((v) -> unitTypeConverters.forEach(c -> v.getFromBaseConverterSteps().add(c)));
} }
public B addToBaseConverterStep(UnitXConverterStep unitTypeConverter) { public B addToBaseConverterStep(UnitXConverterStep unitTypeConverter) {
return make((v) -> v.addToBaseConverterStep(unitTypeConverter)); return make((v) -> v.getToBaseConverterSteps().add(unitTypeConverter));
} }
public B addToBaseConverterSteps(List<UnitXConverterStep> unitTypeConverters) { public B addToBaseConverterSteps(List<UnitXConverterStep> unitTypeConverters) {
return make((v) -> unitTypeConverters.forEach(c -> v.addToBaseConverterStep(c))); return make((v) -> unitTypeConverters.forEach(c -> v.getToBaseConverterSteps().add(c)));
} }
} }

View file

@ -26,6 +26,7 @@ package net.forwardfire.unitxc.config.builder;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
import net.forwardfire.unitxc.config.UnitXCConfig; import net.forwardfire.unitxc.config.UnitXCConfig;
import net.forwardfire.unitxc.model.UnitXCType;
/** /**
* *
@ -37,6 +38,7 @@ public class UnitXCConfigBuilder {
public static final String TYPE_FLAG_IMPERIAL = "IMPERIAL"; // todo move to ... public static final String TYPE_FLAG_IMPERIAL = "IMPERIAL"; // todo move to ...
public static final String TYPE_FLAG_IMPERIAL_EXTRA = "IMPERIAL_EXTRA"; public static final String TYPE_FLAG_IMPERIAL_EXTRA = "IMPERIAL_EXTRA";
public static final String TYPE_FLAG_ALIAS = "ALIAS";
private final UnitXCConfig config; private final UnitXCConfig config;
@ -53,6 +55,19 @@ public class UnitXCConfigBuilder {
} }
public UnitXCTypeBuilder<UnitXCConfigBuilder> createUnitType() { public UnitXCTypeBuilder<UnitXCConfigBuilder> createUnitType() {
return new UnitXCTypeBuilder<>(this,(p,v) -> p.getConfig().addUnitType(v)); return new UnitXCTypeBuilder<>(this,new UnitXCType(),(p,v) -> p.getConfig().addUnitType(v));
}
private UnitXCType findType(String id) {
for (UnitXCType m:config.getUnitTypes()) {
if (m.getId().equals(id)) {
return m;
}
}
throw new IllegalArgumentException("Could not find: "+id);
}
public UnitXCTypeBuilder<UnitXCConfigBuilder> extendUnitType(String id) {
return new UnitXCTypeBuilder<>(this,findType(id),(p,v) -> {});
} }
} }

View file

@ -23,8 +23,9 @@
package net.forwardfire.unitxc.config.builder; package net.forwardfire.unitxc.config.builder;
import net.forwardfire.unitxc.model.UnitXCGroupModel; import net.forwardfire.unitxc.model.UnitXCGroup;
import net.forwardfire.unitxc.model.UnitXCGroupType; import net.forwardfire.unitxc.model.UnitXCGroupLevel;
import net.forwardfire.unitxc.model.UnitXCType;
/** /**
* *
@ -32,10 +33,10 @@ import net.forwardfire.unitxc.model.UnitXCGroupType;
* @author Willem Cazander * @author Willem Cazander
* @version 1.0 Oct 22, 2015 * @version 1.0 Oct 22, 2015
*/ */
public class UnitXCGroupBuilder extends AbstractUnitXCBuilder<UnitXCConfigBuilder,UnitXCGroupModel,UnitXCGroupBuilder> { public class UnitXCGroupBuilder extends AbstractUnitXCBuilder<UnitXCConfigBuilder,UnitXCGroup,UnitXCGroupBuilder> {
public UnitXCGroupBuilder(UnitXCConfigBuilder parent) { public UnitXCGroupBuilder(UnitXCConfigBuilder parent) {
super(parent, new UnitXCGroupModel(), (p,v) -> p.getConfig().addUnitGroup(v)); super(parent, new UnitXCGroup(), (p,v) -> p.getConfig().addUnitGroup(v));
} }
@Override @Override
@ -44,18 +45,21 @@ public class UnitXCGroupBuilder extends AbstractUnitXCBuilder<UnitXCConfigBuilde
} }
public UnitXCTypeBuilder<UnitXCGroupBuilder> createUnitType() { public UnitXCTypeBuilder<UnitXCGroupBuilder> createUnitType() {
return new UnitXCTypeBuilder<>(this,(p,v) -> p.getParent().getConfig().addUnitType(v)).setTypeGroupId(getValue().getId()); return new UnitXCTypeBuilder<>(this,new UnitXCType(),(p,v) -> p.getParent().getConfig().addUnitType(v)).setTypeGroupId(getValue().getId());
} }
public UnitXCTypeSIPrefixBuilder<UnitXCGroupBuilder> createSIUnitTypes() { public UnitXCTypeSIPrefixBuilder<UnitXCGroupBuilder> createSIUnitTypes() {
return new UnitXCTypeSIPrefixBuilder<>(this,getParent()).setTypeGroupId(getValue().getId()); return new UnitXCTypeSIPrefixBuilder<>(this,getParent()).setTypeGroupId(getValue().getId());
} }
public UnitXCTypeCompoundExponentBuilder createCompoundExponentUnitTypes(String unitTypeGroupId,int exponent) { public UnitXCTypeCompoundExponentBuilder createCompoundExponentUnitTypes(String unitTypeGroupId,int exponent) {
return new UnitXCTypeCompoundExponentBuilder(this,getParent(),unitTypeGroupId,exponent); return new UnitXCTypeCompoundExponentBuilder(this,getParent(),unitTypeGroupId,exponent);
} }
public UnitXCTypeCompoundPairBuilder createCompoundPairUnitTypes(String groupId,String perGroupId) {
return new UnitXCTypeCompoundPairBuilder(this,getParent(),groupId,perGroupId);
}
public UnitXCGroupBuilder setId(String id) { public UnitXCGroupBuilder setId(String id) {
return make((v) -> v.setId(id)); return make((v) -> v.setId(id));
} }
@ -72,8 +76,8 @@ public class UnitXCGroupBuilder extends AbstractUnitXCBuilder<UnitXCConfigBuilde
return make((v) -> v.setBaseTypeId(baseTypeId)); return make((v) -> v.setBaseTypeId(baseTypeId));
} }
public UnitXCGroupBuilder setType(UnitXCGroupType type) { public UnitXCGroupBuilder setGroupLevel(UnitXCGroupLevel type) {
return make((v) -> v.setType(type)); return make((v) -> v.setGroupLevel(type));
} }
// public UnitXCGroupBuilder addDerivedFrom(String fromId) { // public UnitXCGroupBuilder addDerivedFrom(String fromId) {

View file

@ -25,7 +25,7 @@ package net.forwardfire.unitxc.config.builder;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import net.forwardfire.unitxc.model.UnitXCTypeModel; import net.forwardfire.unitxc.model.UnitXCType;
/** /**
* *
@ -35,8 +35,8 @@ import net.forwardfire.unitxc.model.UnitXCTypeModel;
*/ */
public class UnitXCTypeBuilder<P> extends AbstractUnitXCTypeBuilder<P,UnitXCTypeBuilder<P>> { public class UnitXCTypeBuilder<P> extends AbstractUnitXCTypeBuilder<P,UnitXCTypeBuilder<P>> {
public UnitXCTypeBuilder(P parent, BiConsumer<P, UnitXCTypeModel> parentBuilder) { public UnitXCTypeBuilder(P parent,UnitXCType model, BiConsumer<P, UnitXCType> parentBuilder) {
super(parent, parentBuilder); super(parent,model, parentBuilder);
} }
@Override @Override

View file

@ -29,8 +29,8 @@ import java.util.List;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
import net.forwardfire.unitxc.converter.step.UnitXConverterStep; import net.forwardfire.unitxc.converter.step.UnitXConverterStep;
import net.forwardfire.unitxc.model.UnitXCGroupModel; import net.forwardfire.unitxc.model.UnitXCGroup;
import net.forwardfire.unitxc.model.UnitXCTypeModel; import net.forwardfire.unitxc.model.UnitXCType;
/** /**
* *
@ -61,8 +61,8 @@ public class UnitXCTypeCompoundExponentBuilder extends AbstractUnitXCBuilder<Uni
@Override @Override
protected void buildPreValue(Object v) { protected void buildPreValue(Object v) {
UnitXCGroupModel group = null; UnitXCGroup group = null;
for (UnitXCGroupModel unitGroup:builder.getConfig().getUnitroups()) { for (UnitXCGroup unitGroup:builder.getConfig().getUnitroups()) {
if (unitGroup.getId().equals(unitGroupId)) { if (unitGroup.getId().equals(unitGroupId)) {
group = unitGroup; group = unitGroup;
break; break;
@ -70,32 +70,36 @@ public class UnitXCTypeCompoundExponentBuilder extends AbstractUnitXCBuilder<Uni
} }
Validate.notNull(group,"Could not find group: "+unitGroupId); Validate.notNull(group,"Could not find group: "+unitGroupId);
getParent().getValue().addDerivedFrom(unitGroupId); getParent().getValue().getDerivedFrom().add(unitGroupId);
for (UnitXCTypeModel unitType:new ArrayList<>(builder.getConfig().getUnitTypes())) { for (UnitXCType unitType:new ArrayList<>(builder.getConfig().getUnitTypes())) {
if (!unitType.getUnitGroupId().equals(group.getId())) { if (!unitType.getUnitGroupId().equals(group.getId())) {
continue; continue;
} }
UnitXCTypeBuilder<UnitXCConfigBuilder> typeBuilder = builder.createUnitType() UnitXCTypeBuilder<UnitXCConfigBuilder> typeBuilder = builder.createUnitType()
.setId(unitType.getId()+unitIdPostfix) .setId(unitType.getId()+unitIdPostfix)
.setName(unitNamePrefix+unitType.getName()) .setName(unitNamePrefix+unitType.getName())
.setTypeGroupId(group.getId()) .setTypeGroupId(getParent().getValue().getId())
.addTypeFlags(unitType.getTypeFlags()); .addTypeFlags(unitType.getTypeFlags());
for (int i=0;i<exponent;i++) { for (int i=0;i<exponent;i++) {
typeBuilder.addToBaseConverterSteps(addStepReason(exponent,unitType.getToBaseConverterSteps())); typeBuilder.addToBaseConverterSteps(cloneSteps(exponent,unitType.getToBaseConverterSteps()));
typeBuilder.addFromBaseConverterSteps(addStepReason(exponent,unitType.getFromBaseConverterSteps())); typeBuilder.addFromBaseConverterSteps(cloneSteps(exponent,unitType.getFromBaseConverterSteps()));
} }
typeBuilder.build(); typeBuilder.build();
} }
} }
private List<UnitXConverterStep> addStepReason(int exponent,List<UnitXConverterStep> steps) { private List<UnitXConverterStep> cloneSteps(int exponent,List<UnitXConverterStep> steps) {
if (exponent == 0) { List<UnitXConverterStep> result = new ArrayList<>();
return steps; for (UnitXConverterStep step:steps) {
UnitXConverterStep clone = step.clone();
if (exponent != 0) {
clone.addStepReason("CompoundExponent: "+exponent);
}
result.add(clone);
} }
steps.forEach(step -> step.addStepReason("CompoundeExponent: "+exponent)); return result;
return steps;
} }
public UnitXCTypeCompoundExponentBuilder setUnitIdPostfix(String unitIdPostfix) { public UnitXCTypeCompoundExponentBuilder setUnitIdPostfix(String unitIdPostfix) {

View file

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

View file

@ -29,7 +29,7 @@ import java.util.Collection;
import java.util.List; import java.util.List;
import net.forwardfire.unitxc.converter.step.UnitXCTypePowerOfTenConverterStep; import net.forwardfire.unitxc.converter.step.UnitXCTypePowerOfTenConverterStep;
import net.forwardfire.unitxc.model.UnitXCTypeModel; import net.forwardfire.unitxc.model.UnitXCType;
/** /**
* *
@ -43,7 +43,7 @@ public class UnitXCTypeSIPrefixBuilder<P> extends AbstractUnitXCTypeBuilder<P,Un
private final List<UnitXCTypeSIPrefix> commonSIPrefixes; private final List<UnitXCTypeSIPrefix> commonSIPrefixes;
public UnitXCTypeSIPrefixBuilder(P parent, UnitXCConfigBuilder builder) { public UnitXCTypeSIPrefixBuilder(P parent, UnitXCConfigBuilder builder) {
super(parent, (p,v) -> {}); super(parent,new UnitXCType(), (p,v) -> {});
this.builder = builder; this.builder = builder;
this.commonSIPrefixes = new ArrayList<>(); this.commonSIPrefixes = new ArrayList<>();
} }
@ -54,11 +54,12 @@ public class UnitXCTypeSIPrefixBuilder<P> extends AbstractUnitXCTypeBuilder<P,Un
} }
@Override @Override
protected void buildPreValue(UnitXCTypeModel v) { protected void buildModel(UnitXCType v) {
builder.createUnitType() builder.createUnitType()
.setId(v.getId()) .setId(v.getId())
.setName(v.getName()) .setName(v.getName())
.addNameAliases(v.getNameAliases()) .setNamePlural(v.getNamePlural())
.addWebsiteLinks(v.getWebsiteLinks())
.setTypeGroupId(v.getUnitGroupId()) .setTypeGroupId(v.getUnitGroupId())
.addTypeFlags(v.getTypeFlags()) .addTypeFlags(v.getTypeFlags())
.addFromBaseConverterSteps(v.getFromBaseConverterSteps()) .addFromBaseConverterSteps(v.getFromBaseConverterSteps())
@ -75,14 +76,19 @@ 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())
.addNameAliases(sim.getNameAliases()) .setNamePlural(sim.getPrefixName()+v.getNamePlural())
.setTypeGroupId(v.getUnitGroupId()) .setTypeGroupId(v.getUnitGroupId())
.addTypeFlags(typeFlags) .addTypeFlags(typeFlags)
.addToBaseConverterStep(new UnitXCTypePowerOfTenConverterStep(sim.getExponent(),false,"Move dot for SI")) //.addWebsiteLinks(v.getWebsiteLinks())
.addToBaseConverterStep(new UnitXCTypePowerOfTenConverterStep(sim.getExponent(),false).buildReason("Move dot for SI"))
.addToBaseConverterSteps(v.getToBaseConverterSteps()) .addToBaseConverterSteps(v.getToBaseConverterSteps())
.addFromBaseConverterSteps(v.getFromBaseConverterSteps()) .addFromBaseConverterSteps(v.getFromBaseConverterSteps())
.addFromBaseConverterStep(new UnitXCTypePowerOfTenConverterStep(sim.getExponent(),true,"Move dot for SI")) .addFromBaseConverterStep(new UnitXCTypePowerOfTenConverterStep(sim.getExponent(),true).buildReason("Move dot for SI"))
.build(); .build();
//for (String typeAlias:sim.getNameAliases()) {
//
//}
} }
} }

View file

@ -60,7 +60,7 @@ public class UnitXConverterStepBuilder<P extends AbstractUnitXCTypeBuilder<?,?>>
} }
private UnitXConverterStepBuilder<P> offset(double offset,boolean offsetPositive,String stepReason) { private UnitXConverterStepBuilder<P> offset(double offset,boolean offsetPositive,String stepReason) {
return make(v -> v.add(new UnitXCTypeOffsetConverterStep(offset,offsetPositive,stepReason))); return make(v -> v.add(new UnitXCTypeOffsetConverterStep(offset,offsetPositive).buildReason(stepReason)));
} }
public UnitXConverterStepBuilder<P> offsetUp(double offset,String stepReason) { public UnitXConverterStepBuilder<P> offsetUp(double offset,String stepReason) {
return offset(offset,true,stepReason); return offset(offset,true,stepReason);
@ -77,10 +77,10 @@ public class UnitXConverterStepBuilder<P extends AbstractUnitXCTypeBuilder<?,?>>
public UnitXConverterStepBuilder<P> multiply(Fraction fraction,String stepReason) { public UnitXConverterStepBuilder<P> multiply(Fraction fraction,String stepReason) {
return make(v -> v.add(new UnitXCTypeMultiplyFractionConverterStep(fraction,stepReason))); return make(v -> v.add(new UnitXCTypeMultiplyFractionConverterStep(fraction.getNumerator(),fraction.getDenominator()).buildReason(stepReason)));
} }
public UnitXConverterStepBuilder<P> multiply(int numerator,int denominator,String stepReason) { public UnitXConverterStepBuilder<P> multiply(int numerator,int denominator,String stepReason) {
return make(v -> v.add(new UnitXCTypeMultiplyFractionConverterStep(numerator,denominator,stepReason))); return make(v -> v.add(new UnitXCTypeMultiplyFractionConverterStep(numerator,denominator).buildReason(stepReason)));
} }
public UnitXConverterStepBuilder<P> multiply(Fraction fraction) { public UnitXConverterStepBuilder<P> multiply(Fraction fraction) {
return multiply(fraction,createStepReason()); return multiply(fraction,createStepReason());
@ -90,10 +90,10 @@ public class UnitXConverterStepBuilder<P extends AbstractUnitXCTypeBuilder<?,?>>
} }
public UnitXConverterStepBuilder<P> multiply(double factor,String stepReason) { public UnitXConverterStepBuilder<P> multiply(double factor,String stepReason) {
return make(v -> v.add(new UnitXCTypeMultiplyConverterStep(factor,stepReason))); return make(v -> v.add(new UnitXCTypeMultiplyConverterStep(factor).buildReason(stepReason)));
} }
public UnitXConverterStepBuilder<P> divide(double factor,String stepReason) { public UnitXConverterStepBuilder<P> divide(double factor,String stepReason) {
return make(v -> v.add(new UnitXCTypeDevideConverterStep(factor,stepReason))); return make(v -> v.add(new UnitXCTypeDevideConverterStep(factor).buildReason(stepReason)));
} }
public UnitXConverterStepBuilder<P> multiply(double factor) { public UnitXConverterStepBuilder<P> multiply(double factor) {
return multiply(factor,createStepReason()); return multiply(factor,createStepReason());
@ -103,7 +103,7 @@ public class UnitXConverterStepBuilder<P extends AbstractUnitXCTypeBuilder<?,?>>
} }
public UnitXConverterStepBuilder<P> power(int exponent,String stepReason) { public UnitXConverterStepBuilder<P> power(int exponent,String stepReason) {
return make(v -> v.add(new UnitXCTypePowerConverterStep(exponent,stepReason))); return make(v -> v.add(new UnitXCTypePowerConverterStep(exponent).buildReason(stepReason)));
} }
public UnitXConverterStepBuilder<P> power(int exponent) { public UnitXConverterStepBuilder<P> power(int exponent) {
return power(exponent,createStepReason()); return power(exponent,createStepReason());
@ -111,7 +111,7 @@ public class UnitXConverterStepBuilder<P extends AbstractUnitXCTypeBuilder<?,?>>
private UnitXConverterStepBuilder<P> powerOfTen(int exponent,boolean rev,String stepReason) { private UnitXConverterStepBuilder<P> powerOfTen(int exponent,boolean rev,String stepReason) {
return make(v -> v.add(new UnitXCTypePowerOfTenConverterStep(exponent,rev,stepReason))); return make(v -> v.add(new UnitXCTypePowerOfTenConverterStep(exponent,rev).buildReason(stepReason)));
} }
public UnitXConverterStepBuilder<P> power10Up(UnitXCTypeSIPrefix exponent,String stepReason) { public UnitXConverterStepBuilder<P> power10Up(UnitXCTypeSIPrefix exponent,String stepReason) {
return powerOfTen(exponent.getExponent(), true,stepReason); return powerOfTen(exponent.getExponent(), true,stepReason);

View file

@ -30,14 +30,11 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import net.forwardfire.unitxc.UnitXCManager; import net.forwardfire.unitxc.UnitXCManager;
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.UnitXCGroup; import net.forwardfire.unitxc.model.UnitXCGroup;
import net.forwardfire.unitxc.model.UnitXCType;
import net.forwardfire.unitxc.model.UnitXConverterResult; import net.forwardfire.unitxc.model.UnitXConverterResult;
import net.forwardfire.unitxc.model.UnitXConverterResultModel;
import net.forwardfire.unitxc.model.UnitXConverterResultStep; import net.forwardfire.unitxc.model.UnitXConverterResultStep;
import net.forwardfire.unitxc.model.UnitXConverterResultStepModel;
/** /**
* Runs requested convert steps and builds result models. * Runs requested convert steps and builds result models.
@ -54,7 +51,7 @@ public final class UnitXConverterEngine {
this.manager = manager; this.manager = manager;
} }
public UnitXConverterResult convertStepped(double value,UnitXCTypeModel fromType,UnitXCTypeModel toType) { public UnitXConverterResult convertStepped(double value,UnitXCType fromType,UnitXCType toType) {
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
double startValue = value; double startValue = value;
List<UnitXConverterResultStep> resultSteps = new ArrayList<>(); List<UnitXConverterResultStep> resultSteps = new ArrayList<>();
@ -73,11 +70,17 @@ public final class UnitXConverterEngine {
long convertTime = System.currentTimeMillis()-startTime; long convertTime = System.currentTimeMillis()-startTime;
UnitXConverterResultModel result = new UnitXConverterResultModel(startValue,value,resultSteps,convertTime, fromType, toType); UnitXConverterResult result = new UnitXConverterResult();
result.setStartValue(startValue);
result.setStartValueType(fromType);
result.setResultValue(value);
result.setResultValueType(toType);
result.setConvertTime(convertTime);
result.setResultSteps(resultSteps);
return result; return result;
} }
private double runConverter(List<UnitXConverterResultStep> resultSteps,UnitXCTypeModel type,double value,boolean toBase) { private double runConverter(List<UnitXConverterResultStep> resultSteps,UnitXCType type,double value,boolean toBase) {
double valueOld = value; double valueOld = value;
List<UnitXConverterStep> steps = toBase?type.getToBaseConverterSteps():type.getFromBaseConverterSteps(); List<UnitXConverterStep> steps = toBase?type.getToBaseConverterSteps():type.getFromBaseConverterSteps();
for (UnitXConverterStep step:steps) { for (UnitXConverterStep step:steps) {
@ -86,7 +89,12 @@ public final class UnitXConverterEngine {
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
value = step.convert(value); value = step.convert(value);
long convertTime = System.currentTimeMillis()-startTime; long convertTime = System.currentTimeMillis()-startTime;
resultSteps.add(new UnitXConverterResultStepModel(step.getStepName()+(toBase?"-toBase":"-fromBase")+" = "+step.getMathExpression(),valueOld,value,convertTime)); UnitXConverterResultStep resultStep = new UnitXConverterResultStep();
resultStep.setName(step.getName()+(toBase?"-toBase":"-fromBase")+" = "+step.getMathExpression());
resultStep.setStartValue(valueOld);
resultStep.setResultValue(value);
resultStep.setConvertTime(convertTime);
resultSteps.add(resultStep);
value = runRounding(resultSteps,value,valueOld); value = runRounding(resultSteps,value,valueOld);
@ -124,7 +132,13 @@ public final class UnitXConverterEngine {
Double result = new Double(valueShortStr); Double result = new Double(valueShortStr);
long roundTime = System.currentTimeMillis()-startTime; long roundTime = System.currentTimeMillis()-startTime;
resultSteps.add(new UnitXConverterResultStepModel("rouding",value,result,roundTime)); UnitXConverterResultStep resultStep = new UnitXConverterResultStep();
resultStep.setName("rounding");
resultStep.setStartValue(value);
resultStep.setResultValue(result);
resultStep.setConvertTime(roundTime);
resultSteps.add(resultStep);
return result.doubleValue(); return result.doubleValue();
} }

View file

@ -26,8 +26,14 @@ package net.forwardfire.unitxc.converter.step;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
import net.forwardfire.unitxc.model.ModelXMLInfoSet;
/** /**
* *
* *
@ -36,27 +42,63 @@ import org.apache.commons.lang3.Validate;
*/ */
public abstract class AbstractUnitXConverterStep implements UnitXConverterStep { public abstract class AbstractUnitXConverterStep implements UnitXConverterStep {
private final String name; private String name;
private final List<String> stepReasons; private final List<String> stepReasons;
protected AbstractUnitXConverterStep(String name,String stepReason) { protected AbstractUnitXConverterStep(String name) {
this.name = Validate.notBlank(name);
this.stepReasons = new ArrayList<>(); this.stepReasons = new ArrayList<>();
this.addStepReason(stepReason); this.setName(name);
}
public AbstractUnitXConverterStep buildReason(String reason) {
addStepReason(reason);
return this;
} }
@Override @Override
public final String getStepName() { @XmlAttribute(name=ModelXMLInfoSet.Attribute.NAME)
public final String getName() {
return name; return name;
} }
public void setName(String name) {
this.name = Validate.notBlank(name);
}
@Override @Override
@XmlElement(name=ModelXMLInfoSet.Element.STEP_REASON)
@XmlElementWrapper(name=ModelXMLInfoSet.Element.STEP_REASONS)
public final List<String> getStepReasons() { public final List<String> getStepReasons() {
return stepReasons; return stepReasons;
} }
public final void setStepReasons(List<String> stepReasons) {
this.stepReasons.clear();
this.stepReasons.addAll(stepReasons);
}
@Override @Override
public final void addStepReason(String stepReason) { public final void addStepReason(String stepReason) {
stepReasons.add(Validate.notBlank(stepReason)); stepReasons.add(Validate.notBlank(stepReason));
} }
abstract protected String toMath();
@Override
public final String getMathExpression() {
return toMath();
}
public void setMathExpression(String notUsed) {
}
@Override
public UnitXConverterStep clone() {
AbstractUnitXConverterStep clone = createClone();
clone.setName(getName());
clone.setStepReasons(getStepReasons());
return clone;
}
abstract protected AbstractUnitXConverterStep createClone();
} }

View file

@ -23,6 +23,8 @@
package net.forwardfire.unitxc.converter.step; package net.forwardfire.unitxc.converter.step;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
/** /**
@ -31,15 +33,24 @@ import org.apache.commons.lang3.Validate;
* @author Willem Cazander * @author Willem Cazander
* @version 1.0 Oct 20, 2015 * @version 1.0 Oct 20, 2015
*/ */
@XmlRootElement
public class UnitXCTypeDevideConverterStep extends AbstractUnitXConverterStep { public class UnitXCTypeDevideConverterStep extends AbstractUnitXConverterStep {
private final static String STEP_NAME = "Devide"; private final static String STEP_NAME = "Devide";
private final double factor; private double factor;
public UnitXCTypeDevideConverterStep(double factor,String stepReason) { public UnitXCTypeDevideConverterStep() {
super(STEP_NAME,stepReason); super(STEP_NAME);
this.factor = factor; }
Validate.isTrue(factor != 0);
public UnitXCTypeDevideConverterStep(double factor) {
this();
setFactor(factor);
}
@Override
protected AbstractUnitXConverterStep createClone() {
return new UnitXCTypeDevideConverterStep(getFactor());
} }
@Override @Override
@ -48,7 +59,22 @@ public class UnitXCTypeDevideConverterStep extends AbstractUnitXConverterStep {
} }
@Override @Override
public String getMathExpression() { protected String toMath() {
return "/"+factor; return "/"+factor;
} }
/**
* @return the factor
*/
public double getFactor() {
return factor;
}
/**
* @param factor the factor to set
*/
public void setFactor(double factor) {
Validate.isTrue(factor != 0);
this.factor = factor;
}
} }

View file

@ -23,20 +23,32 @@
package net.forwardfire.unitxc.converter.step; package net.forwardfire.unitxc.converter.step;
import javax.xml.bind.annotation.XmlRootElement;
/** /**
* *
* *
* @author Willem Cazander * @author Willem Cazander
* @version 1.0 Oct 17, 2015 * @version 1.0 Oct 17, 2015
*/ */
@XmlRootElement
public class UnitXCTypeMultiplyConverterStep extends AbstractUnitXConverterStep { public class UnitXCTypeMultiplyConverterStep extends AbstractUnitXConverterStep {
private final static String STEP_NAME = "Multiply"; private final static String STEP_NAME = "Multiply";
private final double factor; private double factor;
public UnitXCTypeMultiplyConverterStep(double factor,String stepReason) { public UnitXCTypeMultiplyConverterStep() {
super(STEP_NAME,stepReason); super(STEP_NAME);
this.factor = factor; }
public UnitXCTypeMultiplyConverterStep(double factor) {
this();
setFactor(factor);
}
@Override
protected AbstractUnitXConverterStep createClone() {
return new UnitXCTypeMultiplyConverterStep(getFactor());
} }
@Override @Override
@ -45,7 +57,21 @@ public class UnitXCTypeMultiplyConverterStep extends AbstractUnitXConverterStep
} }
@Override @Override
public String getMathExpression() { protected String toMath() {
return "*"+factor; return "*"+factor;
} }
/**
* @return the factor
*/
public double getFactor() {
return factor;
}
/**
* @param factor the factor to set
*/
public void setFactor(double factor) {
this.factor = factor;
}
} }

View file

@ -23,6 +23,8 @@
package net.forwardfire.unitxc.converter.step; package net.forwardfire.unitxc.converter.step;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.math.Fraction; import org.apache.commons.lang3.math.Fraction;
@ -32,12 +34,31 @@ import org.apache.commons.lang3.math.Fraction;
* @author Willem Cazander * @author Willem Cazander
* @version 1.0 Oct 20, 2015 * @version 1.0 Oct 20, 2015
*/ */
@XmlRootElement
public class UnitXCTypeMultiplyFractionConverterStep extends AbstractUnitXConverterStep { public class UnitXCTypeMultiplyFractionConverterStep extends AbstractUnitXConverterStep {
private final static String STEP_NAME = "MultiplyFaction"; private final static String STEP_NAME = "MultiplyFaction";
private final Fraction fraction; //private Fraction fraction;
private final String fractionMath; //private final String fractionMath;
private int numerator;
private int denominator;
public UnitXCTypeMultiplyFractionConverterStep() {
super(STEP_NAME);
}
public UnitXCTypeMultiplyFractionConverterStep(int numerator,int denominator) {
this();
setNumerator(numerator);
setDenominator(denominator);
}
@Override
protected AbstractUnitXConverterStep createClone() {
return new UnitXCTypeMultiplyFractionConverterStep(getNumerator(),getDenominator());
}
/*
public UnitXCTypeMultiplyFractionConverterStep(int numerator,int denominator,String stepReason) { public UnitXCTypeMultiplyFractionConverterStep(int numerator,int denominator,String stepReason) {
this(Fraction.getFraction(numerator, denominator),stepReason); this(Fraction.getFraction(numerator, denominator),stepReason);
} }
@ -47,14 +68,44 @@ public class UnitXCTypeMultiplyFractionConverterStep extends AbstractUnitXConver
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());
} }
*/
@Override @Override
public double convert(double value) { public double convert(double value) {
return value*fraction.doubleValue(); //return value*fraction.doubleValue();
return value*(numerator/denominator);
} }
@Override @Override
public String getMathExpression() { protected String toMath() {
return fractionMath; //return String.format("*(%1s/%2s)", fraction.getNumerator(),fraction.getDenominator());
return String.format("*(%1s/%2s)", numerator,denominator);
}
/**
* @return the numerator
*/
public int getNumerator() {
return numerator;
}
/**
* @param numerator the numerator to set
*/
public void setNumerator(int numerator) {
this.numerator = numerator;
}
/**
* @return the denominator
*/
public int getDenominator() {
return denominator;
}
/**
* @param denominator the denominator to set
*/
public void setDenominator(int denominator) {
this.denominator = denominator;
} }
} }

View file

@ -23,22 +23,34 @@
package net.forwardfire.unitxc.converter.step; package net.forwardfire.unitxc.converter.step;
import javax.xml.bind.annotation.XmlRootElement;
/** /**
* *
* *
* @author Willem Cazander * @author Willem Cazander
* @version 1.0 Oct 17, 2015 * @version 1.0 Oct 17, 2015
*/ */
@XmlRootElement
public class UnitXCTypeOffsetConverterStep extends AbstractUnitXConverterStep { public class UnitXCTypeOffsetConverterStep extends AbstractUnitXConverterStep {
private final static String STEP_NAME = "Offset"; private final static String STEP_NAME = "Offset";
private final double offset; private double offset;
private final boolean offsetPositive; private boolean offsetPositive;
public UnitXCTypeOffsetConverterStep(double offset,boolean offsetPositive,String stepReason) { public UnitXCTypeOffsetConverterStep() {
super(STEP_NAME,stepReason); super(STEP_NAME);
this.offset = offset; }
this.offsetPositive = offsetPositive;
public UnitXCTypeOffsetConverterStep(double offset,boolean offsetPositive) {
this();
setOffset(offset);
setOffsetPositive(offsetPositive);
}
@Override
protected AbstractUnitXConverterStep createClone() {
return new UnitXCTypeOffsetConverterStep(getOffset(),isOffsetPositive());
} }
@Override @Override
@ -51,11 +63,42 @@ public class UnitXCTypeOffsetConverterStep extends AbstractUnitXConverterStep {
} }
@Override @Override
public String getMathExpression() { protected String toMath() {
if (offsetPositive) { if (offsetPositive) {
return "+"+offset; return "+"+offset;
} else { } else {
return "-"+offset; return "-"+offset;
} }
} }
/**
* @return the offset
*/
public double getOffset() {
return offset;
}
/**
* @param offset the offset to set
*/
public void setOffset(double offset) {
this.offset = offset;
}
/**
* @return the offsetPositive
*/
public boolean isOffsetPositive() {
return offsetPositive;
}
/**
* @param offsetPositive the offsetPositive to set
*/
public void setOffsetPositive(boolean offsetPositive) {
this.offsetPositive = offsetPositive;
}
} }

View file

@ -23,29 +23,55 @@
package net.forwardfire.unitxc.converter.step; package net.forwardfire.unitxc.converter.step;
import javax.xml.bind.annotation.XmlRootElement;
/** /**
* *
* *
* @author Willem Cazander * @author Willem Cazander
* @version 1.0 Oct 26, 2015 * @version 1.0 Oct 26, 2015
*/ */
@XmlRootElement
public class UnitXCTypePowerConverterStep extends AbstractUnitXConverterStep { public class UnitXCTypePowerConverterStep extends AbstractUnitXConverterStep {
private final static String STEP_NAME = "power"; private final static String STEP_NAME = "power";
private final int exponent; private int exponent;
public UnitXCTypePowerConverterStep(int exponent,String stepReason) { public UnitXCTypePowerConverterStep() {
super(STEP_NAME,stepReason); super(STEP_NAME);
this.exponent = exponent; }
public UnitXCTypePowerConverterStep(int exponent) {
this();
setExponent(exponent);
} }
@Override @Override
public String getMathExpression() { protected AbstractUnitXConverterStep createClone() {
return "^"+exponent; return new UnitXCTypePowerConverterStep(getExponent());
} }
@Override @Override
public double convert(double value) { public double convert(double value) {
return Math.pow(value, exponent); return Math.pow(value, exponent);
} }
@Override
protected String toMath() {
return "/"+exponent;
}
/**
* @return the exponent
*/
public int getExponent() {
return exponent;
}
/**
* @param exponent the exponent to set
*/
public void setExponent(int exponent) {
this.exponent = exponent;
}
} }

View file

@ -23,39 +23,51 @@
package net.forwardfire.unitxc.converter.step; package net.forwardfire.unitxc.converter.step;
import javax.xml.bind.annotation.XmlRootElement;
/** /**
* *
* *
* @author Willem Cazander * @author Willem Cazander
* @version 1.0 Oct 17, 2015 * @version 1.0 Oct 17, 2015
*/ */
@XmlRootElement
public class UnitXCTypePowerOfTenConverterStep extends AbstractUnitXConverterStep { public class UnitXCTypePowerOfTenConverterStep extends AbstractUnitXConverterStep {
private final static String STEP_NAME = "power(10)"; private final static String STEP_NAME = "power(10)";
private final int exponent; private int exponent;
private final int exponentShift; private boolean exponentReverse;
private final boolean exponentReverse;
public UnitXCTypePowerOfTenConverterStep(int factor,boolean factorReverse,String stepReason) { public UnitXCTypePowerOfTenConverterStep() {
super(STEP_NAME,stepReason); super(STEP_NAME);
this.exponent = factor; }
this.exponentReverse = factorReverse;
this.exponentShift = factor<0?0-factor:factor; public UnitXCTypePowerOfTenConverterStep(int exponent,boolean exponentReverse) {
this();
setExponent(exponent);
setExponentReverse(exponentReverse);
} }
@Override @Override
public String getMathExpression() { protected AbstractUnitXConverterStep createClone() {
return "*10^"+exponent; return new UnitXCTypePowerOfTenConverterStep(getExponent(),isExponentReverse());
} }
@Override @Override
public double convert(double value) { public double convert(double value) {
int exponentShift = exponent<0?0-exponent:exponent;
for (int i=0;i<exponentShift;i++) { for (int i=0;i<exponentShift;i++) {
value = shiftDotStatic(value,exponentReverse?!(exponent>0):exponent>0); value = shiftDotStatic(value,exponentReverse?!(exponent>0):exponent>0);
} }
return value; return value;
} }
@Override
protected String toMath() {
return "*10^"+exponent;
}
private double shiftDotStatic(double value,boolean up) { private double shiftDotStatic(double value,boolean up) {
String valueStr = ""+value; String valueStr = ""+value;
int dotIdx = valueStr.indexOf('.'); int dotIdx = valueStr.indexOf('.');
@ -71,4 +83,34 @@ public class UnitXCTypePowerOfTenConverterStep extends AbstractUnitXConverterSte
Double result = new Double(buf.toString()); Double result = new Double(buf.toString());
return result.doubleValue(); return result.doubleValue();
} }
/**
* @return the exponent
*/
public int getExponent() {
return exponent;
}
/**
* @param exponent the exponent to set
*/
public void setExponent(int exponent) {
this.exponent = exponent;
}
/**
* @return the exponentReverse
*/
public boolean isExponentReverse() {
return exponentReverse;
}
/**
* @param exponentReverse the exponentReverse to set
*/
public void setExponentReverse(boolean exponentReverse) {
this.exponentReverse = exponentReverse;
}
} }

View file

@ -31,15 +31,17 @@ import java.util.List;
* @author Willem Cazander * @author Willem Cazander
* @version 1.0 Oct 10, 2015 * @version 1.0 Oct 10, 2015
*/ */
public interface UnitXConverterStep { public interface UnitXConverterStep extends Cloneable {
String getStepName(); String getName();
List<String> getStepReasons(); List<String> getStepReasons();
void addStepReason(String stepReason); void addStepReason(String stepReason); // TODO: rm : fix builder
String getMathExpression(); String getMathExpression();
double convert(double value); double convert(double value);
UnitXConverterStep clone();
} }

View file

@ -0,0 +1,81 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.model;
/**
* ModelXMLInfoSet has all jaxb meta info.
*
* @author Willem Cazander
* @version 1.0 Jan 16, 2015
*/
public class ModelXMLInfoSet {
protected static final String XML_ENCODING = "UTF-8";
protected ModelXMLInfoSet() {
}
protected static class Meta {
protected static final boolean REQUIRED = true;
protected Meta() {
}
}
public static class Element {
protected static final String DESCRIPTION = "description";
protected static final String CONVERTER_RESULT = "converterResult";
protected static final String CONVERTER_RESULT_STEP = "converterResultStep";
protected static final String START_VALUE_TYPE = "startValueType";
protected static final String RESULT_VALUE_TYPE = "resultValueType";
protected static final String DERIVED_FROM = "derivedFrom";
protected static final String TYPE_FLAG = "typeFlag";
protected static final String TYPE_FLAGS = "typeFlags";
protected static final String WEBSITE_LINK = "websiteLink";
protected static final String WEBSITE_LINKS = "websiteLinks";
public static final String STEP_REASON = "stepReason";
public static final String STEP_REASONS = "stepReasons";
protected Element() {
}
}
public static class Attribute {
protected static final String ID = "id";
public static final String NAME = "name";
protected static final String NAME_PLURAL = "namePlural";
protected static final String START_VALUE = "startValue";
protected static final String RESULT_VALUE = "resultValue";
protected static final String CONVERT_TIME = "convertTime";
protected static final String BASE_TYPE_ID = "baseTypeId";
protected static final String GROUP_LEVEL = "groupLevel";
protected static final String ALIAS_OF_TYPE = "aliasOfType";
protected static final String UNIT_GROUP_ID = "unitGroupId";
protected Attribute() {
}
}
}

View file

@ -0,0 +1,103 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.model;
import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.apache.commons.lang3.Validate;
import net.forwardfire.unitxc.converter.step.UnitXCTypeDevideConverterStep;
import net.forwardfire.unitxc.converter.step.UnitXCTypeMultiplyConverterStep;
import net.forwardfire.unitxc.converter.step.UnitXCTypeMultiplyFractionConverterStep;
import net.forwardfire.unitxc.converter.step.UnitXCTypeOffsetConverterStep;
import net.forwardfire.unitxc.converter.step.UnitXCTypePowerConverterStep;
import net.forwardfire.unitxc.converter.step.UnitXCTypePowerOfTenConverterStep;
/**
* Jaxb marshaller of the query sets.
*
* @author Willem Cazander
* @version 1.0 Jan 14, 2015
*/
public class ModelXMLMarshaller {
private final JAXBContext jaxbContext;
/**
* Creates a query set xml marshaller.
* @throws JAXBException When context could not be build.
*/
public ModelXMLMarshaller() throws JAXBException {
this(JAXBContext.newInstance(UnitXConverterResult.class,
UnitXCTypeDevideConverterStep.class,
UnitXCTypeMultiplyConverterStep.class,
UnitXCTypeMultiplyFractionConverterStep.class,
UnitXCTypeOffsetConverterStep.class,
UnitXCTypePowerConverterStep.class,
UnitXCTypePowerOfTenConverterStep.class
));
}
/**
* Creates a query set xml marshaller.
* @param querySetContext The jaxb query set context.
*/
public ModelXMLMarshaller(JAXBContext querySetContext) {
this.jaxbContext = Validate.notNull(querySetContext,"querySetContext is null.");
}
/**
* Marshal the UnitXConverterResult to xml.
* @param querySet The UnitXConverterResult to marshal.
* @param output The xml output of the query set.
* @throws JAXBException When error.
*/
public void marshal(UnitXConverterResult converterResult, OutputStream output) throws JAXBException {
Validate.notNull(converterResult,"converterResult is null.");
Validate.notNull(output,"OutputStream is null.");
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, ModelXMLInfoSet.XML_ENCODING);
marshaller.marshal(converterResult, output);
}
/**
* Unmarshals xml to UnitXConverterResult objects.
* @param input The xml input stream.
* @return The UnitXConverterResult.
* @throws JAXBException When error.
*/
public UnitXConverterResult unmarshal(InputStream input) throws JAXBException {
Validate.notNull(input,"InputStream is null.");
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
return (UnitXConverterResult) unmarshaller.unmarshal(input);
}
}

View file

@ -23,7 +23,13 @@
package net.forwardfire.unitxc.model; package net.forwardfire.unitxc.model;
import java.util.Collection; import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import org.apache.commons.lang3.Validate;
/** /**
* The unit group. * The unit group.
@ -31,26 +37,123 @@ import java.util.Collection;
* @author Willem Cazander * @author Willem Cazander
* @version 1.0 Oct 10, 2015 * @version 1.0 Oct 10, 2015
*/ */
public interface UnitXCGroup { public class UnitXCGroup {
private String id;
private String name;
private String description;
private String baseTypeId;
private UnitXCGroupLevel groupLevel;
private final List<String> derivedFrom;
public UnitXCGroup() {
derivedFrom = new ArrayList<>();
}
public UnitXCGroup(String id,String name,String description,String baseTypeId) {
this();
setId(id);
setName(name);
setDescription(description);
setBaseTypeId(baseTypeId);
}
public UnitXCGroup validate() {
Validate.notBlank(id,"The id is blank");
Validate.notBlank(name,"The name is blank");
Validate.notBlank(description,"The description is blank");
Validate.notBlank(baseTypeId,"The baseTypeId is blank");
Validate.notNull(groupLevel,"The groupLevel is null");
return this;
}
/** /**
* @return The unit group id. * @return the id
*/ */
String getId(); @XmlAttribute(name=ModelXMLInfoSet.Attribute.ID)
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/** /**
* @return The unit group name. * @return the name
*/ */
String getName(); @XmlAttribute(name=ModelXMLInfoSet.Attribute.NAME)
public String getName() {
return name;
}
/** /**
* @return The unit group description. * @param name the name to set
*/ */
String getDescription(); public void setName(String name) {
this.name = name;
}
UnitXCGroupType getType(); /**
* @return the description
*/
@XmlElement(name=ModelXMLInfoSet.Element.DESCRIPTION)
public String getDescription() {
return description;
}
String getBaseTypeId(); /**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
Collection<String> getDerivedFromIds(); /**
* @return the baseTypeId
*/
@XmlAttribute(name=ModelXMLInfoSet.Attribute.BASE_TYPE_ID)
public String getBaseTypeId() {
return baseTypeId;
}
/**
* @param baseTypeId the baseTypeId to set
*/
public void setBaseTypeId(String baseTypeId) {
this.baseTypeId = baseTypeId;
}
/**
* @return the groupLevel
*/
@XmlAttribute(name=ModelXMLInfoSet.Attribute.GROUP_LEVEL)
public UnitXCGroupLevel getGroupLevel() {
return groupLevel;
}
/**
* @param groupLevel the groupLevel to set
*/
public void setGroupLevel(UnitXCGroupLevel groupLevel) {
this.groupLevel = groupLevel;
}
/**
* @return the derivedFrom
*/
@XmlElement(name=ModelXMLInfoSet.Element.DERIVED_FROM)
public List<String> getDerivedFrom() {
return derivedFrom;
}
/**
* @param derivedFrom the derivedFrom to set
*/
public void setDerivedFrom(List<String> derivedFrom) {
this.derivedFrom.clear();
this.derivedFrom.addAll(derivedFrom);
}
} }

View file

@ -6,7 +6,7 @@ package net.forwardfire.unitxc.model;
* @author Willem Cazander * @author Willem Cazander
* @version 1.0 Oct 26, 2015 * @version 1.0 Oct 26, 2015
*/ */
public enum UnitXCGroupType { public enum UnitXCGroupLevel {
SI_BASE, SI_BASE,
SI_DERIVED SI_DERIVED

View file

@ -1,151 +0,0 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.model;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.lang3.Validate;
/**
*
*
* @author Willem Cazander
* @version 1.0 Oct 17, 2015
*/
public class UnitXCGroupModel implements UnitXCGroup {
private String id;
private String name;
private String description;
private String baseTypeId;
private UnitXCGroupType type;
private final List<String> derivedFrom;
public UnitXCGroupModel() {
derivedFrom = new ArrayList<>();
}
public UnitXCGroupModel(String id,String name,String description,String baseTypeId) {
this();
setId(id);
setName(name);
setDescription(description);
setBaseTypeId(baseTypeId);
}
public UnitXCGroupModel validate() {
Validate.notBlank(id,"The id is blank");
Validate.notBlank(name,"The name is blank");
Validate.notBlank(description,"The description is blank");
Validate.notBlank(baseTypeId,"The baseTypeId is blank");
Validate.notNull(type,"The type is null");
return this;
}
/**
* @return the id
*/
@Override
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the name
*/
@Override
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the description
*/
@Override
public String getDescription() {
return description;
}
/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @return the baseTypeId
*/
@Override
public String getBaseTypeId() {
return baseTypeId;
}
/**
* @param baseTypeId the baseTypeId to set
*/
public void setBaseTypeId(String baseTypeId) {
this.baseTypeId = baseTypeId;
}
/**
* @return the type
*/
@Override
public UnitXCGroupType getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(UnitXCGroupType type) {
this.type = type;
}
@Override
public Collection<String> getDerivedFromIds() {
return derivedFrom;
}
public void addDerivedFrom(String id) {
derivedFrom.add(id);
}
}

View file

@ -23,7 +23,18 @@
package net.forwardfire.unitxc.model; package net.forwardfire.unitxc.model;
import java.util.Collection; import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlTransient;
import org.apache.commons.lang3.Validate;
import net.forwardfire.unitxc.converter.step.UnitXConverterStep;
/** /**
* The unit type. * The unit type.
@ -31,24 +42,172 @@ import java.util.Collection;
* @author Willem Cazander * @author Willem Cazander
* @version 1.0 Oct 10, 2015 * @version 1.0 Oct 10, 2015
*/ */
public interface UnitXCType { public class UnitXCType {
private String id;
private String name;
private String namePlural;
private String aliasOfType;
private String unitGroupId;
private final List<UnitXConverterStep> toBaseConverterSteps;
private final List<UnitXConverterStep> fromBaseConverterSteps;
private final List<String> typeFlags;
private final List<String> websiteLinks;
public UnitXCType() {
typeFlags = new ArrayList<>();
websiteLinks = new ArrayList<>();
toBaseConverterSteps = new ArrayList<>();
fromBaseConverterSteps = new ArrayList<>();
}
public UnitXCType validate() {
Validate.notBlank(id,"The id is blank");
Validate.notBlank(name,"The name is blank of: "+id);
Validate.notBlank(namePlural,"The namePlural is blank of: "+id);
Validate.notBlank(unitGroupId,"The unitGroupId is blank of: "+id);
return this;
}
/** /**
* @return The unit type id. * @return the id
*/ */
String getId(); @XmlAttribute(name=ModelXMLInfoSet.Attribute.ID)
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/** /**
* @return The unit type name. * @return the name
*/ */
String getName(); @XmlAttribute(name=ModelXMLInfoSet.Attribute.NAME)
public String getName() {
Collection<String> getNameAliases(); return name;
}
String getUnitGroupId();
/** /**
* @return The unit type flags. * @param name the name to set
*/ */
Collection<String> getTypeFlags(); public void setName(String name) {
this.name = name;
}
/**
* @return the namePlural
*/
@XmlAttribute(name=ModelXMLInfoSet.Attribute.NAME_PLURAL)
public String getNamePlural() {
return namePlural;
}
/**
* @param namePlural the namePlural to set
*/
public void setNamePlural(String namePlural) {
this.namePlural = namePlural;
}
/**
* @return the aliasOfType
*/
@XmlAttribute(name=ModelXMLInfoSet.Attribute.ALIAS_OF_TYPE)
public String getAliasOfType() {
return aliasOfType;
}
/**
* @param aliasOfType the aliasOfType to set
*/
public void setAliasOfType(String aliasOfType) {
this.aliasOfType = aliasOfType;
}
/**
* @return the unitGroupId
*/
@XmlAttribute(name=ModelXMLInfoSet.Attribute.UNIT_GROUP_ID)
public String getUnitGroupId() {
return unitGroupId;
}
/**
* @param unitGroupId the unitGroupId to set
*/
public void setUnitGroupId(String unitGroupId) {
this.unitGroupId = unitGroupId;
}
/**
* @return the toBaseConverterSteps
*/
@XmlElementWrapper
@XmlAnyElement
public List<UnitXConverterStep> getToBaseConverterSteps() {
return toBaseConverterSteps;
}
/**
* @param toBaseConverterSteps the toBaseConverterSteps to set
*/
public void setToBaseConverterSteps(List<UnitXConverterStep> toBaseConverterSteps) {
this.toBaseConverterSteps.clear();
this.toBaseConverterSteps.addAll(toBaseConverterSteps);
}
/**
* @return the fromBaseConverterSteps
*/
@XmlElementWrapper
@XmlAnyElement
public List<UnitXConverterStep> getFromBaseConverterSteps() {
return fromBaseConverterSteps;
}
/**
* @param fromBaseConverterSteps the fromBaseConverterSteps to set
*/
public void setFromBaseConverterSteps(List<UnitXConverterStep> fromBaseConverterSteps) {
this.fromBaseConverterSteps.clear();
this.fromBaseConverterSteps.addAll(fromBaseConverterSteps);
}
/**
* @return the typeFlags
*/
@XmlElement(name=ModelXMLInfoSet.Element.TYPE_FLAG)
@XmlElementWrapper(name=ModelXMLInfoSet.Element.TYPE_FLAGS)
public List<String> getTypeFlags() {
return typeFlags;
}
/**
* @param typeFlags the typeFlags to set
*/
public void setTypeFlags(List<String> typeFlags) {
this.typeFlags.clear();
this.typeFlags.addAll(typeFlags);
}
/**
* @return the websiteLinks
*/
@XmlElement(name=ModelXMLInfoSet.Element.WEBSITE_LINK)
@XmlElementWrapper(name=ModelXMLInfoSet.Element.WEBSITE_LINKS)
public List<String> getWebsiteLinks() {
return websiteLinks;
}
/**
* @param websiteLinks the websiteLinks to set
*/
public void setWebsiteLinks(List<String> websiteLinks) {
this.websiteLinks.clear();
this.websiteLinks.addAll(websiteLinks);
}
} }

View file

@ -1,142 +0,0 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.model;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.lang3.Validate;
import net.forwardfire.unitxc.converter.step.UnitXConverterStep;
/**
*
*
* @author Willem Cazander
* @version 1.0 Oct 17, 2015
*/
public class UnitXCTypeModel implements UnitXCType {
private String id;
private String name;
private String unitGroupId;
private final List<UnitXConverterStep> toBaseConverterSteps;
private final List<UnitXConverterStep> fromBaseConverterSteps;
private final List<String> typeFlags;
private final List<String> nameAliases;
public UnitXCTypeModel() {
typeFlags = new ArrayList<>();
nameAliases = new ArrayList<>();
toBaseConverterSteps = new ArrayList<>();
fromBaseConverterSteps = new ArrayList<>();
}
public UnitXCTypeModel validate() {
Validate.notBlank(id,"The id is blank");
Validate.notBlank(name,"The name is blank");
Validate.notBlank(unitGroupId,"The unitGroupId is blank");
return this;
}
/**
* @return the id
*/
@Override
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the name
*/
@Override
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the unitGroupId
*/
@Override
public String getUnitGroupId() {
return unitGroupId;
}
/**
* @param unitGroupId the unitGroupId to set
*/
public void setUnitGroupId(String unitGroupId) {
this.unitGroupId = unitGroupId;
}
public List<UnitXConverterStep> getToBaseConverterSteps() {
return toBaseConverterSteps;
}
public void addToBaseConverterStep(UnitXConverterStep unitTypeConverter) {
toBaseConverterSteps.add(Validate.notNull(unitTypeConverter));
}
public List<UnitXConverterStep> getFromBaseConverterSteps() {
return fromBaseConverterSteps;
}
public void addFromBaseConverterStep(UnitXConverterStep unitTypeConverter) {
fromBaseConverterSteps.add(Validate.notNull(unitTypeConverter));
}
@Override
public Collection<String> getTypeFlags() {
return typeFlags;
}
public void addTypeFlag(String flag) {
typeFlags.add(flag);
}
@Override
public Collection<String> getNameAliases() {
return nameAliases;
}
public void addNameAlias(String nameAlias) {
nameAliases.add(nameAlias);
}
}

View file

@ -25,41 +25,113 @@ package net.forwardfire.unitxc.model;
import java.util.List; import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/** /**
* Holds the convert result and meta steps how we got there. * Holds the convert result and meta steps how we got there.
* *
* @author Willem Cazander * @author Willem Cazander
* @version 1.0 Oct 17, 2015 * @version 1.0 Oct 17, 2015
*/ */
public interface UnitXConverterResult { @XmlRootElement(name=ModelXMLInfoSet.Element.CONVERTER_RESULT)
public class UnitXConverterResult {
private Double startValue;
private List<UnitXConverterResultStep> resultSteps;
private Double resultValue;
private Long convertTime;
private UnitXCType startValueType;
private UnitXCType resultValueType;
/** /**
* @return The start value. * @return the startValue
*/ */
Double getStartValue(); @XmlAttribute(name=ModelXMLInfoSet.Attribute.START_VALUE)
public Double getStartValue() {
return startValue;
}
/** /**
* @return The start value type. * @param startValue the startValue to set
*/ */
UnitXCType getStartValueType(); public void setStartValue(Double startValue) {
this.startValue = startValue;
}
/** /**
* @return The result value. * @return the resultSteps
*/ */
Double getResultValue(); @XmlElement(name=ModelXMLInfoSet.Element.CONVERTER_RESULT_STEP)
public List<UnitXConverterResultStep> getResultSteps() {
return resultSteps;
}
/** /**
* @return The result value type. * @param resultSteps the resultSteps to set
*/ */
UnitXCType getResultValueType(); public void setResultSteps(List<UnitXConverterResultStep> resultSteps) {
this.resultSteps = resultSteps;
}
/** /**
* @return The convert steps performed to get to result value. * @return the resultValue
*/ */
List<UnitXConverterResultStep> getSteps(); @XmlAttribute(name=ModelXMLInfoSet.Attribute.RESULT_VALUE)
public Double getResultValue() {
return resultValue;
}
/** /**
* @return The convert time in ms. * @param resultValue the resultValue to set
*/ */
Long getConvertTime(); public void setResultValue(Double resultValue) {
this.resultValue = resultValue;
}
/**
* @return the convertTime
*/
@XmlAttribute(name=ModelXMLInfoSet.Attribute.CONVERT_TIME)
public Long getConvertTime() {
return convertTime;
}
/**
* @param convertTime the convertTime to set
*/
public void setConvertTime(Long convertTime) {
this.convertTime = convertTime;
}
/**
* @return the startValueType
*/
@XmlElement(name=ModelXMLInfoSet.Element.START_VALUE_TYPE)
public UnitXCType getStartValueType() {
return startValueType;
}
/**
* @param startValueType the startValueType to set
*/
public void setStartValueType(UnitXCType startValueType) {
this.startValueType = startValueType;
}
/**
* @return the resultValueType
*/
@XmlElement(name=ModelXMLInfoSet.Element.RESULT_VALUE_TYPE)
public UnitXCType getResultValueType() {
return resultValueType;
}
/**
* @param resultValueType the resultValueType to set
*/
public void setResultValueType(UnitXCType resultValueType) {
this.resultValueType = resultValueType;
}
} }

View file

@ -23,19 +23,78 @@
package net.forwardfire.unitxc.model; package net.forwardfire.unitxc.model;
import javax.xml.bind.annotation.XmlAttribute;
/** /**
* *
* *
* @author Willem Cazander * @author Willem Cazander
* @version 1.0 Oct 17, 2015 * @version 1.0 Oct 17, 2015
*/ */
public interface UnitXConverterResultStep { public class UnitXConverterResultStep {
String getName(); private String name;
private Double startValue;
private Double resultValue;
private Long convertTime;
Double getInputValue(); /**
* @return the name
*/
@XmlAttribute(name=ModelXMLInfoSet.Attribute.NAME)
public String getName() {
return name;
}
Double getOutputValue(); /**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
Long getConvertTime(); /**
* @return the startValue
*/
@XmlAttribute(name=ModelXMLInfoSet.Attribute.START_VALUE)
public Double getStartValue() {
return startValue;
}
/**
* @param startValue the startValue to set
*/
public void setStartValue(Double startValue) {
this.startValue = startValue;
}
/**
* @return the resultValue
*/
@XmlAttribute(name=ModelXMLInfoSet.Attribute.RESULT_VALUE)
public Double getResultValue() {
return resultValue;
}
/**
* @param resultValue the resultValue to set
*/
public void setResultValue(Double resultValue) {
this.resultValue = resultValue;
}
/**
* @return the convertTime
*/
@XmlAttribute(name=ModelXMLInfoSet.Attribute.CONVERT_TIME)
public Long getConvertTime() {
return convertTime;
}
/**
* @param convertTime the convertTime to set
*/
public void setConvertTime(Long convertTime) {
this.convertTime = convertTime;
}
} }

View file

@ -23,8 +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.config.builder.UnitXCNumberTypeSetting.toSuperScript; import static net.forwardfire.unitxc.config.builder.UnitXCNumberTypeSetting.toSuperScript;
import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_DERIVED; import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_DERIVED;
import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
@ -46,10 +47,25 @@ public class UnitXCModuleArea implements UnitXCConfigModule {
public static final String GROUP_DESCRIPTION = "It is defined as the area of a square whose sides measure exactly one metre. The square metre is derived from the SI base unit of the metre,"; public static final String GROUP_DESCRIPTION = "It is defined as the area of a square whose sides measure exactly one metre. The square metre is derived from the SI base unit of the metre,";
public static final String GROUP_BASE_TYPE_ID = UnitXCModuleLength.TYPE_METRE_ID+UNIT_ID_POSTFIX; public static final String GROUP_BASE_TYPE_ID = UnitXCModuleLength.TYPE_METRE_ID+UNIT_ID_POSTFIX;
public static final String TYPE_CENTIARE_ALIAS = "";
public static final String TYPE_CENTIARE_ID = "ca";
public static final String TYPE_CENTIARE_NAME = "centiare";
public static final String TYPE_CENTIARE_URL = "https://en.wikipedia.org/wiki/Hectare#Centiare";
public static final String TYPE_ARE_ALIAS = "dam²";
public static final String TYPE_ARE_ID = "a";
public static final String TYPE_ARE_NAME = "are";
public static final String TYPE_ARE_URL = "https://en.wikipedia.org/wiki/Hectare#Are";
public static final String TYPE_HECTARE_ALIAS = "hm²";
public static final String TYPE_HECTARE_ID = "ha";
public static final String TYPE_HECTARE_NAME = "hectare";
public static final String TYPE_HECTARE_URL = "https://en.wikipedia.org/wiki/Hectare";
@Override @Override
public void configModule(UnitXCConfigBuilder builder) { public void configModule(UnitXCConfigBuilder builder) {
builder.createUnitGroup() builder.createUnitGroup()
.setType( SI_DERIVED) .setGroupLevel( SI_DERIVED)
.setId( GROUP_ID) .setId( GROUP_ID)
.setName( GROUP_NAME) .setName( GROUP_NAME)
.setDescription ( GROUP_DESCRIPTION) .setDescription ( GROUP_DESCRIPTION)
@ -58,6 +74,24 @@ public class UnitXCModuleArea implements UnitXCConfigModule {
.setUnitIdPostfix(UNIT_ID_POSTFIX) .setUnitIdPostfix(UNIT_ID_POSTFIX)
.setUnitNamePrefix(UNIT_NAME_PREFIX) .setUnitNamePrefix(UNIT_NAME_PREFIX)
.build() .build()
.createUnitType()
.setAliasOfType( TYPE_CENTIARE_ALIAS)
.setId( TYPE_CENTIARE_ID)
.setName( TYPE_CENTIARE_NAME)
.addWebsiteLink( TYPE_CENTIARE_URL)
.build()
.createUnitType()
.setAliasOfType( TYPE_ARE_ALIAS)
.setId( TYPE_ARE_ID)
.setName( TYPE_ARE_NAME)
.addWebsiteLink( TYPE_ARE_URL)
.build()
.createUnitType()
.setAliasOfType( TYPE_HECTARE_ALIAS)
.setId( TYPE_HECTARE_ID)
.setName( TYPE_HECTARE_NAME)
.addWebsiteLink( TYPE_HECTARE_URL)
.build()
.build() .build()
; ;
} }

View file

@ -24,7 +24,7 @@
package net.forwardfire.unitxc.module; package net.forwardfire.unitxc.module;
import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag; import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag;
import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE; import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_BASE;
import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
@ -43,19 +43,21 @@ public class UnitXCModuleElectricCurrent implements UnitXCConfigModule {
public static final String TYPE_AMPERE_ID = "A"; public static final String TYPE_AMPERE_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 = buildFlag(GROUP_ID,TYPE_AMPERE_NAME); public static final String TYPE_AMPERE_FLAG = buildFlag(GROUP_ID,TYPE_AMPERE_NAME);
private static final String TYPE_AMPERE_WIKI = "Ampere";
@Override @Override
public void configModule(UnitXCConfigBuilder builder) { public void configModule(UnitXCConfigBuilder builder) {
builder.createUnitGroup() builder.createUnitGroup()
.setType( SI_BASE) .setGroupLevel( SI_BASE)
.setId( GROUP_ID) .setId( GROUP_ID)
.setName( TYPE_AMPERE_NAME) .setName( TYPE_AMPERE_NAME)
.setDescription ( GROUP_DESCRIPTION) .setDescription ( GROUP_DESCRIPTION)
.setBaseTypeId( TYPE_AMPERE_ID) .setBaseTypeId( TYPE_AMPERE_ID)
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_AMPERE_ID) .setId( TYPE_AMPERE_ID)
.setName( TYPE_AMPERE_NAME) .setName( TYPE_AMPERE_NAME)
.addTypeFlag( TYPE_AMPERE_FLAG) .addTypeFlag( TYPE_AMPERE_FLAG)
.addWebsiteWiki( TYPE_AMPERE_WIKI)
.addCommonSIPrefix(UnitXCTypeSIPrefix.MEGA) .addCommonSIPrefix(UnitXCTypeSIPrefix.MEGA)
.addCommonSIPrefix(UnitXCTypeSIPrefix.KILO) .addCommonSIPrefix(UnitXCTypeSIPrefix.KILO)
.addCommonSIPrefix(UnitXCTypeSIPrefix.MILLI) .addCommonSIPrefix(UnitXCTypeSIPrefix.MILLI)

View file

@ -24,7 +24,7 @@
package net.forwardfire.unitxc.module; package net.forwardfire.unitxc.module;
import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag; import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag;
import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE; import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_BASE;
import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
@ -42,164 +42,200 @@ public class UnitXCModuleLength implements UnitXCConfigModule {
public static final String GROUP_DESCRIPTION = "The metre is the length of the path travelled by light in vacuum during a time interval of 1/299792458 of a second."; public static final String GROUP_DESCRIPTION = "The metre is the length of the path travelled by light in vacuum during a time interval of 1/299792458 of a second.";
public static final String TYPE_METRE_ID = "m"; public static final String TYPE_METRE_ID = "m";
public static final String TYPE_METRE_NAME = "metre"; public static final String TYPE_METRE_NAME = "metre";
public static final String TYPE_METRE_NAME_METER = "meter";
public static final String TYPE_METRE_FLAG = buildFlag(GROUP_ID,TYPE_METRE_NAME); public static final String TYPE_METRE_FLAG = buildFlag(GROUP_ID,TYPE_METRE_NAME);
public static final String TYPE_METRE_ALIAS_METER = "meter";
public static final String TYPE_METRE_ALIAS_METER_FLAG = buildFlag(GROUP_ID,TYPE_METRE_ALIAS_METER);
private static final String TYPE_METRE_WIKI = "Metre";
private static final UnitXCTypeSIPrefix[] TYPE_METRE_COMMON_SI = new UnitXCTypeSIPrefix[] {
UnitXCTypeSIPrefix.KILO,
UnitXCTypeSIPrefix.HECTO,
UnitXCTypeSIPrefix.DECA,
UnitXCTypeSIPrefix.DECI,
UnitXCTypeSIPrefix.CENTI,
UnitXCTypeSIPrefix.MILLI,
UnitXCTypeSIPrefix.MICRO,
UnitXCTypeSIPrefix.NANO,
UnitXCTypeSIPrefix.PICO,
UnitXCTypeSIPrefix.FEMTO
};
public static final String TYPE_INCH_ID = "in"; public static final String TYPE_INCH_ID = "in";
public static final String TYPE_INCH_NAME = "inch"; public static final String TYPE_INCH_NAME = "inch";
public static final String TYPE_INCH_NAME_PLURAL = "inches";
public static final String TYPE_INCH_FLAG = buildFlag(GROUP_ID,TYPE_INCH_NAME); 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;
private static final String TYPE_INCH_WIKI = "Inch";
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 = buildFlag(GROUP_ID,TYPE_LINK_NAME); 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_LINK_WIKI = "Link_(unit)";
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_NAME_PLURAL = "feet";
public static final String TYPE_FOOT_FLAG = buildFlag(GROUP_ID,TYPE_FOOT_NAME); 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;
private static final String TYPE_FOOT_WIKI = "Foot_(unit)";
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 = buildFlag(GROUP_ID,TYPE_YARD_NAME); 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;
private static final String TYPE_YARD_WIKI = "Yard";
public static final String TYPE_ROD_ID = "rd"; public static final String TYPE_ROD_ID = "rd";
public static final String TYPE_ROD_NAME = "rod"; public static final String TYPE_ROD_NAME = "rod";
public static final String TYPE_ROD_FLAG = buildFlag(GROUP_ID,TYPE_ROD_NAME); public static final String TYPE_ROD_FLAG = buildFlag(GROUP_ID,TYPE_ROD_NAME);
private static final double TYPE_ROD_FACTOR = 5.029; private static final double TYPE_ROD_FACTOR = 5.029;
private static final String TYPE_ROD_WIKI = "Rod_(unit)";
public static final String TYPE_CHAIN_ID = "ch"; public static final String TYPE_CHAIN_ID = "ch";
public static final String TYPE_CHAIN_NAME = "chain"; public static final String TYPE_CHAIN_NAME = "chain";
public static final String TYPE_CHAIN_FLAG = buildFlag(GROUP_ID,TYPE_CHAIN_NAME); public static final String TYPE_CHAIN_FLAG = buildFlag(GROUP_ID,TYPE_CHAIN_NAME);
private static final double TYPE_CHAIN_FACTOR = 20.117; private static final double TYPE_CHAIN_FACTOR = 20.117;
private static final String TYPE_CHAIN_WIKI = "Chain_(unit)";
public static final String TYPE_FURLONG_ID = "fur"; public static final String TYPE_FURLONG_ID = "fur";
public static final String TYPE_FURLONG_NAME = "furlong"; public static final String TYPE_FURLONG_NAME = "furlong";
public static final String TYPE_FURLONG_FLAG = buildFlag(GROUP_ID,TYPE_FURLONG_NAME); public static final String TYPE_FURLONG_FLAG = buildFlag(GROUP_ID,TYPE_FURLONG_NAME);
private static final double TYPE_FURLONG_FACTOR = 201.17; private static final double TYPE_FURLONG_FACTOR = 201.17;
private static final String TYPE_FURLONG_WIKI = "Furlong";
public static final String TYPE_MILE_ID = "mi"; public static final String TYPE_MILE_ID = "mi";
public static final String TYPE_MILE_NAME = "mile"; public static final String TYPE_MILE_NAME = "mile";
public static final String TYPE_MILE_FLAG = buildFlag(GROUP_ID,TYPE_MILE_NAME); public static final String TYPE_MILE_FLAG = buildFlag(GROUP_ID,TYPE_MILE_NAME);
private static final double TYPE_MILE_FACTOR = 1609.344; private static final double TYPE_MILE_FACTOR = 1609.344;
private static final String TYPE_MILE_WIKI = "Mile";
public static final String TYPE_MILE_US_ID = "us-mi"; public static final String TYPE_MILE_US_ID = "us-mi";
public static final String TYPE_MILE_US_NAME = "us-mile"; 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); public static final String TYPE_MILE_US_FLAG = buildFlag(GROUP_ID,TYPE_MILE_US_NAME);
private static final double TYPE_MILE_US_FACTOR = 1609.34721869; private static final double TYPE_MILE_US_FACTOR = 1609.34721869;
private static final String TYPE_MILE_US_WIKI = "Mile#US_survey_mile";
public static final String TYPE_MILE_NAUTICAL_ID = "nau-mi"; public static final String TYPE_MILE_NAUTICAL_ID = "nau-mi";
public static final String TYPE_MILE_NAUTICAL_NAME = "nautical-mile"; 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); public static final String TYPE_MILE_NAUTICAL_FLAG = buildFlag(GROUP_ID,TYPE_MILE_NAUTICAL_NAME);
private static final double TYPE_MILE_NAUTICAL_FACTOR = 1852; private static final double TYPE_MILE_NAUTICAL_FACTOR = 1852;
private static final String TYPE_MILE_NAUTICAL_WIKI = "Mile#Nautical_mile";
@Override @Override
public void configModule(UnitXCConfigBuilder builder) { public void configModule(UnitXCConfigBuilder builder) {
builder.createUnitGroup() builder.createUnitGroup()
.setType( SI_BASE) .setGroupLevel( SI_BASE)
.setId( GROUP_ID) .setId( GROUP_ID)
.setName( TYPE_METRE_NAME) .setName( TYPE_METRE_NAME)
.setDescription ( GROUP_DESCRIPTION) .setDescription ( GROUP_DESCRIPTION)
.setBaseTypeId( TYPE_METRE_ID) .setBaseTypeId( TYPE_METRE_ID)
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_METRE_ID) .setId( TYPE_METRE_ID)
.setName( TYPE_METRE_NAME) .setName( TYPE_METRE_NAME)
.addNameAlias(TYPE_METRE_NAME_METER) .addTypeFlag( TYPE_METRE_FLAG)
.addTypeFlag( TYPE_METRE_FLAG) .addWebsiteWiki( TYPE_METRE_WIKI)
.addCommonSIPrefix(UnitXCTypeSIPrefix.KILO) .addCommonSIPrefixes(TYPE_METRE_COMMON_SI)
.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) .setAliasOfType(TYPE_METRE_ID)
.setName( TYPE_INCH_NAME) .setId( TYPE_METRE_ALIAS_METER)
.addTypeFlag( TYPE_INCH_FLAG) .setName( TYPE_METRE_ALIAS_METER)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) .addTypeFlag( TYPE_METRE_ALIAS_METER_FLAG)
.addCommonSIPrefixes(TYPE_METRE_COMMON_SI)
.build()
.createSIUnitTypes()
.setId( TYPE_INCH_ID)
.setName( TYPE_INCH_NAME)
.setNamePlural( TYPE_INCH_NAME_PLURAL)
.addTypeFlag( TYPE_INCH_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addWebsiteWiki( TYPE_INCH_WIKI)
.createToBaseConverterSteps().multiply(TYPE_INCH_FACTOR).power10Down(UnitXCTypeSIPrefix.MILLI).build() .createToBaseConverterSteps().multiply(TYPE_INCH_FACTOR).power10Down(UnitXCTypeSIPrefix.MILLI).build()
.createFromBaseConverterSteps().power10Up(UnitXCTypeSIPrefix.MILLI).divide(TYPE_INCH_FACTOR).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)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA)
.addWebsiteWiki( TYPE_LINK_WIKI)
.createToBaseConverterSteps().multiply(TYPE_LINK_FACTOR).power10Down(UnitXCTypeSIPrefix.MILLI).build() .createToBaseConverterSteps().multiply(TYPE_LINK_FACTOR).power10Down(UnitXCTypeSIPrefix.MILLI).build()
.createFromBaseConverterSteps().power10Up(UnitXCTypeSIPrefix.MILLI).divide(TYPE_LINK_FACTOR).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) .setNamePlural( TYPE_FOOT_NAME_PLURAL)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) .addTypeFlag( TYPE_FOOT_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addWebsiteWiki( TYPE_FOOT_WIKI)
.createToBaseConverterSteps().multiply(TYPE_FOOT_FACTOR).power10Down(UnitXCTypeSIPrefix.MILLI).build() .createToBaseConverterSteps().multiply(TYPE_FOOT_FACTOR).power10Down(UnitXCTypeSIPrefix.MILLI).build()
.createFromBaseConverterSteps().power10Up(UnitXCTypeSIPrefix.MILLI).divide(TYPE_FOOT_FACTOR).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) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addWebsiteWiki( TYPE_YARD_WIKI)
.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() .createSIUnitTypes()
.setId( TYPE_ROD_ID) .setId( TYPE_ROD_ID)
.setName( TYPE_ROD_NAME) .setName( TYPE_ROD_NAME)
.addTypeFlag( TYPE_ROD_FLAG) .addTypeFlag( TYPE_ROD_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA)
.addWebsiteWiki( TYPE_ROD_WIKI)
.createToBaseConverterSteps().multiply(TYPE_ROD_FACTOR).build() .createToBaseConverterSteps().multiply(TYPE_ROD_FACTOR).build()
.createFromBaseConverterSteps().divide(TYPE_ROD_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_ROD_FACTOR).build()
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_CHAIN_ID) .setId( TYPE_CHAIN_ID)
.setName( TYPE_CHAIN_NAME) .setName( TYPE_CHAIN_NAME)
.addTypeFlag( TYPE_CHAIN_FLAG) .addTypeFlag( TYPE_CHAIN_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA)
.addWebsiteWiki( TYPE_CHAIN_WIKI)
.createToBaseConverterSteps().multiply(TYPE_CHAIN_FACTOR).build() .createToBaseConverterSteps().multiply(TYPE_CHAIN_FACTOR).build()
.createFromBaseConverterSteps().divide(TYPE_CHAIN_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_CHAIN_FACTOR).build()
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_FURLONG_ID) .setId( TYPE_FURLONG_ID)
.setName( TYPE_FURLONG_NAME) .setName( TYPE_FURLONG_NAME)
.addTypeFlag( TYPE_FURLONG_FLAG) .addTypeFlag( TYPE_FURLONG_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA)
.addWebsiteWiki( TYPE_FURLONG_WIKI)
.createToBaseConverterSteps().multiply(TYPE_FURLONG_FACTOR).build() .createToBaseConverterSteps().multiply(TYPE_FURLONG_FACTOR).build()
.createFromBaseConverterSteps().divide(TYPE_FURLONG_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_FURLONG_FACTOR).build()
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_MILE_ID) .setId( TYPE_MILE_ID)
.setName( TYPE_MILE_NAME) .setName( TYPE_MILE_NAME)
.addTypeFlag( TYPE_MILE_FLAG) .addTypeFlag( TYPE_MILE_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addWebsiteWiki( TYPE_MILE_WIKI)
.createToBaseConverterSteps().multiply(TYPE_MILE_FACTOR).build() .createToBaseConverterSteps().multiply(TYPE_MILE_FACTOR).build()
.createFromBaseConverterSteps().divide(TYPE_MILE_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_MILE_FACTOR).build()
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_MILE_US_ID) .setId( TYPE_MILE_US_ID)
.setName( TYPE_MILE_US_NAME) .setName( TYPE_MILE_US_NAME)
.addTypeFlag( TYPE_MILE_US_FLAG) .addTypeFlag( TYPE_MILE_US_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addWebsiteWiki( TYPE_MILE_US_WIKI)
.createToBaseConverterSteps().multiply(TYPE_MILE_US_FACTOR).build() .createToBaseConverterSteps().multiply(TYPE_MILE_US_FACTOR).build()
.createFromBaseConverterSteps().divide(TYPE_MILE_US_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_MILE_US_FACTOR).build()
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_MILE_NAUTICAL_ID) .setId( TYPE_MILE_NAUTICAL_ID)
.setName( TYPE_MILE_NAUTICAL_NAME) .setName( TYPE_MILE_NAUTICAL_NAME)
.addTypeFlag( TYPE_MILE_NAUTICAL_FLAG) .addTypeFlag( TYPE_MILE_NAUTICAL_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addWebsiteWiki( TYPE_MILE_NAUTICAL_WIKI)
.createToBaseConverterSteps().multiply(TYPE_MILE_NAUTICAL_FACTOR).build() .createToBaseConverterSteps().multiply(TYPE_MILE_NAUTICAL_FACTOR).build()
.createFromBaseConverterSteps().divide(TYPE_MILE_NAUTICAL_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_MILE_NAUTICAL_FACTOR).build()
.build() .build()

View file

@ -24,7 +24,7 @@
package net.forwardfire.unitxc.module; package net.forwardfire.unitxc.module;
import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag; import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag;
import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE; import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_BASE;
import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
@ -42,19 +42,21 @@ public class UnitXCModuleLuminousIntensity implements UnitXCConfigModule {
public static final String TYPE_CANDELA_ID = "cd"; public static final String TYPE_CANDELA_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 = buildFlag(GROUP_ID,TYPE_CANDELA_NAME); public static final String TYPE_CANDELA_FLAG = buildFlag(GROUP_ID,TYPE_CANDELA_NAME);
private static final String TYPE_CANDELA_WIKI = "Candela";
@Override @Override
public void configModule(UnitXCConfigBuilder builder) { public void configModule(UnitXCConfigBuilder builder) {
builder.createUnitGroup() builder.createUnitGroup()
.setType( SI_BASE) .setGroupLevel( SI_BASE)
.setId( GROUP_ID) .setId( GROUP_ID)
.setName( TYPE_CANDELA_NAME) .setName( TYPE_CANDELA_NAME)
.setDescription ( GROUP_DESCRIPTION) .setDescription ( GROUP_DESCRIPTION)
.setBaseTypeId( TYPE_CANDELA_ID) .setBaseTypeId( TYPE_CANDELA_ID)
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_CANDELA_ID) .setId( TYPE_CANDELA_ID)
.setName( TYPE_CANDELA_NAME) .setName( TYPE_CANDELA_NAME)
.addTypeFlag( TYPE_CANDELA_FLAG) .addTypeFlag( TYPE_CANDELA_FLAG)
.addWebsiteWiki( TYPE_CANDELA_WIKI)
.build() .build()
.build() .build()
; ;

View file

@ -24,7 +24,7 @@
package net.forwardfire.unitxc.module; package net.forwardfire.unitxc.module;
import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag; import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag;
import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE; import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_BASE;
import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
@ -44,44 +44,51 @@ public class UnitXCModuleMass implements UnitXCConfigModule {
public static final String TYPE_GRAM_ID = "g"; public static final String TYPE_GRAM_ID = "g";
public static final String TYPE_GRAM_NAME = "gram"; 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_GRAM_FLAG = buildFlag(GROUP_ID,TYPE_GRAM_NAME);
private static final String TYPE_GRAM_WIKI = "Kilogram";
public static final String TYPE_GRAIN_ID = "gr"; public static final String TYPE_GRAIN_ID = "gr";
public static final String TYPE_GRAIN_NAME = "grain"; public static final String TYPE_GRAIN_NAME = "grain";
public static final String TYPE_GRAIN_FLAG = buildFlag(GROUP_ID,TYPE_GRAIN_NAME); public static final String TYPE_GRAIN_FLAG = buildFlag(GROUP_ID,TYPE_GRAIN_NAME);
private static final double TYPE_GRAIN_FACTOR = 64.80; private static final double TYPE_GRAIN_FACTOR = 64.80;
private static final String TYPE_GRAIN_WIKI = "Grain_(unit)";
public static final String TYPE_DRAM_ID = "dr"; public static final String TYPE_DRAM_ID = "dr";
public static final String TYPE_DRAM_NAME = "dram"; public static final String TYPE_DRAM_NAME = "dram";
public static final String TYPE_DRAM_FLAG = buildFlag(GROUP_ID,TYPE_DRAM_NAME); public static final String TYPE_DRAM_FLAG = buildFlag(GROUP_ID,TYPE_DRAM_NAME);
private static final double TYPE_DRAM_FACTOR = 1.772; private static final double TYPE_DRAM_FACTOR = 1.772;
private static final String TYPE_DRAM_WIKI = "Dram_(unit)";
public static final String TYPE_OUNCE_ID = "oz"; public static final String TYPE_OUNCE_ID = "oz";
public static final String TYPE_OUNCE_NAME = "ounce"; public static final String TYPE_OUNCE_NAME = "ounce";
public static final String TYPE_OUNCE_FLAG = buildFlag(GROUP_ID,TYPE_OUNCE_NAME); public static final String TYPE_OUNCE_FLAG = buildFlag(GROUP_ID,TYPE_OUNCE_NAME);
private static final double TYPE_OUNCE_FACTOR = 28.35; private static final double TYPE_OUNCE_FACTOR = 28.35;
private static final String TYPE_OUNCE_WIKI = "Ounce";
public static final String TYPE_POUND_ID = "lb"; public static final String TYPE_POUND_ID = "lb";
public static final String TYPE_POUND_NAME = "pound"; public static final String TYPE_POUND_NAME = "pound";
public static final String TYPE_POUND_FLAG = buildFlag(GROUP_ID,TYPE_POUND_NAME); public static final String TYPE_POUND_FLAG = buildFlag(GROUP_ID,TYPE_POUND_NAME);
private static final double TYPE_POUND_FACTOR = 453.6; private static final double TYPE_POUND_FACTOR = 453.6;
private static final String TYPE_POUND_WIKI = "Pound_(mass)";
public static final String TYPE_STONE_ID = "st"; public static final String TYPE_STONE_ID = "st";
public static final String TYPE_STONE_NAME = "stone"; public static final String TYPE_STONE_NAME = "stone";
public static final String TYPE_STONE_FLAG = buildFlag(GROUP_ID,TYPE_STONE_NAME); public static final String TYPE_STONE_FLAG = buildFlag(GROUP_ID,TYPE_STONE_NAME);
private static final double TYPE_STONE_FACTOR = 6.350; private static final double TYPE_STONE_FACTOR = 6.350;
private static final String TYPE_STONE_WIKI = "Stone_(unit)";
@Override @Override
public void configModule(UnitXCConfigBuilder builder) { public void configModule(UnitXCConfigBuilder builder) {
builder.createUnitGroup() builder.createUnitGroup()
.setType( SI_BASE) .setGroupLevel( SI_BASE)
.setId( GROUP_ID) .setId( GROUP_ID)
.setName( GROUP_NAME) .setName( GROUP_NAME)
.setDescription ( GROUP_DESCRIPTION) .setDescription ( GROUP_DESCRIPTION)
.setBaseTypeId( TYPE_GRAM_ID) .setBaseTypeId( TYPE_GRAM_ID)
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_GRAM_ID) .setId( TYPE_GRAM_ID)
.setName( TYPE_GRAM_NAME) .setName( TYPE_GRAM_NAME)
.addTypeFlag( TYPE_GRAM_FLAG) .addTypeFlag( TYPE_GRAM_FLAG)
.addWebsiteWiki( TYPE_GRAM_WIKI)
.addCommonSIPrefix(UnitXCTypeSIPrefix.MEGA) .addCommonSIPrefix(UnitXCTypeSIPrefix.MEGA)
.addCommonSIPrefix(UnitXCTypeSIPrefix.KILO) .addCommonSIPrefix(UnitXCTypeSIPrefix.KILO)
.addCommonSIPrefix(UnitXCTypeSIPrefix.HECTO) .addCommonSIPrefix(UnitXCTypeSIPrefix.HECTO)
@ -92,44 +99,49 @@ public class UnitXCModuleMass implements UnitXCConfigModule {
.addCommonSIPrefix(UnitXCTypeSIPrefix.MICRO) .addCommonSIPrefix(UnitXCTypeSIPrefix.MICRO)
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_GRAIN_ID) .setId( TYPE_GRAIN_ID)
.setName( TYPE_GRAIN_NAME) .setName( TYPE_GRAIN_NAME)
.addTypeFlag( TYPE_GRAIN_FLAG) .addTypeFlag( TYPE_GRAIN_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addWebsiteWiki( TYPE_GRAIN_WIKI)
.createToBaseConverterSteps().multiply(TYPE_GRAIN_FACTOR).power10Down(UnitXCTypeSIPrefix.MILLI).build() .createToBaseConverterSteps().multiply(TYPE_GRAIN_FACTOR).power10Down(UnitXCTypeSIPrefix.MILLI).build()
.createFromBaseConverterSteps().power10Up(UnitXCTypeSIPrefix.MILLI).divide(TYPE_GRAIN_FACTOR).build() .createFromBaseConverterSteps().power10Up(UnitXCTypeSIPrefix.MILLI).divide(TYPE_GRAIN_FACTOR).build()
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_DRAM_ID) .setId( TYPE_DRAM_ID)
.setName( TYPE_DRAM_NAME) .setName( TYPE_DRAM_NAME)
.addTypeFlag( TYPE_DRAM_FLAG) .addTypeFlag( TYPE_DRAM_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA)
.addWebsiteWiki( TYPE_DRAM_WIKI)
.createToBaseConverterSteps().multiply(TYPE_DRAM_FACTOR).build() .createToBaseConverterSteps().multiply(TYPE_DRAM_FACTOR).build()
.createFromBaseConverterSteps().divide(TYPE_DRAM_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_DRAM_FACTOR).build()
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_OUNCE_ID) .setId( TYPE_OUNCE_ID)
.setName( TYPE_OUNCE_NAME) .setName( TYPE_OUNCE_NAME)
.addTypeFlag( TYPE_OUNCE_FLAG) .addTypeFlag( TYPE_OUNCE_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addWebsiteWiki( TYPE_OUNCE_WIKI)
.createToBaseConverterSteps().multiply(TYPE_OUNCE_FACTOR).build() .createToBaseConverterSteps().multiply(TYPE_OUNCE_FACTOR).build()
.createFromBaseConverterSteps().divide(TYPE_OUNCE_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_OUNCE_FACTOR).build()
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_POUND_ID) .setId( TYPE_POUND_ID)
.setName( TYPE_POUND_NAME) .setName( TYPE_POUND_NAME)
.addTypeFlag( TYPE_POUND_FLAG) .addTypeFlag( TYPE_POUND_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addWebsiteWiki( TYPE_POUND_WIKI)
.createToBaseConverterSteps().multiply(TYPE_POUND_FACTOR).build() .createToBaseConverterSteps().multiply(TYPE_POUND_FACTOR).build()
.createFromBaseConverterSteps().divide(TYPE_POUND_FACTOR).build() .createFromBaseConverterSteps().divide(TYPE_POUND_FACTOR).build()
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_STONE_ID) .setId( TYPE_STONE_ID)
.setName( TYPE_STONE_NAME) .setName( TYPE_STONE_NAME)
.addTypeFlag( TYPE_STONE_FLAG) .addTypeFlag( TYPE_STONE_FLAG)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL_EXTRA)
.addWebsiteWiki( TYPE_STONE_WIKI)
.createToBaseConverterSteps().multiply(TYPE_STONE_FACTOR).power10Up(UnitXCTypeSIPrefix.MILLI).build() .createToBaseConverterSteps().multiply(TYPE_STONE_FACTOR).power10Up(UnitXCTypeSIPrefix.MILLI).build()
.createFromBaseConverterSteps().power10Down(UnitXCTypeSIPrefix.MILLI).divide(TYPE_STONE_FACTOR).build() .createFromBaseConverterSteps().power10Down(UnitXCTypeSIPrefix.MILLI).divide(TYPE_STONE_FACTOR).build()
.build() .build()

View file

@ -21,63 +21,38 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package net.forwardfire.unitxc.model; package net.forwardfire.unitxc.module;
import java.util.List; import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_DERIVED;
import org.apache.commons.lang3.Validate; import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
import net.forwardfire.unitxc.config.builder.UnitXCTypeCompoundPairBuilder;
/** /**
* *
* *
* @author Willem Cazander * @author Willem Cazander
* @version 1.0 Oct 22, 2015 * @version 1.0 Nov 3, 2015
*/ */
public class UnitXConverterResultModel implements UnitXConverterResult { public class UnitXCModuleSpeed implements UnitXCConfigModule {
private final Double startValue; public static final String GROUP_ID = "speed";
private final List<UnitXConverterResultStep> resultSteps; public static final String GROUP_NAME = UnitXCModuleLength.TYPE_METRE_NAME+UnitXCTypeCompoundPairBuilder.PER_NAME+UnitXCModuleTime.TYPE_SECOND_NAME;;
private final Double resultValue; public static final String GROUP_DESCRIPTION = "Speed is the dimensions of a length divided by a time.";
private final Long convertTime; public static final String GROUP_BASE_TYPE_ID = UnitXCModuleLength.TYPE_METRE_ID+UnitXCTypeCompoundPairBuilder.PER_ID+UnitXCModuleTime.TYPE_SECOND_ID;
private final UnitXCType startValueType;
private final UnitXCType resultValueType;
public UnitXConverterResultModel(Double startValue,Double resultValue,List<UnitXConverterResultStep> resultSteps,Long convertTime,UnitXCType startValueType,UnitXCType resultValueType) {
this.startValue = Validate.notNull(startValue);
this.resultSteps = Validate.noNullElements(resultSteps);
this.resultValue = Validate.notNull(resultValue);
this.convertTime = convertTime;
this.startValueType = Validate.notNull(startValueType);
this.resultValueType = Validate.notNull(resultValueType);
}
@Override @Override
public Double getStartValue() { public void configModule(UnitXCConfigBuilder builder) {
return startValue; builder.createUnitGroup()
} .setGroupLevel( SI_DERIVED)
.setId( GROUP_ID)
@Override .setName( GROUP_NAME)
public Double getResultValue() { .setDescription ( GROUP_DESCRIPTION)
return resultValue; .setBaseTypeId( GROUP_BASE_TYPE_ID)
} .createCompoundPairUnitTypes(UnitXCModuleLength.GROUP_ID, UnitXCModuleTime.GROUP_ID)
.build()
@Override .build()
public List<UnitXConverterResultStep> getSteps() { ;
return resultSteps;
}
@Override
public Long getConvertTime() {
return convertTime;
}
@Override
public UnitXCType getStartValueType() {
return startValueType;
}
@Override
public UnitXCType getResultValueType() {
return resultValueType;
} }
} }

View file

@ -24,7 +24,7 @@
package net.forwardfire.unitxc.module; package net.forwardfire.unitxc.module;
import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag; import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag;
import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE; import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_BASE;
import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
@ -43,24 +43,27 @@ public class UnitXCModuleSubstance implements UnitXCConfigModule {
public static final String TYPE_MOLE_ID = "mol"; public static final String TYPE_MOLE_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 = buildFlag(GROUP_ID,TYPE_MOLE_NAME); public static final String TYPE_MOLE_FLAG = buildFlag(GROUP_ID,TYPE_MOLE_NAME);
private static final String TYPE_MOLE_WIKI = "Mole_(unit)";
public static final String TYPE_POUND_MOLE_ID = "lbmol"; public static final String TYPE_POUND_MOLE_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 = buildFlag(GROUP_ID,TYPE_POUND_MOLE_NAME); 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;
private static final String TYPE_POUND_MOLE_WIKI = "Mole_(unit)#Other_units_called_.22mole.22";
@Override @Override
public void configModule(UnitXCConfigBuilder builder) { public void configModule(UnitXCConfigBuilder builder) {
builder.createUnitGroup() builder.createUnitGroup()
.setType( SI_BASE) .setGroupLevel( SI_BASE)
.setId( GROUP_ID) .setId( GROUP_ID)
.setName( TYPE_MOLE_NAME) .setName( TYPE_MOLE_NAME)
.setDescription ( GROUP_DESCRIPTION) .setDescription ( GROUP_DESCRIPTION)
.setBaseTypeId( TYPE_MOLE_ID) .setBaseTypeId( TYPE_MOLE_ID)
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_MOLE_ID) .setId( TYPE_MOLE_ID)
.setName( TYPE_MOLE_NAME) .setName( TYPE_MOLE_NAME)
.addTypeFlag( TYPE_MOLE_FLAG) .addTypeFlag( TYPE_MOLE_FLAG)
.addWebsiteWiki( TYPE_MOLE_WIKI)
.addCommonSIPrefix(UnitXCTypeSIPrefix.MILLI) .addCommonSIPrefix(UnitXCTypeSIPrefix.MILLI)
.addCommonSIPrefix(UnitXCTypeSIPrefix.MICRO) .addCommonSIPrefix(UnitXCTypeSIPrefix.MICRO)
.addCommonSIPrefix(UnitXCTypeSIPrefix.NANO) .addCommonSIPrefix(UnitXCTypeSIPrefix.NANO)
@ -68,9 +71,10 @@ public class UnitXCModuleSubstance implements UnitXCConfigModule {
.addCommonSIPrefix(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) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addWebsiteWiki( TYPE_POUND_MOLE_WIKI)
.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

@ -27,7 +27,7 @@ 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.config.UnitXCConfigModule.buildFlag;
import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE; import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_BASE;
import org.apache.commons.lang3.math.Fraction; import org.apache.commons.lang3.math.Fraction;
@ -45,87 +45,104 @@ public class UnitXCModuleTemperature implements UnitXCConfigModule {
public static final String TYPE_KELVIN_ID = "K"; public static final String TYPE_KELVIN_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 = buildFlag(GROUP_ID,TYPE_KELVIN_NAME); public static final String TYPE_KELVIN_FLAG = buildFlag(GROUP_ID,TYPE_KELVIN_NAME);
private static final String TYPE_KELVIN_WIKI = "Kelvin";
public static final String TYPE_CELSIUS_ID = "°C"; public static final String TYPE_CELSIUS_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 = buildFlag(GROUP_ID,TYPE_CELSIUS_NAME); 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;
private static final String TYPE_CELSIUS_WIKI = "Celsius";
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 = buildFlag(GROUP_ID,TYPE_FAHRENHEIT_NAME); 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);
private static final String TYPE_FAHRENHEIT_WIKI = "Fahrenheit";
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 = buildFlag(GROUP_ID,TYPE_RANKINE_NAME); public static final String TYPE_RANKINE_FLAG = buildFlag(GROUP_ID,TYPE_RANKINE_NAME);
private static final String TYPE_RANKINE_WIKI = "Rankine_scale";
public static final String TYPE_ROMER_ID = ""; 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_NAME_ALIAS = "romer"; public static final String TYPE_ROMER_FLAG = buildFlag(GROUP_ID,TYPE_ROMER_NAME);
public static final String TYPE_ROMER_FLAG = buildFlag(GROUP_ID,TYPE_ROMER_NAME_ALIAS); public static final String TYPE_ROMER_ALIAS_NAME = "romer";
public static final String TYPE_ROMER_ALIAS_FLAG = buildFlag(GROUP_ID,TYPE_ROMER_ALIAS_NAME);
private static final Fraction TYPE_ROMER_FRACTION = Fraction.getFraction(40,21); private static final 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;
private static final String TYPE_ROMER_WIKI = "Rømer_scale";
@Override @Override
public void configModule(UnitXCConfigBuilder builder) { public void configModule(UnitXCConfigBuilder builder) {
builder.createUnitGroup() builder.createUnitGroup()
.setType( SI_BASE) .setGroupLevel( SI_BASE)
.setId( GROUP_ID) .setId( GROUP_ID)
.setName( TYPE_KELVIN_NAME) .setName( TYPE_KELVIN_NAME)
.setDescription( GROUP_DESCRIPTION) .setDescription( GROUP_DESCRIPTION)
.setBaseTypeId( TYPE_KELVIN_ID) .setBaseTypeId( TYPE_KELVIN_ID)
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_KELVIN_ID) .setId( TYPE_KELVIN_ID)
.setName( TYPE_KELVIN_NAME) .setName( TYPE_KELVIN_NAME)
.addTypeFlag( TYPE_KELVIN_FLAG) .addTypeFlag( TYPE_KELVIN_FLAG)
.addWebsiteWiki(TYPE_KELVIN_WIKI)
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_CELSIUS_ID) .setId( TYPE_CELSIUS_ID)
.setName( TYPE_CELSIUS_NAME) .setName( TYPE_CELSIUS_NAME)
.addTypeFlag( TYPE_CELSIUS_FLAG) .addTypeFlag( TYPE_CELSIUS_FLAG)
.addWebsiteWiki(TYPE_CELSIUS_WIKI)
.createToBaseConverterSteps().offsetUp(TYPE_CELSIUS_OFFSET).build() .createToBaseConverterSteps().offsetUp(TYPE_CELSIUS_OFFSET).build()
.createFromBaseConverterSteps().offsetDown(TYPE_CELSIUS_OFFSET).build() .createFromBaseConverterSteps().offsetDown(TYPE_CELSIUS_OFFSET).build()
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.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) .addTypeFlag( UnitXCConfigBuilder.TYPE_FLAG_IMPERIAL)
.addWebsiteWiki( TYPE_FAHRENHEIT_WIKI)
.createToBaseConverterSteps() .createToBaseConverterSteps()
.offsetUp(TYPE_FAHRENHEIT_OFFSET) .offsetUp( TYPE_FAHRENHEIT_OFFSET)
.multiply(TYPE_FAHRENHEIT_FRACTION) .multiply( TYPE_FAHRENHEIT_FRACTION)
.build() .build()
.createFromBaseConverterSteps() .createFromBaseConverterSteps()
.multiply(TYPE_FAHRENHEIT_FRACTION.invert()) .multiply( TYPE_FAHRENHEIT_FRACTION.invert())
.offsetDown(TYPE_FAHRENHEIT_OFFSET) .offsetDown( TYPE_FAHRENHEIT_OFFSET)
.build() .build()
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_RANKINE_ID) .setId( TYPE_RANKINE_ID)
.setName( TYPE_RANKINE_NAME) .setName( TYPE_RANKINE_NAME)
.addTypeFlag( TYPE_RANKINE_FLAG) .addTypeFlag ( TYPE_RANKINE_FLAG)
.addWebsiteWiki( TYPE_RANKINE_WIKI)
.createToBaseConverterSteps().multiply(TYPE_FAHRENHEIT_FRACTION).build() .createToBaseConverterSteps().multiply(TYPE_FAHRENHEIT_FRACTION).build()
.createFromBaseConverterSteps().multiply(TYPE_FAHRENHEIT_FRACTION.invert()).build() .createFromBaseConverterSteps().multiply(TYPE_FAHRENHEIT_FRACTION.invert()).build()
.build() .build()
.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) .addWebsiteWiki( TYPE_ROMER_WIKI)
.createToBaseConverterSteps() .createToBaseConverterSteps()
.offsetUp(TYPE_CELSIUS_OFFSET) .offsetUp( TYPE_CELSIUS_OFFSET)
.multiply(TYPE_ROMER_FRACTION) .multiply( TYPE_ROMER_FRACTION)
.offsetUp(TYPE_ROMER_FRACTION_OFFSET) .offsetUp( TYPE_ROMER_FRACTION_OFFSET)
.build() .build()
.createFromBaseConverterSteps() .createFromBaseConverterSteps()
.offsetDown(TYPE_ROMER_FRACTION_OFFSET) .offsetDown( TYPE_ROMER_FRACTION_OFFSET)
.multiply(TYPE_ROMER_FRACTION.invert()) .multiply( TYPE_ROMER_FRACTION.invert())
.offsetDown(TYPE_CELSIUS_OFFSET) .offsetDown( TYPE_CELSIUS_OFFSET)
.build() .build()
.build() .build()
.createSIUnitTypes()
.setAliasOfType( TYPE_ROMER_ID)
.setId( TYPE_ROMER_ALIAS_NAME)
.setName( TYPE_ROMER_ALIAS_NAME)
.addTypeFlag( TYPE_ROMER_ALIAS_FLAG)
.build()
.build() .build()
; ;
} }
} }

View file

@ -24,7 +24,7 @@
package net.forwardfire.unitxc.module; package net.forwardfire.unitxc.module;
import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag; import static net.forwardfire.unitxc.config.UnitXCConfigModule.buildFlag;
import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_BASE; import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_BASE;
import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
@ -52,21 +52,26 @@ public class UnitXCModuleTime implements UnitXCConfigModule {
private static final double TIME_EARTH_YEAR = 31557600; private static final double TIME_EARTH_YEAR = 31557600;
// related to the rotating earth, or change to be 794 243 384 928 000 hyperfine oscillations // related to the rotating earth, or change to be 794 243 384 928 000 hyperfine oscillations
public static final String TYPE_MINUTE_ID = "min";
public static final String TYPE_MINUTE_NAME = "minute"; public static final String TYPE_MINUTE_NAME = "minute";
public static final String TYPE_MINUTE_FLAG = buildFlag(GROUP_ID,TYPE_MINUTE_NAME); public static final String TYPE_MINUTE_FLAG = buildFlag(GROUP_ID,TYPE_MINUTE_NAME);
public static final String TYPE_HOUR_ID = "h";
public static final String TYPE_HOUR_NAME = "hour"; public static final String TYPE_HOUR_NAME = "hour";
public static final String TYPE_HOUR_FLAG = buildFlag(GROUP_ID,TYPE_HOUR_NAME); public static final String TYPE_HOUR_FLAG = buildFlag(GROUP_ID,TYPE_HOUR_NAME);
public static final String TYPE_DAY_ID = "d";
public static final String TYPE_DAY_NAME = "day"; public static final String TYPE_DAY_NAME = "day";
public static final String TYPE_DAY_FLAG = buildFlag(GROUP_ID,TYPE_DAY_NAME); public static final String TYPE_DAY_FLAG = buildFlag(GROUP_ID,TYPE_DAY_NAME);
public static final String TYPE_WEEK_ID = "wk";
public static final String TYPE_WEEK_NAME = "week"; public static final String TYPE_WEEK_NAME = "week";
public static final String TYPE_WEEK_FLAG = buildFlag(GROUP_ID,TYPE_WEEK_NAME); public static final String TYPE_WEEK_FLAG = buildFlag(GROUP_ID,TYPE_WEEK_NAME);
public static final String TYPE_YEAR_ID = "yr";
public static final String TYPE_YEAR_NAME = "year"; public static final String TYPE_YEAR_NAME = "year";
public static final String TYPE_YEAR_FLAG = buildFlag(GROUP_ID,TYPE_YEAR_NAME); public static final String TYPE_YEAR_FLAG = buildFlag(GROUP_ID,TYPE_YEAR_NAME);
@Override @Override
public void configModule(UnitXCConfigBuilder builder) { public void configModule(UnitXCConfigBuilder builder) {
builder.createUnitGroup() builder.createUnitGroup()
.setType( SI_BASE) .setGroupLevel( SI_BASE)
.setId( GROUP_ID) .setId( GROUP_ID)
.setName( TYPE_SECOND_NAME) .setName( TYPE_SECOND_NAME)
.setDescription ( GROUP_DESCRIPTION) .setDescription ( GROUP_DESCRIPTION)
@ -83,35 +88,35 @@ public class UnitXCModuleTime implements UnitXCConfigModule {
.addCommonSIPrefix(UnitXCTypeSIPrefix.ATTO) .addCommonSIPrefix(UnitXCTypeSIPrefix.ATTO)
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_MINUTE_NAME) .setId( TYPE_MINUTE_ID)
.setName( TYPE_MINUTE_NAME) .setName( TYPE_MINUTE_NAME)
.addTypeFlag( TYPE_MINUTE_FLAG) .addTypeFlag( TYPE_MINUTE_FLAG)
.createToBaseConverterSteps().multiply(TIME_EARTH_MINUTE).build() .createToBaseConverterSteps().multiply(TIME_EARTH_MINUTE).build()
.createFromBaseConverterSteps().divide(TIME_EARTH_MINUTE).build() .createFromBaseConverterSteps().divide(TIME_EARTH_MINUTE).build()
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_HOUR_NAME) .setId( TYPE_HOUR_ID)
.setName( TYPE_HOUR_NAME) .setName( TYPE_HOUR_NAME)
.addTypeFlag( TYPE_HOUR_FLAG) .addTypeFlag( TYPE_HOUR_FLAG)
.createToBaseConverterSteps().multiply(TIME_EARTH_HOUR).build() .createToBaseConverterSteps().multiply(TIME_EARTH_HOUR).build()
.createFromBaseConverterSteps().divide(TIME_EARTH_HOUR).build() .createFromBaseConverterSteps().divide(TIME_EARTH_HOUR).build()
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_DAY_NAME) .setId( TYPE_DAY_ID)
.setName( TYPE_DAY_NAME) .setName( TYPE_DAY_NAME)
.addTypeFlag( TYPE_DAY_FLAG) .addTypeFlag( TYPE_DAY_FLAG)
.createToBaseConverterSteps().multiply(TIME_EARTH_DAY).build() .createToBaseConverterSteps().multiply(TIME_EARTH_DAY).build()
.createFromBaseConverterSteps().divide(TIME_EARTH_DAY).build() .createFromBaseConverterSteps().divide(TIME_EARTH_DAY).build()
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_WEEK_NAME) .setId( TYPE_WEEK_ID)
.setName( TYPE_WEEK_NAME) .setName( TYPE_WEEK_NAME)
.addTypeFlag( TYPE_WEEK_FLAG) .addTypeFlag( TYPE_WEEK_FLAG)
.createToBaseConverterSteps().multiply(TIME_EARTH_WEEK).build() .createToBaseConverterSteps().multiply(TIME_EARTH_WEEK).build()
.createFromBaseConverterSteps().divide(TIME_EARTH_WEEK).build() .createFromBaseConverterSteps().divide(TIME_EARTH_WEEK).build()
.build() .build()
.createSIUnitTypes() .createSIUnitTypes()
.setId( TYPE_YEAR_NAME) .setId( TYPE_YEAR_ID)
.setName( TYPE_YEAR_NAME) .setName( TYPE_YEAR_NAME)
.addTypeFlag( TYPE_YEAR_FLAG) .addTypeFlag( TYPE_YEAR_FLAG)
.createToBaseConverterSteps().multiply(TIME_EARTH_YEAR).build() .createToBaseConverterSteps().multiply(TIME_EARTH_YEAR).build()

View file

@ -24,7 +24,7 @@
package net.forwardfire.unitxc.module; package net.forwardfire.unitxc.module;
import static net.forwardfire.unitxc.config.builder.UnitXCNumberTypeSetting.toSuperScript; import static net.forwardfire.unitxc.config.builder.UnitXCNumberTypeSetting.toSuperScript;
import static net.forwardfire.unitxc.model.UnitXCGroupType.SI_DERIVED; import static net.forwardfire.unitxc.model.UnitXCGroupLevel.SI_DERIVED;
import net.forwardfire.unitxc.config.UnitXCConfigModule; import net.forwardfire.unitxc.config.UnitXCConfigModule;
import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder; import net.forwardfire.unitxc.config.builder.UnitXCConfigBuilder;
@ -49,7 +49,7 @@ public class UnitXCModuleVolume implements UnitXCConfigModule {
@Override @Override
public void configModule(UnitXCConfigBuilder builder) { public void configModule(UnitXCConfigBuilder builder) {
builder.createUnitGroup() builder.createUnitGroup()
.setType( SI_DERIVED) .setGroupLevel( SI_DERIVED)
.setId( GROUP_ID) .setId( GROUP_ID)
.setName( GROUP_NAME) .setName( GROUP_NAME)
.setDescription ( GROUP_DESCRIPTION) .setDescription ( GROUP_DESCRIPTION)

View file

@ -35,6 +35,7 @@ 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.ModelXMLMarshaller;
import net.forwardfire.unitxc.model.UnitXCGroup; 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;
@ -42,7 +43,8 @@ import net.forwardfire.unitxc.model.UnitXConverterResultStep;
public class UnitXCConverterStepTest { public class UnitXCConverterStepTest {
@Test @Test
public void testFac() { public void testFac() throws Exception {
ModelXMLMarshaller xmlDriver = new ModelXMLMarshaller();
UnitXCManager unitManager = UnitXCFactory.createManager(); UnitXCManager unitManager = UnitXCFactory.createManager();
assertNotNull(unitManager); assertNotNull(unitManager);
@ -75,10 +77,11 @@ public class UnitXCConverterStepTest {
System.out.println("startValue: "+result.getStartValue()); System.out.println("startValue: "+result.getStartValue());
System.out.println("resultValue: "+result.getResultValue()); System.out.println("resultValue: "+result.getResultValue());
System.out.println("convertTime: "+result.getConvertTime()); System.out.println("convertTime: "+result.getConvertTime());
for (UnitXConverterResultStep step:result.getSteps()) { for (UnitXConverterResultStep step:result.getResultSteps()) {
System.out.println("step: "+step.getName()+" in: "+step.getInputValue()+" out: "+step.getOutputValue()+" time: "+step.getConvertTime()); System.out.println("step: "+step.getName()+" in: "+step.getStartValue()+" out: "+step.getResultValue()+" time: "+step.getConvertTime());
} }
xmlDriver.marshal(result, System.out);
System.out.println(""); System.out.println("");
result = unitManager.getConverter().convertStepped(10.763, "ft²", "in²"); result = unitManager.getConverter().convertStepped(10.763, "ft²", "in²");
@ -86,8 +89,8 @@ public class UnitXCConverterStepTest {
System.out.println("startValue: "+result.getStartValue()); System.out.println("startValue: "+result.getStartValue());
System.out.println("resultValue: "+result.getResultValue()); System.out.println("resultValue: "+result.getResultValue());
System.out.println("convertTime: "+result.getConvertTime()); System.out.println("convertTime: "+result.getConvertTime());
for (UnitXConverterResultStep step:result.getSteps()) { for (UnitXConverterResultStep step:result.getResultSteps()) {
System.out.println("step: "+step.getName()+" in: "+step.getInputValue()+" out: "+step.getOutputValue()+" time: "+step.getConvertTime()); System.out.println("step: "+step.getName()+" in: "+step.getStartValue()+" out: "+step.getResultValue()+" time: "+step.getConvertTime());
} }
System.out.println(""); System.out.println("");
@ -97,9 +100,21 @@ public class UnitXCConverterStepTest {
System.out.println("startValue: "+result.getStartValue()); System.out.println("startValue: "+result.getStartValue());
System.out.println("resultValue: "+result.getResultValue()); System.out.println("resultValue: "+result.getResultValue());
System.out.println("convertTime: "+result.getConvertTime()); System.out.println("convertTime: "+result.getConvertTime());
for (UnitXConverterResultStep step:result.getSteps()) { for (UnitXConverterResultStep step:result.getResultSteps()) {
System.out.println("step: "+step.getName()+" in: "+step.getInputValue()+" out: "+step.getOutputValue()+" time: "+step.getConvertTime()); System.out.println("step: "+step.getName()+" in: "+step.getStartValue()+" out: "+step.getResultValue()+" time: "+step.getConvertTime());
} }
result = unitManager.getConverter().convertStepped(1079000000, "km/h", "m/s");
System.out.println("Convert from: "+result.getStartValueType().getName()+" to: "+result.getResultValueType().getName());
System.out.println("startValue: "+result.getStartValue());
System.out.println("resultValue: "+result.getResultValue());
System.out.println("convertTime: "+result.getConvertTime());
for (UnitXConverterResultStep step:result.getResultSteps()) {
System.out.println("step: "+step.getName()+" in: "+step.getStartValue()+" out: "+step.getResultValue()+" time: "+step.getConvertTime());
}
xmlDriver.marshal(result, System.out);
} }
} }

View file

@ -0,0 +1,91 @@
/*
* Copyright (c) 2013-2015, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.unitxc.module;
import org.junit.Test;
import static net.forwardfire.unitxc.UnitXCAssert.convertEquals;
public class UnitXCModuleAreaTest {
@Test
public void test1ToSquareKilometre() throws Exception {
convertEquals(0.000001,"km²",1.0, "");
}
@Test
public void test1ToSquareCentimetre() throws Exception {
convertEquals(10000,"cm²",1.0, "");
}
@Test
public void test1ToHectares() throws Exception {
convertEquals(0.0001,"ha",1.0, "");
}
@Test
public void test1ToDecares() throws Exception {
convertEquals(0.001,"daa",1.0, "");
}
@Test
public void test1ToAres() throws Exception {
convertEquals(0.01,"a",1.0, "");
}
@Test
public void test1ToDeciares() throws Exception {
convertEquals(0.1,"da",1.0, "");
}
@Test
public void test1ToCentiare() throws Exception {
convertEquals(1.0,"ca",1.0, "");
}
@Test
public void test1ToAcres() throws Exception {
convertEquals(0.000247105381,"acres",1.0, "");
}
@Test
public void test1ToCents() throws Exception {
convertEquals(0.024710538 ,"cents",1.0, "");
}
@Test
public void test1ToSquareYard() throws Exception {
convertEquals(1.195990 ,"yd²",1.0, "");
}
@Test
public void test1ToSquareFeet() throws Exception {
convertEquals(10.763911 ,"ft²",1.0, "");
}
@Test
public void test1ToSquareInch() throws Exception {
convertEquals(1550.0031 ,"in²",1.0, "");
}
}

View file

@ -21,47 +21,26 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package net.forwardfire.unitxc.model; package net.forwardfire.unitxc.module;
import org.apache.commons.lang3.Validate; import org.junit.Test;
/** import static net.forwardfire.unitxc.UnitXCAssert.convertEquals;
*
*
* @author Willem Cazander
* @version 1.0 Oct 22, 2015
*/
public class UnitXConverterResultStepModel implements UnitXConverterResultStep {
private final String name; public class UnitXCModuleVolumeTest {
private final Double inputValue;
private final Double outputValue;
private final Long convertTime;
public UnitXConverterResultStepModel(String name,Double inputValue,Double outputValue,Long convertTime) { @Test
this.name = Validate.notBlank(name); public void test10CubicFootToCubicInch() throws Exception {
this.inputValue = Validate.notNull(inputValue); convertEquals(18597.989,"in³",10.763, "ft³"); // redo after fix
this.outputValue = Validate.notNull(outputValue);
this.convertTime = Validate.notNull(convertTime);
} }
@Override @Test
public String getName() { public void test1ToCubicCentimetre() throws Exception {
return name; convertEquals(1000000,"cm³",1.0, "");
} }
@Override @Test
public Double getInputValue() { public void test1ToCubicDecimetre() throws Exception {
return inputValue; convertEquals(1000,"dm³",1.0, "");
}
@Override
public Double getOutputValue() {
return outputValue;
}
@Override
public Long getConvertTime() {
return convertTime;
} }
} }

View file

@ -2,14 +2,16 @@ FF-UnitXC
========= =========
FF-UnitXC is a java8 library providing a cross unit converter. FF-UnitXC is a java8 library providing a cross unit converter.
NOTE: Status is in progress.
## Features ## Features
* Many units provided. * Many units provided.
* Unit groups. * Unit groups.
* Unit tags. * Unit flags.
* Full metric system. * Full metric system.
* Imperial values. * Imperial values.
* Weird SI units.
## Installation ## Installation