Skip to content

io microsphere util ObjectUtils

github-actions[bot] edited this page May 28, 2026 · 2 revisions

ObjectUtils

Type: Class | Module: microsphere-java-core | Package: io.microsphere.util | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/util/ObjectUtils.java

Overview

The utility class for Object.

Declaration

public abstract class ObjectUtils implements Utils

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.3.5-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Method Examples

nullSafe

String name = ObjectUtils.nullSafe(user, User::getName); // returns user.getName() if user is not null, otherwise null
Integer length = ObjectUtils.nullSafe(name, String::length); // returns name.length() if name is not null, otherwise null

defaultIfNull

String result = ObjectUtils.defaultIfNull(null, () -> "default"); // returns "default"
String result2 = ObjectUtils.defaultIfNull("value", () -> "default"); // returns "value"

defaultIfNull

String result = ObjectUtils.defaultIfNull(null, "default"); // returns "default"
String result2 = ObjectUtils.defaultIfNull("value", "default"); // returns "value"

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.util.ObjectUtils;

API Reference

Public Methods

Method Description
nullSafe Applies the given function to the source object if it is non-null, otherwise returns null.
defaultIfNull Returns the given object if it is non-null, otherwise returns the default value supplied by the Supplier.
defaultIfNull Returns the given object if it is non-null, otherwise returns the default value.

Method Details

nullSafe

public static <S, T> T nullSafe(@Nullable S source, Function<S, T> function)

Applies the given function to the source object if it is non-null, otherwise returns null. This method helps to avoid NullPointerException when chaining operations on potentially null objects.

Example Usage

`String name = ObjectUtils.nullSafe(user, User::getName); // returns user.getName() if user is not null, otherwise null
Integer length = ObjectUtils.nullSafe(name, String::length); // returns name.length() if name is not null, otherwise null
`

defaultIfNull

public static <T> T defaultIfNull(@Nullable T object, Supplier<T> defaultValueSupplier)

Returns the given object if it is non-null, otherwise returns the default value supplied by the Supplier.

Example Usage

`String result = ObjectUtils.defaultIfNull(null, () -> "default"); // returns "default"
String result2 = ObjectUtils.defaultIfNull("value", () -> "default"); // returns "value"
`

defaultIfNull

public static <T> T defaultIfNull(@Nullable T object, @Nullable T defaultValue)

Returns the given object if it is non-null, otherwise returns the default value.

Example Usage

`String result = ObjectUtils.defaultIfNull(null, "default"); // returns "default"
String result2 = ObjectUtils.defaultIfNull("value", "default"); // returns "value"
`

The source from org.apache.commons.lang3.ObjectUtils#defaultIfNull(Object, Object)

See Also

  • Object

This documentation was auto-generated from the source code of microsphere-java.

Home

annotation-processor

java-annotations

java-core

java-test

jdk-tools

lang-model

Clone this wiki locally