org.jboss.as.server.deployment.SimpleAttachable Maven / Gradle / Ivy
/*
* JBoss, Home of Professional Open Source.
* Copyright 2010, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.server.deployment;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import org.jboss.as.server.ServerMessages;
/**
* A simple implementation of {@link Attachable} which may be used as a base class or on a standalone basis.
*
* This class is thread safe, as all methods are synchronized.
*
* @author David M. Lloyd
*/
public class SimpleAttachable implements Attachable {
private final Map, Object> attachments = new HashMap, Object>();
/** {@inheritDoc} */
public synchronized boolean hasAttachment(AttachmentKey> key) {
if (key == null) {
return false;
}
return attachments.containsKey(key);
}
/** {@inheritDoc} */
public synchronized T getAttachment(final AttachmentKey key) {
if (key == null) {
return null;
}
return key.cast(attachments.get(key));
}
/** {@inheritDoc} */
public synchronized List getAttachmentList(AttachmentKey extends List> key) {
if (key == null) {
return null;
}
List list = key.cast(attachments.get(key));
if (list == null) {
return Collections.emptyList();
}
return list;
}
/** {@inheritDoc} */
public synchronized T putAttachment(final AttachmentKey key, final T value) {
if (key == null) {
throw ServerMessages.MESSAGES.nullAttachmentKey();
}
return key.cast(attachments.put(key, key.cast(value)));
}
/** {@inheritDoc} */
public synchronized T removeAttachment(final AttachmentKey key) {
if (key == null) {
return null;
}
return key.cast(attachments.remove(key));
}
/** {@inheritDoc} */
public synchronized void addToAttachmentList(final AttachmentKey> key, final T value) {
if (key != null) {
final Map, Object> attachments = this.attachments;
final AttachmentList list = key.cast(attachments.get(key));
if (list == null) {
final AttachmentList newList = new AttachmentList(((ListAttachmentKey) key).getValueClass());
attachments.put(key, newList);
newList.add(value);
} else {
list.add(value);
}
}
}
public synchronized Collection> attachmentKeys() {
return new HashSet>(attachments.keySet());
}
}