All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.iohao.game.common.kit.SafeKit Maven / Gradle / Ivy

/*
 * ioGame
 * Copyright (C) 2021 - present  渔民小镇 ([email protected][email protected]) . All Rights Reserved.
 * # iohao.com . 渔民小镇
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 */
package com.iohao.game.common.kit;

import lombok.experimental.UtilityClass;

import java.util.Objects;

/**
 * 获取安全的值, 保证返回的不是 null
 *
 * @author 渔民小镇
 * @date 2021-12-20
 */
@UtilityClass
public class SafeKit {
    public int getInt(Integer value) {
        return getInt(value, 0);
    }

    public int getInt(Integer value, int defaultValue) {
        return value == null ? defaultValue : value;
    }

    public int getInt(String value, int defaultValue) {
        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    public long getLong(Long value) {
        return getLong(value, 0);
    }

    public long getLong(Long value, long defaultValue) {
        return Objects.isNull(value) ? defaultValue : value;
    }

    public long getLong(String value, long defaultValue) {
        try {
            return Long.parseLong(value);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    public boolean getBoolean(Boolean value) {
        return getBoolean(value, false);
    }

    public boolean getBoolean(Boolean value, boolean defaultValue) {
        return Objects.isNull(value) ? defaultValue : value;
    }

    public String getString(String value, String defaultValue) {
        if (StrKit.isEmpty(value)) {
            return defaultValue;
        }

        return value;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy