1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

はじめに

プログラムを作成する上でチェック処理は欠かせませんが、ここではJavaにおけるチェック処理あれこれをまとめました。

前提条件

環境:Spring Framework

チェック処理

オブジェクト全般チェック

import java.util.Objects;

class Check {

    void execute() {
        // message == null
        if (Objects.isNull(message)) {
        }

        // message != null
        if (Objects.nonNull(message)) {
        }

        // message == nullの場合にNullPointerExceptionを投げる
        Objects.requireNonNull(message);
    }
}

文字列チェック

import org.springframework.util.StringUtils;

class Check {

    void execute() {
        // (message == null || "".equals(message))
        if (StringUtils.isEmpty(message)) {
        }

        // (str != null && !str.isEmpty())
        if (StringUtils.hasLength(message)) {
        }

    }
}

Collectionのチェック


import org.springframework.util.CollectionUtils;

class Check {

    void execute() {
        List<String> list = new ArrayList<>();
        list.add("Hello");
        list.add("World");

        // (list == null || list.isEmpty())
        if (CollectionUtils.isEmpty(list)) {
        }

    }
}

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?