com.flyfish.oauth.builder.MapParamBuilder Maven / Gradle / Ivy
package com.flyfish.oauth.builder;
import org.apache.commons.lang3.StringUtils;
import java.util.HashMap;
import java.util.Map;
/**
* Map参数构造器
*
* @author Mr.Wang
* @apiNote 通过重载过滤已知类型的空值,并分别处理
*/
public final class MapParamBuilder {
private static final String PAGE_KEY = "pageUtil";
private Map params;
private MapParamBuilder() {
this.params = new HashMap<>();
}
public static MapParamBuilder builder() {
return new MapParamBuilder();
}
@SuppressWarnings("unchecked")
public static T any(Map map, String key) {
Object value = map.get(key);
return (T) value;
}
public MapParamBuilder with(String key, Object value) {
if (StringUtils.isNotBlank(key) && value != null) {
this.params.put(key, value);
}
return this;
}
public boolean has(String key) {
return this.params.containsKey(key);
}
public MapParamBuilder with(String key, String value) {
if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(value)) {
this.params.put(key, value);
}
return this;
}
public MapParamBuilder with(String key, Integer value) {
// 过滤负值,无意义的值
if (StringUtils.isNotBlank(key) && value != null) {
this.params.put(key, value);
}
return this;
}
/**
* 交换键位对应的值
*
* @param oldKey 要被交换的key
* @param newKey 要交换的key
* @return 结果
*/
public MapParamBuilder exchange(String oldKey, String newKey) {
if (this.params.containsKey(oldKey) && this.params.containsKey(newKey)) {
Object oldValue = this.params.get(oldKey);
Object newValue = this.params.get(newKey);
this.params.put(oldKey, newValue);
this.params.put(newKey, oldValue);
}
return this;
}
/**
* 替换key为新的key,值不变
*
* @param oldKey 旧的key
* @param newKey 新的key
* @return 结果
*/
public MapParamBuilder replace(String oldKey, String newKey) {
Object value = this.params.get(oldKey);
if (null != value) {
this.params.remove(oldKey);
this.params.put(newKey, value);
}
return this;
}
public MapParamBuilder clear(String key) {
this.params.remove(key);
return this;
}
public MapParamBuilder with(String key, Long value) {
// 过滤负值,无意义的值
if (StringUtils.isNotBlank(key) && value != null) {
this.params.put(key, value);
}
return this;
}
@SuppressWarnings("unchecked")
public T take(String key) {
return (T) this.params.get(key);
}
public Map build() {
return this.params;
}
}