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

com.iohao.game.common.kit.attr.AttrOptions 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.attr;

import org.jctools.maps.NonBlockingHashMap;

import java.io.Serial;
import java.io.Serializable;
import java.util.Map;
import java.util.Objects;

/**
 * 动态属性的选项载体
 * 
 *     see {@link AttrOptionDynamic}
 * 
* * @author 渔民小镇 * @date 2022-01-31 */ public class AttrOptions implements Serializable { @Serial private static final long serialVersionUID = 9042891580724596100L; final Map, Object> options = new NonBlockingHashMap<>(); /** * 获取选项值。 *

* 如果选项不存在,返回默认值。 * * @param option 选项值 * @return 如果 option 不存在,则使用默认的 option 值。 */ @SuppressWarnings("unchecked") public T option(AttrOption option) { Object value = options.get(option); if (Objects.nonNull(value)) { return (T) value; } if (Objects.nonNull(option.supplier)) { T newValue = option.supplier.get(); this.option(option, newValue); return newValue; } if (Objects.nonNull(option.defaultValue)) { return option.defaultValue; } return null; } /** * 设置一个具有特定值的新选项。 *

* 使用 null 值删除前一个设置的 {@link AttrOption}。 * * @param option 选项值 * @param value 选项值, null 用于删除前一个 {@link AttrOption}. * @return this */ public AttrOptions option(AttrOption option, T value) { if (Objects.isNull(value)) { options.remove(option); return this; } options.put(option, value); return this; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy