### Truncate string from start with Strings.cutStart() in Java Source: https://context7.com/greendelta/olca-commons/llms.txt Truncates a string to a specified maximum length, prepending '...' if the string was actually shortened. If the original string is null or empty, it returns an empty string. Handles leading/trailing whitespace by trimming before truncation. ```java import org.openlca.commons.Strings; System.out.println(Strings.cutStart("This is a long text", 9)); // "...g text" System.out.println(Strings.cutStart("abcdefg", 6)); // "...efg" System.out.println(Strings.cutStart("abc", 3)); // "abc" System.out.println(Strings.cutStart(" Hello World ", 20)); // "Hello World" ``` -------------------------------- ### Create Error Result with Message using Res in Java Source: https://context7.com/greendelta/olca-commons/llms.txt Demonstrates creating an error `Res` object with a descriptive message using `Res.error(String message)`. It shows how to check if the result is an error and retrieve the error message. It also includes an example of the exception thrown when trying to access a value from an error result. ```java import org.openlca.commons.Res; // Create an error result Res result = Res.error("File not found"); System.out.println(result.isError()); // true System.out.println(result.isOk()); // false System.out.println(result.error()); // "File not found" System.out.println(result.toString()); // "Error: File not found" // Attempting to access value throws IllegalStateException try { result.value(); } catch (IllegalStateException e) { System.out.println(e.getMessage()); // "Result is an error: File not found" } ``` -------------------------------- ### Create Empty Success Result using Res in Java Source: https://context7.com/greendelta/olca-commons/llms.txt Illustrates creating an empty successful `Res` object using `Res.ok()`. This is suitable for operations that do not return a value but complete successfully (e.g., void methods). It shows how to check its state. ```java import org.openlca.commons.Res; // Create an empty result (for void operations) Res result = Res.ok(); System.out.println(result.isOk()); // true System.out.println(result.isEmpty()); // true System.out.println(result.isError()); // false System.out.println(result.toString()); // "ok!" ``` -------------------------------- ### Create Success Result with Value using Res in Java Source: https://context7.com/greendelta/olca-commons/llms.txt Shows how to create a successful `Res` object containing a value using `Res.ok(T value)`. This is used for operations that complete successfully and return a result. It demonstrates checking the state and retrieving the value. ```java import org.openlca.commons.Res; // Create a result with a value Res result = Res.ok(42); System.out.println(result.isOk()); // true System.out.println(result.isError()); // false System.out.println(result.isEmpty()); // false System.out.println(result.value()); // 42 System.out.println(result.toString()); // "42" ``` -------------------------------- ### Implement Copyable Interface in Java Source: https://context7.com/greendelta/olca-commons/llms.txt Demonstrates how to implement the `Copyable` interface in Java for creating type-safe copies of objects. This approach avoids the issues associated with `Cloneable` and `Object.clone()` by providing a dedicated `copy()` method. ```java import org.openlca.commons.Copyable; public class Person implements Copyable { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } @Override public Person copy() { return new Person(this.name, this.age); } public static void main(String[] args) { Person original = new Person("Alice", 30); Person copied = original.copy(); System.out.println("Original: " + original.name); // Alice System.out.println("Copied: " + copied.name); // Alice System.out.println("Same object: " + (original == copied)); // false } } ``` -------------------------------- ### Check for blank strings with Strings.isBlank() and Strings.isNotBlank() in Java Source: https://context7.com/greendelta/olca-commons/llms.txt Provides utility methods to check if a string is null, empty, or consists solely of whitespace characters. `isBlank()` returns true for such strings, while `isNotBlank()` returns true only for strings that contain at least one non-whitespace character. ```java import org.openlca.commons.Strings; System.out.println(Strings.isBlank(null)); // true System.out.println(Strings.isBlank("")); // true System.out.println(Strings.isBlank(" ")); // true System.out.println(Strings.isBlank("hello")); // false System.out.println(Strings.isNotBlank("hello")); // true System.out.println(Strings.isNotBlank(null)); // false ``` -------------------------------- ### Handle Res with orElse Supplier in Java Source: https://context7.com/greendelta/olca-commons/llms.txt Demonstrates using the `orElse(Supplier fn)` method on a `Res` object. If the `Res` contains a value, it's returned; otherwise, the supplier function is executed to provide a default value. This works for both successful and error `Res` objects. ```java import org.openlca.commons.Res; Res success = Res.ok("Hello"); Res failure = Res.error("Failed"); System.out.println(success.orElse(() -> "Default")); // "Hello" System.out.println(failure.orElse(() -> "Default")); // "Default" System.out.println(failure.orElse(null)); // null ``` -------------------------------- ### Create Error Result with Message and Cause using Res in Java Source: https://context7.com/greendelta/olca-commons/llms.txt Shows how to create an error `Res` object that includes both a message and an underlying `Throwable` cause using `Res.error(String message, Throwable err)`. This is useful for preserving the original exception context. ```java import org.openlca.commons.Res; import java.io.IOException; // Create an error with exception cause IOException cause = new IOException("Connection refused"); Res result = Res.error("Network error", cause); System.out.println(result.error()); // "Network error\n -> Connection refused" ``` -------------------------------- ### Strings.equalsIgnoreCase: Case-Insensitive String Comparison (Java) Source: https://context7.com/greendelta/olca-commons/llms.txt Compares two strings for equality, ignoring case differences. It provides null-safe handling, returning true if both strings are null and false if only one is null. ```java import org.openlca.commons.Strings; System.out.println(Strings.equalsIgnoreCase("Hello", "hello")); // true System.out.println(Strings.equalsIgnoreCase("Hello", "World")); // false System.out.println(Strings.equalsIgnoreCase(null, null)); // true System.out.println(Strings.equalsIgnoreCase(null, "hello")); // false ``` -------------------------------- ### Strings.nullIfBlank: Convert Blank String to Null (Java) Source: https://context7.com/greendelta/olca-commons/llms.txt Returns null if the input string is blank (empty or contains only whitespace); otherwise, it returns the original string. This helps in treating blank inputs as null values. ```java import org.openlca.commons.Strings; System.out.println(Strings.nullIfBlank("hello")); // "hello" System.out.println(Strings.nullIfBlank("")); // null System.out.println(Strings.nullIfBlank(" ")); // null System.out.println(Strings.nullIfBlank(null)); // null ``` -------------------------------- ### Add Maven Dependency for olca-commons Source: https://github.com/greendelta/olca-commons/blob/main/README.md This snippet shows how to add the olca-commons library as a dependency to a Maven project. It specifies the group ID, artifact ID, and version required for integration. ```xml org.openlca olca-commons 1.0.0 ``` -------------------------------- ### Case-insensitive string comparison with Strings.compareIgnoreCase() in Java Source: https://context7.com/greendelta/olca-commons/llms.txt Compares two strings lexicographically, ignoring differences in case. It also provides null-safe comparison, treating null strings appropriately in the comparison logic. Returns 0 if strings are equal (ignoring case), a negative value if the first string comes before the second, and a positive value otherwise. ```java import org.openlca.commons.Strings; System.out.println(Strings.compareIgnoreCase("abc", "ABC")); // 0 System.out.println(Strings.compareIgnoreCase("abc", "def")); // negative System.out.println(Strings.compareIgnoreCase("def", "abc")); // positive System.out.println(Strings.compareIgnoreCase(null, null)); // 0 System.out.println(Strings.compareIgnoreCase(null, "abc")); // -1 System.out.println(Strings.compareIgnoreCase("abc", null)); // 1 ``` -------------------------------- ### Handle Res with orElseThrow in Java Source: https://context7.com/greendelta/olca-commons/llms.txt Illustrates the `orElseThrow()` method for `Res` objects. It returns the contained value if the `Res` is successful, otherwise, it throws an `IllegalStateException` with the error message. This is useful for asserting that an operation must succeed. ```java import org.openlca.commons.Res; Res success = Res.ok(100); Res failure = Res.error("Invalid input"); System.out.println(success.orElseThrow()); // 100 try { failure.orElseThrow(); } catch (IllegalStateException e) { System.out.println(e.getMessage()); // "Result is an error: Invalid input" } ``` -------------------------------- ### Truncate string from end with Strings.cutEnd() in Java Source: https://context7.com/greendelta/olca-commons/llms.txt Truncates a string to a specified maximum length, appending '...' if the string was actually shortened. If the original string is null, it returns an empty string. If the length is zero or negative, it also returns an empty string. Handles leading/trailing whitespace by trimming before truncation. ```java import org.openlca.commons.Strings; System.out.println(Strings.cutEnd("This is a long text", 9)); // "This i..." System.out.println(Strings.cutEnd("abcdef", 5)); // "ab..." System.out.println(Strings.cutEnd("abc", 3)); // "abc" System.out.println(Strings.cutEnd(" Hello World ", 20)); // "Hello World" System.out.println(Strings.cutEnd(null, 10)); // "" System.out.println(Strings.cutEnd("test", 0)); // "" ``` -------------------------------- ### Wrap errors with additional context using Res.wrapError() in Java Source: https://context7.com/greendelta/olca-commons/llms.txt Wraps an existing error within a Res object with an additional context message. This method is useful for adding layers of information to an error as it propagates through different parts of an application, aiding in debugging. The new message is prepended to the original error message. ```java import org.openlca.commons.Res; Res innerError = Res.error("Database connection failed"); Res wrappedError = innerError.wrapError("User lookup failed"); System.out.println(wrappedError.error()); // "User lookup failed\n -> Database connection failed" ``` -------------------------------- ### Strings.notNull: Ensure Non-Null String (Java) Source: https://context7.com/greendelta/olca-commons/llms.txt Returns the original string if it is not null; otherwise, it returns an empty string. This is useful for preventing NullPointerExceptions when a string is expected. ```java import org.openlca.commons.Strings; System.out.println(Strings.notNull(null)); // "" System.out.println(Strings.notNull("hello")); // "hello" System.out.println(Strings.notNull("")); // "" ``` -------------------------------- ### Chain operations with Res.then() in Java Source: https://context7.com/greendelta/olca-commons/llms.txt Chains operations on a Res object, applying a function only if the preceding operation was successful. This is useful for sequential processing of results or errors. It takes a function that transforms the successful value of the Res object into a new Res object. ```java import org.openlca.commons.Res; public static Res parseNumber(String s) { try { return Res.ok(Integer.parseInt(s)); } catch (NumberFormatException e) { return Res.error("Invalid number: " + s, e); } } public static Res doubleValue(Integer n) { return Res.ok(n * 2); } public static void main(String[] args) { // Successful chain Res result1 = parseNumber("21").then(n -> doubleValue(n)); System.out.println(result1.value()); // 42 // Error propagation - error from first step passes through Res result2 = parseNumber("abc").then(n -> doubleValue(n)); System.out.println(result2.isError()); // true System.out.println(result2.error()); // "Invalid number: abc\n -> For input string: \"abc\"" } ``` -------------------------------- ### Truncate string from middle with Strings.cutMid() in Java Source: https://context7.com/greendelta/olca-commons/llms.txt Truncates a string in the middle, replacing the removed portion with '...'. The goal is to preserve context from both the beginning and the end of the string. If the string is shorter than or equal to the specified length, it is returned unchanged. Handles null input by returning an empty string. ```java import org.openlca.commons.Strings; System.out.println(Strings.cutMid("This is a long text", 10)); // "This...ext" System.out.println(Strings.cutMid("This is a long text", 11)); // "This...text" System.out.println(Strings.cutMid("abcdef", 5)); // "a...f" System.out.println(Strings.cutMid("abcdef", 4)); // "a..." System.out.println(Strings.cutMid("abcdef", 6)); // "abcdef" ``` -------------------------------- ### Cast error types with Res.castError() in Java Source: https://context7.com/greendelta/olca-commons/llms.txt Casts an error result to a different type parameter without changing the error message or status. This is useful when you need to return a Res object with a different generic type but the operation has already failed. The underlying error details are preserved. ```java import org.openlca.commons.Res; Res intError = Res.error("Integer error"); Res stringError = intError.castError(); System.out.println(stringError.isError()); // true System.out.println(stringError.error()); // "Integer error" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.