/** * Unmarshalls a collection of integers. * * @param in the {@link ObjectInput} to read from. * @param builder the {@link CollectionBuilder} to build the collection of integer. * @param <T> the concrete type of the collection. * @return the collection. * @throws IOException if an error occurs. */ public static <T extends Collection<Integer>> T unmarshallIntCollection(ObjectInput in, CollectionBuilder<Integer, T> builder) throws IOException { final int size = unmarshallSize(in); if (size == NULL_VALUE) { return null; } T collection = Objects.requireNonNull(builder, "CollectionBuilder must be non-null").build(size); for (int i = 0; i < size; ++i) { collection.add(in.readInt()); } return collection; }
/** * Unmarshal a {@link Collection}. * * @param in {@link ObjectInput} to read. * @param builder {@link CollectionBuilder} builds the concrete {@link Collection} based on size. * @param reader {@link ElementReader} reads one element from the input. * @param <E> Collection's element type. * @param <T> {@link Collection} implementation. * @return The concrete {@link Collection} implementation. * @throws IOException If any of the usual Input/Output related exceptions occur. * @throws ClassNotFoundException If the class of a serialized object cannot be found. */ public static <E, T extends Collection<E>> T unmarshallCollection(ObjectInput in, CollectionBuilder<E, T> builder, ElementReader<E> reader) throws IOException, ClassNotFoundException { final int size = unmarshallSize(in); if (size == NULL_VALUE) { return null; } T collection = Objects.requireNonNull(builder, "CollectionBuilder must be non-null").build(size); for (int i = 0; i < size; ++i) { //noinspection unchecked collection.add(reader.readFrom(in)); } return collection; }
public static <E, T extends Collection<E>> T readCollection(ObjectInput input, Externalizer<E> valueExternalizer, MarshallUtil.CollectionBuilder<E, T> colBuilder) throws ClassNotFoundException, IOException { byte b = input.readByte(); if (b == 0) { return null; } else { int size = input.readInt(); T col = colBuilder.build(size); for (int i=0 ; i<size ; i++) { E value = valueExternalizer.readObject(input); col.add(value); } return col; } }