Class Utils

java.lang.Object
org.pgcodekeeper.core.Utils

public final class Utils extends Object
Utility class providing common helper methods for various operations including serialization, XML parsing, schema validation, and string manipulation.
  • Method Details

    • serialize

      public static void serialize(String path, Serializable object)
      Serializes an object to a file at the specified path.
      Parameters:
      path - the string path to the output file
      object - the serializable object to write
    • serialize

      public static void serialize(Path path, Serializable object)
      Serializes an object to a file at the specified path.
      Parameters:
      path - - full path to file where the serialized object will be
      object - - the object that you want to serialize
    • readXml

      public static Document readXml(Reader reader) throws ParserConfigurationException, SAXException, IOException
      Parses XML content from a Reader with secure configuration.
      Parameters:
      reader - the Reader containing XML content
      Returns:
      the parsed XML Document
      Throws:
      ParserConfigurationException - if a DocumentBuilder cannot be created
      SAXException - if XML parsing fails
      IOException - if an I/O error occurs
    • writeXml

      public static void writeXml(Document xml, boolean encrypt, StreamResult stream) throws TransformerException
      Writes an XML document to the output stream
      Parameters:
      xml - - Document witch store xml document
      encrypt - - if true, enables secure XML processing
      stream - - The output stream to write the XML document to
      Throws:
      TransformerException - if an error occurred during the XML transformation
    • isSystemSchema

      public static boolean isSystemSchema(String schema, DatabaseType dbType)
      Checks if a schema is a system schema for the given database type.
      Parameters:
      schema - the schema name to check
      dbType - the database type
      Returns:
      true if the schema is a system schema, false otherwise
    • getQuotedName

      public static String getQuotedName(String name, DatabaseType dbType)
      Returns a properly quoted name for the given database type.
      Parameters:
      name - the name to quote
      dbType - the database type
      Returns:
      the quoted name
    • getQuoter

      public static UnaryOperator<String> getQuoter(DatabaseType dbType)
      Returns a quoting function for the given database type.
      Parameters:
      dbType - the database type
      Returns:
      a function that quotes names appropriately for the database
    • isChSystemSchema

      public static boolean isChSystemSchema(String schema)
      Checks if a schema is a ClickHouse system schema.
      Parameters:
      schema - the schema name to check
      Returns:
      true if the schema is 'system' or 'information_schema', false otherwise
    • isPgSystemSchema

      public static boolean isPgSystemSchema(String schema)
      Checks if a schema is a PostgreSQL system schema.
      Parameters:
      schema - the schema name to check
      Returns:
      true if the schema is 'pg_catalog' or 'information_schema', false otherwise
    • isMsSystemSchema

      public static boolean isMsSystemSchema(String schema)
      Checks if a schema is a Microsoft SQL Server system schema.
      Parameters:
      schema - the schema name to check
      Returns:
      true if the schema is 'sys', false otherwise
    • stringContainsAnyItem

      public static boolean stringContainsAnyItem(String input, List<String> items)
      Checks if a string contains any of the items in a list.
      Parameters:
      input - the string to search
      items - the list of strings to search for
      Returns:
      true if any item is found in the input string, false otherwise
    • checkNewLines

      public static String checkNewLines(String text, boolean isKeepNewlines)
      Processes newlines in a string according to the specified setting.
      Parameters:
      text - the input text
      isKeepNewlines - if true, preserves newlines; if false, removes carriage returns
      Returns:
      the processed string
    • getVersion

      public static String getVersion()
      Gets the implementation version of the core package.
      Returns:
      the version string, or "unknown" if not available
    • streamIterator

      public static <T> Iterable<T> streamIterator(Stream<T> stream)
      Casts a Stream into Iterable. Stream is consumed after Iterable.iterator() is called.
    • endsWithIgnoreCase

      public static boolean endsWithIgnoreCase(String str, String suffix)
      Checks if string ends with suffix (case-insensitive).
      Parameters:
      str - the string to check
      suffix - the suffix to look for
      Returns:
      true if string ends with suffix (case-insensitive), false otherwise
    • getHash

      public static byte[] getHash(String s, String instance) throws NoSuchAlgorithmException
      Computes hash of string using specified algorithm.
      Parameters:
      s - the string to hash
      instance - the hash algorithm to use
      Returns:
      the hash bytes
      Throws:
      NoSuchAlgorithmException - if algorithm is not available
    • hash

      public static String hash(String s, String instance)
      Returns hexadecimal string representation of hash.
      Parameters:
      s - the string to hash
      instance - the hash algorithm to use
      Returns:
      hexadecimal hash string
    • md5

      public static String md5(String s)
      Returns MD5 hash of string as hexadecimal.
      Parameters:
      s - the string to hash
      Returns:
      lowercase hex MD5 for UTF-8 representation of given string
    • sha

      public static String sha(String s)
      Returns SHA-256 hash of string as hexadecimal.
      Parameters:
      s - the string to hash
      Returns:
      lowercase hex SHA-256 for UTF-8 representation of given string
    • checkedEncodeUtf8

      public static String checkedEncodeUtf8(String string)
      URL-encodes string using UTF-8 encoding.
      Parameters:
      string - the string to encode
      Returns:
      URL-encoded string
    • getErrorSubstring

      public static String getErrorSubstring(String s, int pos)
      Gets error substring from specified position with default length.
      Parameters:
      s - the string to extract from
      pos - the starting position
      Returns:
      substring of error text
    • getErrorSubstring

      public static String getErrorSubstring(String s, int pos, int len)
      Gets error substring from specified position with custom length.
      Parameters:
      s - the string to extract from
      pos - the starting position
      len - the maximum length of substring
      Returns:
      substring of error text
    • setLikeEquals

      public static boolean setLikeEquals(Collection<?> c1, Collection<?> c2)
      Compares 2 collections for equality unorderedly as if they were Sets.
      Does not eliminate duplicate elements as sets do and counts them instead. Thus it achieves complementarity with setLikeHashcode while not requiring it to eliminate duplicates, nor does it require a List.containsAll() O(N^2) call here. In general, duplicate elimination is an undesired side-effect of comparison using Sets, so this solution is overall better and only *slightly* slower.

      Performance: best case O(1), worst case O(N) + new HashMap (in case N1 == N2), assuming size() takes constant time.

      Parameters:
      c1 - first collection
      c2 - second collection
      Returns:
      true if collections contain same elements in any order, false otherwise
    • getRandom

      public static Random getRandom()