org.apache.ignite.cache.affinity

## Class AffinityKey<K>

- java.lang.Object
- - org.apache.ignite.cache.affinity.AffinityKey<K>

- All Implemented Interfaces:
    Externalizable, Serializable
  Direct Known Subclasses:
    AffinityUuid

      public class AffinityKey<K>
      extends Object
      implements Externalizable

  Optional wrapper for cache keys to provide support for custom affinity mapping. The value returned by `affinityKey(Object)` method will be used for key-to-node affinity.

  Note that the `equals(Object)` and `hashCode()` methods delegate directly to the wrapped cache key provided by `key()` method.

  This class is optional and does not have to be used. It only provides extra convenience whenever custom affinity mapping is required. Here is an example of how `Person` objects can be collocated with `Company` objects they belong to:

       Object personKey = new AffinityKey(myPersonId, myCompanyId);

       // Both, the company and the person objects will be cached on the same node.
       cache.put(myCompanyId, new Company(..));
       cache.put(personKey, new Person(..));

  For more information and examples of cache affinity refer to `AffinityKeyMapper` and `@AffinityKeyMapped` documentation.

  See Also:
    `AffinityKeyMapped`, `AffinityKeyMapper`, `AffinityFunction`, Serialized Form

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `AffinityKey()` Empty constructor.
    `AffinityKey(K key)` Initializes key wrapper for a given key.
    `AffinityKey(K key, Object affKey)` Initializes key together with its affinity key counter-part.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `<T> T` | `affinityKey()` Gets affinity key to use for affinity mapping.
    `void` | `affinityKey(Object affKey)` Sets affinity key to use for affinity mapping.
    `boolean` | `equals(Object obj)` Equality check which delegates to the underlying key equality.
    `int` | `hashCode()` Hash code implementation which delegates to the underlying `key()`.
    `K` | `key()` Gets wrapped key.
    `void` | `key(K key)` Sets wrapped key.
    `void` | `readExternal(ObjectInput in)`
    `String` | `toString()`
    `void` | `writeExternal(ObjectOutput out)`

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

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

- - ### Constructor Detail

    - #### AffinityKey

          public AffinityKey()

      Empty constructor.

    - #### AffinityKey

          public AffinityKey(K key)

      Initializes key wrapper for a given key. If affinity key is not initialized, then this key will be used for affinity.
      Parameters:
        `key` - Key.

    - #### AffinityKey

          public AffinityKey(K key,
                             Object affKey)

      Initializes key together with its affinity key counter-part.
      Parameters:
        `key` - Key.
        `affKey` - Affinity key.

  - ### Method Detail

    - #### key

          public K key()

      Gets wrapped key.
      Returns:
        Wrapped key.

    - #### key

          public void key(K key)

      Sets wrapped key.
      Parameters:
        `key` - Wrapped key.

    - #### affinityKey

          public <T> T affinityKey()

      Gets affinity key to use for affinity mapping. If affinity key is not provided, then `key` value will be returned.

      This method is annotated with `AffinityKeyMapped` and will be picked up by `GridCacheDefaultAffinityKeyMapper` automatically.

      Returns:
        Affinity key to use for affinity mapping.

    - #### affinityKey

          public void affinityKey(Object affKey)

      Sets affinity key to use for affinity mapping. If affinity key is not provided, then `key` value will be returned.
      Parameters:
        `affKey` - Affinity key to use for affinity mapping.

    - #### writeExternal

          public void writeExternal(ObjectOutput out)
                             throws IOException

      Specified by:
        `writeExternal` in interface `Externalizable`
      Throws:
        `IOException`

    - #### readExternal

          public void readExternal(ObjectInput in)
                            throws IOException,
                                   ClassNotFoundException

      Specified by:
        `readExternal` in interface `Externalizable`
      Throws:
        `IOException`
        `ClassNotFoundException`

    - #### hashCode

          public int hashCode()

      Hash code implementation which delegates to the underlying `key()`. Note, however, that different subclasses of `AffinityKey` will produce different hash codes.

      Users should override this method if different behavior is desired.

      Overrides:
        `hashCode` in class `Object`
      Returns:
        Hash code.

    - #### equals

          public boolean equals(Object obj)

      Equality check which delegates to the underlying key equality. Note, however, that different subclasses of `AffinityKey` will never be equal.

      Users should override this method if different behavior is desired.

      Overrides:
        `equals` in class `Object`
      Parameters:
        `obj` - Object to check for equality.
      Returns:
        `True` if objects are equal.

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

2020 Copyright © Apache Software Foundation
| Follow @ApacheIgnite
Ignite Database and Caching Platform | : ver. 2.9.0
Release Date | : October 15 2020