/*********************************************************************************
 * Copyright 2015-present trivago GmbH
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **********************************************************************************/

package com.trivago.triava.tcache;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

import com.trivago.triava.tcache.core.Builder;
import com.trivago.triava.tcache.eviction.Cache;
import com.trivago.triava.tcache.util.CacheSizeInfo;
import com.trivago.triava.tcache.util.ObjectSizeCalculatorInterface;

/**
 * The TCacheFactory allows to create Cache instances via {@link #builder()}, and also supplies administrative methods for the
 * managed caches, like shutting down all registered Caches. The preferred way of obtaining a TCacheFactory
 * instance for application code is a call to {@link #standardFactory()}. Library code should instantiate a new
 * TCacheFactory instance, so it can manage its own Cache collection.
 * 
 * @author cesken
 * @since 2015-03-10
 *
 */
public class TCacheFactory
{
	private final CopyOnWriteArrayList<Cache<?, ?>> CacheInstances = new CopyOnWriteArrayList<>();

	static final TCacheFactory standardFactory = new TCacheFactory();

	/**
	 * Returns the standard factory. The factory can produce Builder instances via {@link #builder()}.
	 * <br>
	 * Library code should not use this method, but create a new TCacheFactory
	 * instance for managing its own Cache collection.
	 * 
	 * @return The standard cache factory.
	 */
	public static TCacheFactory standardFactory()
	{
		return standardFactory;
	}

	/**
	 * Returns a Builder 
	 * @return A Builder
	 */
	public <K, V> Builder<K, V> builder()
	{
		return new Builder<>(this);
	}

	/**
	 * Registers a Cache to this factory. Registered caches will be used for bulk operations like
	 * {@link #shutdownAll()}.
	 * 
	 * @param cache
	 */
	public void registerCache(Cache<?, ?> cache)
	{
		// Hint: "cache" cannot escape. It is safely published, as it is put in a concurrent collection
		CacheInstances.add(cache);
	}

	/**
	 * Shuts down all Cache instances, which were registered via {@link #registerCache(Cache)}. It waits until
	 * all cleaners have stopped.
	 */
	public void shutdownAll()
	{
		for (Cache<?, ?> cache : CacheInstances)
		{
			cache.shutdown();
		}
	}

	/**
	 * Reports size of all Cache instances, which were registered via {@link #registerCache(Cache)}. Using
	 * this method can create high load, and may require particular permissions, depending on the used object
	 * size calculator.
	 * <p>
	 * <b>USE WITH CARE!!!</b>
	 */
	public Map<String, CacheSizeInfo> reportAllCacheSizes(ObjectSizeCalculatorInterface objectSizeCalculator)
	{
		Map<String, CacheSizeInfo> infoMap = new HashMap<>();
		for (Cache<?, ?> cache : CacheInstances)
		{
			infoMap.put(cache.id(), cache.reportSize(objectSizeCalculator));
		}
		return infoMap;
	}

	/**
	 * Returns the list of Caches that have been registered via {@link #registerCache(Cache)}.
	 * 
	 * @return The cache list
	 */
	public List<Cache<?, ?>> instances()
	{
		return new ArrayList<>(CacheInstances);
	}

}

@@ -1760,16 +1760,66 @@
 ist%3C%3E();
+%0A%09private final Object factoryLock = new Object();
 %0A%0A%09stati
@@ -2540,59 +2540,465 @@
 %0A%09 *
-/%0A%09public void registerCache(Cache%3C?, ?%3E cache)%0A%09%7B%0A
+  @throws IllegalStateException If a cache with the same id is already registered in this factory.%0A%09 */%0A%09public void registerCache(Cache%3C?, ?%3E cache)%0A%09%7B%0A%09%09String id = cache.id();%0A%09%09synchronized (factoryLock)%0A%09%09%7B%0A%09%09%09for (Cache%3C?, ?%3E registeredCache : CacheInstances)%0A%09%09%09%7B%0A%09%09%09%09if (registeredCache.id().equals(id))%0A%09%09%09%09%7B%0A%09%09%09%09%09throw new IllegalStateException(%22Cache with the same id is already registered: %22 + id);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%0A%09%09%09CacheInstances.add(cache);%09%09%09%0A%09
 %09%09//
@@ -3097,34 +3097,9 @@
 n%0A%09%09
-CacheInstances.add(cache);
+%7D
 %0A%09%7D%0A

Throw IllegalStateException when trying to register two caches with identical ID