### ArrayFill and ArraySorter Examples Source: https://context7.com/apache/commons-lang/llms.txt Demonstrates ArrayFill for index-based array initialization with lambdas and ArraySorter for fluent in-place sorting. Also shows chaining ArrayFill and ArraySorter. ```java import org.apache.commons.lang3.ArrayFill; import org.apache.commons.lang3.ArraySorter; // ArrayFill — fill with index-based lambda String[] labels = ArrayFill.fill(new String[5], i -> "item_" + i); // ["item_0","item_1","item_2","item_3","item_4"] Integer[] squares = ArrayFill.fill(new Integer[5], i -> i * i); // [0, 1, 4, 9, 16] // ArrayFill with null-safe failable supplier String[] defaults = ArrayFill.fill(new String[3], () -> "default"); // ["default","default","default"] // ArraySorter — sorts and returns the same array (fluent) String[] words = ArraySorter.sort(new String[]{"banana", "apple", "cherry"}); // words == sorted in-place AND returned: ["apple","banana","cherry"] int[] nums = ArraySorter.sort(new int[]{5, 2, 8, 1, 9}); // [1,2,5,8,9] // Chain: fill then sort String[] sorted = ArraySorter.sort( ArrayFill.fill(new String[4], i -> String.valueOf((char)('d' - i))) ); // ["a","b","c","d"] ``` -------------------------------- ### Range Examples Source: https://context7.com/apache/commons-lang/llms.txt Illustrates creating and using generic inclusive ranges with methods like contains, fit, intersectionWith, and overlap detection. Includes typed ranges for primitives and custom comparators. ```java import org.apache.commons.lang3.Range; import org.apache.commons.lang3.IntegerRange; // Create ranges Range r = Range.of(1, 10); // [1..10] Range s = Range.of("a", "m"); // ["a".."m"] // Containment checks r.contains(5); // true r.contains(0); // false r.contains(10); // true (inclusive) // fit — clamp value to range bounds r.fit(15); // 10 (clamped to max) r.fit(-3); // 1 (clamped to min) r.fit(5); // 5 (within range) // Range intersection Range other = Range.of(5, 20); Range inter = r.intersectionWith(other); // [5..10] r.isOverlappedBy(other); // true // isAfter / isBefore / containsRange r.isAfterRange(Range.of(-5, 0)); // true r.containsRange(Range.of(3, 7)); // true // Typed ranges for primitives (IntegerRange, LongRange, DoubleRange) IntegerRange ir = IntegerRange.of(1, 100); ir.fit(150); // 100 ir.toIntStream().sum(); // 5050 // Custom comparator Range caseInsensitive = Range.of("A", "Z", String.CASE_INSENSITIVE_ORDER); caseInsensitive.contains("m"); // true ``` -------------------------------- ### Validate Examples Source: https://context7.com/apache/commons-lang/llms.txt Shows how to use Validate for guard-clause argument validation, throwing standard JDK exceptions with formatted messages. Covers null, empty, blank, state, range, index, and type checks. ```java import org.apache.commons.lang3.Validate; import java.util.List; // notNull — throws NullPointerException String name = Validate.notNull(inputName, "name must not be null"); // notEmpty — throws IllegalArgumentException for empty string/collection/array/map String s = Validate.notEmpty(input, "input must not be empty"); List list = Validate.notEmpty(items, "items must not be empty: %s", items); // notBlank — rejects null, empty, and whitespace-only strings String trimmed = Validate.notBlank(value, "value must not be blank"); // isTrue — throws IllegalArgumentException when condition is false Validate.isTrue(count > 0, "count must be positive, got: %d", count); Validate.isTrue(amount >= 0.0, "amount must be >= 0, got: %f", amount); // validState — throws IllegalStateException Validate.validState(connection.isOpen(), "connection must be open"); // inclusiveBetween / exclusiveBetween Validate.inclusiveBetween(0, 100, score, "score %d out of [0,100]", score); Validate.exclusiveBetween(0.0, 1.0, ratio, "ratio must be in (0,1)"); // noNullElements — null element in collection/array throws IllegalArgumentException Validate.noNullElements(tags, "tags must not contain null"); // validIndex — throws IndexOutOfBoundsException Validate.validIndex(list, index, "invalid index: %d", index); // isInstanceOf / isAssignableFrom Validate.isInstanceOf(String.class, obj, "expected String, got: %s", obj.getClass()); Validate.isAssignableFrom(Number.class, type); // With Supplier for lazy message construction (avoids String format overhead) Validate.isTrue(x > 0, () -> "x must be positive, got: " + x); ``` -------------------------------- ### SystemUtils Source: https://context7.com/apache/commons-lang/llms.txt Provides convenient access to system properties and runtime environment information as constants, initialized at class-load time. ```APIDOC ## SystemUtils — Runtime environment constants `SystemUtils` exposes system properties and pre-computed boolean flags for OS and Java version detection as public constants, all initialized at class-load time for zero-cost access. ```java import org.apache.commons.lang3.SystemUtils; // Java version detection SystemUtils.IS_JAVA_8; // true on Java 8 SystemUtils.IS_JAVA_11; // true on Java 11 SystemUtils.IS_JAVA_17; // true on Java 17 SystemUtils.IS_JAVA_21; // true on Java 21 SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_11); // programmatic check // OS detection SystemUtils.IS_OS_WINDOWS; // true on any Windows SystemUtils.IS_OS_WINDOWS_10; // true on Windows 10 SystemUtils.IS_OS_LINUX; // true on Linux SystemUtils.IS_OS_MAC; // true on macOS SystemUtils.IS_OS_MAC_OSX; // true on macOS X SystemUtils.IS_OS_UNIX; // true for Unix-like systems SystemUtils.IS_OS_ANDROID; // true on Android // Key system properties as pre-read String constants SystemUtils.JAVA_HOME; // e.g. "/usr/lib/jvm/java-17" SystemUtils.JAVA_VERSION; // "17.0.5" SystemUtils.OS_NAME; // "Linux" SystemUtils.OS_ARCH; // "amd64" SystemUtils.USER_HOME; // "/home/alice" SystemUtils.USER_DIR; // current working directory // Path variants (since 3.18.0) Path javaHome = SystemUtils.getJavaHomePath(); Path userHome = SystemUtils.getUserHomePath(); Path tmpDir = SystemUtils.getJavaIoTmpDirPath(); // Runtime environment checks RuntimeEnvironment.inContainer(); // true when running inside Docker/container // Java version object JavaVersion jv = SystemUtils.JAVA_SPECIFICATION_VERSION_AS_ENUM; jv.atLeast(JavaVersion.JAVA_11); // true on Java 11+ ``` ``` -------------------------------- ### System Environment Constants with SystemUtils Source: https://context7.com/apache/commons-lang/llms.txt Access system properties and OS/Java version detection flags. Constants are pre-initialized for zero-cost access. Path variants are available since 3.18.0. ```java import org.apache.commons.lang3.SystemUtils; // Java version detection SystemUtils.IS_JAVA_8; // true on Java 8 SystemUtils.IS_JAVA_11; // true on Java 11 SystemUtils.IS_JAVA_17; // true on Java 17 SystemUtils.IS_JAVA_21; // true on Java 21 SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_11); // programmatic check ``` ```java // OS detection SystemUtils.IS_OS_WINDOWS; SystemUtils.IS_OS_WINDOWS_10; SystemUtils.IS_OS_LINUX; SystemUtils.IS_OS_MAC; SystemUtils.IS_OS_MAC_OSX; SystemUtils.IS_OS_UNIX; SystemUtils.IS_OS_ANDROID; ``` ```java // Key system properties as pre-read String constants SystemUtils.JAVA_HOME; SystemUtils.JAVA_VERSION; SystemUtils.OS_NAME; SystemUtils.OS_ARCH; SystemUtils.USER_HOME; SystemUtils.USER_DIR; ``` ```java // Path variants (since 3.18.0) Path javaHome = SystemUtils.getJavaHomePath(); Path userHome = SystemUtils.getUserHomePath(); Path tmpDir = SystemUtils.getJavaIoTmpDirPath(); ``` ```java // Runtime environment checks RuntimeEnvironment.inContainer(); // true when running inside Docker/container ``` ```java // Java version object JavaVersion jv = SystemUtils.JAVA_SPECIFICATION_VERSION_AS_ENUM; jv.atLeast(JavaVersion.JAVA_11); // true on Java 11+ ``` -------------------------------- ### SystemUtils Constants Source: https://github.com/apache/commons-lang/blob/master/src/site/resources/release-notes/RELEASE-NOTES-2.0.txt Provides access to system properties and operating system information. ```APIDOC ## SystemUtils Constants ### Description Provides constants related to the Java runtime environment and operating system properties. ### Constants - `public static final String FILE_ENCODING`: The file encoding for the current JVM. - `public static final String JAVA_RUNTIME_NAME`: The name of the Java runtime. - `public static final String JAVA_RUNTIME_VERSION`: The version of the Java runtime. - `public static final String JAVA_VM_INFO`: Information about the Java Virtual Machine. - `public static final String USER_COUNTRY`: The country code of the current user. - `public static final String USER_LANGUAGE`: The language code of the current user. - `public static final float JAVA_VERSION_FLOAT`: The Java version as a float. - `public static final int JAVA_VERSION_INT`: The Java version as an integer. - `public static final boolean IS_OS_AIX`: True if the OS is AIX. - `public static final boolean IS_OS_HP_UX`: True if the OS is HP-UX. - `public static final boolean IS_OS_IRIX`: True if the OS is IRIX. - `public static final boolean IS_OS_LINUX`: True if the OS is Linux. - `public static final boolean IS_OS_MAC`: True if the OS is a Mac OS. - `public static final boolean IS_OS_MAC_OSX`: True if the OS is Mac OS X. - `public static final boolean IS_OS_OS2`: True if the OS is OS/2. - `public static final boolean IS_OS_SOLARIS`: True if the OS is Solaris. - `public static final boolean IS_OS_SUN_OS`: True if the OS is SunOS. - `public static final boolean IS_OS_WINDOWS`: True if the OS is Windows. - `public static final boolean IS_OS_WINDOWS_2000`: True if the OS is Windows 2000. - `public static final boolean IS_OS_WINDOWS_95`: True if the OS is Windows 95. - `public static final boolean IS_OS_WINDOWS_98`: True if the OS is Windows 98. - `public static final boolean IS_OS_WINDOWS_ME`: True if the OS is Windows ME. - `public static final boolean IS_OS_WINDOWS_NT`: True if the OS is Windows NT. - `public static final boolean IS_OS_WINDOWS_XP`: True if the OS is Windows XP. ### Methods - `public static boolean isJavaVersionAtLeast(int)`: Checks if the Java version is at least the specified integer value. ``` -------------------------------- ### Fluent toString() with ToStringBuilder Source: https://context7.com/apache/commons-lang/llms.txt Implement toString() using a fluent API. Supports various built-in styles and reflection. ```java import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.apache.commons.lang3.builder.ReflectionToStringBuilder; class Person { String name; int age; boolean active; Person(String name, int age, boolean active) { this.name = name; this.age = age; this.active = active; } @Override public String toString() { return new ToStringBuilder(this) .append("name", name) .append("age", age) .append("active", active) .toString(); // Person@7f54[name=Alice,age=30,active=true] } } // Different built-in styles Person p = new Person("Alice", 30, true); new ToStringBuilder(p, ToStringStyle.SHORT_PREFIX_STYLE) .append("name", p.name).append("age", p.age).toString(); // Person[name=Alice,age=30] new ToStringBuilder(p, ToStringStyle.JSON_STYLE) .append("name", p.name).append("age", p.age).toString(); // {"name":"Alice","age":30} new ToStringBuilder(p, ToStringStyle.NO_CLASS_NAME_STYLE) .append("name", p.name).append("age", p.age).toString(); // [name=Alice,age=30] // Reflection-based — automatically uses all non-static, non-transient fields ReflectionToStringBuilder.toString(p); // Person@7f54[name=Alice,age=30,active=true] // Exclude fields with @ToStringExclude annotation or by name list ReflectionToStringBuilder.toStringExclude(p, "active"); // Person@7f54[name=Alice,age=30] ``` -------------------------------- ### StandardToStringStyle Configuration Source: https://github.com/apache/commons-lang/blob/master/src/site/resources/release-notes/RELEASE-NOTES-2.0.txt Methods for configuring the behavior of `StandardToStringStyle`. ```APIDOC ## StandardToStringStyle Methods ### Description Methods to control the output format of `ToStringBuilder` when using `StandardToStringStyle`. ### Setters - `public void setUseShortClassName(boolean)` - `public void setFieldSeparatorAtStart(boolean)` - `public void setFieldSeparatorAtEnd(boolean)` ### Getters - `public boolean isUseShortClassName()` - `public boolean isFieldSeparatorAtStart()` - `public boolean isFieldSeparatorAtEnd()` ``` -------------------------------- ### SerializationUtils Source: https://context7.com/apache/commons-lang/llms.txt Provides deep cloning via serialization, serialize-to-byte-array, and deserialize-from-byte-array, all with proper stream cleanup and wrapping of checked exceptions. ```APIDOC ## SerializationUtils ### Description Provides deep cloning via serialization, serialize-to-byte-array, and deserialize-from-byte-array, all with proper stream cleanup and wrapping of checked exceptions. ### Methods #### `clone(Object)` Performs a deep clone of an object using serialization. #### `serialize(Object)` Serializes an object to a byte array. #### `deserialize(byte[])` Deserializes an object from a byte array. #### `deserialize(byte[], ClassLoader)` Deserializes an object from a byte array using a custom ClassLoader. #### `roundtrip(Object)` Performs a roundtrip serialization and deserialization of an object. ``` -------------------------------- ### org.apache.commons.lang.StringUtils Additions and Removals Source: https://github.com/apache/commons-lang/blob/master/src/site/resources/release-notes/RELEASE-NOTES-2.0.txt Highlights new and removed methods in the StringUtils class. ```APIDOC ## Class org.apache.commons.lang.StringUtils ### Description This section details additions and removals of methods in the `StringUtils` class. ### New Methods - `public static java.lang.String trimToNull(java.lang.String)` - `public static java.lang.String trimToEmpty(java.lang.String)` - `public static java.lang.String strip(java.lang.String)` - `public static java.lang.String stripToNull(java.lang.String)` - `public static java.lang.String stripToEmpty(java.lang.String)` - `public static java.lang.String strip(java.lang.String, java.lang.String)` - `public static java.lang.String stripStart(java.lang.String, java.lang.String)` - `public static java.lang.String stripEnd(java.lang.String, java.lang.String)` - `public static java.lang.String stripAll(java.lang.String[])[]` - `public static java.lang.String stripAll(java.lang.String[], java.lang.String)[]` - `public static int indexOf(java.lang.String, char)` - `public static int indexOf(java.lang.String, char, int)` - `public static int indexOf(java.lang.String, java.lang.String)` - `public static int indexOf(java.lang.String, java.lang.String, int)` - `public static int lastIndexOf(java.lang.String, char)` - `public static int lastIndexOf(java.lang.String, char, int)` - `public static int lastIndexOf(java.lang.String, java.lang.String)` - `public static int lastIndexOf(java.lang.String, java.lang.String, int)` - `public static boolean contains(java.lang.String, char)` - `public static boolean contains(java.lang.String, java.lang.String)` - `public static int indexOfAny(java.lang.String, char[])` - `public static int indexOfAny(java.lang.String, java.lang.String)` - `public static int indexOfAnyBut(java.lang.String, char[])` - `public static int indexOfAnyBut(java.lang.String, java.lang.String)` - `public static boolean containsOnly(java.lang.String, char[])` - `public static boolean containsOnly(java.lang.String, java.lang.String)` - `public static boolean containsNone(java.lang.String, char[])` - `public static boolean containsNone(java.lang.String, java.lang.String)` - `public static java.lang.String substringBefore(java.lang.String, java.lang.String)` - `public static java.lang.String substringAfter(java.lang.String, java.lang.String)` - `public static java.lang.String substringBeforeLast(java.lang.String, java.lang.String)` ### Removed Methods - `public static java.lang.String deleteSpaces(java.lang.String)` - `public static java.lang.String deleteWhitespace(java.lang.String)` ``` -------------------------------- ### ObjectUtils Methods Source: https://github.com/apache/commons-lang/blob/master/src/site/resources/lang2-lang3-clirr-report.html Provides utility methods for operating on Objects. ```APIDOC ## firstNonNull ### Description Returns the first non-null element from an array of objects. ### Method `public java.lang.Object firstNonNull(java.lang.Object[])` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) - **return value** (java.lang.Object) - The first non-null object in the array, or null if all elements are null. #### Response Example None ``` -------------------------------- ### Deep Cloning and Serialization with SerializationUtils Source: https://context7.com/apache/commons-lang/llms.txt Provides deep cloning via serialization and methods to serialize objects to byte arrays and deserialize them back. Useful for creating independent copies of objects and for data persistence or transfer. ```java import org.apache.commons.lang3.SerializationUtils; import java.io.Serializable; import java.util.ArrayList; import java.util.List; // Deep clone — completely independent copy via serialize + deserialize List original = new ArrayList<>(List.of("a", "b", "c")); List clone = SerializationUtils.clone(original); clone.add("d"); original.size(); // 3 — unaffected // serialize to byte[] and back byte[] bytes = SerializationUtils.serialize(original); // byte[] List restored = SerializationUtils.deserialize(bytes); // List // roundtrip — serialize then immediately deserialize (useful in tests) List rt = SerializationUtils.roundtrip(original); // rt is an equal but independent copy // Custom ClassLoader for deserialization (useful in OSGi / web apps) ClassLoader cl = Thread.currentThread().getContextClassLoader(); List withCL = SerializationUtils.deserialize(bytes, cl); ``` -------------------------------- ### SerializationUtils Constructor Source: https://github.com/apache/commons-lang/blob/master/src/site/resources/release-notes/RELEASE-NOTES-2.0.txt Provides utility methods for serialization and deserialization. ```APIDOC ## SerializationUtils ### Description Provides utility methods for serializing and deserializing objects. ### Constructors - `public SerializationUtils()`: Default constructor. ``` -------------------------------- ### ArrayFill and ArraySorter Source: https://context7.com/apache/commons-lang/llms.txt ArrayFill provides factory-style array filling using lambdas, while ArraySorter offers a fluent sort that returns the same array, enabling method chaining. ```APIDOC ## ArrayFill ### Description Provides factory-style array filling using lambdas. ### Method Signature `ArrayFill.fill(T[] array, IntFunction generator)` ### Parameters - `array` (T[]) - The array to fill. - `generator` (IntFunction) - A function that takes an index and returns a value. ### Example ```java String[] labels = ArrayFill.fill(new String[5], i -> "item_" + i); // ["item_0","item_1","item_2","item_3","item_4"] Integer[] squares = ArrayFill.fill(new Integer[5], i -> i * i); // [0, 1, 4, 9, 16] ``` ## ArraySorter ### Description Offers a fluent sort that returns the same array, enabling method chaining. ### Method Signature `ArraySorter.sort(T[] array)` ### Parameters - `array` (T[]) - The array to sort. ### Example ```java String[] words = ArraySorter.sort(new String[]{"banana", "apple", "cherry"}); // words == sorted in-place AND returned: ["apple","banana","cherry"] int[] nums = ArraySorter.sort(new int[]{5, 2, 8, 1, 9}); // [1,2,5,8,9] // Chain: fill then sort String[] sorted = ArraySorter.sort( ArrayFill.fill(new String[4], i -> String.valueOf((char)('d' - i))) ); // ["a","b","c","d"] ``` ```