Example usage for java.lang.ref Reference enqueue

List of usage examples for java.lang.ref Reference enqueue

Introduction

In this page you can find the example usage for java.lang.ref Reference enqueue.

Prototype

public boolean enqueue() 

Source Link

Document

Clears this reference object and adds it to the queue with which it is registered, if any.

Usage

From source file:SoftValueMap.java

/**
 * Returns the value that is mapped to a given 'key'. Returns
 * null if (a) this key has never been mapped or (b) a previously mapped
 * value has been cleared by the garbage collector and removed from the table.
 *
 * @param key mapping key [may not be null].
 *
 * @return Object value mapping for 'key' [can be null].
 *///w w  w  .  j  ava  2 s .  co m
public Object get(final Object key) {
    if (key == null)
        throw new IllegalArgumentException("null input: key");

    if ((++m_readAccessCount % m_readClearCheckFrequency) == 0)
        removeClearedValues();

    // index into the corresponding hash bucket:
    final int keyHashCode = key.hashCode();
    final SoftEntry[] buckets = m_buckets;
    final int bucketIndex = (keyHashCode & 0x7FFFFFFF) % buckets.length;

    Object result = null;

    // traverse the singly-linked list of entries in the bucket:
    for (SoftEntry entry = buckets[bucketIndex]; entry != null; entry = entry.m_next) {
        final Object entryKey = entry.m_key;

        if (IDENTITY_OPTIMIZATION) {
            // note: this uses an early identity comparison opimization, making this a bit
            // faster for table keys that do not override equals() [Thread, etc]
            if ((key == entryKey) || ((keyHashCode == entryKey.hashCode()) && key.equals(entryKey))) {
                final Reference ref = entry.m_softValue;
                result = ref.get(); // may return null to the caller

                // [see comment for ENQUEUE_FOUND_CLEARED_ENTRIES]
                if (ENQUEUE_FOUND_CLEARED_ENTRIES && (result == null)) {
                    ref.enqueue();
                }

                return result;
            }
        } else {
            if ((keyHashCode == entryKey.hashCode()) && key.equals(entryKey)) {
                final Reference ref = entry.m_softValue;
                result = ref.get(); // may return null to the caller

                // [see comment for ENQUEUE_FOUND_CLEARED_ENTRIES]
                if (ENQUEUE_FOUND_CLEARED_ENTRIES && (result == null)) {
                    ref.enqueue();
                }

                return result;
            }
        }
    }

    return null;
}