
context.ContextStore Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Utilities Show documentation
Show all versions of Utilities Show documentation
Java Utilities library containing general use utilities
package context;
import properties.PropertyUtilities;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import static properties.PropertyUtilities.*;
/**
* The ContextStore class provides a thread-safe storage for key-value pairs in a ConcurrentHashMap.
* Each thread has its own map, which avoids concurrency issues and ensures thread safety.
*
* @author Umut Ay Bora
* @version 1.4.0 (Documented in 1.4.0, released in an earlier version)
*/
@SuppressWarnings({"unused", "unchecked"})
public class ContextStore {
/**
* ThreadLocal variable to store a ConcurrentHashMap for each thread.
*/
private static final ThreadLocal> map = ThreadLocal.withInitial(ConcurrentHashMap::new);
/**
* Associates the specified value with the specified key in the ContextStore.
* If the key or value is null, the operation is skipped.
* This method is synchronized to ensure thread safety during the put operation.
*
* @param The type of the keys in the ContextStore.
* @param The type of the values in the ContextStore.
* @param key The key with which the specified value is to be associated.
* @param value The value to be associated with the specified key.
* @throws IllegalArgumentException If either the provided key or value is null.
* @see ContextStore#map
*/
public static synchronized void put(K key, V value) {
if (key != null && value != null) {
map.get().put(key, value);
}
}
/**
* Removes the entry with the specified key from the ContextStore.
* If the key is not present in the ContextStore, null is returned.
* This method is synchronized to ensure thread safety during the removal process.
*
* @param The type of the keys in the ContextStore.
* @param The type of the values in the ContextStore.
* @param key The key whose associated entry is to be removed.
* @throws IllegalArgumentException If the provided key is null.
* @see ContextStore#map
*/
public static synchronized void remove(K key) {
((ConcurrentHashMap) map.get()).remove(key);
}
/**
* Retrieves the value associated with the specified key from the ContextStore.
* If the key is not present in the ContextStore, null is returned.
* This method is synchronized to ensure thread safety during the retrieval process.
*
* @param The type of the keys in the map.
* @param The type of the values in the map.
* @param key The key whose associated value is to be retrieved.
* @return The value associated with the specified key, or null if the key is not present.
* @throws IllegalArgumentException If the provided key is null.
* @see ContextStore#map
*/
public static synchronized V get(K key) {
return key != null ? ((ConcurrentHashMap) map.get()).get(key) : null;
}
/**
* Retrieves the value associated with the specified key from the ContextStore.
* If the key is not present in the ContextStore, the provided defaultValue is returned.
* This method is synchronized to ensure thread safety during the retrieval process.
*
* @param The type of the keys in the map.
* @param The type of the values in the map.
* @param key The key whose associated value is to be retrieved.
* @param defaultValue The default value to be returned if the key is not present in the map.
* @return The value associated with the specified key, or the defaultValue if the key is not present.
* @throws IllegalArgumentException If the provided key is null.
* @see ContextStore#map
*/
public static synchronized V get(K key, V defaultValue) {
return key != null && map.get().get(key) != null ? ((ConcurrentHashMap) map.get()).get(key) : defaultValue;
}
/**
* Retrieves an unmodifiable set view of the keys contained in the ContextStore.
* This method is synchronized to ensure thread safety during the retrieval process.
*
* @param The type of the keys in the ContextStore.
* @param The type of the values in the ContextStore.
* @return An unmodifiable set view of the keys in the ContextStore.
* @see ContextStore#map
*/
public static synchronized Set items() {
return Collections.unmodifiableSet(((ConcurrentHashMap) map.get()).keySet());
}
/**
* Clears all key-value mappings from the ContextStore.
* This method is synchronized to ensure thread safety during the clear operation.
*
* @see ContextStore#map
*/
static synchronized void clear() {
map.get().clear();
}
/**
* Updates the value associated with the specified key in the ContextStore.
* If the key or value is null, the update operation is skipped.
* This method is synchronized to ensure thread safety during the update process.
*
* @param The type of the keys in the ContextStore.
* @param The type of the values in the ContextStore.
* @param key The key whose associated value is to be updated.
* @param value The new value to be associated with the specified key.
* @throws IllegalArgumentException If either the provided key or value is null.
* @see ContextStore#map
*/
public static synchronized void update(K key, V value) {
if (key != null && value != null) {
((ConcurrentHashMap) map.get()).computeIfPresent(key, (k, oldValue) -> value);
}
}
/**
* Merges the entries from the specified map into the ContextStore.
* This method is synchronized to ensure thread safety during the merge operation.
*
* @param maps Maps containing entries to be merged into the ContextStore.
* @see ContextStore#map
*/
public static synchronized void merge(Map, ?>... maps) {
for (Map, ?> map:maps) ContextStore.map.get().putAll(map);
}
/**
* Loads properties from one or more property files, merging them into the ContextStore for the current thread.
* The method is synchronized to ensure thread safety during the loading and merging process.
*
* @param propertyNames An array of property file names or paths to be loaded and merged.
* @throws IllegalArgumentException If the provided array of property names is null or empty.
* @throws RuntimeException If an error occurs during the loading or merging of properties.
* This can be an IOException or any other runtime exception.
* The specific exception details are logged for further investigation.
* @see PropertyUtilities#fromPropertyFile(String) fromPropertyFile(String)
* @see ContextStore#merge(Map...) UtilityPropertiesMap.merge(Map)
* @see ContextStore#map
*/
public static synchronized void loadProperties(String... propertyNames) {
for (String propertyName : propertyNames) merge(fromPropertyFile(propertyName));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy