From 6ceae5d586b9f71480ad2981b1aa237f9bcce9bb Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Mon, 22 Jul 2019 16:06:34 +0200 Subject: [PATCH] Document Type of Collections Returned by StreamInput (#44686) (#44688) * As a result of #44665 the collections returned by the deserialization methods on `StreamInput` may be either mutable or immutable now, this PR adds documentation for that fact --- .../common/io/stream/StreamInput.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java b/server/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java index fbc3d9deb92..85e6a866016 100644 --- a/server/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java +++ b/server/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java @@ -530,6 +530,9 @@ public abstract class StreamInput extends InputStream { return null; } + /** + * If the returned map contains any entries it will be mutable. If it is empty it might be immutable. + */ public Map readMap(Writeable.Reader keyReader, Writeable.Reader valueReader) throws IOException { int size = readArraySize(); if (size == 0) { @@ -549,6 +552,8 @@ public abstract class StreamInput extends InputStream { *

      * Map<String, List<String>> map = in.readMapOfLists(StreamInput::readString, StreamInput::readString);
      * 
+ * If the map or a list in it contains any elements it will be mutable, otherwise either the empty map or empty lists it contains + * might be immutable. * * @param keyReader The key reader * @param valueReader The value reader @@ -567,12 +572,19 @@ public abstract class StreamInput extends InputStream { return map; } + /** + * If the returned map contains any entries it will be mutable. If it is empty it might be immutable. + */ @Nullable @SuppressWarnings("unchecked") public Map readMap() throws IOException { return (Map) readGenericValue(); } + /** + * Reads a value of unspecified type. If a collection is read then the collection will be mutable if it contains any entry but might + * be immutable if it is empty. + */ @Nullable public Object readGenericValue() throws IOException { byte type = readByte(); @@ -1026,6 +1038,7 @@ public abstract class StreamInput extends InputStream { /** * Reads a list of objects. The list is expected to have been written using {@link StreamOutput#writeList(List)}. + * If the returned list contains any entries it will be mutable. If it is empty it might be immutable. * * @return the list of objects * @throws IOException if an I/O exception occurs reading the list @@ -1036,6 +1049,7 @@ public abstract class StreamInput extends InputStream { /** * Reads a list of strings. The list is expected to have been written using {@link StreamOutput#writeStringCollection(Collection)}. + * If the returned list contains any entries it will be mutable. If it is empty it might be immutable. * * @return the list of strings * @throws IOException if an I/O exception occurs reading the list @@ -1045,7 +1059,7 @@ public abstract class StreamInput extends InputStream { } /** - * Reads a set of objects + * Reads a set of objects. If the returned set contains any entries it will be mutable. If it is empty it might be immutable. */ public Set readSet(Writeable.Reader reader) throws IOException { return readCollection(reader, HashSet::new, Collections.emptySet()); @@ -1069,7 +1083,8 @@ public abstract class StreamInput extends InputStream { } /** - * Reads a list of {@link NamedWriteable}s. + * Reads a list of {@link NamedWriteable}s. If the returned list contains any entries it will be mutable. + * If it is empty it might be immutable. */ public List readNamedWriteableList(Class categoryClass) throws IOException { int count = readArraySize();