adoroszlai commented on code in PR #4782: URL: https://github.com/apache/ozone/pull/4782#discussion_r1213435493
########## hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/CollectionUtils.java: ########## @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hdds.utils; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** Utility methods for Java Collections. */ +public interface CollectionUtils { + static <KEY, VALUE> Map<KEY, VALUE> newUnmodifiableMap( + List<VALUE> values, Function<VALUE, KEY> getKey, + Map<KEY, VALUE> existing) { + final Map<KEY, VALUE> map = new HashMap<>(existing); + for (VALUE v : values) { + final KEY key = getKey.apply(v); + final VALUE previous = map.put(key, v); + if (previous != null) { + throw new IllegalArgumentException("Already exists: " + key + + ", previous " + previous.getClass()); + } + } + return Collections.unmodifiableMap(map); + } + + static <KEY, VALUE> Map<KEY, List<VALUE>> newUnmodifiableMultiMap( + List<VALUE> values, Function<VALUE, KEY> getKey) { + final Map<KEY, List<VALUE>> map = new HashMap<>(); + for (VALUE v : values) { + final KEY key = getKey.apply(v); + map.computeIfAbsent(key, k -> new ArrayList<>()).add(v); + } + return Collections.unmodifiableMap(map.entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, + e -> Collections.unmodifiableList(e.getValue())))); + } + + static <T> Iterator<T> newIterator(Collection<List<T>> values) { + final Iterator<List<T>> listIterator = values.iterator(); + return new Iterator<T>() { + private Iterator<T> i; + + private boolean hasNextItem() { + return i != null && i.hasNext(); + } + + @Override + public boolean hasNext() { + return listIterator.hasNext() || hasNextItem(); + } + + @Override + public T next() { + if (hasNextItem()) { + return i.next(); + } + if (listIterator.hasNext()) { + i = listIterator.next().iterator(); + return i.next(); + } + throw new NoSuchElementException(); + } Review Comment: If the current list has no more items, and the next list is empty, then `hasNext()` returns `true`, but `next()` throws `NoSuchElementException`. I think a safe implementation would be: ```java private Iterator<T> i = emptyIterator(); private Iterator<T> nextIterator() { if (i.hasNext()) { return i; } while (listIterator.hasNext()) { i = listIterator.next().iterator(); if (i.hasNext()) { return i; } } return emptyIterator(); } @Override public boolean hasNext() { return nextIterator().hasNext(); } @Override public T next() { if (hasNext()) { return i.next(); } throw new NoSuchElementException(); } ``` Test: ```java import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static org.junit.jupiter.api.Assertions.assertEquals; class TestCollectionUtils { @Test void someEmptyLists() { List<List<String>> listOfLists = new ArrayList<>(); listOfLists.add(asList("a", "b")); listOfLists.add(emptyList()); listOfLists.add(emptyList()); listOfLists.add(asList("c", "d")); listOfLists.add(emptyList()); assertIteration(asList("a", "b", "c", "d"), listOfLists); } @Test void allEmptyLists() { List<List<String>> listOfLists = new ArrayList<>(); listOfLists.add(emptyList()); listOfLists.add(emptyList()); assertIteration(emptyList(), listOfLists); } @Test void empty() { assertIteration(emptyList(), emptyList()); } private static <T> void assertIteration(List<T> expected, List<List<T>> listOfLists) { List<T> actual = new ArrayList<>(); CollectionUtils.newIterator(listOfLists).forEachRemaining(actual::add); assertEquals(expected, actual); } } ``` -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
