Groovy Examples
Render an aligned plain-text table in Groovy
Measures column widths and pads each cell so rows line up in a plain-text table with a separator rule.
Base64 encode and decode strings in Groovy
Converts text to Base64 and back using UTF-8, turning malformed input into a clear IllegalArgumentException.
Validate form input fields with clear Groovy errors
Checks required, email, and age fields, collecting field-level messages instead of stopping at the first problem.
Encode and parse URL query strings in Groovy
Builds URL-encoded query strings from maps and parses them back, keeping encoded separators inside values intact.
Groovy JSON parsing with readable error messages
Wraps JsonSlurper so empty or malformed input fails with a short, specific message while keeping the original cause.
Groovy fixed worker pool with collected errors
Submits tasks to a fixed thread pool, then prints each result or failure without letting one job cancel the rest.
Groovy HTTP GET JSON request with timeouts
Sends a GET with connect and request timeouts, rejects non-200 responses, and parses the body as JSON.
Validate environment variables with Groovy defaults
Reads settings from environment variables with sane defaults, then reports all validation failures in one message.
Thread-safe token bucket limiter in Groovy
Refills tokens at a steady rate up to a fixed capacity, allowing short bursts while limiting the long-run rate.
Groovy LRU cache backed by LinkedHashMap
Extends LinkedHashMap in access order so the least recently used entry is evicted once the size limit is passed.
Split an iterable into fixed-size Groovy batches
Splits any iterable into ordered lists of a given size and keeps the final short batch instead of dropping it.
Multi-key Groovy list sort with null handling
Sorts a list of maps by score descending and age ascending, with explicit null placement and a name tie-break.
Read a text file line by line in Groovy
Streams a text file line by line with a closing reader, skipping blank and comment lines while counting stats.
Groovy deep clone and recursive map merge
Copies nested maps and lists so the source stays untouched, then merges two maps recursively key by key.
Groovy retry wrapper with capped exponential backoff
Runs a closure up to a fixed number of attempts with capped exponential backoff and jitter between tries.