CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-cn-hutool--hutool-core

Comprehensive Java utility library providing collections, strings, beans, dates, I/O, and numerous other utility functions.

Pending
Overview
Eval results
Files

object-operations.mddocs/

Object Operations

Object comparison, validation, cloning, and manipulation utilities through the ObjectUtil class.

Capabilities

Object Comparison and Equality

Compare objects with null-safe operations and special handling for different types.

/**
 * Check if two objects are equal
 * @param obj1 first object
 * @param obj2 second object
 * @return true if objects are equal
 */
public static boolean equals(Object obj1, Object obj2);

/**
 * Check if two objects are equal (alias for equals)
 * @param obj1 first object
 * @param obj2 second object
 * @return true if objects are equal
 */
public static boolean equal(Object obj1, Object obj2);

/**
 * Check if two objects are not equal
 * @param obj1 first object
 * @param obj2 second object
 * @return true if objects are not equal
 */
public static boolean notEqual(Object obj1, Object obj2);

Usage Examples:

import cn.hutool.core.util.ObjectUtil;
import java.math.BigDecimal;

// Basic equality
boolean same1 = ObjectUtil.equals("hello", "hello"); // true
boolean same2 = ObjectUtil.equals(null, null); // true
boolean diff = ObjectUtil.notEqual("hello", "world"); // true

// BigDecimal special handling
BigDecimal bd1 = new BigDecimal("1.0");
BigDecimal bd2 = new BigDecimal("1.00");
boolean bdEqual = ObjectUtil.equals(bd1, bd2); // true (uses compareTo)

Object Validation and Null Checking

Validate objects for null, empty, and other conditions.

/**
 * Check if object is null
 * @param obj object to check
 * @return true if object is null
 */
public static boolean isNull(Object obj);

/**
 * Check if object is not null
 * @param obj object to check
 * @return true if object is not null
 */
public static boolean isNotNull(Object obj);

/**
 * Check if object is empty
 * @param obj object to check
 * @return true if object is null, empty string, empty collection, or empty array
 */
public static boolean isEmpty(Object obj);

/**
 * Check if object is not empty
 * @param obj object to check
 * @return true if object is not empty
 */
public static boolean isNotEmpty(Object obj);

/**
 * Get length of object (string, collection, array, etc.)
 * @param obj object to measure
 * @return length of object, or 0 if null
 */
public static int length(Object obj);

/**
 * Check if object contains element
 * @param obj container object (array, collection, string)
 * @param element element to find
 * @return true if container contains element
 */
public static boolean contains(Object obj, Object element);

Usage Examples:

import cn.hutool.core.util.ObjectUtil;
import java.util.Arrays;
import java.util.List;

// Null checking
String str = null;
boolean isNull = ObjectUtil.isNull(str); // true
boolean isNotNull = ObjectUtil.isNotNull("hello"); // true

// Empty checking
boolean emptyStr = ObjectUtil.isEmpty(""); // true
boolean emptyList = ObjectUtil.isEmpty(Arrays.asList()); // true
boolean notEmptyArray = ObjectUtil.isNotEmpty(new int[]{1, 2, 3}); // true

// Length calculation
int strLength = ObjectUtil.length("hello"); // 5
int arrayLength = ObjectUtil.length(new String[]{"a", "b"}); // 2
int nullLength = ObjectUtil.length(null); // 0

// Contains checking
boolean hasElement = ObjectUtil.contains(Arrays.asList("a", "b", "c"), "b"); // true
boolean hasChar = ObjectUtil.contains("hello", 'e'); // true

Default Value Handling

Provide default values when objects are null, empty, or blank.

/**
 * Return default value if object is null
 * @param object object to check
 * @param defaultValue default value if null
 * @return object if not null, otherwise defaultValue
 */
public static <T> T defaultIfNull(T object, T defaultValue);

/**
 * Return default value using supplier if object is null
 * @param source object to check
 * @param defaultValueSupplier supplier for default value
 * @return object if not null, otherwise supplied default
 */
public static <T> T defaultIfNull(T source, Supplier<? extends T> defaultValueSupplier);

/**
 * Return default value using function if object is null
 * @param source object to check
 * @param defaultValueSupplier function to generate default value
 * @return object if not null, otherwise generated default
 */
public static <T> T defaultIfNull(T source, Function<T, ? extends T> defaultValueSupplier);

/**
 * Return default value if string is empty
 * @param str string to check
 * @param defaultValue default value if empty
 * @return string if not empty, otherwise defaultValue
 */
public static <T extends CharSequence> T defaultIfEmpty(T str, T defaultValue);

/**
 * Return default value if string is blank (empty or whitespace only)
 * @param str string to check
 * @param defaultValue default value if blank
 * @return string if not blank, otherwise defaultValue
 */
public static <T extends CharSequence> T defaultIfBlank(T str, T defaultValue);

Usage Examples:

import cn.hutool.core.util.ObjectUtil;
import java.util.function.Supplier;

// Default for null
String name = null;
String safeName = ObjectUtil.defaultIfNull(name, "Unknown"); // "Unknown"

// Default with supplier (lazy evaluation)
String config = ObjectUtil.defaultIfNull(null, () -> "default-config"); // "default-config"

// Default for empty strings
String emptyStr = "";
String nonEmpty = ObjectUtil.defaultIfEmpty(emptyStr, "default"); // "default"

// Default for blank strings
String blankStr = "   ";
String nonBlank = ObjectUtil.defaultIfBlank(blankStr, "default"); // "default"

Object Conversion and Transformation

Handle conversions with default values and transformations.

/**
 * Handle object with function, return default if null
 * @param source source object
 * @param handle function to process non-null object
 * @param defaultValue default value if source is null
 * @return processed result or default value
 */
public static <T, R> T defaultIfNull(R source, Function<R, ? extends T> handle, T defaultValue);

/**
 * Handle string with function, return default if empty
 * @param str string to process
 * @param handle function to process non-empty string
 * @param defaultValue default value if string is empty
 * @return processed result or default value
 */
public static <T> T defaultIfEmpty(String str, Function<CharSequence, ? extends T> handle, T defaultValue);

Object Cloning

Clone objects using various methods with fallback strategies.

/**
 * Clone object if it implements Cloneable
 * @param obj object to clone
 * @return cloned object
 */
public static <T> T clone(T obj);

/**
 * Clone object if possible, otherwise return original
 * @param obj object to clone
 * @return cloned object or original if cloning fails
 */
public static <T> T cloneIfPossible(T obj);

/**
 * Clone object using serialization
 * @param obj object to clone (must be Serializable)
 * @return deep cloned object
 */
public static <T> T cloneByStream(T obj);

Usage Examples:

import cn.hutool.core.util.ObjectUtil;
import java.util.ArrayList;
import java.util.List;

// Clone collections
List<String> original = new ArrayList<>();
original.add("item1");
original.add("item2");

List<String> cloned = ObjectUtil.clone(original);
// Modify cloned without affecting original

// Safe cloning (won't throw exception)
String str = "hello";
String clonedStr = ObjectUtil.cloneIfPossible(str); // Returns same string (immutable)

// Deep cloning using serialization
MySerializableClass original = new MySerializableClass();
MySerializableClass deepCopy = ObjectUtil.cloneByStream(original);

Hash Code Generation

Generate hash codes for objects and arrays with null safety.

/**
 * Generate hash code for object
 * @param obj object to hash
 * @return hash code
 */
public static int hashCode(Object obj);

/**
 * Generate hash code for multiple objects
 * @param objects objects to hash
 * @return combined hash code
 */
public static int hashCode(Object... objects);

Common Types

// Function interfaces for transformations
import java.util.function.Function;
import java.util.function.Supplier;

// Collections and arrays
import java.util.Collection;
import java.util.Map;
import java.lang.reflect.Array;

// Cloning support
interface Cloneable {
    Object clone() throws CloneNotSupportedException;
}

interface Serializable {
    // Marker interface for serialization-based cloning
}

Install with Tessl CLI

npx tessl i tessl/maven-cn-hutool--hutool-core

docs

collection-operations.md

compression-operations.md

date-time-operations.md

file-io-operations.md

index.md

object-operations.md

random-operations.md

string-operations.md

type-conversion.md

url-operations.md

xml-operations.md

tile.json