Class Keys

java.lang.Object
org.eclipse.aether.Keys

public final class Keys extends Object
A helper class to create keys, to be used with SessionData and RepositoryCache instances as keys. Resolver codebase started with String keys, that are generally perfect for keys in these constructs, as long as they are used by components loaded once in system. As we saw, some subsystems like transports can be loaded multiple times. For example, in case of Maven, some transport may be present in Maven core, but also loaded up by some extension. In this case, key should distinguish between their ClassLoaders.

It is this class caller responsibility to use proper objects as keys, this class merely helps one to create composite keys out of object instances that are expected to be anyway good candidates as key. Examples of these objects are String instances but also Class instances, and many other types also usable as keys.

Important: never forget to perform null-check, when getting cache from RepositorySystemSession.getCache() method as it may return null, when cache is disabled session-wise.

Historical note: As mentioned above, use of String instances for keys is perfect match, and it worked from very start. But, as use cases got more and more complex and keys used started to be constructed in more and more sophisticated ways, but were still String instances. Believe, or not, the sole purpose of string keys was easier debugging. By using this helper class, this convenience should remain.

Since:
2.0.19
See Also:
  • Method Details

    • of

      public static Object of(Object... keys)
      Creates object instance usable as key in session data and cache. Objects passed to this method may or may not implement equals/hashCode, but it is responsibility of caller to understand what is she or he doing.

      If the first element of key elements is an object instance that was created by this method, creation of "subkeys" happens, where original key elements expanded from first element and are concatenated with new ones. This expansion happens only, if the first key element was already a key created by this class. If the arguments contain only one argument, and it is an object instance that was created by this method, the passed in instance is returned unmodified.

      Based on what kind of elements are used, one can create multiple kind of keys:

      • To create globally matched keys, preferred is to use String key elements
      • To create ClassLoader wide matched keys, make sure at least one key element is Class that needs to be scoped to ClassLoader
      • To create private, matched by creator only keys, make sure to have one key element, that requires instance equality matching, and there is no other same instance of element
      Parameters:
      keys - The key elements, it may not be null and may not have zero elements.
      Returns:
      An object instance usable as key.