Package org.apache.commons.collections4.map

# Class AbstractReferenceMap.ReferenceEntry<K,V>

java.lang.Object
org.apache.commons.collections4.map.AbstractHashedMap.HashEntry<K,V>
org.apache.commons.collections4.map.AbstractReferenceMap.ReferenceEntry<K,V>

Type Parameters:
  `K` - The type of the keys
  `V` - The type of the values

All Implemented Interfaces:
  `Map.Entry<K,V>`, `KeyValue<K,V>`

Enclosing class:
  `AbstractReferenceMap<K,V>`

protected static class AbstractReferenceMap.ReferenceEntry<K,V> extends AbstractHashedMap.HashEntry<K,V>
A MapEntry implementation for the map.

If getKey() or getValue() returns null, it means the mapping is stale and should be removed.

Since:
  3.1

- ## Field Summary

  ### Fields inherited from class org.apache.commons.collections4.map.AbstractHashedMap.HashEntry

  `hashCode, key, next, value`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ReferenceEntry(AbstractReferenceMap<K,V> parent, AbstractHashedMap.HashEntry<K,V> next, int hashCode, K key, V value)`
  Creates a new entry object for the ReferenceMap.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `equals(Object obj)`
  Compares this map entry to another.
  `K`
  `getKey()`
  Gets the key from the entry.
  `V`
  `getValue()`
  Gets the value from the entry.
  `int`
  `hashCode()`
  Gets the hash code of the entry using temporary hard references.
  `protected AbstractReferenceMap.ReferenceEntry<K,V>`
  `next()`
  Gets the next entry in the bucket.
  `protected void`
  `nullValue()`
  This method can be overridden to provide custom logic to purge value
  `protected void`
  `onPurge()`
  This is the callback for custom "after purge" logic
  `protected boolean`
  `purge(Reference<?> ref)`
  Purges the specified reference
  `V`
  `setValue(V value)`
  Sets the value of the entry.
  `protected <T> Object`
  `toReference(AbstractReferenceMap.ReferenceStrength type, T referent, int hash)`
  Constructs a reference of the given type to the given referent.

  ### Methods inherited from class org.apache.commons.collections4.map.AbstractHashedMap.HashEntry

  `toString`

  ### Methods inherited from class java.lang.Object

  `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### ReferenceEntry

    public ReferenceEntry(AbstractReferenceMap<K,V> parent, AbstractHashedMap.HashEntry<K,V> next, int hashCode, K key, V value)
    Creates a new entry object for the ReferenceMap.
    Parameters:
      `parent` - The parent map
      `next` - The next entry in the hash bucket
      `hashCode` - The hash code of the key
      `key` - The key
      `value` - The value

- ## Method Details

  - ### equals

    public boolean equals(Object obj)
    Compares this map entry to another.

    This implementation uses `isEqualKey` and `isEqualValue` on the main map for comparison.

    Specified by:
      `equals` in interface `Map.Entry<K,V>`
    Overrides:
      `equals` in class `AbstractHashedMap.HashEntry<K,V>`
    Parameters:
      `obj` - The other map entry to compare to
    Returns:
      true if equal, false if not

  - ### getKey

    public K getKey()
    Gets the key from the entry. This method dereferences weak and soft keys and thus may return null.
    Specified by:
      `getKey` in interface `KeyValue<K,V>`
    Specified by:
      `getKey` in interface `Map.Entry<K,V>`
    Overrides:
      `getKey` in class `AbstractHashedMap.HashEntry<K,V>`
    Returns:
      The key, which may be null if it was garbage collected

  - ### getValue

    public V getValue()
    Gets the value from the entry. This method dereferences weak and soft value and thus may return null.
    Specified by:
      `getValue` in interface `KeyValue<K,V>`
    Specified by:
      `getValue` in interface `Map.Entry<K,V>`
    Overrides:
      `getValue` in class `AbstractHashedMap.HashEntry<K,V>`
    Returns:
      The value, which may be null if it was garbage collected

  - ### hashCode

    public int hashCode()
    Gets the hash code of the entry using temporary hard references.

    This implementation uses `hashEntry` on the main map.

    Specified by:
      `hashCode` in interface `Map.Entry<K,V>`
    Overrides:
      `hashCode` in class `AbstractHashedMap.HashEntry<K,V>`
    Returns:
      The hash code of the entry

  - ### next

    protected AbstractReferenceMap.ReferenceEntry<K,V> next()
    Gets the next entry in the bucket.
    Returns:
      The next entry in the bucket

  - ### nullValue

    protected void nullValue()
    This method can be overridden to provide custom logic to purge value

  - ### onPurge

    protected void onPurge()
    This is the callback for custom "after purge" logic

  - ### purge

    protected boolean purge(Reference<?> ref)
    Purges the specified reference
    Parameters:
      `ref` - The reference to purge
    Returns:
      true or false

  - ### setValue

    public V setValue(V value)
    Sets the value of the entry.
    Specified by:
      `setValue` in interface `Map.Entry<K,V>`
    Overrides:
      `setValue` in class `AbstractHashedMap.HashEntry<K,V>`
    Parameters:
      `value` - The object to store
    Returns:
      The previous value

  - ### toReference

    protected <T> Object toReference(AbstractReferenceMap.ReferenceStrength type, T referent, int hash)
    Constructs a reference of the given type to the given referent. The reference is registered with the queue for later purging.
    Type Parameters:
      `T` - The type of the referenced object
    Parameters:
      `type` - HARD, SOFT or WEAK
      `referent` - The object to refer to
      `hash` - The hash code of the key of the mapping; this number might be different from referent.hashCode() if the referent represents a value and not a key
    Returns:
      The reference to the object