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

com.neko233.skilltree.game.redDot.RedDotTree Maven / Gradle / Ivy

There is a newer version: 0.3.6
Show newest version
package com.neko233.skilltree.game.redDot;

import com.neko233.skilltree.annotation.Nullable;
import com.neko233.skilltree.commons.core.utils.CollectionUtils233;
import com.neko233.skilltree.game.redDot.dto.RedDotDto;
import com.neko233.skilltree.game.redDot.dto.RedPointRemoveResult;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 红点, 以 "/" 作为父节点
 *
 * @author LuoHaoJun on 2023-05-22
 **/
public interface RedDotTree {

    // 永久过期时间
    long FOREVER_EXPIRE_MS = -1;

    /**
     * 清理
     */
    void clear();

    /**
     * 获取某个路径过期时间
     *
     * @param path 路径
     * @return 过期时间, 如果不存在 null / 永久 = -1
     */
    @Nullable
    Long getExpireMsByPath(String path);


    /**
     * 不刷新获取
     *
     * @param path 路径
     * @return 红点数据
     */
    RedDotDto getRedDot(String path);

    /**
     * 刷新数据后获取
     *
     * @param path 路径
     * @return 存在 + 不过期 = obj / 过期 null
     */
    @Nullable
    RedDotDto getByRefresh(String path);

    /**
     * 获取所有子路径
     *
     * @param path 路径
     * @return 自己 + 子路径
     */
    List getAllChildPathList(String path);

    default List getAllChildPathAndTargetList(String path) {
        List allChildPathList = getAllChildPathList(path);
        allChildPathList.add(path);
        return allChildPathList;
    }


    /**
     * 获取所有父路径
     *
     * @param path 路径
     * @return 父路径 List
     */
    List getAllParentPathList(String path);

    default List getAllParentPathAndTargetList(String path) {
        List allParentPathList = getAllParentPathList(path);
        allParentPathList.add(path);
        return allParentPathList;
    }

    /**
     * 在 path 刷新后, 获取值
     *
     * @param path 路径
     * @return 红点值
     */
    default long getValueByRefresh(String path) {
        RedDotDto byRefresh = getByRefresh(path);
        if (byRefresh == null) {
            return 0;
        }
        return byRefresh.getCount();
    }

    /**
     * 是否过期了
     *
     * @param path 路径
     * @return isExpire ?
     */
    boolean isExpire(String path);

    /**
     * 没过期 ?
     */
    default boolean isNotExpire(String path) {
        return !isExpire(path);
    }

    /**
     * @return 获取 path: value
     */
    Map getPathToValueMap();

    /**
     * 移除红点
     *
     * @param path 路径
     * @return
     */
    RedPointRemoveResult removeRedDot(String path);


    default List reduceRedDot(String path) {
        return addRedDot(path, -1);
    }


    default List reduceRedDot(String path, int value) {
        return addRedDot(path, -value, FOREVER_EXPIRE_MS);
    }

    default List reduceRedDot(String path, int value, int delayExpireTime, TimeUnit timeUnit) {
        return addRedDot(path, -value, delayExpireTime, timeUnit);
    }


    default List addRedDot(String path) {
        return addRedDot(path, 1);
    }

    /**
     * 添加红点, 永久
     *
     * @param path  路径
     * @param value 红点数
     * @return toUpdatePathList
     */
    default List addRedDot(String path, int value) {
        return addRedDot(path, value, FOREVER_EXPIRE_MS);
    }

    /**
     * 添加红点, 延迟过期
     *
     * @param path  路径
     * @param value 红点数
     */
    default List addRedDot(String path, int value, int delayExpireTime, TimeUnit timeUnit) {
        long currentMs = System.currentTimeMillis();
        long expireMs = currentMs + timeUnit.toMillis(delayExpireTime);

        return addRedDot(path, value, expireMs);
    }

    /**
     * 添加红点, 某个时间点过期
     *
     * @param path  路径
     * @param value 红点数
     * @return 所有受影响更新的 path
     */
    List addRedDot(String path, int value, long expireTimeMs);

    /**
     * set 红点值, 并传递更新
     *
     * @param path  路径
     * @param value 值
     * @return toUpdatePathList
     */
    List updateRedDotValue(String path, long value);

    /**
     * 纯粹为了更新
     */
    default List updateRedDotValue(String path) {
        return updateRedDotValue(path, 0);
    }

    Map getAllDataMap(Collection filterPathList);

    Map getAllDataMap();


    /**
     * 获取最新的 redPoint value
     *
     * @param path 红点路径
     * @return 红点值
     */
    long getRedDotValue(String path);

    List getAllRedDotList();

    default void flush() {
        List redDotDtoList = getAllRedDotList();
        List toUpdatePathList = redDotDtoList.stream().map(RedDotDto::getPath).collect(Collectors.toList());
        flush(toUpdatePathList);
    }

    /**
     * 刷数据
     *
     * @param toUpdatePathList 要更新的路径
     */
    void flush(List toUpdatePathList);

    default boolean isEmpty() {
        List redDotDtoList = getAllRedDotList();
        return CollectionUtils233.isEmpty(redDotDtoList);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy