Class KeyUtils


  • @BetaApi
    public class KeyUtils
    extends java.lang.Object
    Common utilities for handling key declarations and configurations.
    Since:
    25.2.0
    • Field Detail

      • DEFAULT_KEY_NAME

        public static final java.lang.String DEFAULT_KEY_NAME
        Default name of key columns. This is not a valid key column name, but can be used in case an entity provider only has one key column. While it is allowed to use this name in such cases, it is always recommended to set key column names for every key column in every provider.
        See Also:
        Constant Field Values
      • GENERATED_KEY_COLUMN_NAME

        public static final java.lang.String GENERATED_KEY_COLUMN_NAME
        Default generated name of key column, this is a generated name used when an entity provider has a composite key or an unsupported key type
        See Also:
        Constant Field Values
      • GENERATED_SOURCE_VERTEX_KEY_COLUMN_NAME

        public static final java.lang.String GENERATED_SOURCE_VERTEX_KEY_COLUMN_NAME
        Default generated name of source vertex key column, this is a generated name used when the source vertex provider has a composite key or an unsupported key type
        See Also:
        Constant Field Values
      • GENERATED_DESTINATION_VERTEX_KEY_COLUMN_NAME

        public static final java.lang.String GENERATED_DESTINATION_VERTEX_KEY_COLUMN_NAME
        Default generated name of source vertex key column, this is a generated name used when the destination vertex provider has a composite key or an unsupported key type
        See Also:
        Constant Field Values
    • Method Detail

      • createKeyDescriptors

        public static java.util.List<KeyColumnDescriptor> createKeyDescriptors​(java.lang.String keyColumnName,
                                                                               IdType keyColumnType)
        Create a list of key column descriptors for a single key column. This will also validate the resulting key column list.
        Parameters:
        keyColumnName - The key column name.
        keyColumnType - The key column type.
        Returns:
        A list of key column descriptors matching the single key column.
      • createKeyDescriptors

        public static java.util.List<KeyColumnDescriptor> createKeyDescriptors​(java.util.List<java.lang.String> keyColumnNames,
                                                                               java.util.List<IdType> keyColumnTypes)
        Create a list of key column descriptors from lists of key column names and types. The resulting list of key columns will have the same size as both of the lists. Input lists must have the same size. This will also validate the resulting key column list.
        Parameters:
        keyColumnNames - A list of key column names (if there is only one key column, the name can be null).
        keyColumnTypes - A list of key column types with the same length as the previous list.
        Returns:
        A list of key column descriptors.
      • getType

        public static IdType getType​(java.util.List<? extends KeyColumnDescriptor> keyColumns)
        Get the type of the first key column in a list of key columns, if and only if there is exactly one key column.
        Parameters:
        keyColumns - The list of key column.
        Returns:
        Type of the first and only key column.
        Throws:
        java.lang.IllegalArgumentException - if there is not exactly one key column in the list.
      • getTypeOrDefault

        public static IdType getTypeOrDefault​(java.util.List<? extends KeyColumnDescriptor> keyColumns,
                                              IdType defaultType)
        Get the type of the only key column if and only if there is exactly one, otherwise return the default type
        Parameters:
        keyColumns - The list of key column.
        defaultType - the default type
        Returns:
        Type of the first and only key column or default type.
      • getSingleKeyColumn

        public static <K> K getSingleKeyColumn​(java.util.List<? extends K> keyColumns)
        Get the key column descriptor for the first key column in the list of key columns, if and only if there is exactly one key column. An exception will be thrown if there is any other number of key column in the list. Note that this function is generic and can be used with any list.
        Type Parameters:
        K - Key object type.
        Parameters:
        keyColumns - A list of object (typically key column descriptors).
        Returns:
        The first (and only) element in the list.
        Throws:
        java.lang.IllegalArgumentException - if there is not exactly one element in the list.
      • getSingleKeyColumnOrNull

        public static <K> K getSingleKeyColumnOrNull​(java.util.List<? extends K> keyColumns)
        Similar to getSingleKeyColumn(List), except returns null, if the provided list is empty.
        Type Parameters:
        K - The object type.
        Parameters:
        keyColumns - A list of objects (key column descriptors).
        Returns:
        The single object in the list, or null.
      • getNameOrNull

        public static java.lang.String getNameOrNull​(KeyColumnDescriptor keyColumnDescriptor)
        Get the name of a primary key column. If the column descriptor is null or the key column name is not specified, this will also return null.
        Parameters:
        keyColumnDescriptor - Primary key column descriptor.
        Returns:
        The key column name or null.
      • getNameOrNull

        public static java.lang.String getNameOrNull​(ForeignKeyColumnDescriptor foreignKeyColumnDescriptor)
        Get the name of a foreign key column. If the column descriptor is null, this will also return null.
        Parameters:
        foreignKeyColumnDescriptor - Foreign key column descriptor.
        Returns:
        The foreign key column name or null.
      • getForeignKeyColumnName

        public static java.lang.String getForeignKeyColumnName​(java.util.List<? extends ForeignKeyColumnDescriptor> foreignKeyColumns)
        Get the name of a foreign key column, if and only if there is exactly one foreign key column.
        Parameters:
        foreignKeyColumns - The list of foreign key column.
        Returns:
        The name of the column.
        Throws:
        java.lang.IllegalArgumentException - if there is not exactly one element in the list.
      • assertHasOneKeyColumnLegacy

        public static void assertHasOneKeyColumnLegacy​(int numKeyColumns)
        Check if the number of key columns is exactly one. This function should be used to check if legacy APIs can still be used.
        Parameters:
        numKeyColumns - The number of key columns.
        Throws:
        java.lang.IllegalArgumentException - if the number of key columns is not exactly 1.
      • validateKey

        public static void validateKey​(java.util.List<? extends KeyColumnDescriptor> keyColumns)
        Validate key columns for an entity provider. This will check:
        • that name and type of each column are set
        • that names of key columns are unique, if there is more than key column
        Parameters:
        keyColumns - The list of key columns to validate.
      • validateForeignKeyToPrimaryKey

        public static void validateForeignKeyToPrimaryKey​(java.util.List<? extends ForeignKeyColumnDescriptor> foreignKeys,
                                                          java.util.List<? extends KeyColumnDescriptor> primaryKeys)
        Check is a foreign key definition matches a primary key definition. Both keys should previously be validated by validateKey(List) and validateForeignKey(List).
        Parameters:
        foreignKeys - Foreign keys.
        primaryKeys - Primary keys.
      • getKeyColumnDescriptorOrDefault

        public static KeyColumnDescriptor getKeyColumnDescriptorOrDefault​(java.util.List<? extends KeyColumnDescriptor> keyColumns,
                                                                          java.lang.String defaultName)