pro.jk.ejoker.common.system.enhance.MapUtilx Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ejoker-common Show documentation
Show all versions of ejoker-common Show documentation
EJoker is a CQRS + EventSourcing framwork
package pro.jk.ejoker.common.system.enhance;
import java.util.Map;
import pro.jk.ejoker.common.system.functional.IFunction;
import pro.jk.ejoker.common.system.functional.IFunction1;
public final class MapUtilx {
/**
* 此方法并不负责线程安全,线程安全由具体的map实现类型负责
* @param
* @param
* @param map
* @param uniqueKey
* @param f
* @return
*/
public static T getOrAdd(Map map, K uniqueKey, IFunction f) {
T current, newOne = null;
if(null == (current = map.get(uniqueKey))) {
current = map.putIfAbsent(uniqueKey, newOne = f.trigger());
}
return null != current ? current : newOne;
}
/**
* 此方法并不负责线程安全,线程安全由具体的map实现类型负责
* @param
* @param
* @param map
* @param uniqueKey
* @param f
* @return
*/
public static T getOrAdd(Map map, K uniqueKey, IFunction1 f) {
T current, newOne = null;
if(null == (current = map.get(uniqueKey))) {
current = map.putIfAbsent(uniqueKey, newOne = f.trigger(uniqueKey));
}
return null != current ? current : newOne;
}
}