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

com.owlike.genson.ThreadLocalHolder Maven / Gradle / Ivy

package com.owlike.genson;

import static com.owlike.genson.Operations.checkNotNull;

import java.util.HashMap;
import java.util.Map;

/**
 * Just another data holder that stores data in a threadlocal map.
 * If you only want to share data across serializers and deserializers prefer using {@link Context}.
 * Internally Genson uses it for the spring webmvc integration, so it can pass method signatures and
 * extract its annotations, etc.
 *
 * @author eugen
 * @see Context
 * @see com.owlike.genson.ext.spring.ExtendedReqRespBodyMethodProcessor ExtendedReqRespBodyMethodProcessor
 * @see com.owlike.genson.ext.spring.GensonMessageConverter GensonMessageConverter
 */
public final class ThreadLocalHolder {
  private final static ThreadLocal> _data = new ThreadLocal>();

  public static Object store(String key, Object parameter) {
    checkNotNull(key);
    return getPutIfMissing().put(key, parameter);
  }

  public static  T remove(String key, Class valueType) {
    checkNotNull(key, valueType);
    Map map = getPutIfMissing();
    T value = valueType.cast(map.get(key));
    map.remove(key);
    return value;
  }

  public static  T get(String key, Class valueType) {
    checkNotNull(key, valueType);
    return valueType.cast(getPutIfMissing().get(key));
  }

  private static Map getPutIfMissing() {
    Map map = _data.get();
    if (map == null) {
      map = new HashMap();
      _data.set(map);
    }
    return map;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy