Skip to content

Commit

Permalink
Move Unit from Energy to Core
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Feb 26, 2017
1 parent 4872e5f commit 7504084
Show file tree
Hide file tree
Showing 8 changed files with 895 additions and 40 deletions.
151 changes: 147 additions & 4 deletions src/main/java/nova/core/util/math/MathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,84 @@ public static int max(int... numbers) {
return max;
}

/**
* Returns the smaller number of a and b.
* @param a value.
* @param b value.
* @return min
*/
public static long min(long a, long b) {
return a < b ? a : b;
}

/**
* Returns the smaller number of a, b and c.
* @param a value.
* @param b value.
* @param c value.
* @return min
*/
public static long min(long a, long b, long c) {
return min(min(a, b), c);
}

/**
* Returns the smallest number contained in the provided array.
* @param numbers Array of numbers
* @return min
*/
public static long min(long... numbers) {
if (numbers.length < 1) {
throw new IllegalArgumentException();
}
long min = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < min) {
min = numbers[i];
}
}
return min;
}

/**
* Returns the bigger number of a and b.
* @param a value.
* @param b value.
* @return max
*/
public static long max(long a, long b) {
return a > b ? a : b;
}

/**
* Returns the bigger number of a, b and c.
* @param a value.
* @param b value.
* @param c value.
* @return max
*/
public static long max(long a, long b, long c) {
return max(max(a, b), c);
}

/**
* Returns the biggest number contained in the provided array.
* @param numbers Array of numbers
* @return max
*/
public static long max(long... numbers) {
if (numbers.length < 1) {
throw new IllegalArgumentException();
}
long max = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
return max;
}

/**
* Returns the smaller number of a and b.
* @param a value.
Expand Down Expand Up @@ -263,7 +341,7 @@ public static float max(float... numbers) {
}
return max;
}

/**
* Clamps the given number so that {@code min <= a <= max}
* @param a value.
Expand All @@ -275,6 +353,17 @@ public static int clamp(int a, int min, int max) {
return min(max(a, min), max);
}

/**
* Clamps the given number so that {@code min <= a <= max}
* @param a value.
* @param min lower limit
* @param max upper limit
* @return {@code min <= a <= max}
*/
public static long clamp(long a, long min, long max) {
return min(max(a, min), max);
}

/**
* Clamps the given number so that {@code min <= a <= max}
* @param a value.
Expand All @@ -297,23 +386,57 @@ public static double lerp(double a, double b, double f) {
return a + f * (b - a);
}

/**
* Linear interpolates isBetween point a and point b
* @param a value.
* @param b value.
* @param f A percentage value isBetween 0 to 1
* @return The interpolated value
*/
public static float lerp(float a, float b, float f) {
return a + f * (b - a);
}

/**
* Linear interpolates isBetween point a and point b
* @param a value.
* @param b value.
* @param f A percentage value isBetween 0 to 1
* @return The interpolated value
*/
public static Vector3D lerp(Vector3D a, Vector3D b, float f) {
return a.add((b.subtract(a)).scalarMultiply(f));
}

/**
* Clamps a value isBetween -bounds to +bounds
* @return A value capped isBetween two bounds.
* Clamps a value between -bounds to +bounds
* @return A value capped between two bounds.
*/
public static int absClamp(int value, int bounds) {
return min(max(value, -bounds), bounds);
}

/**
* Clamps a value between -bounds to +bounds
* @return A value capped between two bounds.
*/
public static long absClamp(long value, long bounds) {
return min(max(value, -bounds), bounds);
}

/**
* Clamps a value between -bounds to +bounds
* @return A value capped between two bounds.
*/
public static double absClamp(double value, double bounds) {
return min(max(value, -bounds), bounds);
}

public static float absClamp(int value, int bounds) {
/**
* Clamps a value between -bounds to +bounds
* @return A value capped between two bounds.
*/
public static float absClamp(float value, float bounds) {
return min(max(value, -bounds), bounds);
}

Expand All @@ -325,6 +448,10 @@ public static int log(int x, int base) {
return (int) (Math.log(x) / Math.log(base));
}

public static long log(long x, long base) {
return (long) (Math.log(x) / Math.log(base));
}

public static double log(double x, double base) {
return Math.log(x) / Math.log(base);
}
Expand All @@ -339,4 +466,20 @@ public static boolean isBetween(double a, double x, double b) {
return a <= x && x <= b;
}

/**
* 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) {
long i = Math.round(d * Math.pow(10, decimalPlaces));
return i / Math.pow(10, decimalPlaces);
}

public static String toString(double d, boolean removeTrailingZeroes) {
if (removeTrailingZeroes && d % 1 == 0)
return Long.toString((long) d);
return Double.toString(d);
}
}
119 changes: 119 additions & 0 deletions src/main/java/nova/core/util/unit/Unit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/

package nova.core.util.unit;

import nova.core.util.Identifiable;
import nova.core.util.registry.Registry;

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<Unit> REGISTRY = new Registry<>();

public static final Unit METRE = getOrCreateUnit("nova:metre", "Meter", "m");
public static final Unit GRAM = getOrCreateUnit("nova:gram", "Gram", "g", UnitPrefix.KILO);
// TODO: Define More Units

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");

private final String id;
public final String name;
public final String symbol;
private String plural = null;
private UnitPrefix 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(UnitPrefix 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;
}

// TODO: Move to Registry
public Set<Unit> getUnitsFromMod(String modId) {
return REGISTRY.stream().filter((e) -> {
String id = e.getID();
if (id.contains(":")) {
return id.substring(0, id.lastIndexOf(':')).equals(modId);
} else {
return modId == null || modId.isEmpty();
}
}).collect(Collectors.toSet());
}

public Optional<Unit> 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);
}

public static Unit getOrCreateUnit(String id, String name, String unit, UnitPrefix basePrefix) {
if (REGISTRY.contains(id)) return REGISTRY.get(id).get();
return REGISTRY.register(new Unit(id, name, unit)).setBasePrefix(basePrefix);
}

public static Unit getOrCreateUnit(String id, String name, String unit, String plural, UnitPrefix basePrefix) {
if (REGISTRY.contains(id)) return REGISTRY.get(id).get();
return REGISTRY.register(new Unit(id, name, unit)).setPlural(plural).setBasePrefix(basePrefix);
}
}
Loading

0 comments on commit 7504084

Please sign in to comment.