[1098] | 1 | package org.expeditee;
|
---|
| 2 |
|
---|
| 3 | import java.text.DateFormat;
|
---|
| 4 | import java.text.ParseException;
|
---|
| 5 | import java.text.SimpleDateFormat;
|
---|
| 6 | import java.util.Collections;
|
---|
| 7 | import java.util.Date;
|
---|
| 8 | import java.util.HashSet;
|
---|
| 9 | import java.util.LinkedList;
|
---|
| 10 | import java.util.List;
|
---|
| 11 |
|
---|
| 12 | import org.expeditee.stats.Formatter;
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
| 15 | * Static collection of various helper methods.
|
---|
| 16 | *
|
---|
| 17 | * @author cts16
|
---|
| 18 | *
|
---|
| 19 | */
|
---|
| 20 | public class Util {
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
| 23 | * Using Microsofts commandline convention: Args seperated with white
|
---|
| 24 | * spaces. Options with white spaces enclosed with quotes. Args with quotes
|
---|
| 25 | * must be double quoted args1 args2=sfasas args3="option with spaces"
|
---|
| 26 | * arg""4""
|
---|
| 27 | *
|
---|
| 28 | * @param args
|
---|
| 29 | * Null and empty excepted
|
---|
| 30 | * @return An array of args. null if none provided
|
---|
| 31 | */
|
---|
| 32 | public static String[] parseArgs(String args) {
|
---|
| 33 |
|
---|
| 34 | if (args == null)
|
---|
| 35 | return null;
|
---|
| 36 |
|
---|
| 37 | args = args.trim();
|
---|
| 38 | if (args.length() == 0)
|
---|
| 39 | return null;
|
---|
| 40 |
|
---|
| 41 | List<String> vargs = new LinkedList<String>();
|
---|
| 42 | StringBuilder sb = new StringBuilder();
|
---|
| 43 | boolean quoteOn = false;
|
---|
| 44 | for (int i = 0; i < args.length(); i++) {
|
---|
| 45 |
|
---|
| 46 | char c = args.charAt(i);
|
---|
| 47 | if (c == ' ' && !quoteOn) {
|
---|
| 48 |
|
---|
| 49 | // Extract arg
|
---|
| 50 | vargs.add(sb.toString());
|
---|
| 51 | sb = new StringBuilder();
|
---|
| 52 |
|
---|
| 53 | // Consume white spaces
|
---|
| 54 | while (args.charAt(++i) == ' ' && i < args.length()) {
|
---|
| 55 | }
|
---|
| 56 | i--;
|
---|
| 57 |
|
---|
| 58 | } else if (c == '\"') {
|
---|
| 59 | // If double quoted
|
---|
| 60 | if (args.length() >= (i + 2) && args.charAt(i + 1) == '\"') {
|
---|
| 61 |
|
---|
| 62 | sb.append(c); // add escaped
|
---|
| 63 | i++;
|
---|
| 64 |
|
---|
| 65 | } else {
|
---|
| 66 | quoteOn = !quoteOn;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | } else {
|
---|
| 70 | sb.append(c);
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | if (sb.length() > 0)
|
---|
| 75 | vargs.add(sb.toString());
|
---|
| 76 |
|
---|
| 77 | if (vargs.size() == 0)
|
---|
| 78 | return null;
|
---|
| 79 | else
|
---|
| 80 | return vargs.toArray(new String[vargs.size()]);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
| 84 | * Reverse of parseArgs
|
---|
| 85 | *
|
---|
| 86 | * @return Null if args is null or empty / all whitespace
|
---|
| 87 | */
|
---|
| 88 | public static String formatArgs(String[] args) {
|
---|
| 89 | if (args == null)
|
---|
| 90 | return null;
|
---|
| 91 |
|
---|
| 92 | StringBuilder sb = new StringBuilder();
|
---|
| 93 |
|
---|
| 94 | for (String s : args) {
|
---|
| 95 | if (s == null)
|
---|
| 96 | continue;
|
---|
| 97 |
|
---|
| 98 | // Escape quotes
|
---|
| 99 | StringBuilder formatted = new StringBuilder(s.replaceAll("\"",
|
---|
| 100 | "\"\""));
|
---|
| 101 |
|
---|
| 102 | // Encapsulate spaces
|
---|
| 103 | int index = formatted.indexOf(" ");
|
---|
| 104 | if (index >= 0) {
|
---|
| 105 | formatted.insert(index, "\"");
|
---|
| 106 | formatted.append('\"');
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | if (sb.length() > 0)
|
---|
| 110 | sb.append(' ');
|
---|
| 111 | sb.append(formatted);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | return sb.length() > 0 ? sb.toString() : null;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | /**
|
---|
| 118 | * Parses a date from the given string.
|
---|
| 119 | *
|
---|
| 120 | * @param dateString
|
---|
| 121 | * The string to parse.
|
---|
| 122 | *
|
---|
| 123 | * @return
|
---|
| 124 | * The date parsed from the string.
|
---|
| 125 | *
|
---|
| 126 | * @throws ParseException
|
---|
| 127 | */
|
---|
| 128 | public static Date parseDate(String dateString) throws ParseException {
|
---|
| 129 | // Select the best match for a date or time format
|
---|
| 130 | DateFormat df = null;
|
---|
| 131 | if (dateString.length() > Formatter.DATE_FORMAT.length()) {
|
---|
| 132 | df = new SimpleDateFormat(Formatter.DATE_TIME_FORMAT);
|
---|
| 133 | } else if (dateString.length() <= Formatter.TIME_FORMAT.length()) {
|
---|
| 134 | df = new SimpleDateFormat(Formatter.TIME_FORMAT);
|
---|
| 135 | } else {
|
---|
| 136 | df = new SimpleDateFormat(Formatter.DATE_FORMAT);
|
---|
| 137 | }
|
---|
| 138 | Date date = df.parse(dateString);
|
---|
| 139 | return date;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | /**
|
---|
| 143 | * Sets all members of the given array to the given value.
|
---|
| 144 | *
|
---|
| 145 | * @param arr
|
---|
| 146 | * The array to initialise.
|
---|
| 147 | *
|
---|
| 148 | * @param val
|
---|
| 149 | * The value to set each element to.
|
---|
| 150 | */
|
---|
| 151 | public static void initialiseBoolArray(boolean[] arr, boolean val)
|
---|
| 152 | {
|
---|
| 153 | for (int i = 0; i < arr.length; i++) arr[i] = val;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | /** Whether or not the system has been checked to see if the Java version is >= 1.6 */
|
---|
| 157 | private static boolean _minimum_version6_checked = false;
|
---|
| 158 | /** Whether or not the Java version running is >= 1.6 */
|
---|
| 159 | private static boolean _minimum_version6 = false;
|
---|
| 160 |
|
---|
| 161 | /**
|
---|
| 162 | * Checks if the running version of Java is >= 1.6.
|
---|
| 163 | *
|
---|
| 164 | * @return
|
---|
| 165 | * True if the Java version is >= 1.6, false otherwise.
|
---|
| 166 | */
|
---|
| 167 | public static boolean isMinimumVersion6()
|
---|
| 168 | {
|
---|
| 169 | // If we haven't checked yet, do so
|
---|
| 170 | if (!_minimum_version6_checked) {
|
---|
| 171 | String full_version = System.getProperty("java.version");
|
---|
| 172 | String[] version_parts = full_version.split("\\.");
|
---|
| 173 | if (version_parts.length>=2) {
|
---|
| 174 | String version_str = version_parts[0] + "." + version_parts[1];
|
---|
| 175 | double version = Double.parseDouble(version_str);
|
---|
| 176 | if (version >= 1.6) {
|
---|
| 177 | _minimum_version6 = true;
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | _minimum_version6_checked = true;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | return _minimum_version6;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | /**
|
---|
| 188 | * Returns an array of roots to the quadratic equation ax^2 + bx + c = 0.
|
---|
| 189 | * Will be null, or contain 1 or 2 values depending on the constants given.
|
---|
| 190 | */
|
---|
| 191 | public static double[] quadraticEquation(double a, double b, double c)
|
---|
| 192 | {
|
---|
| 193 | // Calculate the discriminant
|
---|
| 194 | double discriminant = b * b - 4 * a * c;
|
---|
| 195 |
|
---|
| 196 | // See how many roots we have
|
---|
| 197 | if (discriminant < 0) {
|
---|
| 198 | return null;
|
---|
| 199 | } else if (discriminant == 0) {
|
---|
| 200 | double[] ret = new double[1];
|
---|
| 201 | ret[0] = -b / (2 * a);
|
---|
| 202 | return ret;
|
---|
| 203 | } else {
|
---|
| 204 | double[] ret = new double[2];
|
---|
| 205 | ret[0] = (-b + Math.sqrt(discriminant)) / (2 * a);
|
---|
| 206 | ret[1] = (-b - Math.sqrt(discriminant)) / (2 * a);
|
---|
| 207 | return ret;
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | public static double[] toDoubleArray(float[] floatArray)
|
---|
| 212 | {
|
---|
| 213 | if (floatArray == null) return null;
|
---|
| 214 |
|
---|
| 215 | double[] ret = new double[floatArray.length];
|
---|
| 216 |
|
---|
| 217 | for (int i = 0; i < floatArray.length; i++) {
|
---|
| 218 | ret[i] = floatArray[i];
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | return ret;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | /** Creates a hash-set from an array. */
|
---|
| 225 | public static <T> HashSet<T> hashSetFromArray(T[] array)
|
---|
| 226 | {
|
---|
| 227 | if (array == null) return null;
|
---|
| 228 |
|
---|
| 229 | HashSet<T> ret = new HashSet<T>();
|
---|
| 230 | for (T element : array) ret.add(element);
|
---|
| 231 |
|
---|
| 232 | return ret;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | /** Returns the string which is n concatenations of the given character. */
|
---|
| 236 | public static String nCopiesOf(int n, char character)
|
---|
| 237 | {
|
---|
| 238 | if (n < 0) {
|
---|
| 239 | return null;
|
---|
| 240 | } else if (n == 0) {
|
---|
| 241 | return "";
|
---|
| 242 | } else {
|
---|
| 243 | return String.join("", Collections.nCopies(n, Character.toString(character)));
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | /** Returns the linear interpolation between a and b. */
|
---|
| 248 | public static float lerp(float a, float b, float t)
|
---|
| 249 | {
|
---|
| 250 | return t * (b - a) + a;
|
---|
| 251 | }
|
---|
| 252 | }
|
---|