CIdFactory
We use a factory to create IDs.
The reason for this is the possibility to introduce additional ID types.
The
We have not designed the
Create an ID
The simplest method is to create IDs from an integer, from a string or from a UUID:
@NotNull public static IId fromObject(@Nullable final Object aId);
Beispiele:
IId id1 =CIdFactory .create(15); IId id2 =CIdFactory .create("SYSTEM"); IId id3 =CIdFactory .create("e87092d7-2f74-4d7b-9eef-8bdd7789b049");
To make sure that the correct ID type is used, a type can be supplied.
@NotNull public static IId fromObject(@NotNull final EIdType aType, @NotNull final Object aTemplate);
Random IDs
For each ID type a "random" ID can be created. The exact way the type is created depends on the ID factory. Integer IDs, for example, have a global positive number that is incremented with each access. Strings use a random UUID in the form of a string.
@NotNull public static IId randomOfType(@NotNull final EIdType aType);
If there is no preference for the type of ID, it can be shorter. The type UUID is used.
@NotNull public static IId random();
Another method creates a random ID based on integers and
prefixes the resulting ID.
Example: random("Test")
will return something like "Test-87"
@NotNull public static IId random(@NotNull final String aPrefix);
To create an array of random IDs (e.g. for tests), the following method can be used:
@NotNull public static IId[] randomArray();
An array of a certain length with randomly inserted null values can also be created.
@NotNull public static IId[] randomArray(final int aLength, final boolean aWithNullValues);
Writing to and reading from an I/O stream
This method writes an ID into a DataOutput stream. The ID may be null.
public static void toStream(@NotNull final DataOutput aStream, @Nullable final IId aId) throws IOException;
The following method reads an ID from an I/O stream. Optionally an extra boolean can be read, which decides whether an ID can also be null. Usually this is not necessary, because for this case also an invalid ID can be used.
@Nullable public static IId fromStream(@NotNull final DataInput aStream, final boolean aCanBeNull) throws IOException;
Arrays and I/O streams
An array can be written into a DataOutput stream. Null values are taken into account.
public static void arrayToStream(@NotNull final DataOutput aStream, @Nullable final IId @Nullable [] aValue) throws IOException;
This can be used to read an ID array from a DataInput stream.
@NotNull public static IId @Nullable [] arrayFromStream(@NotNull final DataInput aStream) throws IOException;
Writing an ID array into a single string and reading it out again
The method writes an ID array into a single string. A vertical line (|) is used as delimiter.
public static String writeArrayToString(final IId @Nullable [] aValue);
The method reads an ID array from a single string. A vertical line (|) is used as delimiter.
@NotNull public static IId[] arrayFromString(@Nullable final String aValue);
Writing an ID array into a string list and reading it out again
The method writes an ID array to a string list.
public static void writeArrayToStringList(@Nullable final IId[] aValue, @NotNull final List<String> aList);
The method creates an array of IDs from a string list.
public static IId @NotNull [] createArrayFromList(@Nullable final List<?> aList);
Creating an ID array from a string array
The method creates an array of IDs from a string array.
@NotNull private static IId[] arrayFromStringArray(final String @Nullable [] aList);
Copy an ID array
For copying an ID array you can use this method:
public static IId @Nullable [] copy(final IId @Nullable [] aValue)
Comparison
There is also a comparison method that considers null values:
public static boolean equals(@Nullable final IId aId1, @Nullable final IId aId2);
Arrays can also be compared. Null values are also taken into account here.
public static boolean isArrayEqual(@NotNull final IId[] aValue1, @NotNull final IId[] aValue2);
This is a comparison function for IDs.
public static int compareTo(@Nullable final IId aId1, @Nullable final IId aId2);
Handling of custom ID types
Custom ID types require a factory, which can be registered here.
public static void registerFactory(@NotNull final IIdFactory aFactory);
The factory for an ID type can be fetched with this:
@Nullable private static IIdFactory getFactory(@NotNull final EIdType aType);
Handling of null values
The method returns true
if the passed ID is null or INVALID.
public static boolean isEmpty(@Nullable final IId aId);
Inversely, false
is returned here if an ID is null or INVALID:
public static boolean isValid(@Nullable final IId aId);
The method returns IId.INVALID
if the passed ID is null,
otherwise the passed value.
@NotNull public static IId getNonNullId(@Nullable final IId aId);
This method returns IId.INVALID
.
@NotNull public static IId getInvalid();
The class
EMPTY_ARRAY
.