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

org.javasimon.AttributesSupport Maven / Gradle / Ivy

There is a newer version: 4.2.0
Show newest version
package org.javasimon;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

/**
 * Attributes implementation that creates attributes map lazily. Is synchronized to ensure thread-safety.
 *
 * @author Richard "Virgo" Richter
 * @since 3.4
 */
final class AttributesSupport implements HasAttributes {
	private Map attributes;

	@Override
	public synchronized void setAttribute(String name, Object value) {
		if (attributes == null) {
			attributes = new HashMap();
		}
		attributes.put(name, value);
	}

	@Override
	public synchronized Object getAttribute(String name) {
		if (attributes == null) {
			return null;
		}
		return attributes.get(name);
	}

	@SuppressWarnings("unchecked")
	@Override
	public  T getAttribute(String name, Class clazz) {
		return (T) getAttribute(name);
	}

	@Override
	public synchronized void removeAttribute(String name) {
		if (attributes != null) {
			attributes.remove(name);
		}
	}

	@Override
	public synchronized Iterator getAttributeNames() {
		if (attributes == null) {
			return Collections.emptySet().iterator();
		}
		return attributes.keySet().iterator();
	}

	@Override
	public Map getCopyAsSortedMap() {
		return new TreeMap(attributes);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy