/** * Unmarshall the {@link Map}. * <p> * If the marshalled map is {@link null}, then the {@link MapBuilder} is not invoked. * * @param in {@link ObjectInput} to read. * @param builder {@link MapBuilder} to create the concrete {@link Map} implementation. * @return The populated {@link Map} created by the {@link MapBuilder} or {@code null}. * @throws IOException If any of the usual Input/Output related exceptions occur. * @throws ClassNotFoundException If the class of a serialized object cannot be found. * @see #marshallMap(Map, ObjectOutput) */ public static <K, V, T extends Map<K, V>> T unmarshallMap(ObjectInput in, MapBuilder<K, V, T> builder) throws IOException, ClassNotFoundException { final int size = unmarshallSize(in); if (size == NULL_VALUE) { return null; } final T map = Objects.requireNonNull(builder, "MapBuilder must be non-null").build(computeCapacity(size)); for (int i = 0; i < size; i++) //noinspection unchecked map.put((K) in.readObject(), (V) in.readObject()); return map; }
/** * Unmarshall the {@link Map}. * <p> * If the marshalled map is {@link null}, then the {@link MapBuilder} is not invoked. * * @param in {@link ObjectInput} to read. * @param builder {@link MapBuilder} to create the concrete {@link Map} implementation. * @return The populated {@link Map} created by the {@link MapBuilder} or {@code null}. * @throws IOException If any of the usual Input/Output related exceptions occur. * @throws ClassNotFoundException If the class of a serialized object cannot be found. * @see #marshallMap(Map, ElementWriter, ElementWriter, ObjectOutput) */ public static <K, V, T extends Map<K, V>> T unmarshallMap(ObjectInput in, ElementReader<K> keyReader, ElementReader<V> valueReader, MapBuilder<K, V, T> builder) throws IOException, ClassNotFoundException { final int size = unmarshallSize(in); if (size == NULL_VALUE) { return null; } final T map = Objects.requireNonNull(builder, "MapBuilder must be non-null").build(computeCapacity(size)); for (int i = 0; i < size; i++) //noinspection unchecked map.put(keyReader.readFrom(in), valueReader.readFrom(in)); return map; }
public static <K, V, TYPED_MAP extends Map<K, V>> TYPED_MAP readMap(ObjectInput input, Externalizer<K> keyExternalizer, Externalizer<V> valueExternalizer, MarshallUtil.MapBuilder<K, V, TYPED_MAP> mapBuilder) throws IOException, ClassNotFoundException { byte b = input.readByte(); if (b == 0) { return null; } else { int size = input.readInt(); TYPED_MAP map = mapBuilder.build(size); for (int i=0 ; i<size ; i++) { K key = keyExternalizer.readObject(input); V value = valueExternalizer.readObject(input); map.put(key, value); } return map; } }