From 992251644cfe13971d66a1a09836dad5a9cc2fc3 Mon Sep 17 00:00:00 2001 From: Tamir Bahar Date: Wed, 27 Jan 2021 19:57:04 +0200 Subject: [PATCH] Add helpers for serializing collections --- .../java/org/capnproto/CollectionHelpers.java | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 runtime/src/main/java/org/capnproto/CollectionHelpers.java diff --git a/runtime/src/main/java/org/capnproto/CollectionHelpers.java b/runtime/src/main/java/org/capnproto/CollectionHelpers.java new file mode 100644 index 00000000..71d688dd --- /dev/null +++ b/runtime/src/main/java/org/capnproto/CollectionHelpers.java @@ -0,0 +1,137 @@ +package org.capnproto; + + +import java.util.Collection; +import java.util.function.BiConsumer; +import java.util.function.Function; + +public final class CollectionHelpers { + private CollectionHelpers() {} + + + public static void serializeCollection( + Collection items, + Function> getListBuilder, + BiConsumer serializeItem) { + StructList.Builder listBuilder = getListBuilder.apply(items.size()); + + int i = 0; + for (T item : items) { + serializeItem.accept(item, listBuilder.get(i)); + ++i; + } + } + + public static void serializeCollection( + T[] items, + Function> getListBuilder, + BiConsumer serializeItem) { + StructList.Builder listBuilder = getListBuilder.apply(items.length); + + int i = 0; + for (T item : items) { + serializeItem.accept(item, listBuilder.get(i)); + ++i; + } + } + + public static void serializeLongCollection( + Collection items, + Function getListBuilder) { + PrimitiveList.Long.Builder listBuilder = getListBuilder.apply(items.size()); + + int i = 0; + for (Long item : items) { + listBuilder.set(i, item); + ++i; + } + } + + public static void serializeLongCollection( + Collection items, + Function getListBuilder, + Function transform) { + PrimitiveList.Long.Builder listBuilder = getListBuilder.apply(items.size()); + + int i = 0; + for (T item : items) { + listBuilder.set(i, transform.apply(item)); + ++i; + } + } + + public static void serializeLongCollection( + long[] items, + Function getListBuilder) { + PrimitiveList.Long.Builder listBuilder = getListBuilder.apply(items.length); + + int i = 0; + for (long item : items) { + listBuilder.set(i, item); + ++i; + } + } + + public static void serializeIntCollection( + Collection items, + Function getListBuilder) { + PrimitiveList.Int.Builder listBuilder = getListBuilder.apply(items.size()); + + int i = 0; + for (Integer item : items) { + listBuilder.set(i, item); + ++i; + } + } + + public static void serializeIntCollection( + int[] ints, + java.util.function.Function getListBuilder) { + PrimitiveList.Int.Builder listBuilder = getListBuilder.apply(ints.length); + + int i = 0; + for (int item : ints) { + listBuilder.set(i, item); + ++i; + } + } + + public static void serializeIntCollection( + byte[] items, + java.util.function.Function getListBuilder) { + PrimitiveList.Int.Builder listBuilder = getListBuilder.apply(items.length); + + int i = 0; + for (byte item : items) { + listBuilder.set(i, item); + ++i; + } + } + + public static void serializeIntCollection( + T[] items, + java.util.function.Function getListBuilder, + java.util.function.Function transform) { + PrimitiveList.Int.Builder listBuilder = getListBuilder.apply(items.length); + + int i = 0; + for (T item : items) { + listBuilder.set(i, transform.apply(item)); + ++i; + } + } + + public static void serializeIntCollection( + Collection collection, + java.util.function.Function getListBuilder, + java.util.function.Function transform) { + PrimitiveList.Int.Builder listBuilder = getListBuilder.apply(collection.size()); + + int i = 0; + for (T item : collection) { + listBuilder.set(i, transform.apply(item)); + ++i; + } + } +} +