Skip to content

Commit

Permalink
1.新增 DevVariableExt
Browse files Browse the repository at this point in the history
Former-commit-id: 3f445c3
Former-commit-id: ec7951f
  • Loading branch information
afkT committed Jan 22, 2022
1 parent 5a16238 commit e76c800
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/DevAssist/src/main/java/dev/base/DevVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
import java.util.Map;

/**
* detail: 变量基类 ( 方便判断处理 )
* detail: 变量操作基类
* @author Ttt
* <pre>
* 内部封装逻辑, 对外提供快捷方法
* 减少逻辑实现代码
* </pre>
*/
public class DevVariable<K, V> {

Expand Down
90 changes: 90 additions & 0 deletions lib/DevAssist/src/main/java/dev/base/DevVariableExt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package dev.base;

/**
* detail: 变量操作基类扩展类
* @author Ttt
* <pre>
* {@link DevVariable} 变量操作基类基础上进行扩展
* 支持通过接口方式进行创建存储值
* </pre>
*/
public class DevVariableExt<K, V, P> {

// 变量操作基类
private final DevVariable<K, V> mVariable = new DevVariable<>();
// 变量创建器
private Creator<K, V, P> mCreator = null;

// ========
// = 创建器 =
// ========

/**
* detail: 变量创建器
* @author Ttt
*/
public interface Creator<K, V, P> {

/**
* 创建存储值
* @param param 额外参数
* @param key 存储 key
* @return 存储值
*/
V create(
P param,
K key
);
}

/**
* 获取变量创建器
* @return {@link Creator}
*/
public Creator<K, V, P> getCreator() {
return mCreator;
}

/**
* 设置变量创建器
* @param creator {@link Creator}
* @return {@link DevVariableExt}
*/
public DevVariableExt<K, V, P> setCreator(final Creator<K, V, P> creator) {
this.mCreator = creator;
return this;
}

// =============
// = 对外公开方法 =
// =============

/**
* 获取变量操作基类
* @return {@link DevVariable}
*/
public DevVariable<K, V> getVariable() {
return mVariable;
}

/**
* 通过 key 获取 value
* @param key Key
* @param param 额外参数
* @return Value
*/
public V getVariableValue(
final P param,
final K key
) {
V value = mVariable.getVariableValue(key);
if (value != null) return value;
if (mCreator != null) {
value = mCreator.create(param, key);
if (value != null) {
mVariable.putVariable(key, value);
}
}
return value;
}
}

0 comments on commit e76c800

Please sign in to comment.