WarpPI/core/src/main/java/it/cavallium/warppi/util/Utils.java

599 lines
19 KiB
Java
Raw Normal View History

package it.cavallium.warppi.util;
2016-09-02 20:32:37 +02:00
import java.io.ByteArrayOutputStream;
2018-09-04 12:12:41 +02:00
import java.io.File;
2016-09-02 20:32:37 +02:00
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
2017-12-23 01:14:38 +01:00
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;
2018-09-04 12:12:41 +02:00
import java.util.Map;
2016-09-02 20:32:37 +02:00
import org.nevec.rjm.BigDecimalMath;
import org.nevec.rjm.Rational;
2019-02-27 23:29:03 +01:00
import it.cavallium.warppi.WarpPI;
import it.cavallium.warppi.StaticVars;
import it.cavallium.warppi.gui.graphicengine.BinaryFont;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionOperator;
import it.cavallium.warppi.math.FunctionSingle;
import it.cavallium.warppi.math.functions.Division;
import it.cavallium.warppi.math.functions.Expression;
import it.cavallium.warppi.math.functions.Multiplication;
import it.cavallium.warppi.math.functions.Negative;
import it.cavallium.warppi.math.functions.Number;
import it.cavallium.warppi.math.functions.Subtraction;
import it.cavallium.warppi.math.functions.Sum;
import it.cavallium.warppi.math.functions.SumSubtraction;
import it.cavallium.warppi.math.functions.Variable;
import it.cavallium.warppi.math.functions.equations.Equation;
import it.cavallium.warppi.math.functions.equations.EquationsSystemPart;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
2016-09-02 20:32:37 +02:00
public class Utils {
public static final int scale = 24;
public static final int displayScale = 8;
public static final BigInteger maxFactor = BigInteger.valueOf(1000000L);
2016-09-02 20:32:37 +02:00
public static final int scaleMode = BigDecimal.ROUND_HALF_UP;
public static final RoundingMode scaleMode2 = RoundingMode.HALF_UP;
2018-09-22 14:10:36 +02:00
public static final int maxAutoFractionDigits = 5;
2016-09-02 20:32:37 +02:00
2018-03-29 00:30:47 +02:00
public static boolean newtMode = true;
2018-05-12 21:18:29 +02:00
2018-09-22 11:17:30 +02:00
public static <T> boolean isInArray(final T ch, final T[] a) {
return Arrays.stream(a).anyMatch(item -> ch.equals(item));
2016-09-02 20:32:37 +02:00
}
2018-09-22 11:17:30 +02:00
public static boolean isInArray(final char ch, final char[] a) {
2018-09-28 11:39:28 +02:00
for (final char c : a) {
if (c == ch) {
2018-08-28 02:39:41 +02:00
return true;
2018-09-28 11:39:28 +02:00
}
}
return false;
}
private static final String[] regexNormalSymbols = new String[] { "\\", ".", "[", "]", "{", "}", "(", ")", "*", "+", "-", "?", "^", "$", "|" };
2018-09-22 11:17:30 +02:00
public static String ArrayToRegex(final String[] array) {
2016-09-02 20:32:37 +02:00
String regex = null;
for (final String symbol : array) {
2016-11-02 21:56:40 +01:00
boolean contained = false;
2018-09-28 11:39:28 +02:00
for (final String smb : Utils.regexNormalSymbols) {
2016-11-02 21:56:40 +01:00
if (smb.equals(symbol)) {
contained = true;
break;
}
2018-09-28 11:39:28 +02:00
}
2016-11-02 21:56:40 +01:00
if (contained) {
2018-09-28 11:39:28 +02:00
if (regex != null) {
2016-11-02 21:56:40 +01:00
regex += "|\\" + symbol;
2018-09-28 11:39:28 +02:00
} else {
2016-11-02 21:56:40 +01:00
regex = "\\" + symbol;
2018-09-28 11:39:28 +02:00
}
} else if (regex != null) {
2018-09-22 11:17:30 +02:00
regex += "|" + symbol;
2018-09-28 11:39:28 +02:00
} else {
2018-09-22 11:17:30 +02:00
regex = symbol;
2018-09-28 11:39:28 +02:00
}
2016-09-02 20:32:37 +02:00
}
return regex;
}
2018-09-22 11:17:30 +02:00
public static String ArrayToRegex(final char[] array) {
String regex = null;
for (final char symbol : array) {
boolean contained = false;
2018-09-28 11:39:28 +02:00
for (final String smb : Utils.regexNormalSymbols) {
2018-09-22 11:17:30 +02:00
if (smb.equals(symbol + "")) {
contained = true;
break;
}
2018-09-28 11:39:28 +02:00
}
if (contained) {
2018-09-28 11:39:28 +02:00
if (regex != null) {
regex += "|\\" + symbol;
2018-09-28 11:39:28 +02:00
} else {
regex = "\\" + symbol;
2018-09-28 11:39:28 +02:00
}
} else if (regex != null) {
2018-09-22 11:17:30 +02:00
regex += "|" + symbol;
2018-09-28 11:39:28 +02:00
} else {
2018-09-22 11:17:30 +02:00
regex = symbol + "";
2018-09-28 11:39:28 +02:00
}
}
return regex;
}
2018-09-22 11:17:30 +02:00
public static String[] concat(final String[] a, final String[] b) {
final int aLen = a.length;
final int bLen = b.length;
final String[] c = new String[aLen + bLen];
2016-09-02 20:32:37 +02:00
System.arraycopy(a, 0, c, 0, aLen);
System.arraycopy(b, 0, c, aLen, bLen);
return c;
}
2018-09-22 11:17:30 +02:00
public static char[] concat(final char[] a, final char[] b) {
final int aLen = a.length;
final int bLen = b.length;
final char[] c = new char[aLen + bLen];
System.arraycopy(a, 0, c, 0, aLen);
System.arraycopy(b, 0, c, aLen, bLen);
return c;
}
2018-09-22 11:17:30 +02:00
public static String[] add(final String[] a, final String b) {
final int aLen = a.length;
final String[] c = new String[aLen + 1];
2016-09-02 20:32:37 +02:00
System.arraycopy(a, 0, c, 0, aLen);
c[aLen] = b;
return c;
}
2018-09-22 11:17:30 +02:00
public static char[] add(final char[] a, final char b) {
final int aLen = a.length;
final char[] c = new char[aLen + 1];
System.arraycopy(a, 0, c, 0, aLen);
c[aLen] = b;
return c;
}
2018-09-22 11:17:30 +02:00
public static boolean areThereOnlySettedUpFunctionsSumsEquationsAndSystems(final List<Function> fl) {
2018-09-28 11:39:28 +02:00
for (int i = 0; i < fl.size(); i++) {
if (!(fl.get(i) instanceof Number || fl.get(i) instanceof Variable || fl.get(i) instanceof Sum || fl.get(i) instanceof SumSubtraction || fl.get(i) instanceof Subtraction || fl.get(i) instanceof Equation || fl.get(i) instanceof EquationsSystemPart || fl.get(i) instanceof Expression)) {
if (fl.get(i) instanceof FunctionSingle) {
2018-09-28 11:39:28 +02:00
if (((FunctionSingle) fl.get(i)).getParameter() == null) {
2016-09-02 20:32:37 +02:00
return false;
2018-09-28 11:39:28 +02:00
}
} else if (fl.get(i) instanceof FunctionOperator) {
2018-09-28 11:39:28 +02:00
if (((FunctionOperator) fl.get(i)).getParameter1() == null || ((FunctionOperator) fl.get(i)).getParameter2() == null) {
2016-09-02 20:32:37 +02:00
return false;
2018-09-28 11:39:28 +02:00
}
} else {
2016-09-02 20:32:37 +02:00
return false;
2018-09-28 11:39:28 +02:00
}
}
}
2016-09-02 20:32:37 +02:00
return true;
}
2018-09-22 11:17:30 +02:00
public static boolean areThereOnlySettedUpFunctionsSumsMultiplicationsEquationsAndSystems(final List<Function> fl) {
2018-09-28 11:39:28 +02:00
for (int i = 0; i < fl.size(); i++) {
if (!(fl.get(i) instanceof Number || fl.get(i) instanceof Variable || fl.get(i) instanceof Multiplication || fl.get(i) instanceof Sum || fl.get(i) instanceof SumSubtraction || fl.get(i) instanceof Subtraction || fl.get(i) instanceof Equation || fl.get(i) instanceof EquationsSystemPart || fl.get(i) instanceof Expression)) {
if (fl.get(i) instanceof FunctionSingle) {
2018-09-28 11:39:28 +02:00
if (((FunctionSingle) fl.get(i)).getParameter() == null) {
2016-09-02 20:32:37 +02:00
return false;
2018-09-28 11:39:28 +02:00
}
} else if (fl.get(i) instanceof FunctionOperator) {
2018-09-28 11:39:28 +02:00
if (((FunctionOperator) fl.get(i)).getParameter1() == null || ((FunctionOperator) fl.get(i)).getParameter2() == null) {
2016-09-02 20:32:37 +02:00
return false;
2018-09-28 11:39:28 +02:00
}
} else {
2016-09-02 20:32:37 +02:00
return false;
2018-09-28 11:39:28 +02:00
}
}
}
2016-09-02 20:32:37 +02:00
return true;
}
2018-09-22 11:17:30 +02:00
public static boolean areThereOnlySettedUpFunctionsEquationsAndSystems(final List<Function> fl) {
2018-09-28 11:39:28 +02:00
for (int i = 0; i < fl.size(); i++) {
if (!(fl.get(i) instanceof Number || fl.get(i) instanceof Variable || fl.get(i) instanceof Equation || fl.get(i) instanceof EquationsSystemPart || fl.get(i) instanceof Expression)) {
if (fl.get(i) instanceof FunctionSingle) {
2018-09-28 11:39:28 +02:00
if (((FunctionSingle) fl.get(i)).getParameter() == null) {
2016-09-02 20:32:37 +02:00
return false;
2018-09-28 11:39:28 +02:00
}
} else if (fl.get(i) instanceof FunctionOperator) {
2018-09-28 11:39:28 +02:00
if (((FunctionOperator) fl.get(i)).getParameter1() == null || ((FunctionOperator) fl.get(i)).getParameter2() == null) {
2016-09-02 20:32:37 +02:00
return false;
2018-09-28 11:39:28 +02:00
}
} else {
2016-09-02 20:32:37 +02:00
return false;
2018-09-28 11:39:28 +02:00
}
}
}
2016-09-02 20:32:37 +02:00
return true;
}
2018-09-22 11:17:30 +02:00
public static boolean areThereOnlySettedUpFunctionsAndSystems(final List<Function> fl) {
2018-09-28 11:39:28 +02:00
for (int i = 0; i < fl.size(); i++) {
if (!(fl.get(i) instanceof Number || fl.get(i) instanceof Variable || fl.get(i) instanceof Equation || fl.get(i) instanceof EquationsSystemPart || fl.get(i) instanceof Expression)) {
if (fl.get(i) instanceof FunctionSingle) {
2018-09-28 11:39:28 +02:00
if (((FunctionSingle) fl.get(i)).getParameter() == null) {
2016-09-02 20:32:37 +02:00
return false;
2018-09-28 11:39:28 +02:00
}
} else if (fl.get(i) instanceof FunctionOperator) {
2018-09-28 11:39:28 +02:00
if (((FunctionOperator) fl.get(i)).getParameter1() == null || ((FunctionOperator) fl.get(i)).getParameter2() == null) {
2016-09-02 20:32:37 +02:00
return false;
2018-09-28 11:39:28 +02:00
}
} else {
2016-09-02 20:32:37 +02:00
return false;
2018-09-28 11:39:28 +02:00
}
}
}
2016-09-02 20:32:37 +02:00
return true;
}
2018-09-22 11:17:30 +02:00
public static boolean areThereOnlyEmptySNFunctions(final List<Function> fl) {
2018-09-28 11:39:28 +02:00
for (int i = 0; i < fl.size(); i++) {
if (fl.get(i) instanceof FunctionSingle) {
if (((FunctionSingle) fl.get(i)).getParameter() == null) {
2016-09-02 20:32:37 +02:00
return true;
2018-09-28 11:39:28 +02:00
}
}
}
2016-09-02 20:32:37 +02:00
return false;
}
2018-09-22 11:17:30 +02:00
public static boolean areThereOnlyEmptyNSNFunctions(final List<Function> fl) {
2018-09-28 11:39:28 +02:00
for (int i = 0; i < fl.size(); i++) {
if (fl.get(i) instanceof FunctionOperator && !(fl.get(i) instanceof Sum) && !(fl.get(i) instanceof SumSubtraction) && !(fl.get(i) instanceof Subtraction) && !(fl.get(i) instanceof Multiplication) && !(fl.get(i) instanceof Division)) {
if (((FunctionOperator) fl.get(i)).getParameter1() == null && ((FunctionOperator) fl.get(i)).getParameter2() == null) {
2016-09-02 20:32:37 +02:00
return true;
2018-09-28 11:39:28 +02:00
}
}
}
2016-09-02 20:32:37 +02:00
return false;
}
2018-09-22 11:17:30 +02:00
public static boolean areThereEmptyMultiplications(final List<Function> fl) {
2018-09-28 11:39:28 +02:00
for (int i = 0; i < fl.size(); i++) {
if (fl.get(i) instanceof Multiplication || fl.get(i) instanceof Division) {
if (((FunctionOperator) fl.get(i)).getParameter1() == null && ((FunctionOperator) fl.get(i)).getParameter2() == null) {
2016-09-02 20:32:37 +02:00
return true;
2018-09-28 11:39:28 +02:00
}
}
}
2016-09-02 20:32:37 +02:00
return false;
}
2018-09-22 11:17:30 +02:00
public static boolean areThereEmptySums(final List<Function> fl) {
2018-09-28 11:39:28 +02:00
for (int i = 0; i < fl.size(); i++) {
if (fl.get(i) instanceof Sum || fl.get(i) instanceof SumSubtraction || fl.get(i) instanceof Subtraction) {
if (((FunctionOperator) fl.get(i)).getParameter1() == null && ((FunctionOperator) fl.get(i)).getParameter2() == null) {
2016-09-02 20:32:37 +02:00
return true;
2018-09-28 11:39:28 +02:00
}
}
}
2016-09-02 20:32:37 +02:00
return false;
}
2018-09-22 11:17:30 +02:00
public static boolean areThereEmptySystems(final List<Function> fl) {
2018-09-28 11:39:28 +02:00
for (int i = 0; i < fl.size(); i++) {
if (fl.get(i) instanceof EquationsSystemPart) {
if (((EquationsSystemPart) fl.get(i)).getParameter() == null) {
2016-09-02 20:32:37 +02:00
return true;
2018-09-28 11:39:28 +02:00
}
}
}
2016-09-02 20:32:37 +02:00
return false;
}
2018-09-22 11:17:30 +02:00
public static boolean areThereOtherSettedUpFunctions(final List<Function> fl) {
2018-09-28 11:39:28 +02:00
for (int i = 0; i < fl.size(); i++) {
if (!(fl.get(i) instanceof Number || fl.get(i) instanceof Variable || fl.get(i) instanceof Sum || fl.get(i) instanceof SumSubtraction || fl.get(i) instanceof Expression || fl.get(i) instanceof FunctionSingle || fl.get(i) instanceof Multiplication || fl.get(i) instanceof Division)) {
if (fl.get(i) instanceof FunctionSingle) {
2018-09-28 11:39:28 +02:00
if (((FunctionSingle) fl.get(i)).getParameter() == null) {
2016-09-02 20:32:37 +02:00
return true;
2018-09-28 11:39:28 +02:00
}
} else if (fl.get(i) instanceof FunctionOperator) {
2018-09-28 11:39:28 +02:00
if (((FunctionOperator) fl.get(i)).getParameter1() == null || ((FunctionOperator) fl.get(i)).getParameter2() == null) {
2016-09-02 20:32:37 +02:00
return true;
2018-09-28 11:39:28 +02:00
}
} else {
2016-09-02 20:32:37 +02:00
return true;
2018-09-28 11:39:28 +02:00
}
}
}
2016-09-02 20:32:37 +02:00
return false;
}
2018-09-22 11:17:30 +02:00
public static Rational getRational(final BigDecimal str) {
2016-09-02 20:32:37 +02:00
try {
2018-09-22 11:17:30 +02:00
return Utils.getRational(str.toString());
} catch (final Error e) {
2016-09-02 20:32:37 +02:00
//E' IMPOSSIBILE CHE VENGA THROWATO UN ERRORE
return new Rational("0");
}
}
public static Rational getRational(String str) throws Error {
2016-09-02 20:32:37 +02:00
try {
return new Rational(str);
} catch (final NumberFormatException ex) {
2016-09-02 20:32:37 +02:00
if (new BigDecimal(str).compareTo(new BigDecimal(8000.0)) < 0 && new BigDecimal(str).compareTo(new BigDecimal(-8000.0)) > 0) {
2018-09-28 11:39:28 +02:00
if (str.equals("-")) {
2016-09-02 20:32:37 +02:00
str = "-1";
2018-09-28 11:39:28 +02:00
}
final long bits = Double.doubleToLongBits(Double.parseDouble(str));
2016-09-02 20:32:37 +02:00
final long sign = bits >>> 63;
2018-09-22 11:17:30 +02:00
final long exponent = (bits >>> 52 ^ sign << 11) - 1023;
final long fraction = bits << 12; // bits are "reversed" but that's
// not a problem
2016-09-02 20:32:37 +02:00
long a = 1L;
long b = 1L;
for (int i = 63; i >= 12; i--) {
2018-09-22 11:17:30 +02:00
a = a * 2 + (fraction >>> i & 1);
2016-09-02 20:32:37 +02:00
b *= 2;
}
2018-09-28 11:39:28 +02:00
if (exponent > 0) {
2016-09-02 20:32:37 +02:00
a *= 1 << exponent;
2018-09-28 11:39:28 +02:00
} else {
2016-09-02 20:32:37 +02:00
b *= 1 << -exponent;
2018-09-28 11:39:28 +02:00
}
2016-09-02 20:32:37 +02:00
2018-09-28 11:39:28 +02:00
if (sign == 1) {
2016-09-02 20:32:37 +02:00
a *= -1;
2018-09-28 11:39:28 +02:00
}
2016-09-02 20:32:37 +02:00
if (b == 0) {
a = 0;
b = 1;
}
return new Rational(new BigInteger(a + ""), new BigInteger(b + ""));
} else {
final BigDecimal original = new BigDecimal(str);
2016-09-02 20:32:37 +02:00
final BigInteger numerator = original.unscaledValue();
2016-09-02 20:32:37 +02:00
final BigInteger denominator = BigDecimalMath.pow(BigDecimal.TEN, new BigDecimal(original.scale())).toBigIntegerExact();
2016-09-02 20:32:37 +02:00
return new Rational(numerator, denominator);
}
}
}
2018-09-22 11:17:30 +02:00
public static BigDecimal rationalToIrrationalString(final Rational r) {
2016-09-02 20:32:37 +02:00
return BigDecimalMath.divideRound(new BigDecimal(r.numer()).setScale(Utils.scale, Utils.scaleMode), new BigDecimal(r.denom()).setScale(Utils.scale, Utils.scaleMode));
}
2018-09-22 11:17:30 +02:00
public static boolean equalsVariables(final List<Variable> variables, final List<Variable> variables2) {
2018-09-28 11:39:28 +02:00
if (variables.size() != variables2.size()) {
2016-09-02 20:32:37 +02:00
return false;
2018-09-28 11:39:28 +02:00
} else {
for (final Variable v : variables) {
if (!variables2.contains(v)) {
2016-09-02 20:32:37 +02:00
return false;
2018-09-28 11:39:28 +02:00
}
}
2016-09-02 20:32:37 +02:00
return true;
}
}
@Deprecated
2018-09-22 11:17:30 +02:00
public static void writeSquareRoot(final Function var, final int x, final int y, final boolean small) {
// var.setSmall(small);
// final int w1 = var.getWidth();
// final int h1 = var.getHeight();
// final int wsegno = 5;
// final int hsegno = h1 + 2;
//
// var.draw(x + wsegno, y + (hsegno - h1), null, null);
//
// Engine.INSTANCE.getHardwareDevice().getDisplayManager().renderer.glDrawLine(x + 1, y + hsegno - 3, x + 1, y + hsegno - 3);
// Engine.INSTANCE.getHardwareDevice().getDisplayManager().renderer.glDrawLine(x + 2, y + hsegno - 2, x + 2, y + hsegno - 2);
// Engine.INSTANCE.getHardwareDevice().getDisplayManager().renderer.glDrawLine(x + 3, y + hsegno - 1, x + 3, y + hsegno - 1);
// Engine.INSTANCE.getHardwareDevice().getDisplayManager().renderer.glDrawLine(x + 3, y + (hsegno - 1) / 2 + 1, x + 3, y + hsegno - 1);
// Engine.INSTANCE.getHardwareDevice().getDisplayManager().renderer.glDrawLine(x + 4, y, x + 4, y + (hsegno - 1) / 2);
// Engine.INSTANCE.getHardwareDevice().getDisplayManager().renderer.glDrawLine(x + 4, y, x + 4 + 1 + w1 + 1, y);
2016-09-02 20:32:37 +02:00
}
public static final int getFontHeight() {
2018-09-22 11:17:30 +02:00
return Utils.getFontHeight(false);
2016-09-02 20:32:37 +02:00
}
2018-09-22 11:17:30 +02:00
public static final BinaryFont getFont(final boolean small) {
return Utils.getFont(small, StaticVars.zoomed);
2016-10-02 16:01:41 +02:00
}
2018-09-22 11:17:30 +02:00
public static final BinaryFont getFont(final boolean small, final boolean zoomed) {
2019-02-27 23:29:03 +01:00
return WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().fonts[Utils.getFontIndex(small, zoomed)];
2016-11-02 21:56:40 +01:00
}
2018-09-22 11:17:30 +02:00
public static final int getFontIndex(final boolean small, final boolean zoomed) {
2016-09-02 20:32:37 +02:00
if (small) {
2018-09-28 11:39:28 +02:00
if (zoomed) {
2016-11-02 21:56:40 +01:00
return 3;
2018-09-28 11:39:28 +02:00
} else {
2016-11-02 21:56:40 +01:00
return 1;
2018-09-28 11:39:28 +02:00
}
} else if (zoomed) {
2018-09-22 11:17:30 +02:00
return 2;
2018-09-28 11:39:28 +02:00
} else {
2018-09-22 11:17:30 +02:00
return 0;
2018-09-28 11:39:28 +02:00
}
2016-09-02 20:32:37 +02:00
}
2018-09-22 11:17:30 +02:00
public static final int getFontHeight(final boolean small) {
return Utils.getFontHeight(small, StaticVars.zoomed);
2016-10-02 16:01:41 +02:00
}
2018-09-22 11:17:30 +02:00
public static final int getFontHeight(final boolean small, final boolean zoomed) {
2016-10-02 16:01:41 +02:00
if (small) {
2018-09-28 11:39:28 +02:00
if (zoomed) {
2019-02-27 23:29:03 +01:00
return WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().glyphsHeight[3];
2018-09-28 11:39:28 +02:00
} else {
2019-02-27 23:29:03 +01:00
return WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().glyphsHeight[1];
2018-09-28 11:39:28 +02:00
}
} else if (zoomed) {
2019-02-27 23:29:03 +01:00
return WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().glyphsHeight[2];
2018-09-28 11:39:28 +02:00
} else {
2019-02-27 23:29:03 +01:00
return WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().glyphsHeight[0];
2018-09-28 11:39:28 +02:00
}
2016-09-02 20:32:37 +02:00
}
2018-09-22 11:17:30 +02:00
public static byte[] convertStreamToByteArray(final InputStream stream, final long size) throws IOException {
2016-09-02 20:32:37 +02:00
// check to ensure that file size is not larger than Integer.MAX_VALUE.
2018-09-28 11:39:28 +02:00
if (size > Integer.MAX_VALUE) {
return new byte[0];
2018-09-28 11:39:28 +02:00
}
2016-09-02 20:32:37 +02:00
final byte[] buffer = new byte[(int) size];
final ByteArrayOutputStream os = new ByteArrayOutputStream();
2016-09-02 20:32:37 +02:00
int line = 0;
// read bytes from stream, and store them in buffer
2018-09-28 11:39:28 +02:00
while ((line = stream.read(buffer)) != -1) {
// Writes bytes from byte array (buffer) into output stream.
os.write(buffer, 0, line);
2018-09-28 11:39:28 +02:00
}
stream.close();
os.flush();
os.close();
return os.toByteArray();
2016-09-02 20:32:37 +02:00
}
2018-09-22 11:17:30 +02:00
public static int[] realBytes(final byte[] bytes) {
final int len = bytes.length;
final int[] realbytes = new int[len];
2018-09-28 11:39:28 +02:00
for (int i = 0; i < len; i++) {
realbytes[i] = bytes[i] & 0xFF;
2018-09-28 11:39:28 +02:00
}
2016-09-02 20:32:37 +02:00
return realbytes;
}
2018-09-22 11:17:30 +02:00
public static Function[][] joinFunctionsResults(final List<Function> l1, final List<Function> l2) {
final int size1 = l1.size();
final int size2 = l2.size();
2016-10-02 16:01:41 +02:00
int cur1 = 0;
int cur2 = 0;
final int total = size1 * size2;
final Function[][] results = new Function[total][2];
2016-10-02 16:01:41 +02:00
for (int i = 0; i < total; i++) {
results[i] = new Function[] { l1.get(cur1), l2.get(cur2) };
2018-09-28 11:39:28 +02:00
if (i % size2 == 0) {
cur1 += 1;
2018-09-28 11:39:28 +02:00
}
if (i % size1 == 0) {
cur2 += 1;
2018-09-28 11:39:28 +02:00
}
if (cur1 >= size1) {
cur1 = 0;
2018-09-28 11:39:28 +02:00
}
if (cur2 >= size2) {
cur2 = 0;
2018-09-28 11:39:28 +02:00
}
2016-10-02 16:01:41 +02:00
}
return results;
}
2018-09-22 11:17:30 +02:00
public static Function[][] joinFunctionsResults(final ObjectArrayList<ObjectArrayList<Function>> ln) {
final int[] sizes = new int[ln.size()];
2018-09-28 11:39:28 +02:00
for (int i = 0; i < ln.size(); i++) {
sizes[i] = ln.get(i).size();
2018-09-28 11:39:28 +02:00
}
final int[] curs = new int[sizes.length];
int total = 0;
2018-09-28 11:39:28 +02:00
for (int i = 0; i < ln.size(); i++) {
if (i == 0) {
total = sizes[i];
2018-09-28 11:39:28 +02:00
} else {
total *= sizes[i];
2018-09-28 11:39:28 +02:00
}
}
final Function[][] results = new Function[total][sizes.length];
for (int i = 0; i < total; i++) {
results[i] = new Function[sizes.length];
2018-09-28 11:39:28 +02:00
for (int j = 0; j < sizes.length; j++) {
results[i][j] = ln.get(j).get(curs[j]);
2018-09-28 11:39:28 +02:00
}
for (int k = 0; k < sizes.length; k++) {
if (i % sizes[k] == 0) {
for (int l = 0; l < sizes.length; l++) {
if (l != k) {
curs[l] += 1;
2018-09-28 11:39:28 +02:00
}
}
}
}
for (int k = 0; k < sizes.length; k++) {
if (curs[k] >= sizes[k]) {
curs[k] = 0;
2018-09-28 11:39:28 +02:00
}
}
}
return results;
}
2018-09-22 11:17:30 +02:00
public static boolean isNegative(final Function b) {
2018-09-28 11:39:28 +02:00
if (b instanceof Negative) {
return true;
2018-09-28 11:39:28 +02:00
} else if (b instanceof Number && ((Number) b).getTerm().compareTo(BigDecimal.ZERO) < 0) {
return true;
2018-09-28 11:39:28 +02:00
}
return false;
}
2016-12-04 22:22:16 +01:00
2018-09-22 11:17:30 +02:00
public static CharSequence multipleChars(final String string, final int i) {
2016-12-04 22:22:16 +01:00
String result = "";
2018-09-28 11:39:28 +02:00
for (int j = 0; j < i; j++) {
result += string;
2018-09-28 11:39:28 +02:00
}
2016-12-04 22:22:16 +01:00
return result;
}
2018-09-22 11:17:30 +02:00
public static boolean isIntegerValue(final BigDecimal bd) {
return bd.signum() == 0 || bd.scale() <= 0 || bd.stripTrailingZeros().scale() <= 0;
}
2017-01-16 17:57:09 +01:00
@SafeVarargs
2018-09-22 11:17:30 +02:00
public static <T> String arrayToString(final T... data) {
2017-01-16 17:57:09 +01:00
String sdata = "";
2018-09-28 11:39:28 +02:00
for (final T o : data) {
sdata += "," + o;
2018-09-28 11:39:28 +02:00
}
2017-01-16 17:57:09 +01:00
return sdata.substring(1);
}
2018-09-22 11:17:30 +02:00
public static String arrayToString(final boolean... data) {
2017-01-16 17:57:09 +01:00
String sdata = "";
2018-09-28 11:39:28 +02:00
for (final boolean o : data) {
2018-09-22 11:17:30 +02:00
sdata += o ? 1 : 0;
2018-09-28 11:39:28 +02:00
}
2017-01-16 17:57:09 +01:00
return sdata;
}
public static boolean isWindows() {
2019-02-27 23:29:03 +01:00
return WarpPI.getPlatform().getOsName().indexOf("win") >= 0;
}
2017-11-19 22:58:37 +01:00
2018-09-22 11:17:30 +02:00
public static <T> ObjectArrayList<T> newArrayList(final T o) {
2018-05-12 21:18:29 +02:00
final ObjectArrayList<T> t = new ObjectArrayList<>();
2017-12-22 22:39:58 +01:00
t.add(o);
return t;
}
2017-12-23 01:14:38 +01:00
2018-09-22 11:17:30 +02:00
public static InputStream getResourceStreamSafe(final String string) throws IOException, URISyntaxException {
2018-03-22 10:06:31 +01:00
try {
2019-02-27 23:29:03 +01:00
return WarpPI.getPlatform().getStorageUtils().getResourceStream(string);
2018-05-12 21:18:29 +02:00
} catch (final Exception ex) {
2018-03-22 10:06:31 +01:00
return null;
}
}
2018-05-12 21:18:29 +02:00
2018-09-04 12:12:41 +02:00
public static File getJarDirectory() {
return new File("").getAbsoluteFile();
}
2018-09-22 11:17:30 +02:00
public static <T, U> U getOrDefault(final Map<T, U> enginesList, final T key, final U object) {
2018-09-28 11:39:28 +02:00
if (enginesList.containsKey(key)) {
2018-09-04 12:12:41 +02:00
return enginesList.get(key);
2018-09-28 11:39:28 +02:00
} else {
2018-09-04 12:12:41 +02:00
return object;
2018-09-28 11:39:28 +02:00
}
2018-03-22 20:40:41 +01:00
}
2016-09-02 20:32:37 +02:00
}