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

org.wildfly.common.ref.References Maven / Gradle / Ivy

There is a newer version: 62
Show newest version
/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2014 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * 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 org.wildfly.common.ref;

import static java.security.AccessController.doPrivileged;

import java.lang.ref.ReferenceQueue;
import java.security.PrivilegedAction;
import java.util.concurrent.atomic.AtomicInteger;

import org.wildfly.common.Assert;

/**
 * A set of utility methods for reference types.
 */
public final class References {
    private References() {
    }

    private static final Reference NULL = new Reference() {
        public Object get() {
            return null;
        }

        public Object getAttachment() {
            return null;
        }

        public void clear() {
        }

        public Type getType() {
            return Type.NULL;
        }

        public String toString() {
            return "NULL reference";
        }
    };

    static final class ReaperThread extends Thread {
        static final ReferenceQueue REAPER_QUEUE = new ReferenceQueue();

        static {
            final AtomicInteger cnt = new AtomicInteger(1);
            final PrivilegedAction action = () -> {
                final ReaperThread thr = new ReaperThread();
                thr.setName("Reference Reaper #" + cnt.getAndIncrement());
                thr.setDaemon(true);
                thr.start();
                return null;
            };
            for (int i = 0; i < 3; i ++) {
                doPrivileged(action);
            }
        }

        public void run() {
            for (;;) try {
                final java.lang.ref.Reference ref = REAPER_QUEUE.remove();
                if (ref instanceof CleanerReference) {
                    ((CleanerReference) ref).clean();
                }
                if (ref instanceof Reapable) {
                    reap((Reapable) ref);
                }
            } catch (InterruptedException ignored) {
                // we consume interrupts.
            } catch (Throwable cause) {
                Log.log.reapFailed(cause);
            }
        }

        @SuppressWarnings({ "unchecked" })
        private static  void reap(final Reapable reapable) {
            reapable.getReaper().reap((Reference) reapable);
        }
    }

    /**
     * Create a reference of a given type with the provided value and attachment.  If the reference type is
     * {@link Reference.Type#STRONG} or {@link Reference.Type#NULL} then the reaper argument is ignored.  If
     * the reference type is {@link Reference.Type#NULL} then the value and attachment arguments are ignored.
     *
     * @param type the reference type
     * @param value the reference value
     * @param attachment the attachment value
     * @param reaper the reaper to use, if any
     * @param  the reference value type
     * @param  the reference attachment type
     * @return the reference
     */
    public static  Reference create(Reference.Type type, T value, A attachment, Reaper reaper) {
        Assert.checkNotNullParam("type", type);
        if (value == null) return getNullReference();
        switch (type) {
            case STRONG:
                return new StrongReference(value, attachment);
            case WEAK:
                return new WeakReference(value, attachment, reaper);
            case PHANTOM:
                return new PhantomReference(value, attachment, reaper);
            case SOFT:
                return new SoftReference(value, attachment, reaper);
            case NULL:
                return getNullReference();
            default:
                throw Assert.impossibleSwitchCase(type);
        }
    }

    /**
     * Create a reference of a given type with the provided value and attachment.  If the reference type is
     * {@link Reference.Type#STRONG} or {@link Reference.Type#NULL} then the reference queue argument is ignored.  If
     * the reference type is {@link Reference.Type#NULL} then the value and attachment arguments are ignored.
     *
     * @param type the reference type
     * @param value the reference value
     * @param attachment the attachment value
     * @param referenceQueue the reference queue to use, if any
     * @param  the reference value type
     * @param  the reference attachment type
     * @return the reference
     */
    public static  Reference create(Reference.Type type, T value, A attachment, ReferenceQueue referenceQueue) {
        Assert.checkNotNullParam("type", type);
        if (referenceQueue == null) return create(type, value, attachment);
        if (value == null) return getNullReference();
        switch (type) {
            case STRONG:
                return new StrongReference(value, attachment);
            case WEAK:
                return new WeakReference(value, attachment, referenceQueue);
            case PHANTOM:
                return new PhantomReference(value, attachment, referenceQueue);
            case SOFT:
                return new SoftReference(value, attachment, referenceQueue);
            case NULL:
                return getNullReference();
            default:
                throw Assert.impossibleSwitchCase(type);
        }
    }

    /**
     * Create a reference of a given type with the provided value and attachment.  If the reference type is
     * {@link Reference.Type#PHANTOM} then this method will return a {@code null} reference because
     * such references are not constructable without a queue or reaper.  If the reference type is
     * {@link Reference.Type#NULL} then the value and attachment arguments are ignored.
     *
     * @param type the reference type
     * @param value the reference value
     * @param attachment the attachment value
     * @param  the reference value type
     * @param  the reference attachment type
     * @return the reference
     */
    public static  Reference create(Reference.Type type, T value, A attachment) {
        Assert.checkNotNullParam("type", type);
        if (value == null) return getNullReference();
        switch (type) {
            case STRONG:
                return new StrongReference(value, attachment);
            case WEAK:
                return new WeakReference(value, attachment);
            case PHANTOM:
                return getNullReference();
            case SOFT:
                return new SoftReference(value, attachment);
            case NULL:
                return getNullReference();
            default:
                throw Assert.impossibleSwitchCase(type);
        }
    }

    /**
     * Get a null reference.  This reference type is always cleared and does not retain an attachment; as such
     * there is only one single instance of it.
     *
     * @param  the reference value type
     * @param  the attachment value type
     * @return the null reference
     */
    @SuppressWarnings({ "unchecked" })
    public static  Reference getNullReference() {
        return (Reference) NULL;
    }
}