package org.expeditee; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Collections; import java.util.Date; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import org.expeditee.stats.Formatter; /** * Static collection of various helper methods. * * @author cts16 * */ public class Util { /** * Using Microsofts commandline convention: Args seperated with white * spaces. Options with white spaces enclosed with quotes. Args with quotes * must be double quoted args1 args2=sfasas args3="option with spaces" * arg""4"" * * @param args * Null and empty excepted * @return An array of args. null if none provided */ public static String[] parseArgs(String args) { if (args == null) return null; args = args.trim(); if (args.length() == 0) return null; List vargs = new LinkedList(); StringBuilder sb = new StringBuilder(); boolean quoteOn = false; for (int i = 0; i < args.length(); i++) { char c = args.charAt(i); if (c == ' ' && !quoteOn) { // Extract arg vargs.add(sb.toString()); sb = new StringBuilder(); // Consume white spaces while (args.charAt(++i) == ' ' && i < args.length()) { } i--; } else if (c == '\"') { // If double quoted if (args.length() >= (i + 2) && args.charAt(i + 1) == '\"') { sb.append(c); // add escaped i++; } else { quoteOn = !quoteOn; } } else { sb.append(c); } } if (sb.length() > 0) vargs.add(sb.toString()); if (vargs.size() == 0) return null; else return vargs.toArray(new String[vargs.size()]); } /** * Reverse of parseArgs * * @return Null if args is null or empty / all whitespace */ public static String formatArgs(String[] args) { if (args == null) return null; StringBuilder sb = new StringBuilder(); for (String s : args) { if (s == null) continue; // Escape quotes StringBuilder formatted = new StringBuilder(s.replaceAll("\"", "\"\"")); // Encapsulate spaces int index = formatted.indexOf(" "); if (index >= 0) { formatted.insert(index, "\""); formatted.append('\"'); } if (sb.length() > 0) sb.append(' '); sb.append(formatted); } return sb.length() > 0 ? sb.toString() : null; } /** * Parses a date from the given string. * * @param dateString * The string to parse. * * @return * The date parsed from the string. * * @throws ParseException */ public static Date parseDate(String dateString) throws ParseException { // Select the best match for a date or time format DateFormat df = null; if (dateString.length() > Formatter.DATE_FORMAT.length()) { df = new SimpleDateFormat(Formatter.DATE_TIME_FORMAT); } else if (dateString.length() <= Formatter.TIME_FORMAT.length()) { df = new SimpleDateFormat(Formatter.TIME_FORMAT); } else { df = new SimpleDateFormat(Formatter.DATE_FORMAT); } Date date = df.parse(dateString); return date; } /** * Sets all members of the given array to the given value. * * @param arr * The array to initialise. * * @param val * The value to set each element to. */ public static void initialiseBoolArray(boolean[] arr, boolean val) { for (int i = 0; i < arr.length; i++) arr[i] = val; } /** Whether or not the system has been checked to see if the Java version is >= 1.6 */ private static boolean _minimum_version6_checked = false; /** Whether or not the Java version running is >= 1.6 */ private static boolean _minimum_version6 = false; /** * Checks if the running version of Java is >= 1.6. * * @return * True if the Java version is >= 1.6, false otherwise. */ public static boolean isMinimumVersion6() { // If we haven't checked yet, do so if (!_minimum_version6_checked) { String full_version = System.getProperty("java.version"); String[] version_parts = full_version.split("\\."); if (version_parts.length>=2) { String version_str = version_parts[0] + "." + version_parts[1]; double version = Double.parseDouble(version_str); if (version >= 1.6) { _minimum_version6 = true; } } _minimum_version6_checked = true; } return _minimum_version6; } /** * Returns an array of roots to the quadratic equation ax^2 + bx + c = 0. * Will be null, or contain 1 or 2 values depending on the constants given. */ public static double[] quadraticEquation(double a, double b, double c) { // Calculate the discriminant double discriminant = b * b - 4 * a * c; // See how many roots we have if (discriminant < 0) { return null; } else if (discriminant == 0) { double[] ret = new double[1]; ret[0] = -b / (2 * a); return ret; } else { double[] ret = new double[2]; ret[0] = (-b + Math.sqrt(discriminant)) / (2 * a); ret[1] = (-b - Math.sqrt(discriminant)) / (2 * a); return ret; } } public static double[] toDoubleArray(float[] floatArray) { if (floatArray == null) return null; double[] ret = new double[floatArray.length]; for (int i = 0; i < floatArray.length; i++) { ret[i] = floatArray[i]; } return ret; } /** Creates a hash-set from an array. */ public static HashSet hashSetFromArray(T[] array) { if (array == null) return null; HashSet ret = new HashSet(); for (T element : array) ret.add(element); return ret; } /** Returns the string which is n concatenations of the given character. */ public static String nCopiesOf(int n, char character) { if (n < 0) { return null; } else if (n == 0) { return ""; } else { return String.join("", Collections.nCopies(n, Character.toString(character))); } } /** Returns the linear interpolation between a and b. */ public static float lerp(float a, float b, float t) { return t * (b - a) + a; } }