CRecord

The record has two important functions within a message. Firstly, it contains the ID of the message (type: ID). Secondly, it contains the payload of the message.

Records have a map to store the payload. Strings are used as keys of the map. The data are stored as values of the map.

We call a key-value pair a slot. The data of a slot is determined by a slot type.

Constructors

Copy constructor:

public CRecord(@NotNull final CRecord aTemplate);

With message ID:

public CRecord(@NotNull final IId aId);

Streams

Records can of course be stored and transported in streams.

public static CRecord fromStream(@NotNull final DataInput aStream) throws IOException;
public void toStream(@NotNull final DataOutput aStream) throws IOException;

ID

The ID can be fetched and also written. It can be IId.INVALID, but not null.

@NotNull
public IId getId();
public void setId(@NotNull final IId aId);

Add slots

Slot can be added easily. For this you need a key and an instance of the slot. The method returns the slot itself.

@NotNull
public ISlot addSlot(@NotNull final String aKey,
                     @NotNull final ISlot aSlot);

Slots are created via the Slot Factory.

It is possible to set a slot indirectly. In this case, you must specify the slot type in addition to the value. An exception is thrown if the slot type does not match the supplied value.

public void setValue(@NotNull final String aKey,
                     @NotNull final CSlotType aType,
                     @Nullable final Object aValue) throws CException;

Another method catches the thrown exception and logs it.

public void setValueSafe(@NotNull final String aKey,
                         @NotNull final CSlotType aType,
                         @Nullable final Object aValue);

Remove slots

Deleting slots is just as simple. The method returns the found slot, otherwise null.

@Nullable
public ISlot removeSlot(@NotNull final String aKey);

Another method removes all existing slots:

public void clearSlots();

Get a slot

The simplest method for fetching a slot is a getter that returns null if the key could not be found.

@Nullable
public ISlot getSlot(@NotNull final String aKey);

Another method also allows checking the slot type. It throws an exception if the assumed type does not match the real type. If the key is not found, the method returns null.

@Nullable
public ISlot getSlot(@NotNull final String aKey,
                     @NotNull final CSlotType aType) throws CException;

It can also only be checked whether a key exists:

public boolean exist(@NotNull final String aKey);

Get a slot type

To check the slot type there is this method. If the key is not found, the method returns null.

@NotNull
public CSlotType getSlotType(@NotNull final String aKey);

Get all slots

This method fetches all slots.

@NotNull
public Map<String, ISlot> getSlots();

Get all keys

A similar method fetches all keys:

@NotNull
public Collection<String> getKeys();

Get a value for a key

A value in the form of an object can be fetched this way. If the key is not found, null is returned.

@Nullable
public Object getValue(@NotNull final String aKey);

You can also specify a default value that will be returned if the key is not found.

@Nullable
public Object getValue(@NotNull final String aKey,
                       @Nullable final Object aDefaultValue);

Analogous to the getter for a slot, a value can also be fetched with type checking. It throws an exception if the assumed type does not match the real type. If the key is not found, null is returned.

@Nullable
public Object getValue(@NotNull final String aKey,
                       @NotNull final CSlotType aType) throws CException;

Another possibility is the additional input of a default value, which is returned if the key is not found. It throws an exception if the assumed type does not match the real type.

@Nullable
public Object getValue(@NotNull final String aKey,
                       @NotNull final CSlotType aType,
                       @Nullable final Object aDefaultValue) throws CException;

The number of slots

public int size();

Transfer of slots from another record

Slots from another record can simply be taken over. No slot is removed from the target record beforehand. Existing slots are overwritten during the transfer.

public void takeSlots(@NotNull final CRecord aRecord);

Debug-Ausgabe

Besides the full output of a string with toString() there is also a more compact output.

public String toShortString();

Notes

Records support the equals() and hashCode() methods.