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

org.jboss.modules.ref.References Maven / Gradle / Ivy

There is a newer version: 2.1.5.Final
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.jboss.modules.ref;

import java.lang.ref.ReferenceQueue;

/**
 * A set of utility methods for reference types.
 */
public final class References {
    @SuppressWarnings({ "RawUseOfParameterizedType" })
    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";
        }
    };

    private References() {
    }

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

        static {
            final ReaperThread thr = new ReaperThread();
            thr.setName("Reference Reaper");
            thr.setDaemon(true);
            thr.start();
        }

        public void run() {
            for (;;) try {
                final java.lang.ref.Reference ref = REAPER_QUEUE.remove();
                if (ref instanceof Reapable) {
                    reap((Reapable) ref);
                }
            } catch (InterruptedException ignored) {
            } catch (Throwable ignored) {
            }
        }

        @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) {
        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 new IllegalStateException();
        }
    }

    /**
     * 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) {
        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 new IllegalStateException();
        }
    }

    /**
     * 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 throw an {@code IllegalArgumentException} 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
     * @throws IllegalArgumentException if the reference type is {@link Reference.Type#PHANTOM}
     */
    public static  Reference create(Reference.Type type, T value, A attachment) throws IllegalArgumentException {
        switch (type) {
            case STRONG:
                return new StrongReference(value, attachment);
            case WEAK:
                return new WeakReference(value, attachment);
            case PHANTOM:
                throw new IllegalArgumentException("Phantom reference may not be created without a queue or reaper");
            case SOFT:
                return new SoftReference(value, attachment);
            case NULL:
                return getNullReference();
            default:
                throw new IllegalStateException();
        }
    }

    /**
     * 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;
    }
}