Skip to content

Commit 7504084

Browse files
committed
Move Unit from Energy to Core
1 parent 4872e5f commit 7504084

File tree

8 files changed

+895
-40
lines changed

8 files changed

+895
-40
lines changed

src/main/java/nova/core/util/math/MathUtil.java

+147-4
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,84 @@ public static int max(int... numbers) {
108108
return max;
109109
}
110110

111+
/**
112+
* Returns the smaller number of a and b.
113+
* @param a value.
114+
* @param b value.
115+
* @return min
116+
*/
117+
public static long min(long a, long b) {
118+
return a < b ? a : b;
119+
}
120+
121+
/**
122+
* Returns the smaller number of a, b and c.
123+
* @param a value.
124+
* @param b value.
125+
* @param c value.
126+
* @return min
127+
*/
128+
public static long min(long a, long b, long c) {
129+
return min(min(a, b), c);
130+
}
131+
132+
/**
133+
* Returns the smallest number contained in the provided array.
134+
* @param numbers Array of numbers
135+
* @return min
136+
*/
137+
public static long min(long... numbers) {
138+
if (numbers.length < 1) {
139+
throw new IllegalArgumentException();
140+
}
141+
long min = numbers[0];
142+
for (int i = 1; i < numbers.length; i++) {
143+
if (numbers[i] < min) {
144+
min = numbers[i];
145+
}
146+
}
147+
return min;
148+
}
149+
150+
/**
151+
* Returns the bigger number of a and b.
152+
* @param a value.
153+
* @param b value.
154+
* @return max
155+
*/
156+
public static long max(long a, long b) {
157+
return a > b ? a : b;
158+
}
159+
160+
/**
161+
* Returns the bigger number of a, b and c.
162+
* @param a value.
163+
* @param b value.
164+
* @param c value.
165+
* @return max
166+
*/
167+
public static long max(long a, long b, long c) {
168+
return max(max(a, b), c);
169+
}
170+
171+
/**
172+
* Returns the biggest number contained in the provided array.
173+
* @param numbers Array of numbers
174+
* @return max
175+
*/
176+
public static long max(long... numbers) {
177+
if (numbers.length < 1) {
178+
throw new IllegalArgumentException();
179+
}
180+
long max = numbers[0];
181+
for (int i = 1; i < numbers.length; i++) {
182+
if (numbers[i] > max) {
183+
max = numbers[i];
184+
}
185+
}
186+
return max;
187+
}
188+
111189
/**
112190
* Returns the smaller number of a and b.
113191
* @param a value.
@@ -263,7 +341,7 @@ public static float max(float... numbers) {
263341
}
264342
return max;
265343
}
266-
344+
267345
/**
268346
* Clamps the given number so that {@code min <= a <= max}
269347
* @param a value.
@@ -275,6 +353,17 @@ public static int clamp(int a, int min, int max) {
275353
return min(max(a, min), max);
276354
}
277355

356+
/**
357+
* Clamps the given number so that {@code min <= a <= max}
358+
* @param a value.
359+
* @param min lower limit
360+
* @param max upper limit
361+
* @return {@code min <= a <= max}
362+
*/
363+
public static long clamp(long a, long min, long max) {
364+
return min(max(a, min), max);
365+
}
366+
278367
/**
279368
* Clamps the given number so that {@code min <= a <= max}
280369
* @param a value.
@@ -297,23 +386,57 @@ public static double lerp(double a, double b, double f) {
297386
return a + f * (b - a);
298387
}
299388

389+
/**
390+
* Linear interpolates isBetween point a and point b
391+
* @param a value.
392+
* @param b value.
393+
* @param f A percentage value isBetween 0 to 1
394+
* @return The interpolated value
395+
*/
300396
public static float lerp(float a, float b, float f) {
301397
return a + f * (b - a);
302398
}
303399

400+
/**
401+
* Linear interpolates isBetween point a and point b
402+
* @param a value.
403+
* @param b value.
404+
* @param f A percentage value isBetween 0 to 1
405+
* @return The interpolated value
406+
*/
304407
public static Vector3D lerp(Vector3D a, Vector3D b, float f) {
305408
return a.add((b.subtract(a)).scalarMultiply(f));
306409
}
307410

308411
/**
309-
* Clamps a value isBetween -bounds to +bounds
310-
* @return A value capped isBetween two bounds.
412+
* Clamps a value between -bounds to +bounds
413+
* @return A value capped between two bounds.
414+
*/
415+
public static int absClamp(int value, int bounds) {
416+
return min(max(value, -bounds), bounds);
417+
}
418+
419+
/**
420+
* Clamps a value between -bounds to +bounds
421+
* @return A value capped between two bounds.
422+
*/
423+
public static long absClamp(long value, long bounds) {
424+
return min(max(value, -bounds), bounds);
425+
}
426+
427+
/**
428+
* Clamps a value between -bounds to +bounds
429+
* @return A value capped between two bounds.
311430
*/
312431
public static double absClamp(double value, double bounds) {
313432
return min(max(value, -bounds), bounds);
314433
}
315434

316-
public static float absClamp(int value, int bounds) {
435+
/**
436+
* Clamps a value between -bounds to +bounds
437+
* @return A value capped between two bounds.
438+
*/
439+
public static float absClamp(float value, float bounds) {
317440
return min(max(value, -bounds), bounds);
318441
}
319442

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

451+
public static long log(long x, long base) {
452+
return (long) (Math.log(x) / Math.log(base));
453+
}
454+
328455
public static double log(double x, double base) {
329456
return Math.log(x) / Math.log(base);
330457
}
@@ -339,4 +466,20 @@ public static boolean isBetween(double a, double x, double b) {
339466
return a <= x && x <= b;
340467
}
341468

469+
/**
470+
* Rounds a number to a specific number place places
471+
*
472+
* @param d - the number
473+
* @return The rounded number
474+
*/
475+
public static double roundDecimals(double d, int decimalPlaces) {
476+
long i = Math.round(d * Math.pow(10, decimalPlaces));
477+
return i / Math.pow(10, decimalPlaces);
478+
}
479+
480+
public static String toString(double d, boolean removeTrailingZeroes) {
481+
if (removeTrailingZeroes && d % 1 == 0)
482+
return Long.toString((long) d);
483+
return Double.toString(d);
484+
}
342485
}
+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright (c) 2015 NOVA, All rights reserved.
3+
* This library is free software, licensed under GNU Lesser General Public License version 3
4+
*
5+
* This file is part of NOVA.
6+
*
7+
* NOVA is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* NOVA is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with NOVA. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
package nova.core.util.unit;
22+
23+
import nova.core.util.Identifiable;
24+
import nova.core.util.registry.Registry;
25+
26+
import java.util.Optional;
27+
import java.util.Set;
28+
import java.util.stream.Collectors;
29+
30+
/**
31+
* @author ExE Boss
32+
*/
33+
public final class Unit implements Identifiable {
34+
private static final Registry<Unit> REGISTRY = new Registry<>();
35+
36+
public static final Unit METRE = getOrCreateUnit("nova:metre", "Meter", "m");
37+
public static final Unit GRAM = getOrCreateUnit("nova:gram", "Gram", "g", UnitPrefix.KILO);
38+
// TODO: Define More Units
39+
40+
public static final Unit AMPERE = getOrCreateUnit("nova:ampere", "Amp", "I");
41+
public static final Unit AMP_HOUR = getOrCreateUnit("nova:amp_hour", "Amp Hour", "Ah");
42+
public static final Unit VOLTAGE = getOrCreateUnit("nova:voltage", "Volt", "V");
43+
public static final Unit WATT = getOrCreateUnit("nova:watt", "Watt", "W");
44+
public static final Unit WATT_HOUR = getOrCreateUnit("nova:watt_hour", "Watt Hour", "Wh");
45+
public static final Unit RESISTANCE = getOrCreateUnit("nova:resistance", "Ohm", "R");
46+
public static final Unit CONDUCTANCE = getOrCreateUnit("nova:conductance", "Siemen", "S");
47+
public static final Unit JOULE = getOrCreateUnit("nova:joule", "Joule", "J");
48+
public static final Unit LITER = getOrCreateUnit("nova:liter", "Liter", "L");
49+
public static final Unit NEWTON_METER = getOrCreateUnit("nova:newton_meter", "Newton Meter", "Nm");
50+
51+
private final String id;
52+
public final String name;
53+
public final String symbol;
54+
private String plural = null;
55+
private UnitPrefix basePrefix = null;
56+
57+
private Unit(String id, String name, String symbol) {
58+
this.id = id;
59+
this.name = name;
60+
this.symbol = symbol;
61+
}
62+
63+
private Unit setPlural(String plural) {
64+
if (this.plural == null)
65+
this.plural = plural;
66+
return this;
67+
}
68+
69+
private Unit setBasePrefix(UnitPrefix basePrefix) {
70+
if (this.basePrefix == null)
71+
this.basePrefix = basePrefix;
72+
return this;
73+
}
74+
75+
public String getPlural() {
76+
return this.plural == null ? this.name + "s" : this.plural;
77+
}
78+
79+
@Override
80+
public String getID() {
81+
return id;
82+
}
83+
84+
// TODO: Move to Registry
85+
public Set<Unit> getUnitsFromMod(String modId) {
86+
return REGISTRY.stream().filter((e) -> {
87+
String id = e.getID();
88+
if (id.contains(":")) {
89+
return id.substring(0, id.lastIndexOf(':')).equals(modId);
90+
} else {
91+
return modId == null || modId.isEmpty();
92+
}
93+
}).collect(Collectors.toSet());
94+
}
95+
96+
public Optional<Unit> getUnit(String id) {
97+
return REGISTRY.get(id);
98+
}
99+
100+
public static Unit getOrCreateUnit(String id, String name, String unit) {
101+
if (REGISTRY.contains(id)) return REGISTRY.get(id).get();
102+
return REGISTRY.register(new Unit(id, name, unit));
103+
}
104+
105+
public static Unit getOrCreateUnit(String id, String name, String unit, String plural) {
106+
if (REGISTRY.contains(id)) return REGISTRY.get(id).get();
107+
return REGISTRY.register(new Unit(id, name, unit)).setPlural(plural);
108+
}
109+
110+
public static Unit getOrCreateUnit(String id, String name, String unit, UnitPrefix basePrefix) {
111+
if (REGISTRY.contains(id)) return REGISTRY.get(id).get();
112+
return REGISTRY.register(new Unit(id, name, unit)).setBasePrefix(basePrefix);
113+
}
114+
115+
public static Unit getOrCreateUnit(String id, String name, String unit, String plural, UnitPrefix basePrefix) {
116+
if (REGISTRY.contains(id)) return REGISTRY.get(id).get();
117+
return REGISTRY.register(new Unit(id, name, unit)).setPlural(plural).setBasePrefix(basePrefix);
118+
}
119+
}

0 commit comments

Comments
 (0)