laurentgo commented on a change in pull request #11063:
URL: https://github.com/apache/arrow/pull/11063#discussion_r700647478
##########
File path:
java/vector/src/main/java/org/apache/arrow/vector/types/pojo/FieldType.java
##########
@@ -68,11 +69,9 @@ public FieldType(boolean nullable, ArrowType type,
DictionaryEncoding dictionary
extensionMetadata.put(ExtensionType.EXTENSION_METADATA_KEY_NAME,
((ExtensionType) type).extensionName());
extensionMetadata.put(ExtensionType.EXTENSION_METADATA_KEY_METADATA,
((ExtensionType) type).serialize());
if (metadata != null) {
- for (Map.Entry<String, String> entry : metadata.entrySet()) {
- extensionMetadata.put(entry.getKey(), entry.getValue());
- }
+ extensionMetadata.putAll(metadata);
}
- this.metadata = Collections2.immutableMapCopy(extensionMetadata);
+ this.metadata = Collections.unmodifiableMap(extensionMetadata);
Review comment:
because `extensionMetadata` is being by the constructor itself and not
exposed else, I believe this to be safe, yes.
##########
File path:
java/memory/memory-core/src/main/java/org/apache/arrow/util/Collections2.java
##########
@@ -46,23 +45,55 @@ private Collections2() {}
* Converts the iterable into a new {@link List}.
*/
public static <T> List<T> toList(Iterable<T> iterable) {
- return StreamSupport.stream(iterable.spliterator(),
false).collect(Collectors.toList());
+ if (iterable instanceof Collection<?>) {
+ // If iterable is a collection, take advantage of it for a more
efficient copy
+ return new ArrayList<T>((Collection<T>) iterable);
+ }
+ return toList(iterable.iterator());
}
+ /**
+ * Converts the iterable into a new immutable {@link List}.
+ */
+ public static <T> List<T> toImmutableList(Iterable<T> iterable) {
+ return Collections.unmodifiableList(toList(iterable));
+ }
+
+
/** Copies the elements of <code>map</code> to a new unmodifiable map. */
public static <K, V> Map<K, V> immutableMapCopy(Map<K, V> map) {
- Map<K, V> newMap = new HashMap<>();
- newMap.putAll(map);
- return java.util.Collections.unmodifiableMap(newMap);
+ return Collections.unmodifiableMap(new HashMap<>(map));
}
/** Copies the elements of list to a new unmodifiable list. */
public static <V> List<V> immutableListCopy(List<V> list) {
- return
Collections.unmodifiableList(list.stream().collect(Collectors.toList()));
+ return Collections.unmodifiableList(new ArrayList<>(list));
}
/** Copies the values to a new unmodifiable list. */
public static <V> List<V> asImmutableList(V...values) {
return Collections.unmodifiableList(Arrays.asList(values));
}
+
+ /**
+ * Creates a human readable string from the remaining elements in iterator.
+ *
+ * The output should be similar to {@code Arrays#toString(Object[])}
+ */
+ public static String toString(Iterator<?> iterator) {
+ if (!iterator.hasNext()) {
Review comment:
I guess something like
```
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator,
Spliterator.ORDERED), false)
.map(String::valueOf)
.collect(Collectors.joining(", ", "[", "]"));
```
should work
##########
File path:
java/memory/memory-core/src/main/java/org/apache/arrow/util/Collections2.java
##########
@@ -46,23 +45,55 @@ private Collections2() {}
* Converts the iterable into a new {@link List}.
*/
public static <T> List<T> toList(Iterable<T> iterable) {
- return StreamSupport.stream(iterable.spliterator(),
false).collect(Collectors.toList());
+ if (iterable instanceof Collection<?>) {
+ // If iterable is a collection, take advantage of it for a more
efficient copy
+ return new ArrayList<T>((Collection<T>) iterable);
+ }
+ return toList(iterable.iterator());
}
+ /**
+ * Converts the iterable into a new immutable {@link List}.
+ */
+ public static <T> List<T> toImmutableList(Iterable<T> iterable) {
+ return Collections.unmodifiableList(toList(iterable));
Review comment:
Unlike Guava `Immutable*` collections, there's no officlal way to
inspect for a collection to check if the collection is unmodifiable. Probably
the best way would be to introduce Arrow's own set of immutable collections,
but that's a way bigger issue. In this patch I tried to only focus on the
obvious copy duplicates
##########
File path:
java/vector/src/main/java/org/apache/arrow/vector/types/pojo/Schema.java
##########
@@ -97,7 +97,7 @@ public static Schema
convertSchema(org.apache.arrow.flatbuf.Schema schema) {
String value = kv.value();
metadata.put(key == null ? "" : key, value == null ? "" : value);
}
- return new Schema(Collections2.immutableListCopy(fields),
Collections2.immutableMapCopy(metadata));
+ return new Schema(true, Collections.unmodifiableList(fields),
Collections.unmodifiableMap(metadata));
Review comment:
Both defensive copies are done by the constructor itself when the
original argument is processed to create a `fields` and a `metadata` collections
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]