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

org.hibernate.validator.resourceloading.CachingResourceBundleLocator Maven / Gradle / Ivy

Go to download

JPMS Module-Info's for a few of the Jakarta Libraries. These will be removed as time goes by

There is a newer version: 62
Show newest version
/*
 * Hibernate Validator, declare and validate application constraints
 *
 * License: Apache License, Version 2.0
 * See the license.txt file in the root directory or .
 */
package org.hibernate.validator.resourceloading;

import java.util.Locale;
import java.util.ResourceBundle;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.hibernate.validator.spi.resourceloading.ResourceBundleLocator;

/**
 * A {@link ResourceBundleLocator} implementation that wraps around another
 * locator and caches values retrieved from that locator.
 *
 * @author Gunnar Morling
 * @author Kevin Pollet <[email protected]> (C) 2011 SERLI
 */
public class CachingResourceBundleLocator extends DelegatingResourceBundleLocator {

	private final ConcurrentMap bundleCache = new ConcurrentHashMap();

	/**
	 * Creates a new CachingResourceBundleLocator.
	 *
	 * @param delegate The locator from which the values actually will be retrieved.
	 */
	public CachingResourceBundleLocator(ResourceBundleLocator delegate) {
		super( delegate );
	}

	@Override
	public ResourceBundle getResourceBundle(Locale locale) {
		ResourceBundle cachedResourceBundle = bundleCache.get( locale );
		if ( cachedResourceBundle == null ) {
			final ResourceBundle bundle = super.getResourceBundle( locale );
			if ( bundle != null ) {
				cachedResourceBundle = bundleCache.putIfAbsent( locale, bundle );
				if ( cachedResourceBundle == null ) {
					return bundle;
				}
			}
		}
		return cachedResourceBundle;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy