diff --git a/.gitignore b/.gitignore
index 6bdd20d..ce73626 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,6 +3,7 @@
# Except:
!LICENSE.txt
+!Mod Energy Conversion.md
# Sources
!/src
diff --git a/Mod Energy Conversion.md b/Mod Energy Conversion.md
new file mode 100644
index 0000000..483bb70
--- /dev/null
+++ b/Mod Energy Conversion.md
@@ -0,0 +1,56 @@
+###Author:
+[ExE Boss](https://github.com/ExE-Boss)
+
+### About
+This file contains my docs of mod energy conversion.
+It was created by observing what ratios other mods used when implementing energy conversion.
+
+# Recalculated Energy Conversions to remove infinite power generation loops
+- Defined by Player:
+ - 1 EU = 1 FZ-Charge
+ - 3 EU = 25 RF
+- Defined by Eloraam:
+ - 1 kW (RP2) = 1 McJ
+- Defined by Thermal Expansion:
+ - 1 McJ = 25 RF
+
+## Derived conversions:
+- 3 EU = 1 McJ
+- 3 EU = 1 kW (RP2)
+- 1 EU = 40 J (Defined by AE)
+- 1 McJ = 120 J (Prevents infinite loop)
+
+---
+
+Original file:
+
+# Mod Energy Conversion by Unit
+- 1 McJ = 3 EU (Probably from Forestry)
+- 1 EU = 2 Unit
+- 1 McJ = 5 Unit (<^ These lead to an infinite power generation loop, should redefine McJ:Unit to 1:6)
+- 1 EU = 45 J (Average of AE and MMMPS)
+- 1 McJ = 60 J (Leads to infinite loop)
+- 1 kW (RP2) = 1 McJ (As defined by Eloraam)
+- 1 kW (RP2) = ~3 EU
+- 1 kW (RP2) = 60 J
+- 1 kW (RP2) = 5-ish Units
+- 3 EU = 25 RF
+- 1 EU = 1 FZ-Charge
+- 1 McJ = 25 RF
+
+## Math behind some of the conversions
+- 1 McJ = 3 EU = 1 kW (RP2)
+- ~3 EU = 1 kW (RP2)
+- 1 McJ = 5 Units = 2.5 EU
+- 1 EU = 2 Units
+- 20 J = 1 Unit = 0.5 EU
+- 40 J = 1 EU (AE)
+- 50 J = 1 EU (MMMPS)
+- 60 J = 1 McJ = 1 kW (RP2)
+
+---
+
+# Licence
+This work by [ExE Boss](https://github.com/ExE-Boss) is dual-licensed
+under a [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/)
+and a [GNU Lesser General Public License (LGPL) version 3](https://www.gnu.org/licenses/lgpl-3.0.html).
diff --git a/settings.gradle b/settings.gradle
index 33e60a6..0f85d03 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -1 +1 @@
-rootProject.name = 'NOVA-Energy'
\ No newline at end of file
+rootProject.name = 'NOVA-Energy'
diff --git a/src/main/java/nova/energy/EnergyItem.java b/src/main/java/nova/energy/EnergyItem.java
deleted file mode 100644
index a0c126c..0000000
--- a/src/main/java/nova/energy/EnergyItem.java
+++ /dev/null
@@ -1,40 +0,0 @@
-package nova.energy;
-
-/**
- * An interface for items that store energy in joules.
- */
-public interface EnergyItem {
- /**
- * Adds energy to an item. Returns the quantity of energy that was accepted. This should always
- * return 0 if the item cannot be externally charged.
- * @param energy Maximum amount of energy to be sent into the item.
- * @param doRecharge If false, the charge will only be simulated.
- * @return Amount of energy that was accepted by the item.
- */
- public double recharge(double energy, boolean doRecharge);
-
- /**
- * Removes energy from an item. Returns the quantity of energy that was removed. This should
- * always return 0 if the item cannot be externally discharged.
- * @param energy Maximum amount of energy to be removed from the item.
- * @param doDischarge If false, the discharge will only be simulated.
- * @return Amount of energy that was removed from the item.
- */
- public double discharge(double energy, boolean doDischarge);
-
- /**
- * Get the amount of energy currently stored in the item.
- */
- public double getEnergy();
-
- /**
- * Sets the amount of energy in the ItemStack.
- * @param energy - Amount of electrical energy.
- */
- public void setEnergy(double energy);
-
- /**
- * Get the max amount of energy that can be stored in the item.
- */
- public double getEnergyCapacity();
-}
diff --git a/src/main/java/nova/energy/EnergyStorage.java b/src/main/java/nova/energy/EnergyStorage.java
new file mode 100644
index 0000000..8e45a14
--- /dev/null
+++ b/src/main/java/nova/energy/EnergyStorage.java
@@ -0,0 +1,132 @@
+/*
+ * Copyright (c) 2015 NOVA, All rights reserved.
+ * This library is free software, licensed under GNU Lesser General Public License version 3
+ *
+ * This file is part of NOVA.
+ *
+ * NOVA is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * NOVA is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with NOVA. If not, see .
+ */
+package nova.energy;
+
+import nova.core.component.Component;
+import nova.core.retention.Storable;
+import nova.core.retention.Store;
+
+/**
+ * A component for items and blocks that store energy in joules.
+ *
+ * @author ExE Boss
+ */
+public class EnergyStorage extends Component implements Storable {
+
+ @Store
+ protected double energy;
+ protected double maxEnergy;
+ protected double maxRecharge;
+ protected double maxDischarge;
+
+ protected EnergyStorage() {
+ }
+
+ public EnergyStorage(double maxEnergy) {
+ this(maxEnergy, maxEnergy, maxEnergy);
+ }
+
+ public EnergyStorage(double maxEnergy, double maxTransfer) {
+ this(maxEnergy, maxTransfer, maxTransfer);
+ }
+
+ public EnergyStorage(double maxEnergy, double maxRecharge, double maxDischarge) {
+ this.maxEnergy = maxEnergy;
+ this.maxRecharge = Math.min(0, Math.max(maxRecharge, maxEnergy));
+ this.maxDischarge = Math.min(0, Math.max(maxDischarge, maxEnergy));
+ }
+
+ public EnergyStorage(double energy, double maxEnergy, double maxRecharge, double maxDischarge) {
+ this.maxEnergy = maxEnergy;
+ this.setEnergy(energy);
+ this.maxRecharge = maxRecharge;
+ this.maxDischarge = maxDischarge;
+ }
+
+ /**
+ * Adds energy to an item. Returns the quantity of energy that was accepted. This should always
+ * return 0 if the item cannot be externally charged.
+ *
+ * @param energy Maximum amount of energy to be sent into the item (in Joules).
+ * @param doRecharge If false, the charge will only be simulated.
+ * @return Amount of energy that was accepted by the item (in Joules).
+ */
+ public double recharge(double energy, boolean doRecharge) {
+ if (!canRecharge()) return 0;
+
+ energy = Math.min(this.maxEnergy - this.energy, Math.min(this.maxRecharge, energy));
+ if (doRecharge) this.energy += energy;
+
+ return energy;
+ }
+
+ /**
+ * Removes energy from an item. Returns the quantity of energy that was removed. This should
+ * always return 0 if the item cannot be externally discharged.
+ *
+ * @param energy Maximum amount of energy to be removed from the item (in Joules).
+ * @param doDischarge If false, the discharge will only be simulated.
+ * @return Amount of energy that was removed from the item (in Joules).
+ */
+ public double discharge(double energy, boolean doDischarge) {
+ if (!canRecharge()) return 0;
+
+ energy = Math.min(this.energy, Math.min(this.maxDischarge, energy));
+ if (doDischarge) this.energy -= energy;
+
+ return energy;
+ }
+
+ /**
+ * Get the amount of energy currently stored in the item.
+ */
+ public double getEnergy() {
+ return this.energy;
+ }
+
+ /**
+ * Sets the amount of energy in the ItemStack.
+ * @param energy - Amount of electrical energy (in Joules).
+ */
+ public void setEnergy(double energy) {
+ this.energy = Math.min(0, Math.max(energy, maxEnergy));
+ }
+
+ /**
+ * Get the max amount of energy that can be stored in the item.
+ */
+ public double getEnergyCapacity() {
+ return maxEnergy;
+ }
+
+ /**
+ * @return Whether or not this item can be externally recharged.
+ */
+ public boolean canRecharge() {
+ return maxRecharge > 0;
+ }
+
+ /**
+ * @return Whether or not this item can be externally discharged.
+ */
+ public boolean canDischarge() {
+ return maxDischarge > 0;
+ }
+}
diff --git a/src/main/java/nova/energy/Unit.java b/src/main/java/nova/energy/Unit.java
new file mode 100644
index 0000000..3183bd6
--- /dev/null
+++ b/src/main/java/nova/energy/Unit.java
@@ -0,0 +1,179 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package nova.energy;
+
+import nova.core.util.Identifiable;
+import nova.core.util.registry.Registry;
+
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * @author ExE Boss
+ */
+public final class Unit implements Identifiable {
+ private static final Registry REGISTRY = new Registry<>();
+
+ public static final Unit METRE = getOrCreateUnit("nova:metre", "Metre", "m");
+ public static final Unit GRAM = getOrCreateUnit("nova:gram", "Gram", "g").setBasePrefix(Prefix.KILO);
+
+ public static final Unit AMPERE = getOrCreateUnit("nova:ampere", "Amp", "I");
+ public static final Unit AMP_HOUR = getOrCreateUnit("nova:amp_hour", "Amp Hour", "Ah");
+ public static final Unit VOLTAGE = getOrCreateUnit("nova:voltage", "Volt", "V");
+ public static final Unit WATT = getOrCreateUnit("nova:watt", "Watt", "W");
+ public static final Unit WATT_HOUR = getOrCreateUnit("nova:watt_hour", "Watt Hour", "Wh");
+ public static final Unit RESISTANCE = getOrCreateUnit("nova:resistance", "Ohm", "R");
+ public static final Unit CONDUCTANCE = getOrCreateUnit("nova:conductance", "Siemen", "S");
+ public static final Unit JOULE = getOrCreateUnit("nova:joule", "Joule", "J");
+ public static final Unit LITER = getOrCreateUnit("nova:liter", "Liter", "L");
+ public static final Unit NEWTON_METER = getOrCreateUnit("nova:newton_meter", "Newton Meter", "Nm");
+
+ /**
+ * Redstone Flux, the default energy unit in Minecraft Forge since 1.10-ish.
+ */
+ public static final Unit REDFLUX = getOrCreateUnit("forge:redstone_flux", "Redstone-Flux", "RF").setPlural("Redstone-Flux");
+ public static final Unit MINECRAFT_JOULES = getOrCreateUnit("buildcraft:minecraft_joule", "Minecraft-Joule", "McJ"); // MJ is confusing with Megajoules
+ public static final Unit ELECTRICAL_UNITS = getOrCreateUnit("ic2:electrical_unit", "Electrical-Unit", "EU");
+
+ private final String id;
+ public final String name;
+ public final String symbol;
+ private String plural = null;
+ private Prefix basePrefix = null;
+
+ private Unit(String id, String name, String symbol) {
+ this.id = id;
+ this.name = name;
+ this.symbol = symbol;
+ }
+
+ private Unit setPlural(String plural) {
+ if (this.plural == null)
+ this.plural = plural;
+ return this;
+ }
+
+ private Unit setBasePrefix(Prefix basePrefix) {
+ if (this.basePrefix == null)
+ this.basePrefix = basePrefix;
+ return this;
+ }
+
+ public String getPlural() {
+ return this.plural == null ? this.name + "s" : this.plural;
+ }
+
+ @Override
+ public String getID() {
+ return id;
+ }
+
+ public Set getUnitsFromMod(String modId) {
+ return REGISTRY.stream().filter((e) -> {
+ String id = e.getID();
+ if (id.contains(":")) {
+ return id.substring(0, id.lastIndexOf(':')).startsWith(modId);
+ } else {
+ return modId == null || modId.isEmpty();
+ }
+ }).collect(Collectors.toSet());
+ }
+
+ public Optional getUnit(String id) {
+ return REGISTRY.get(id);
+ }
+
+ public static Unit getOrCreateUnit(String id, String name, String unit) {
+ if (REGISTRY.contains(id)) return REGISTRY.get(id).get();
+ return REGISTRY.register(new Unit(id, name, unit));
+ }
+
+ public static Unit getOrCreateUnit(String id, String name, String unit, String plural) {
+ if (REGISTRY.contains(id)) return REGISTRY.get(id).get();
+ return REGISTRY.register(new Unit(id, name, unit)).setPlural(plural);
+ }
+
+ /**
+ * Metric system of measurement.
+ */
+ public static class Prefix {
+ private static final List UNIT_PREFIXES = new LinkedList<>();
+
+// public static final Prefix YOCTO = new Prefix("Yocto", "y", 0.000000000000000000000001);
+// public static final Prefix ZEPTO = new Prefix("Zepto", "z", 0.000000000000000000001);
+// public static final Prefix ATTO = new Prefix("Atto", "a", 0.000000000000000001);
+// public static final Prefix FEMTO = new Prefix("Femto", "p", 0.000000000000001);
+// public static final Prefix PICO = new Prefix("Pico", "p", 0.000000000001);
+// public static final Prefix NANO = new Prefix("Nano", "n", 0.000000001);
+ public static final Prefix MICRO = new Prefix("Micro", "μ", 0.000001);
+ public static final Prefix MILLI = new Prefix("Milli", "m", 0.001);
+ public static final Prefix BASE = new Prefix("", "", 1);
+ public static final Prefix KILO = new Prefix("Kilo", "k", 1000);
+ public static final Prefix MEGA = new Prefix("Mega", "M", 1000000);
+ public static final Prefix GIGA = new Prefix("Giga", "G", 1000000000);
+ public static final Prefix TERA = new Prefix("Tera", "T", 1000000000000d);
+ public static final Prefix PETA = new Prefix("Peta", "P", 1000000000000000d);
+ public static final Prefix EXA = new Prefix("Exa", "E", 1000000000000000000d);
+ public static final Prefix ZETTA = new Prefix("Zetta", "Z", 1000000000000000000000d);
+ public static final Prefix YOTTA = new Prefix("Yotta", "Y", 1000000000000000000000000d);
+ /**
+ * long name for the unit
+ */
+ public final String name;
+ /**
+ * short unit version of the unit
+ */
+ public final String symbol;
+ /**
+ * Point by which a number is consider to be of this unit
+ */
+ public final double value;
+
+ private Prefix(String name, String symbol, double value) {
+ this.name = name;
+ this.symbol = symbol;
+ this.value = value;
+ UNIT_PREFIXES.add(this);
+ }
+
+ public String getName(boolean getShort) {
+ if (getShort) {
+ return symbol;
+ } else {
+ return name;
+ }
+ }
+
+ /**
+ * Divides the value by the unit value start
+ */
+ public double process(double value) {
+ return value / this.value;
+ }
+
+ /**
+ * Checks if a value is above the unit value start
+ */
+ public boolean isAbove(double value) {
+ return value > this.value;
+ }
+
+ /**
+ * Checks if a value is lower than the unit value start
+ */
+ public boolean isBellow(double value) {
+ return value < this.value;
+ }
+
+ public static List getPrefixes() {
+ return Collections.unmodifiableList(UNIT_PREFIXES);
+ }
+ }
+}
diff --git a/src/main/java/nova/energy/UnitConversion.java b/src/main/java/nova/energy/UnitConversion.java
new file mode 100644
index 0000000..a755396
--- /dev/null
+++ b/src/main/java/nova/energy/UnitConversion.java
@@ -0,0 +1,130 @@
+/*
+ * Copyright (c) 2015 NOVA, All rights reserved.
+ * This library is free software, licensed under GNU Lesser General Public License version 3
+ *
+ * This file is part of NOVA.
+ *
+ * NOVA is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * NOVA is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with NOVA. If not, see .
+ */
+
+package nova.energy;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * Convert between different units. (Where supported)
+ *
+ * @author ExE Boss
+ */
+public class UnitConversion {
+
+ private static final Map> CONVERSION = new HashMap<>();
+
+ private final Unit unit1, unit2;
+ private final double ratio, reverseRatio;
+ private final UnitConversion reverse;
+
+ private UnitConversion(Unit unit1, Unit unit2, double ratio) {
+ this.unit1 = unit1;
+ this.unit2 = unit2;
+ this.ratio = ratio;
+ this.reverseRatio = 1/ratio;
+ this.reverse = new UnitConversion(unit1, unit2, this.reverseRatio, this);
+ }
+ private UnitConversion(Unit unit1, Unit unit2, double ratio, UnitConversion reverse) {
+ this.unit1 = unit1;
+ this.unit2 = unit2;
+ this.ratio = ratio;
+ this.reverse = reverse;
+ this.reverseRatio = this.reverse.ratio;
+ }
+ public double convert(double value) {
+ return (this.reverseRatio == 0 && this.ratio != 0 ? value / ratio : value * this.reverseRatio);
+ }
+
+ public Unit unit1() {
+ return unit1;
+ }
+ public Unit unit2() {
+ return unit2;
+ }
+ public double ratio() {
+ return ratio;
+ }
+ public UnitConversion reverse() {
+ return reverse;
+ }
+
+ public static Optional getConvertion(Unit unit1, Unit unit2) {
+ Map conv = CONVERSION.get(unit1);
+ if (conv == null) return Optional.empty();
+ return Optional.ofNullable(conv.get(unit2));
+ }
+
+ /**
+ *
+ * @param unit1 The unit to convert from
+ * @param unit2 The unit to convert to
+ * @param ratio unit1/unit2
+ * @return The UnitConversion instance.
+ * @throws IllegalArgumentException If ratio is 0
+ */
+ public static UnitConversion registerConversion(Unit unit1, Unit unit2, double ratio) throws IllegalArgumentException {
+ if (ratio == 0)
+ throw new IllegalArgumentException("Ratio cannot be 0");
+
+ Map conv1 = CONVERSION.get(unit1);
+ if (conv1 == null) {
+ conv1 = new HashMap<>();
+ CONVERSION.put(unit1, conv1);
+ }
+ if (conv1.containsKey(unit2)) return conv1.get(unit2);
+
+ Map conv2 = CONVERSION.get(unit2);
+ if (conv2 == null) {
+ conv2 = new HashMap<>();
+ CONVERSION.put(unit2, conv2);
+ }
+
+ UnitConversion uc = new UnitConversion(unit1, unit2, ratio);
+
+ conv1.put(unit2, uc);
+ conv2.put(unit1, uc.reverse());
+
+ calculateConversions();
+ return uc;
+ }
+
+ /**
+ * If UnitA can be converted to UnitB
+ * and UnitB can be converted to UnitC,
+ * then UnitA must be convertible to UnitC.
+ */
+ public static void calculateConversions() {
+ // TODO
+ }
+
+ static {
+ registerConversion(Unit.ELECTRICAL_UNITS, Unit.JOULE, 40d/1d);
+ registerConversion(Unit.REDFLUX, Unit.JOULE, 24/5d);
+ registerConversion(Unit.MINECRAFT_JOULES, Unit.JOULE, 40d/3d/1d);
+
+ // This should also happen automatically
+ registerConversion(Unit.ELECTRICAL_UNITS, Unit.REDFLUX, 3d/25d);
+ registerConversion(Unit.ELECTRICAL_UNITS, Unit.MINECRAFT_JOULES, 3d/1d);
+ registerConversion(Unit.REDFLUX, Unit.MINECRAFT_JOULES, 25d/1d);
+ }
+}
diff --git a/src/main/java/nova/energy/UnitDisplay.java b/src/main/java/nova/energy/UnitDisplay.java
index 143d9c6..f5e87b2 100644
--- a/src/main/java/nova/energy/UnitDisplay.java
+++ b/src/main/java/nova/energy/UnitDisplay.java
@@ -1,40 +1,77 @@
-package nova.energy;
+/*
+ * Copyright (c) 2015 NOVA, All rights reserved.
+ * This library is free software, licensed under GNU Lesser General Public License version 3
+ *
+ * This file is part of NOVA.
+ *
+ * NOVA is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * NOVA is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with NOVA. If not, see .
+ */
-import java.util.LinkedList;
-import java.util.List;
+package nova.energy;
/**
* An easy way to display information on electricity for the client.
+ *
* @author Calclavia
*/
public class UnitDisplay {
- public Unit unit;
- public double value;
- public boolean useSymbol = false;
- public int decimalPlaces = 2;
- public boolean isSimple = false;
+ public final Unit unit;
+ public final double value;
+ public final boolean useSymbol;
+ public final int decimalPlaces;
+ public final boolean isSimple;
- public UnitDisplay(Unit unit, double value, boolean simple) {
+ public UnitDisplay(Unit unit, double value, boolean useSymbol, int decimalPlaces, boolean simple) {
this.unit = unit;
this.value = value;
+ this.useSymbol = useSymbol;
+ this.decimalPlaces = decimalPlaces;
+ this.isSimple = simple;
}
- public UnitDisplay(Unit unit, double value) {
- this(unit, value, false);
+ public UnitDisplay(Unit unit, double value, boolean useSymbol, boolean simple) {
+ this(unit, value, useSymbol, 2, simple);
+ }
+
+ public UnitDisplay(Unit unit, double value, int decimalPlaces, boolean simple) {
+ this(unit, value, false, decimalPlaces, simple);
+ }
+
+ public UnitDisplay(Unit unit, double value, boolean useSymbol, int decimalPlaces) {
+ this(unit, value, useSymbol, decimalPlaces, false);
}
- @Deprecated
- public UnitDisplay(double value, Unit unit) {
- this(unit, value);
+ public UnitDisplay(Unit unit, double value, int decimalPlaces) {
+ this(unit, value, false, decimalPlaces, false);
+ }
+
+ public UnitDisplay(Unit unit, double value, boolean simple) {
+ this(unit, value, false, 2, simple);
+ }
+
+ public UnitDisplay(Unit unit, double value) {
+ this(unit, value, false, 2, false);
}
/**
* Rounds a number to a specific number place places
+ *
* @param d - the number
* @return The rounded number
*/
public static double roundDecimals(double d, int decimalPlaces) {
- int j = (int) (d * Math.pow(10, decimalPlaces));
+ long j = Math.round(d * Math.pow(10, decimalPlaces));
return j / Math.pow(10, decimalPlaces);
}
@@ -43,13 +80,15 @@ public static double roundDecimals(double d) {
}
public UnitDisplay multiply(double value) {
- this.value *= value;
- return this;
+ return new UnitDisplay(unit, value * this.value);
}
public UnitDisplay simple() {
- isSimple = true;
- return this;
+ return (isSimple ? this : new UnitDisplay(unit, value, true));
+ }
+
+ public UnitDisplay notSimple() {
+ return (!isSimple ? this : new UnitDisplay(unit, value, false));
}
public UnitDisplay symbol() {
@@ -57,19 +96,18 @@ public UnitDisplay symbol() {
}
public UnitDisplay symbol(boolean useSymbol) {
- this.useSymbol = useSymbol;
- return this;
+ return (this.useSymbol ^ useSymbol ? new UnitDisplay(unit, value, isSimple, decimalPlaces, useSymbol) : this);
}
public UnitDisplay decimal(int decimalPlaces) {
- this.decimalPlaces = decimalPlaces;
- return this;
+ return (this.decimalPlaces == decimalPlaces ? this : new UnitDisplay(unit, value, isSimple, decimalPlaces, useSymbol));
}
@Override
public String toString() {
String unitName = unit.name;
String prefix = "";
+ double value = this.value;
if (isSimple) {
if (value > 1) {
@@ -101,17 +139,17 @@ public String toString() {
if (value == 0) {
return value + " " + unitName;
} else {
- for (int i = 0; i < UnitPrefix.unitPrefixes.size(); i++) {
- UnitPrefix lowerMeasure = UnitPrefix.unitPrefixes.get(i);
+ for (int i = 0; i < Unit.Prefix.getPrefixes().size(); i++) {
+ Unit.Prefix lowerMeasure = Unit.Prefix.getPrefixes().get(i);
if (lowerMeasure.isBellow(value) && i == 0) {
return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(useSymbol) + unitName;
}
- if (i + 1 >= UnitPrefix.unitPrefixes.size()) {
+ if (i + 1 >= Unit.Prefix.getPrefixes().size()) {
return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(useSymbol) + unitName;
}
- UnitPrefix upperMeasure = UnitPrefix.unitPrefixes.get(i + 1);
+ Unit.Prefix upperMeasure = Unit.Prefix.getPrefixes().get(i + 1);
if ((lowerMeasure.isAbove(value) && upperMeasure.isBellow(value)) || lowerMeasure.value == value) {
return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(useSymbol) + unitName;
@@ -121,110 +159,4 @@ public String toString() {
return prefix + roundDecimals(value, decimalPlaces) + " " + unitName;
}
-
- /**
- * Universal Electricity's units are in KILOJOULES, KILOWATTS and KILOVOLTS. Try to make your
- * energy ratio as close to real life as possible.
- */
- public static class Unit {
- public static final Unit AMPERE = new Unit("Amp", "I");
- public static final Unit AMP_HOUR = new Unit("Amp Hour", "Ah");
- public static final Unit VOLTAGE = new Unit("Volt", "V");
- public static final Unit WATT = new Unit("Watt", "W");
- public static final Unit WATT_HOUR = new Unit("Watt Hour", "Wh");
- public static final Unit RESISTANCE = new Unit("Ohm", "R");
- public static final Unit CONDUCTANCE = new Unit("Siemen", "S");
- public static final Unit JOULES = new Unit("Joule", "J");
- public static final Unit LITER = new Unit("Liter", "L");
- public static final Unit NEWTON_METER = new Unit("Newton Meter", "Nm");
-
- public static final Unit REDFLUX = new Unit("Redstone-Flux", "Rf").setPlural("Redstone-Flux");
- public static final Unit MINECRAFT_JOULES = new Unit("Minecraft-Joule", "Mj");
- public static final Unit ELECTRICAL_UNITS = new Unit("Electrical-Unit", "Eu");
-
- public final String name;
- public final String symbol;
- private String plural = null;
-
- private Unit(String name, String symbol) {
- this.name = name;
- this.symbol = symbol;
- }
-
- private Unit setPlural(String plural) {
- this.plural = plural;
- return this;
- }
-
- public String getPlural() {
- return this.plural == null ? this.name + "s" : this.plural;
- }
- }
-
- /**
- * Metric system of measurement.
- */
- public static class UnitPrefix {
- public static final List unitPrefixes = new LinkedList();
-
- public static final UnitPrefix MICRO = new UnitPrefix("Micro", "u", 0.000001);
- public static final UnitPrefix MILLI = new UnitPrefix("Milli", "m", 0.001);
- public static final UnitPrefix BASE = new UnitPrefix("", "", 1);
- public static final UnitPrefix KILO = new UnitPrefix("Kilo", "k", 1000);
- public static final UnitPrefix MEGA = new UnitPrefix("Mega", "M", 1000000);
- public static final UnitPrefix GIGA = new UnitPrefix("Giga", "G", 1000000000);
- public static final UnitPrefix TERA = new UnitPrefix("Tera", "T", 1000000000000d);
- public static final UnitPrefix PETA = new UnitPrefix("Peta", "P", 1000000000000000d);
- public static final UnitPrefix EXA = new UnitPrefix("Exa", "E", 1000000000000000000d);
- public static final UnitPrefix ZETTA = new UnitPrefix("Zetta", "Z", 1000000000000000000000d);
- public static final UnitPrefix YOTTA = new UnitPrefix("Yotta", "Y", 1000000000000000000000000d);
- /**
- * long name for the unit
- */
- public final String name;
- /**
- * short unit version of the unit
- */
- public final String symbol;
- /**
- * Point by which a number is consider to be of this unit
- */
- public final double value;
-
- private UnitPrefix(String name, String symbol, double value) {
- this.name = name;
- this.symbol = symbol;
- this.value = value;
- unitPrefixes.add(this);
- }
-
- public String getName(boolean getShort) {
- if (getShort) {
- return symbol;
- } else {
- return name;
- }
- }
-
- /**
- * Divides the value by the unit value start
- */
- public double process(double value) {
- return value / this.value;
- }
-
- /**
- * Checks if a value is above the unit value start
- */
- public boolean isAbove(double value) {
- return value > this.value;
- }
-
- /**
- * Checks if a value is lower than the unit value start
- */
- public boolean isBellow(double value) {
- return value < this.value;
- }
- }
}
diff --git a/src/test/java/nova/energy/UnitConversionTest.java b/src/test/java/nova/energy/UnitConversionTest.java
new file mode 100644
index 0000000..bad163c
--- /dev/null
+++ b/src/test/java/nova/energy/UnitConversionTest.java
@@ -0,0 +1,30 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package nova.energy;
+
+import org.assertj.core.data.Offset;
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ *
+ * @author ExE Boss
+ */
+public class UnitConversionTest {
+
+ public UnitConversionTest() {
+ }
+
+ @Test
+ public void testConvert() {
+ UnitConversion RF_JOULE = UnitConversion.getConvertion(Unit.REDFLUX, Unit.JOULE).get();
+ UnitConversion JOULE_RF = UnitConversion.getConvertion(Unit.JOULE, Unit.REDFLUX).get();
+
+ assertThat(RF_JOULE.convert(24)).isEqualTo(5, Offset.offset(1 / 10_000_000D));
+ assertThat(JOULE_RF.convert(5)).isEqualTo(24, Offset.offset(1 / 10_000_000D));
+ }
+}