fromJson<DataType extends Object> static method

SharedMultiQueueImage<DataType> fromJson<DataType extends Object>(
  1. Map<String, Object?> json,
  2. StateValueDecoder<DataType> decodeData
)

Decodes a shared multi-queue image.

Implementation

static SharedMultiQueueImage<DataType> fromJson<DataType extends Object>(
  Map<String, Object?> json,
  StateValueDecoder<DataType> decodeData,
) {
  if (json['kind'] != kind || json['version'] != version) {
    throw const FormatException(
        'Expected $kind state image version $version.');
  }
  final capacity = json['capacity'];
  final encodedQueues = json['queues'];
  if (capacity is! int || capacity <= 0) {
    throw const FormatException('capacity must be a positive integer.');
  }
  if (encodedQueues is! List<Object?> || encodedQueues.isEmpty) {
    throw const FormatException('queues must be a non-empty list.');
  }
  final queues = <List<DataType>>[];
  for (final encodedQueue in encodedQueues) {
    if (encodedQueue is! List<Object?>) {
      throw const FormatException('Every queue must be a list.');
    }
    queues.add([for (final item in encodedQueue) decodeData(item)]);
  }
  final occupancy = queues.fold(0, (total, queue) => total + queue.length);
  if (occupancy > capacity) {
    throw FormatException(
        'Queue occupancy $occupancy exceeds capacity $capacity.');
  }
  return SharedMultiQueueImage(capacity: capacity, queues: queues);
}