Class SQUtils

java.lang.Object
com.strategyquant.lib.SQUtils

public class SQUtils extends Object
The Class SQUtils.
  • Constructor Details

    • SQUtils

      public SQUtils()
  • Method Details

    • openUrlInDefaultWebBrowser

      public static void openUrlInDefaultWebBrowser(String url, Component parentComponent)
      Open url in default web browser.
      Parameters:
      url - the url
      parentComponent - the parent component
    • forceOpenUrlInDefaultWebBrowser

      public static void forceOpenUrlInDefaultWebBrowser(String url, Component parentComponent)
      Force open url in default web browser.
      Parameters:
      url - the url
      parentComponent - the parent component
    • fileToBytes

      public static byte[] fileToBytes(File file) throws Exception
      File to bytes.
      Parameters:
      file - the file
      Returns:
      the byte[]
      Throws:
      Exception - the exception
    • md5CheckSum

      public static String md5CheckSum(byte[] bytes) throws Exception
      Md 5 check sum.
      Parameters:
      bytes - the bytes
      Returns:
      the string
      Throws:
      Exception - the exception
    • file2md5Hash

      public static String file2md5Hash(File f)
      File 2 md 5 hash.
      Parameters:
      f - the f
      Returns:
      the string
      Throws:
      RuntimeException - (unchecked) on error calculating hash
    • fileToString

      public static String fileToString(String path) throws Exception
      File to string.
      Parameters:
      path - the path
      Returns:
      the string
      Throws:
      Exception - the exception
    • fileToString

      public static String fileToString(File file) throws Exception
      File to string.
      Parameters:
      file - the file
      Returns:
      the string
      Throws:
      Exception - the exception
    • fileToString

      public static String fileToString(File file, String charsetName) throws Exception
      File to string.
      Parameters:
      file - the file
      charsetName - the charset name
      Returns:
      the string
      Throws:
      Exception - the exception
    • createAndHandleBackupFiles

      public static void createAndHandleBackupFiles(String path, String fileContentsNew, int numberOfBackups)
      Create named backups of file & keep last 10
      Parameters:
      path - the path
      fileContentsNew -
      numberOfBackups -
    • createAndHandleBackupFiles

      public static void createAndHandleBackupFiles(String path)
    • listBackupFiles

      public static org.json.JSONArray listBackupFiles(String path)
      List backups of file specified in path
      Parameters:
      path - the path
    • stringToFile

      public static void stringToFile(String path, String str)
      String to file.
      Parameters:
      path - the path
      str - the str
    • stringToFile

      public static void stringToFile(File file, String str)
      String to file.
      Parameters:
      file - the file
      str - the str
    • getFirstFileFromDir

      public static File getFirstFileFromDir(File dir) throws Exception
      Gets the first file from dir.
      Parameters:
      dir - the dir
      Returns:
      the first file from dir
      Throws:
      Exception - the exception
    • checkWorkDirectoryIsWritable

      public static boolean checkWorkDirectoryIsWritable(String path)
      Check work directory is writable.
      Parameters:
      path - the path
      Returns:
      true, if successful
    • convertPriceToInt

      public static int convertPriceToInt(double value, int decimals)
      Convert price to int.
      Parameters:
      value - the value
      decimals - the decimals
      Returns:
      the int
    • round

      public static double round(double value, int decimals)
      Round.
      Parameters:
      value - the value
      decimals - the decimals
      Returns:
      the double
    • round7

      public static double round7(double value)
      Round 7.
      Parameters:
      value - the value
      Returns:
      the double
    • round6

      public static double round6(double value)
      Round 6.
      Parameters:
      value - the value
      Returns:
      the double
    • round

      public static Integer round(double value)
      Round.
      Parameters:
      value - the value
      Returns:
      the integer
    • roundLong

      public static Long roundLong(double value)
      Round long.
      Parameters:
      value - the value
      Returns:
      the long
    • adjustToTickSize

      public static double adjustToTickSize(double value, double tickSize)
      Adjust to tick size.
      Parameters:
      value - the value
      tickSize - the tick size
      Returns:
      the double
    • round2

      public static double round2(double value)
      Round 2.
      Parameters:
      value - the value
      Returns:
      the double
    • safeDivide

      public static double safeDivide(double f1, double f2)
      Safe divide.
      Parameters:
      f1 - the f 1
      f2 - the f 2
      Returns:
      the double
    • trimFilePath

      public static String trimFilePath(String filePath, String trim)
      Trim file path.
      Parameters:
      filePath - the file path
      trim - the trim
      Returns:
      the string
    • remapFilePathByMap

      public static String remapFilePathByMap(String filePath, Map<String,​String> fileMap)
      Remap file paths by map
      Parameters:
      filePath - file path to be normalized
      fileMap - file path remapping map key: matchPath => replaceValue key: matchPath => replaceValue example: {"/path1/path2/cc/dd" => "$REMAPPED_PATH12$/"} example: "/path1/path3/bb" => "$REMAPPED_PATH13$/" example: "/path1/aa" => "${REMAPPED_PATH1}/"
      Returns:
      Normalized/remapped file path
    • getExtension

      public static String getExtension(String name)
      Gets the extension.
      Parameters:
      name - the name
      Returns:
      the extension
    • stripExtension

      public static String stripExtension(String name)
      Strip extension.
      Parameters:
      name - the name
      Returns:
      the string
    • replaceLast

      public static String replaceLast(String string, String from, String to)
      Replace last.
      Parameters:
      string - the string
      from - the from
      to - the to
      Returns:
      the string
    • replaceAll

      public static String replaceAll(String source, String os, String ns)
      Replace all.
      Parameters:
      source - the source
      os - the os
      ns - the ns
      Returns:
      the string
    • joinStrings

      public static String joinStrings(String... constants)
      Join strings.
      Parameters:
      constants - the constants
      Returns:
      the string
    • listToStringArray

      public static String[] listToStringArray(ArrayList<String> valueList)
      List to string array.
      Parameters:
      valueList - the value list
      Returns:
      the string[]
    • deleteRecursive

      public static boolean deleteRecursive(File path) throws FileNotFoundException
      Delete recursive.
      Parameters:
      path - the path
      Returns:
      true, if successful
      Throws:
      FileNotFoundException - the file not found exception
    • deleteNestedFiles

      public static boolean deleteNestedFiles(File path)
      Delete nested files.
      Parameters:
      path - the path
      Returns:
      true, if successful
    • generateUniqueName

      public static String generateUniqueName(String name, com.strategyquant.lib.utils.IUniqueNameChecker uniqueNameChecker) throws Exception
      Generate unique name.
      Parameters:
      name - the name
      uniqueNameChecker - the unique name checker
      Returns:
      the string
      Throws:
      Exception - the exception
    • roundDown

      public static double roundDown(double value, int decimals)
      Round down.
      Parameters:
      value - the value
      decimals - the decimals
      Returns:
      the double
    • invokeUnchecked

      public static <T> T invokeUnchecked(Constructor<T> constructor, Object... arguments)
      Invoke unchecked.
      Type Parameters:
      T - the generic type
      Parameters:
      constructor - the constructor
      arguments - the arguments
      Returns:
      the t
    • inputStreamToString

      public static String inputStreamToString(InputStream is) throws Exception
      Input stream to string.
      Parameters:
      is - the is
      Returns:
      the string
      Throws:
      Exception - the exception
    • loadDataFromServer

      public static byte[] loadDataFromServer(String url) throws Exception
      Load data from server.
      Parameters:
      url - the url
      Returns:
      the byte[]
      Throws:
      Exception - the exception
    • inputStreamToBytes

      public static byte[] inputStreamToBytes(InputStream is) throws Exception
      Input stream to bytes.
      Parameters:
      is - the is
      Returns:
      the byte[]
      Throws:
      Exception - the exception
    • inputStreamToFile

      public static void inputStreamToFile(InputStream inputStream, File file) throws Exception
      Input stream to file.
      Parameters:
      inputStream - the input stream
      file - the file
      Throws:
      Exception - the exception
    • saveFileAs

      public static String saveFileAs(byte[] data, String destDir, String fileName) throws Exception
      Save file as.
      Parameters:
      data - the data
      destDir - the dest dir
      fileName - the file name
      Returns:
      the string
      Throws:
      Exception - the exception
    • formatDate

      public static String formatDate(long time, String pattern)
      Format date.
      Parameters:
      time - the time
      pattern - the pattern
      Returns:
      the string
    • getTimeFromDate

      public static long getTimeFromDate(String date, String pattern)
      Gets the time from date.
      Parameters:
      date - the date
      pattern - the pattern
      Returns:
      the time from date
    • dateFormatValid

      public static boolean dateFormatValid(String date, String pattern)
      Date format valid.
      Parameters:
      date - the date
      pattern - the pattern
      Returns:
      true, if successful
    • callMethod

      public static Object callMethod(Object instance, String methodName, Object arg) throws Exception
      Call method.
      Parameters:
      instance - the instance
      methodName - the method name
      arg - the arg
      Returns:
      the object
      Throws:
      Exception - the exception
    • callMethod

      public static Object callMethod(Object instance, String methodName, Object arg, Class argClass) throws Exception
      Call method.
      Parameters:
      instance - the instance
      methodName - the method name
      arg - the arg
      argClass - the arg class
      Returns:
      the object
      Throws:
      Exception - the exception
    • callMethod

      public static Object callMethod(Object instance, String methodName, Object[] args, boolean replacePrimitives) throws Exception
      Call method.
      Parameters:
      instance - the instance
      methodName - the method name
      args - the args
      replacePrimitives - the replace primitives
      Returns:
      the object
      Throws:
      Exception - the exception
    • callMethod

      public static Object callMethod(Object instance, String methodName, Object[] args, Class[] argsClasses) throws Exception
      Call method.
      Parameters:
      instance - the instance
      methodName - the method name
      args - the args
      argsClasses - the args classes
      Returns:
      the object
      Throws:
      Exception - the exception
    • removeUTF8BOM

      public static String removeUTF8BOM(String s)
      Removes the UTF 8 BOM.
      Parameters:
      s - the s
      Returns:
      the string
    • listSubdirectories

      public static ArrayList<String> listSubdirectories(String directoryName)
      List subdirectories.
      Parameters:
      directoryName - the directory name
      Returns:
      the array list
    • encodeXmlKey

      public static String encodeXmlKey(String key)
      Encode xml key.
      Parameters:
      key - the key
      Returns:
      the string
    • decodeXmlKey

      public static String decodeXmlKey(String key)
      Decode xml key.
      Parameters:
      key - the key
      Returns:
      the string
    • d2

      public static String d2(Number value)
      D 2.
      Parameters:
      value - the value
      Returns:
      the string
    • dCommon

      public static String dCommon(double d, long constant)
      Parameters:
      d -
      constant -
      Returns:
    • d2

      public static String d2(double d)
      efficient conversion to 0.00 DecimalFormat
      Parameters:
      d -
      Returns:
    • d2String

      public static String d2String(double value, int decimals)
      D 2 string.
      Parameters:
      value - the value
      decimals - the decimals
      Returns:
      the string
    • doubleToString

      public static String doubleToString(double value)
      Double to string.
      Parameters:
      value - the value
      Returns:
      the string
    • getStackTrace

      public static String getStackTrace()
      Gets the stack trace.
      Returns:
      the stack trace
    • getStackTrace

      public static String getStackTrace(Throwable throwable)
      Gets the stack trace.
      Parameters:
      throwable - the throwable
      Returns:
      the stack trace
    • correctResultKeyToDirname

      public static String correctResultKeyToDirname(String resultKey)
      Correct result key to dirname.
      Parameters:
      resultKey - the result key
      Returns:
      the string
    • correctDirnameToResultKey

      public static String correctDirnameToResultKey(String resultKey)
      Correct dirname to result key.
      Parameters:
      resultKey - the result key
      Returns:
      the string
    • fixPrice

      public static double fixPrice(double tickStep, double price)
      Fix price.
      Parameters:
      tickStep - the tick step
      price - the price
      Returns:
      the double
    • fixPrice

      public static double fixPrice(double tickStep, double price, int decimals)
      Fix price.
      Parameters:
      tickStep - the tick step
      price - the price
      decimals - the decimals
      Returns:
      the double
    • fixPriceSpecial

      public static double fixPriceSpecial(int orderType, double tickStep, double price, int decimals)
    • format

      public static String format(double val, int precision)
      Format.
      Parameters:
      val - the val
      precision - the precision
      Returns:
      the string
    • compare

      public static int compare(double a, double b, int decimals)
      Compare.
      Parameters:
      a - the a
      b - the b
      decimals - the decimals
      Returns:
      the int
    • getDecimalPlaces

      public static int getDecimalPlaces(double value)
      Gets the decimal places.
      Parameters:
      value - the value
      Returns:
      the decimal places
    • insertUppercaseSpaces

      public static String insertUppercaseSpaces(String str)
      Insert uppercase spaces.
      Parameters:
      str - the str
      Returns:
      the string
    • fileToArrayList

      public static void fileToArrayList(ArrayList<String> list, String filePath, String delimiter) throws Exception
      File to array list.
      Parameters:
      list - the list
      filePath - the file path
      delimiter - the delimiter
      Throws:
      Exception - the exception
    • arrayListToFile

      public static void arrayListToFile(ArrayList<String> list, String filePath, String delimiter) throws Exception
      Array list to file.
      Parameters:
      list - the list
      filePath - the file path
      delimiter - the delimiter
      Throws:
      Exception - the exception
    • countFilesInDirectory

      public static int countFilesInDirectory(String folderName)
      Count files in directory.
      Parameters:
      folderName - the folder name
      Returns:
      the int
    • createFile

      public static File createFile(String filePath) throws Exception
      Creates the file.
      Parameters:
      filePath - the file path
      Returns:
      the file
      Throws:
      Exception - the exception
    • correctNullString

      public static String correctNullString(String string)
      Correct null string.
      Parameters:
      string - the string
      Returns:
      the string
    • computeFileHash

      public static int computeFileHash(String filePath, int skipFirstLines) throws FileNotFoundException, IOException
      Compute file hash.
      Parameters:
      filePath - the file path
      skipFirstLines - the skip first lines
      Returns:
      the int
      Throws:
      FileNotFoundException - the file not found exception
      IOException - Signals that an I/O exception has occurred.
    • ensureDirExists

      public static void ensureDirExists(String dirPath)
      Ensure dir exists.
      Parameters:
      dirPath - the dir path
    • removeXmlns

      public static String removeXmlns(String content)
      Removes the xmlns.
      Parameters:
      content - the content
      Returns:
      the string
    • stackTraceToString

      public static String stackTraceToString(Throwable e)
      Stack trace to string.
      Parameters:
      e - the e
      Returns:
      the string
    • extractNumber

      public static String extractNumber(String input)
      Extract number.
      Parameters:
      input - the input
      Returns:
      the string
    • removeHtmlTags

      public static String removeHtmlTags(String input)
      Removes the html tags.
      Parameters:
      input - the input
      Returns:
      the string
    • shortenWord

      public static String shortenWord(String name, int charCount)
      Shorten word.
      Parameters:
      name - the name
      charCount - the char count
      Returns:
      the string
    • isVowel

      public static boolean isVowel(char c)
      Checks if is vowel.
      Parameters:
      c - the c
      Returns:
      true, if is vowel
    • getClasses

      public static String getClasses(Class<?> aClass)
      Gets the classes.
      Parameters:
      aClass - the a class
      Returns:
      the classes
    • addToErrors

      public static ArrayList<String> addToErrors(ArrayList<String> errors, String errorMessage)
      Adds the to errors.
      Parameters:
      errors - the errors
      errorMessage - the error message
      Returns:
      the array list
    • deleteDirectory

      public static boolean deleteDirectory(String path)
      Delete directory.
      Parameters:
      path - the path
      Returns:
      true, if successful
    • isInStringArray

      public static boolean isInStringArray(String value, ArrayList<String> array)
      Checks if is in string array.
      Parameters:
      value - the value
      array - the array
      Returns:
      true, if is in string array
    • isInByteArray

      public static boolean isInByteArray(byte value, byte[] array)
      Checks if is in byte array.
      Parameters:
      value - the value
      array - the array
      Returns:
      true, if is in byte array
    • fixRenamedActionParams

      public static String fixRenamedActionParams(String content)
      Fix renamed action params.
      Parameters:
      content - the content
      Returns:
      the string
    • renameActionParams

      public static String renameActionParams(String content)
      Rename action params.
      Parameters:
      content - the content
      Returns:
      the string
    • fixHeikenAshi

      public static String fixHeikenAshi(String content)
      Fix heiken ashi.
      Parameters:
      content - the content
      Returns:
      the string
    • fixExitmethodAttributes

      public static void fixExitmethodAttributes(org.jdom2.Element elStrategy)
      Fix exitmethod attributes.
      Parameters:
      elStrategy - the el strategy
    • loadLines

      public static ArrayList<String[]> loadLines(String filePath, String separator) throws Exception
      Load lines.
      Parameters:
      filePath - the file path
      separator - the separator
      Returns:
      the array list
      Throws:
      Exception - the exception
    • pctToDouble

      public static double pctToDouble(int value)
      Pct to double.
      Parameters:
      value - the value
      Returns:
      the double
    • httpUploadFile

      public static String httpUploadFile(String requestURL, String data, Map<String,​String> headers, String attachmentName, String fileName, byte[] fileContent) throws Exception
      Http upload file.
      Parameters:
      requestURL - the request URL
      data - the data
      headers - the headers
      attachmentName - the attachment name
      fileName - the file name
      fileContent - the file content
      Returns:
      the string
      Throws:
      Exception - the exception
    • httpPost

      public static String httpPost(String url, String data, Map<String,​String> headers) throws Exception
      Http post.
      Parameters:
      url - the url
      data - the data
      headers - the headers
      Returns:
      the string
      Throws:
      Exception - the exception
    • httpsPost

      public static String httpsPost(String url, List<org.apache.http.NameValuePair> data, boolean throwInvalidStatusCode) throws Exception
      Https post.
      Parameters:
      url - the url
      data - the data
      throwInvalidStatusCode - the throw invalid status code
      Returns:
      the string
      Throws:
      Exception - the exception
    • httpGet

      public static String httpGet(String url) throws Exception
      Http get.
      Parameters:
      url - the url
      Returns:
      the string
      Throws:
      Exception - the exception
    • httpGet

      public static String httpGet(String url, int timeout) throws Exception
      Http get.
      Parameters:
      url - the url
      timeout - the timeout in ms
      Returns:
      the string
      Throws:
      Exception - the exception
    • doublesAreEqual

      public static boolean doublesAreEqual(double value1, double value2)
      Doubles are equal.
      Parameters:
      value1 - the value 1
      value2 - the value 2
      Returns:
      true, if successful
    • convertIntTimetoHHMM

      public static String convertIntTimetoHHMM(int time)
      Convert int timeto HHMM.
      Parameters:
      time - the time
      Returns:
      the string
    • formatBytesToHumanFormat

      public static String formatBytesToHumanFormat(long allocatedSize)
      Format bytes to human format.
      Parameters:
      allocatedSize - the allocated size
      Returns:
      the string
    • formatDuration

      public static String formatDuration(long milliseconds)
      Format duration.
      Parameters:
      milliseconds - the milliseconds
      Returns:
      the string
    • levenshteinDistance

      public static int levenshteinDistance(CharSequence lhs, CharSequence rhs)
      Levenshtein distance.
      Parameters:
      lhs - the lhs
      rhs - the rhs
      Returns:
      the int
    • cloneHashMap

      public static HashMap<String,​Object> cloneHashMap(HashMap<String,​Object> map)
      Clone hash map.
      Parameters:
      map - the map
      Returns:
      the hash map
    • cloneValuesMapOld

      public static HashMap<Integer,​Object> cloneValuesMapOld(HashMap<Integer,​Object> map)
      Clone values map old.
      Parameters:
      map - the map
      Returns:
      the hash map
    • cloneValuesMap

      public static it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap<Object> cloneValuesMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap<Object> map)
      Clone values map.
      Parameters:
      map - the map
      Returns:
      the int 2 object open hash map
    • cloneArrayList

      public static ArrayList cloneArrayList(ArrayList list)
      Clone array list.
      Parameters:
      list - the list
      Returns:
      the array list
    • cloneObject

      public static Object cloneObject(Object value)
      Clone object.
      Parameters:
      value - the value
      Returns:
      the object
    • optimizeLastSettings

      public static String optimizeLastSettings(String source)
      Optimize last settings.
      Parameters:
      source - the source
      Returns:
      the string
    • waitForKey

      public static void waitForKey(String message)
      Wait for key.
      Parameters:
      message - the message
    • toJSONArray

      public static org.json.JSONArray toJSONArray(String str)
      To JSON array.
      Parameters:
      str - the str
      Returns:
      the JSON array
    • writeUTF

      public static void writeUTF(ObjectOutput dos, String string) throws IOException
      Write UTF.
      Parameters:
      dos - the dos
      string - the string
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • readUTFIntern

      public static String readUTFIntern(ObjectInput dis) throws IOException
      Read UTF intern.
      Parameters:
      dis - the dis
      Returns:
      the string
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • readUTF

      public static String readUTF(ObjectInput dis) throws IOException
      Read UTF.
      Parameters:
      dis - the dis
      Returns:
      the string
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • betterHashCode

      public static int betterHashCode(String input)
      Better hash code.
      Parameters:
      input - the input
      Returns:
      the int
    • longHashCode

      public static long longHashCode(String input)
      Long hash code.
      Parameters:
      input - the input
      Returns:
      the long
    • betterHashNumbers

      public static int betterHashNumbers(int... params)
      Better hash numbers.
      Parameters:
      params - the params
      Returns:
      the int
    • intsHash

      public static int intsHash(int... params)
    • doublesHash

      public static int doublesHash(double... params)
    • booleansHash

      public static int booleansHash(boolean... params)
    • xmlPrettyFormat

      public static String xmlPrettyFormat(String uglyXml) throws Exception
      Xml pretty format.
      Parameters:
      uglyXml - the ugly xml
      Returns:
      the string
      Throws:
      Exception - the exception
    • fixAllowedRange

      public static int fixAllowedRange(int value, int min, int max, int defaultValue)
      Fix allowed range.
      Parameters:
      value - the value
      min - the min
      max - the max
      defaultValue - the default value
      Returns:
      the int
    • correctClassName

      public static String correctClassName(String name)
      Correct class name
      Parameters:
      name -
      Returns:
      the string
      Throws:
      Exception - the exception
    • getFileCrc

      public static long getFileCrc(File file) throws Exception
      Parameters:
      file -
      Returns:
      Throws:
      Exception