Class Keys


  • public class Keys
    extends Object

    Gives us a slightly more organized interface for manipulating keys. While this is part of Objectify's public interface, you probably shouldn't use it. It's subject to change without notice. If you want to work with keys, use the Key.create() methods.

    Author:
    Jeff Schnitzer
    • Constructor Detail

      • Keys

        public Keys()
    • Method Detail

      • keyOf

        public <T> Key<T> keyOf​(T pojo,
                                String namespaceHint)
        Returns:
        the Key for a registered pojo entity.
      • rawKeyOf

        public com.google.cloud.datastore.Key rawKeyOf​(Object pojo,
                                                       String namespaceHint)
        Returns:
        the native datastore key for a registered pojo entity (or FullEntity).
      • getMetadataSafe

        public <T> KeyMetadata<T> getMetadataSafe​(Class<T> clazz)
        Returns:
        the metadata for a registered pojo, or throw exception if none
        Throws:
        IllegalStateException - if the pojo class has not been registered
      • getMetadataSafe

        public <T> KeyMetadata<T> getMetadataSafe​(T pojo)
        Returns:
        the metadata for a registeerd pojo, or throw exception if none
        Throws:
        IllegalStateException - if the pojo class has not been registered
      • getMetadata

        public <T> KeyMetadata<T> getMetadata​(Key<T> key)
        Returns:
        the metadata for a registered pojo, or null if there is none
      • anythingToKey

        public <T> Key<T> anythingToKey​(Object keyOrEntity,
                                        String namespaceHint)

        Gets the Key given an object that might be a Key, Key, or entity.

        Parameters:
        keyOrEntity - must be a Key, Key, or registered entity.
        Throws:
        NullPointerException - if keyOrEntity is null
        IllegalArgumentException - if keyOrEntity is not a Key, Key, or registered entity
      • anythingToRawKey

        public com.google.cloud.datastore.Key anythingToRawKey​(Object keyOrEntity,
                                                               String namespaceHint)

        Gets the raw datstore Key given an object that might be a Key, Key, or entity.

        Parameters:
        keyOrEntity - must be a Key, Key, or registered entity.
        Throws:
        NullPointerException - if keyOrEntity is null
        IllegalArgumentException - if keyOrEntity is not a Key, Key, or registered entity
      • getKey

        public static com.google.cloud.datastore.Key getKey​(com.google.cloud.datastore.FullEntity<?> entity)
        Returns:
        the Key, or throw an exception if entity's key is missing or incomplete
      • requiresAutogeneratedId

        public boolean requiresAutogeneratedId​(Object entity)
        Returns:
        true of the entity has a null id which can be autogenerated on save
      • createRaw

        public com.google.cloud.datastore.Key createRaw​(String namespace,
                                                        com.google.cloud.datastore.Key parent,
                                                        String kind,
                                                        long id)
        namespace and parent are mutually exclusive
        Parameters:
        parent - can be null for root keys
      • createRaw

        public com.google.cloud.datastore.Key createRaw​(String namespace,
                                                        com.google.cloud.datastore.Key parent,
                                                        String kind,
                                                        String name)
        namespace and parent are mutually exclusive
        Parameters:
        parent - can be null for root keys
      • createRawAny

        public com.google.cloud.datastore.Key createRawAny​(String namespace,
                                                           com.google.cloud.datastore.Key parent,
                                                           String kind,
                                                           Object id)
        Construct a Key from a Long or String id
        Parameters:
        id - must be either Long or String
      • createRawIncomplete

        public com.google.cloud.datastore.IncompleteKey createRawIncomplete​(String namespace,
                                                                            com.google.cloud.datastore.Key parent,
                                                                            String kind)
        namespace and parent are mutually exclusive
        Parameters:
        parent - can be null for root keys
      • createKey

        public <T> Key<T> createKey​(String namespace,
                                    Key<?> parent,
                                    Class<T> kind,
                                    long id)
        namespace and parent are mutually exclusive
        Parameters:
        parent - can be null for root keys
      • createKey

        public <T> Key<T> createKey​(String namespace,
                                    Key<?> parent,
                                    Class<T> kind,
                                    String name)
        namespace and parent are mutually exclusive
        Parameters:
        parent - can be null for root keys
      • createKeyAny

        public <T> Key<T> createKeyAny​(String namespace,
                                       Key<?> parent,
                                       Class<T> kind,
                                       Object id)
        Construct a Key from a Long or String id namespace and parent are mutually exclusive
        Parameters:
        id - must be either Long or String
      • raw

        public static com.google.cloud.datastore.Key raw​(Key<?> key)
        Null-safe extraction of the raw key
      • createKeys

        public <T> List<Key<T>> createKeys​(String namespace,
                                           Key<?> parent,
                                           Class<T> kind,
                                           Iterable<?> ids)
        Make a list of Keys
        Parameters:
        namespace - must be exclusive with parent; only one can be provided
        parent - must be exclusive with namespace; only one can be provided
        ids - must contain either Long or String
      • toArray

        public static com.google.cloud.datastore.Key[] toArray​(Collection<com.google.cloud.datastore.Key> collection)
      • getIdValue

        public static <S> com.google.cloud.datastore.Value<S> getIdValue​(com.google.cloud.datastore.IncompleteKey key)
        Gets the String or Long id from the key as a Value, or null if incomplete
      • fromUrlSafe

        public static com.google.cloud.datastore.Key fromUrlSafe​(String urlSafeKey)
        Understands both the legacy format "ag1zfnZvb2Rvb2R5bmUwcgcLEgFCGAEM" and new format, providing the key either way.
      • adjustNamespace

        public static com.google.cloud.datastore.KeyFactory adjustNamespace​(com.google.cloud.datastore.KeyFactory keyFactory,
                                                                            String namespace)
        Be sensitive to the thread local namespace, if set
      • adjustNamespace

        public static <B extends com.google.cloud.datastore.BaseKey.Builder<B>> B adjustNamespace​(B builder,
                                                                                                  String namespace)
        Be sensitive to the thread local namespace, if set