com.liferay.petra.lang.CentralizedThreadLocal Maven / Gradle / Ivy
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library 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 Lesser General Public License for more
* details.
*/
package com.liferay.petra.lang;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* @author Shuyang Zhou
*/
public class CentralizedThreadLocal extends ThreadLocal {
public static void clearLongLivedThreadLocals() {
_longLivedThreadLocals.remove();
}
public static void clearShortLivedThreadLocals() {
_shortLivedThreadLocals.remove();
}
public static Map, Object>
getLongLivedThreadLocals() {
return _toMap(_longLivedThreadLocals.get());
}
public static Map, Object>
getShortLivedThreadLocals() {
return _toMap(_shortLivedThreadLocals.get());
}
public static void setThreadLocals(
Map, Object> longLivedCentralizedThreadLocals,
Map, Object>
shortLivedCentralizedThreadLocals) {
ThreadLocalMap threadLocalMap = _longLivedThreadLocals.get();
for (Map.Entry, Object> entry :
longLivedCentralizedThreadLocals.entrySet()) {
threadLocalMap.putEntry(entry.getKey(), entry.getValue());
}
threadLocalMap = _shortLivedThreadLocals.get();
for (Map.Entry, Object> entry :
shortLivedCentralizedThreadLocals.entrySet()) {
threadLocalMap.putEntry(entry.getKey(), entry.getValue());
}
}
public CentralizedThreadLocal(boolean shortLived) {
this(null, () -> null, shortLived);
}
public CentralizedThreadLocal(String name) {
this(name, () -> null, true);
}
public CentralizedThreadLocal(String name, Supplier supplier) {
this(name, supplier, true);
}
public CentralizedThreadLocal(
String name, Supplier supplier, boolean shortLived) {
this(name, supplier, null, shortLived);
}
public CentralizedThreadLocal(
String name, Supplier supplier, Function copyFunction,
boolean shortLived) {
if (shortLived) {
_hashCode = _shortLivedNextHasCode.getAndAdd(_HASH_INCREMENT);
}
else {
_hashCode = _longLivedNextHasCode.getAndAdd(_HASH_INCREMENT);
}
if (name == null) {
_name = super.toString();
}
else {
_name = name;
}
if (supplier == null) {
_supplier = () -> null;
}
else {
_supplier = supplier;
}
if (copyFunction == null) {
_copyFunction = this::_copy;
}
else {
_copyFunction = copyFunction;
}
_shortLived = shortLived;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
return false;
}
@Override
public T get() {
ThreadLocalMap threadLocalMap = _getThreadLocalMap();
Entry entry = threadLocalMap.getEntry(this);
if (entry == null) {
T value = initialValue();
threadLocalMap.putEntry(this, value);
return value;
}
return (T)entry._value;
}
@Override
public int hashCode() {
return _hashCode;
}
@Override
public void remove() {
ThreadLocalMap threadLocalMap = _getThreadLocalMap();
threadLocalMap.removeEntry(this);
}
@Override
public void set(T value) {
ThreadLocalMap threadLocalMap = _getThreadLocalMap();
threadLocalMap.putEntry(this, value);
}
@Override
public String toString() {
return _name;
}
@Override
protected T initialValue() {
return _supplier.get();
}
private static Map, Object> _toMap(
ThreadLocalMap threadLocalMap) {
Map, Object> map = new HashMap<>(
threadLocalMap._table.length);
for (Entry entry : threadLocalMap._table) {
while (entry != null) {
CentralizedThreadLocal
© 2015 - 2024 Weber Informatics LLC | Privacy Policy