{"id":18962120,"url":"https://github.com/zandero/utils","last_synced_at":"2025-08-02T20:32:55.132Z","repository":{"id":57730010,"uuid":"83405318","full_name":"zandero/utils","owner":"zandero","description":"Common utilities","archived":false,"fork":false,"pushed_at":"2022-04-03T11:37:46.000Z","size":140,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-31T15:37:11.010Z","etag":null,"topics":["common","java","java-8","utilities"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zandero.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-28T07:55:15.000Z","updated_at":"2022-04-03T11:37:49.000Z","dependencies_parsed_at":"2022-09-08T00:21:11.818Z","dependency_job_id":null,"html_url":"https://github.com/zandero/utils","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/zandero/utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zandero%2Futils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zandero%2Futils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zandero%2Futils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zandero%2Futils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zandero","download_url":"https://codeload.github.com/zandero/utils/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zandero%2Futils/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268448362,"owners_count":24252019,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-02T02:00:12.353Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["common","java","java-8","utilities"],"created_at":"2024-11-08T14:15:23.912Z","updated_at":"2025-08-02T20:32:55.088Z","avatar_url":"https://github.com/zandero.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# utils\nCollection of commonly used utilities (pure Java - without external dependencies)\n\n## Setup\n```xml\n\u003cdependency\u003e      \n     \u003cgroupId\u003ecom.zandero\u003c/groupId\u003e      \n     \u003cartifactId\u003eutils\u003c/artifactId\u003e      \n     \u003cversion\u003e1.2.10\u003c/version\u003e      \n\u003c/dependency\u003e\n```\n\n## Assert\nAssertion utilities to check input parameters and throw IllegalArgumentException in case arguments are invalid.  \n\n### true / false test\n```java\n    Assert.isTrue(text.length \u003e= 15, \"Text is to short, must be at least 15 characters long!\");\n\n    Assert.isFalse(text.length \u003c 15, \"Text is to long, must be less than 15 characters long!\");\n```\n    \n### null / not null test\n```java\n    Assert.isNull(value, \"Value was given, but null was expected!\");\n    \n    Assert.notNull(value, \"Value expected, but null was given!\");\n```\n\n### String null / empty tests\n```java\n    Assert.notNullOrEmptyTrimmed(value, \"Expected value but was not given!\");\n```\n\n### Collection, array, set, list ... tests\n```java\n    Assert.notNullOrEmpty(array, \"Expected array but null or empty provided!\");\n    Assert.notNullOrEmpty(list, \"Expected list but null or empty provided!\");\n    Assert.notNullOrEmpty(set, \"Expected set but null or empty provided!\");\n    Assert.notNullOrEmpty(map, \"Expected map but null or empty provided!\");\n    \n    Assert.isNullOrEmpty(array, \"Expected empty array!\");\n    Assert.isNullOrEmpty(list, \"Expected empty list!\");\n    Assert.isNullOrEmpty(set, \"Expected empty set!\");\n    Assert.isNullOrEmpty(map, \"Expected empty map!\");\n```\n\n\n## Date time\nTime conversion between long, Date and String\n\n* Date time formatting, \n* Timezone support\n* Some handy functions to get the start/end of a period \n\n### Utils\n```java\n    // return UTC initialized instance of Calendar\n    DateTimeUtils.getCalendar(); \n    \n    DateTimeUtils.getCalendar(long time); // returns UTC initialized instance of Calendar with set time provided in milliseconds \n```\n\n### Date time formatting\n```java\n    // formats time to yyyy-MM-dd HH:mm:ss Z\n    DateTimeUtils.formatDateTime(long time); \n    \n    // formats time to yyyy-MM-dd HH:mm:ss\n    DateTimeUtils.formatDateTimeShort(long time); \n    \n    // formats time to yyyy-MM-dd\n    DateTimeUtils.formatDate(long time); \n    \n    // format time with custom format or to yyyy-MM-dd HH:mm:ss Z if format not provided\n    DateTimeUtils.format(long time, SimpleDateFormat format); \n```\n\n### Timezone helpers\n```java\n    // Returns time for given time zone (-12 / +12)\n    DateTimeUtils.getTimezoneTime(long time, int timezone);\n\n    // Converts local hour back to UTC hour\n    DateTimeUtils.getUtcHour(int hour, int timezone);\n    \n    // Converts UTC hour to local hour\n    DateTimeUtils.getTimezoneHour(int hour, int timezone);\n```\n\n### Conversions\n```java\n    // Converts timestamp into OffsetDatetime\n    DateTimeUtils.toOffsetDateTime(long timestamp);\n    \n    // Converts offset date time to long timestamp\n    DateTimeUtils.fromOffsetDateTime(OffsetDateTime timestamp);\n```\n   \n```java\n    // Gets first millisecond of first day in month for given time    \n    DateTimeUtils.getMonthStart(long time);\n    \n    // Returns last millisecond of last day in month \n    DateTimeUtils.getMonthEnd(long time);\n    \n    // Returns day in month, where first day of month == 1\n    DateTimeUtils.getDayInMonth(long time);\n    \n    // Gets first millisecond of first day in week for given time\n    DateTimeUtils.getWeekStart(long time);\n    \n    // Returns last millisecond of last day in week for given time\n    DateTimeUtils.getWeekEnd(long time);\n    \n    // Returns number of day in week, where monday is the first day of the week (monday = 1, tuesday = 2 ..., sunday = 7)\n    DateTimeUtils.getDayInWeek(long time);\n    \n    // Check if it is a weekend day (Saturday or Sunday)\n    DateTimeUtils.isWeekend(long time);\n    \n    // Removes time component from date time\n    DateTimeUtils.getDateOnly(long time);\n    \n    // Convert any given timestamp string to long trying a list of provided formats\n    DateTimeUtils.getTimestamp(String value, SimpleDateFormat[] formats);\n```\n\n## Dir \nDirectory, path and file utilities\n \n```java\n    // Gets file extension: someFile.zip -\u003e zip\n    DirUtils.getFileExtension(String file);\n\n    // Lists all files in given directory recursive (with option to filter by extension)\n    DirUtils.getAllFilesRecursive(File rootDir, String fileExtension);\n    DirUtils.getAllFilesRecursive(File rootDir, String[] fileExtensions);\n    \n    // Lists all files in single directory, (with option to filter by extension)\n    DirUtils.getAllFilesInDir(File rootDir, String fileExtension)\n    DirUtils.getAllFilesInDir(File rootDir, String[] fileExtensions);\n    \n    // Calculates size of all files in given directory and it's sub-directories.\n    DirUtils.dirSize(File directory);\n```\n\n## Equality utils\n* NPE safe checks to compare two objects of same type: Long, Integer ... \n\n```java\n    // compare integers\n    Integer one = null;\n    Integer two = 3;\n\n    EqualsUtils.equals(one, two);\n    \n    // compare longs\n    Integer longOne = null;\n    Integer longTwo = 3;\n    \n    EqualsUtils.equals(longOne, longTwo);\n    \n    // compare strings (case sensitive)\n    EqualsUtils.equals(\"Some string\", null);\n    \n    // Compares if two maps hold the same set of keys and same values\n    // comparison of values utilizes the equals call, so make sure equals is implemented \n    // position of key is not considered (like in LinkedHashMap)\n    Map\u003cString, T\u003e one;\n    Map\u003cString, T\u003e two;\n    EqualsUtils.equals(mapOne, mapTwo);\n```\n\n## JUnit probe\nChecks if code runs inside a JUnit test or not\n\n```java\n    // Checks if code is run inside a unit test\n    JUnitProbe.isUnitTest();\n```\n\n## KeyGenerator\nGenerates random keys of given length.\n\n```java\n    // Generates random string from A-Z, a-z, 0-9 set of chars and numbers\n    // with lenght from 1-100\n    KeyGenerator.generateString(int length);\n    \n    // Generates non negative long key of maximum length 18\n    KeyGenerator.generateLong(int length);\n```\n\n## Maps\nMap helpers and manipulation utils\n\n### Merging two maps\n```java\n    Map outputMap = MapUtils.mergeMaps(firatMap, mergeFunction, otherMap);\n    \n    // Example\n    Map\u003cString, Long\u003e m1 = new HashMap\u003c\u003e();\n    m1.put(\"A\", 10L);\n    m1.put(\"B\", 20L);\n    \n    Map\u003cString, Long\u003e m2 = new HashMap\u003c\u003e();\n    m2.put(\"C\", 2L);\n    m2.put(\"D\", 3L);\n    m2.put(\"E\", 1L);\n   \n    // sum those two maps together\n    Map\u003cString, Long\u003e result = MapUtils.mergeMaps(Stream.of(m1, m2), Long::sum, HashMap::new);\n```\n\n### Sorting \n```java\n    // sort map with given comparator ... \n    LinkedHashMap sorted = sort(map, comparator);\n\n    // example\n    LinkedHashMap sortedMap = MapUtils.sort(map, Comparator.comparing(Map.Entry::getValue));\n```\n\n### Compare maps\n```java\n    // compare maps by keys\n    MapUtils.equals(mapOne, mapTwo);\n\t\n\t// compare maps by keys and values \n    MapUtils.equals(mapOne, mapTwo, true);\n```\n\n## Resources\n\n### Loading of resource files\n\n```java\n    // Loads resource into string\n    String output = ResourceUtils.getResourceAsString(String resourceFile, Class clazz);\n\n     // Loads resource as a set of Strings, where each word is added to the set\n     Set\u003cString\u003e output = ResourceUtils.getResourceWords(file, clazz);\n     \n     // Get resource absolute path\n     String output = ResourceUtils.getResourceAbsolutePath(String resource, Class clazz);\n```\n\n### Reading streams and files into strings\n ```java\n     // Loads resource to String \n     String output = ResourceUtils.readFileToString(file);\n     \n     // Get resource last modified time stamp\n     Long time = ResourceUtils.getLastModifiedTime(file);\n     \n     // Load input stream into string\n     String output = ResourceUtils.getString(inputStream);\n     \n     // Load input stream into string\n     String output = ResourceUtils.getString(inputStream, encoding);\n     \n     // Load input stream into byte array\n     byte[] output = ResourceUtils.getBytes(InputStream is);\n ```\n\n## Strings\nVarious handy string utilities.\n\n### NPE safe String comparison \n\n```java\n    // NPE safe compare of two strings (case sensitive)\n    StringUtils.equals(original, compare);\n    \n    // NPE safe compare of two strings - case sensitive or insensitive\n    StringUtils.equals(original, compare, ignoreCase);\n```\n\n```java\n    // NPE safe compare same as String.compare()\n    int result = StringUtils.compare(one, two);\n```\n\n### Null and empty checks\n\n```java\n    // Checks if string is null or empty == \"\"\n    StringUtils.isNullOrEmpty(value);\n    \n    // Checks if string is null, empty or contains only spaces\n    StringUtils.isNullOrEmptyTrimmed(value);\n```\n\n### String trimming\n\n```java\n    // NPE safe String.trim()\n    StringUtils.trim(String value);\n    \n    // Trim with inner double space trim\n    StringUtils.trimDoubleSpaces(text);\n    \n    // Complete trim of all spaces\n    StringUtils.trimInner(text);\n    \n    // Trims only end of text\n    StringUtils.trimEnd(text);\n    \n    // Trims only start of text\n    StringUtils.trimStart(text);\n    \n    // Trims specific text only\n    StringUtils.trimAll(text, toBeTrimmed);\n    \n    // Trims to null if empty\n    StringUtils.trimToNull(text);\n```\n\n### String transformation\n```java\n    // Capitalizes first found character in given string\n    // \"123 abc\" -\u003e \"123 Abc\"\n    String TEXT = StringUtils.capitalize(text);\n    \n    // Removes punctuation from text\n    // \"test!\" -\u003e \"test\"\n    StringUtils.removePunctuation(text);\n    \n    // Removes all multiple-consecutive whitespace characters (space, tab, newline) and replaces them with single space.\n    // Also removes leading and trailing spaces.\n    Stirng out = StringUtils.sanitizeWhitespace(text)\n    byte[] out = sanitizeWhitespace(byte[] input);\n    \n    // Reduces text to max given size preserving words\n    StringUtils.trimTextDown(text, maxWidth);\n    \n    // Reduces text size to a given size preserving words with appendix\n    StringUtils.trimTextDown(text, widthMinusAppend, append)\n    \n    // Simple enumeration: first, second, third ... 5th, 6th .. etc for given number\n    StringUtils.enumerate(number);\n    \n    // Removes double quotes if any are present (in begging and end)\n    StringUtils.unQuote(text);\n```\n\n### String joining\n```java\n    // Joins list of string items to a single string, where items are separated\n    // with a defined separator. Limiting number of included items. \n    StringUtils.join(List\u003c?\u003e list, separator);\n    StringUtils.join(List\u003c?\u003e list, separator, includeMaxLimit);\n    \n    // Joins array of objects to a single string, where items are separated\n    // with a defined separator. Limiting number of included items.\n    StringUtils.join(Object[] args, separator);\n    StringUtils.join(Object[] args, separator, includeMaxLimit);\n    \n    // Joins set of objects to a single string, where items are separated\n    // with a defined separator.\n    StringUtils.join(Set\u003c?\u003e items, separator);\n    StringUtils.join(Set\u003c?\u003e items, separator, includeMaxLimit);\n    \n    // Joins map of items to a single string, where items are separated with a defined separator.\n    StringUtils.join(Map\u003cString, String\u003e map, separator);\n```\n\n### Word extraction\n\n```java\n    // Extracts words from text removing non alpha characters\n    List\u003cString\u003e words = StringUtils.getWords(text);\n    \n    // Converts text to list of characters\n    List\u003cString\u003e chars = asListOfChars(text);\n    \n    // Checks if given string is a single word (doesn't accepts words with \"-\" as a single word!)\n    isWord(word);\n```\n\n### Search by relevance\n```java\n    // Calculates matching relevance between given string and search expression\n    // -1 not relevant, 0..N - where lower values represents more relevant results\n    relevance(text, search); \n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzandero%2Futils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzandero%2Futils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzandero%2Futils/lists"}