Package org.pgcodekeeper.core.utils
Class Utils
java.lang.Object
org.pgcodekeeper.core.utils.Utils
Utility class providing common helper methods for various operations including
serialization, XML parsing, schema validation, and string manipulation.
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringcheckedEncodeUtf8(String string) URL-encodes string using UTF-8 encoding.static StringcheckNewLines(String text, boolean isKeepNewlines) Processes newlines in a string according to the specified setting.static booleanendsWithIgnoreCase(String str, String suffix) Checks if string ends with suffix (case-insensitive).static StringgetErrorSubstring(String s, int pos) Gets error substring from specified position with default length.static StringgetErrorSubstring(String s, int pos, int len) Gets error substring from specified position with custom length.static byte[]Computes hash of string using specified algorithm.static Randomstatic StringGets the implementation version of the core package.static StringReturns hexadecimal string representation of hash.loadDatabases(ILoader oldDbLoader, ILoader newDbLoader, IMonitor subMonitor, boolean isParallelLoad) Loads databases from loadersstatic StringReturns MD5 hash of string as hexadecimal.static StringQuotes string with single quotes for SQL.static DocumentParses XML content from a Reader with secure configuration.static voidserialize(String path, Serializable object) Serializes an object to a file at the specified path.static voidserialize(Path path, Serializable object) Serializes an object to a file at the specified path.static booleansetLikeEquals(Collection<?> c1, Collection<?> c2) Compares 2 collections for equality unorderedly as if they wereSets.
Does not eliminate duplicate elements as sets do and counts them instead.static StringReturns SHA-256 hash of string as hexadecimal.static <T> Iterable<T>streamIterator(Stream<T> stream) Casts a Stream into Iterable.static booleanstringContainsAnyItem(String input, List<String> items) Checks if a string contains any of the items in a list.static voidwriteXml(Document xml, boolean encrypt, StreamResult stream) Writes an XML document to the output stream
-
Method Details
-
serialize
Serializes an object to a file at the specified path.- Parameters:
path- the string path to the output fileobject- the serializable object to write
-
serialize
Serializes an object to a file at the specified path.- Parameters:
path- - full path to file where the serialized object will beobject- - 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 createdSAXException- if XML parsing failsIOException- 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- -Documentwitch store xml documentencrypt- - if true, enables secure XML processingstream- - The output stream to write the XML document to- Throws:
TransformerException- if an error occurred during the XML transformation
-
quoteString
Quotes string with single quotes for SQL.- Parameters:
s- the string to quote- Returns:
- quoted string
-
stringContainsAnyItem
Checks if a string contains any of the items in a list.- Parameters:
input- the string to searchitems- the list of strings to search for- Returns:
- true if any item is found in the input string, false otherwise
-
checkNewLines
Processes newlines in a string according to the specified setting.- Parameters:
text- the input textisKeepNewlines- if true, preserves newlines; if false, removes carriage returns- Returns:
- the processed string
-
getVersion
Gets the implementation version of the core package.- Returns:
- the version string, or "unknown" if not available
-
streamIterator
Casts a Stream into Iterable. Stream is consumed after Iterable.iterator() is called. -
endsWithIgnoreCase
Checks if string ends with suffix (case-insensitive).- Parameters:
str- the string to checksuffix- the suffix to look for- Returns:
- true if string ends with suffix (case-insensitive), false otherwise
-
getHash
Computes hash of string using specified algorithm.- Parameters:
s- the string to hashinstance- the hash algorithm to use- Returns:
- the hash bytes
- Throws:
NoSuchAlgorithmException- if algorithm is not available
-
hash
Returns hexadecimal string representation of hash.- Parameters:
s- the string to hashinstance- the hash algorithm to use- Returns:
- hexadecimal hash string
-
md5
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
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
URL-encodes string using UTF-8 encoding.- Parameters:
string- the string to encode- Returns:
- URL-encoded string
-
getErrorSubstring
Gets error substring from specified position with default length.- Parameters:
s- the string to extract frompos- the starting position- Returns:
- substring of error text
-
getErrorSubstring
Gets error substring from specified position with custom length.- Parameters:
s- the string to extract frompos- the starting positionlen- the maximum length of substring- Returns:
- substring of error text
-
setLikeEquals
Compares 2 collections for equality unorderedly as if they wereSets.
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 aList.containsAll()O(N^2) call here. In general, duplicate elimination is an undesired side-effect of comparison usingSets, 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 collectionc2- second collection- Returns:
- true if collections contain same elements in any order, false otherwise
-
loadDatabases
public static Pair<IDatabase,IDatabase> loadDatabases(ILoader oldDbLoader, ILoader newDbLoader, IMonitor subMonitor, boolean isParallelLoad) throws IOException, InterruptedException Loads databases from loaders- Parameters:
oldDbLoader- loader for the old databasenewDbLoader- loader for the new databasesubMonitor- the progress monitor for tracking operation progressisParallelLoad- flag indicating whether to load databases in parallel mode- Returns:
- pair of databases (old and new)
- Throws:
IOException- if I/O operations failInterruptedException- if the thread is interrupted during the operation
-
getRandom
-