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

org.milyn.javabean.context.BeanIdStore Maven / Gradle / Ivy

The newest version!
package org.milyn.javabean.context;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.milyn.assertion.AssertArgument;
import org.milyn.javabean.repository.BeanId;

/**
 * Bean Id Store
 *
 * Represents a map of BeanId's. Every BeanId has it own unique index. The index
 * is incremental. The index starts with zero.
 *
 * Once a BeanId is registered it can never be unregistered.
 *
 * This object is thread safe.
 *
 * @author [email protected]
 *
 */
public class BeanIdStore {

	private volatile HashMap  beanIdMap = new  HashMap();

	public  BeanId register(String beanIdName) {
		AssertArgument.isNotEmpty(beanIdName, "beanIdName");

		BeanId beanId = beanIdMap.get(beanIdName);
		if(beanId == null) {

			synchronized(this) {

				beanId = beanIdMap.get(beanIdName);
				if(beanId == null) {
					@SuppressWarnings("unchecked")
					HashMap newBeanIdMap = (HashMap) beanIdMap.clone();

					beanId = new BeanId(this, newBeanIdMap.size(), beanIdName);

					newBeanIdMap.put(beanIdName, beanId);

					beanIdMap = newBeanIdMap;
				}

			}
		}

		return beanId;
	}

	public BeanId getBeanId(String beanId) {
		return beanIdMap.get(beanId);
	}

	public boolean containsBeanId(String beanId) {
		return beanIdMap.containsKey(beanId);
	}

	/**
	 * Returns a copy of the internal bean id map
	 *
	 * @return An map where the key is the string based beanId and the value is the BeanId.
	 *
	 */
	public synchronized Map getBeanIdMap() {
		return Collections.unmodifiableMap(beanIdMap);
	}

	/**
	 * @return the current index size.
	 *
	 */
	public int size() {
		return beanIdMap.size();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy