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

org.jboss.ejb.client.Attachable Maven / Gradle / Ivy

Go to download

This artifact provides a single jar that contains all classes required to use remote Jakarta Enterprise Beans and Jakarta Messaging, including all dependencies. It is intended for use by those not using maven, maven users should just import the Jakarta Enterprise Beans and Jakarta Messaging BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up with different versions on classes on the class path).

There is a newer version: 35.0.0.Final
Show newest version
/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2011, 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.ejb.client;

import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Map;

/**
 * An object which may have attachments.  Even if the object is serializable, its
 * attachment map is not and will always deserialize empty.
 *
 * @author David M. Lloyd
 */
public abstract class Attachable {
    private final Map attachments;

    private Attachable(Map attachments) {
        this.attachments = attachments;
    }

    Attachable() {
        this(new IdentityHashMap());
    }

    /**
     * Construct a new instance, sharing attachments with another instance.
     *
     * @param attachable the attachments to share
     */
    Attachable(final Attachable attachable) {
        this(attachable.attachments);
    }

    /**
     * Get an attachment from this object.
     *
     * @param key the attachment key
     * @param  the attachment type
     * @return the attachment value
     */
    @SuppressWarnings("unchecked")
    public  T getAttachment(AttachmentKey key) {
        if (key == null) return null;
        final Map attachments = this.attachments;
        synchronized (attachments) {
            return (T) attachments.get(key);
        }
    }

    /**
     * Returns the attachments applicable for this {@link Attachable}. The returned {@link Map}
     * is an unmodifiable {@link Map}. If there are no attachments for this {@link Attachable}
     * then this method returns an empty {@link Map}
     *
     * @return
     */
    public Map getAttachments() {
        if (this.attachments == null) {
            return Collections.emptyMap();
        }
        return Collections.unmodifiableMap(this.attachments);
    }

    /**
     * Set an attachment on this object.
     *
     * @param key   the attachment key
     * @param value the attachment's new value (may not be {@code null})
     * @param    the attachment type
     * @return the previous attachment value, or {@code null} if there was none
     */
    @SuppressWarnings("unchecked")
    public  T putAttachment(AttachmentKey key, T value) {
        if (key == null) {
            throw Logs.MAIN.paramCannotBeNull("key");
        }
        if (value == null) {
            throw Logs.MAIN.paramCannotBeNull("value");
        }
        final Map attachments = this.attachments;
        synchronized (attachments) {
            return (T) attachments.put(key, value);
        }
    }

    /**
     * Set an attachment on this object if an existing attachment does not already exist.
     *
     * @param key   the attachment key
     * @param value the attachment's new value (may not be {@code null})
     * @param    the attachment type
     * @return the previous attachment value, or {@code null} if there was none
     */
    @SuppressWarnings("unchecked")
    public  T putAttachmentIfAbsent(AttachmentKey key, T value) {
        if (key == null) {
            throw Logs.MAIN.paramCannotBeNull("key");
        }
        if (value == null) {
            throw Logs.MAIN.paramCannotBeNull("value");
        }
        final Map attachments = this.attachments;
        synchronized (attachments) {
            return (T) (attachments.containsKey(key) ? attachments.get(key) : attachments.put(key, value));
        }
    }

    /**
     * Replace an attachment on this object if an existing attachment exists.
     *
     * @param key   the attachment key
     * @param value the attachment's new value (may not be {@code null})
     * @param    the attachment type
     * @return the previous attachment value, or {@code null} if there was none
     */
    @SuppressWarnings("unchecked")
    public  T replaceAttachment(AttachmentKey key, T value) {
        if (key == null) return null;
        if (value == null) {
            throw Logs.MAIN.paramCannotBeNull("value");
        }
        final Map attachments = this.attachments;
        synchronized (attachments) {
            return (T) (attachments.containsKey(key) ? attachments.put(key, value) : null);
        }
    }

    /**
     * Replace an attachment on this object if an existing attachment exists with a certain value.
     *
     * @param key      the attachment key
     * @param oldValue the attachment's expected value (may not be {@code null})
     * @param newValue the attachment's new value (may not be {@code null})
     * @param       the attachment type
     * @return {@code true} if the old value matched and the value was replaced; {@code false} otherwise
     */
    @SuppressWarnings("unchecked")
    public  boolean replaceAttachment(AttachmentKey key, T oldValue, T newValue) {
        if (key == null) return false;
        if (oldValue == null) return false;
        if (newValue == null) {
            throw Logs.MAIN.paramCannotBeNull("newValue");
        }
        final Map attachments = this.attachments;
        synchronized (attachments) {
            Object lhs = attachments.get(key);
            return attachments.containsKey(key) && oldValue.equals(lhs) && attachments.put(key, newValue) != null;
        }
    }

    /**
     * Remove and return an attachment value.
     *
     * @param key the attachment key
     * @param  the attachment type
     * @return the previous value of the attachment, or {@code null} if there was none
     */
    @SuppressWarnings("unchecked")
    public  T removeAttachment(AttachmentKey key) {
        if (key == null) return null;
        final Map attachments = this.attachments;
        synchronized (attachments) {
            return (T) attachments.remove(key);
        }
    }

    /**
     * Remove an attachment if it has a certain value.
     *
     * @param key   the attachment key
     * @param value the attachment's expected value (may not be {@code null})
     * @param    the attachment type
     * @return {@code true} if the value was removed, {@code false} if there was no attachment
     */
    @SuppressWarnings("unchecked")
    public  boolean removeAttachment(AttachmentKey key, T value) {
        if (key == null) return false;
        if (value == null) return false;
        final Map attachments = this.attachments;
        synchronized (attachments) {
            Object lhs = attachments.get(key);
            return attachments.containsKey(key) && value.equals(lhs) && attachments.remove(key) != null;
        }
    }

    void clearAttachments() {
        attachments.clear();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy