blackdrag commented on code in PR #3260:
URL: https://github.com/apache/groovy/pull/3260#discussion_r4531102938


##########
src/main/java/org/apache/groovy/vmplugin/internal/DefaultImportPackages.java:
##########
@@ -0,0 +1,127 @@
+/*
+ *  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.groovy.vmplugin.internal;
+
+import groovy.transform.Internal;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Orders default-import package prefixes by a caller-supplied precedence
+ * array. Iteration order of the returned sets is that array's order.
+ * Packages that are not in the array are dropped, so a {@code HashSet}
+ * walk cannot put a later package ahead of an earlier one.
+ *
+ * @since 7.0.0
+ */
+@Internal
+public final class DefaultImportPackages {
+
+    private DefaultImportPackages() {
+    }
+
+    /**
+     * Returns the members of {@code packages} that occur in {@code 
precedence},
+     * iterated in {@code precedence} order. The result is unmodifiable.
+     * An empty result is {@link Collections#emptySet()}. A single match is
+     * {@link Collections#singleton(Object)}.
+     *
+     * @param packages packages discovered for one simple name; {@code null} 
is empty
+     * @param precedence package prefixes in the order they must be tried; 
{@code null} yields an empty set
+     * @return an immutable set in precedence order
+     */
+    public static Set<String> ordered(final Collection<String> packages, final 
String[] precedence) {
+        if (packages == null || packages.isEmpty() || precedence == null || 
precedence.length == 0) {
+            return Collections.emptySet();
+        }
+        List<String> matches = new ArrayList<>(Math.min(packages.size(), 
precedence.length));
+        for (String candidate : precedence) {
+            if (packages.contains(candidate)) {
+                matches.add(candidate);
+            }
+        }
+        if (matches.isEmpty()) {
+            return Collections.emptySet();
+        }
+        if (matches.size() == 1) {
+            return Collections.singleton(matches.get(0));
+        }
+        return Collections.unmodifiableSet(new LinkedHashSet<>(matches));

Review Comment:
   > The unmodifiable wrapper stays. `Collections.unmodifiableSet` does not 
copy; it only wraps that set.  Callers publish the result: `ResolveVisitor` 
stores it in a `ConcurrentHashMap`, and until a later hit replaces the entry 
the same instance is the startup index (`Groovy12725` checks that with 
`assertSame`). A reader on another thread must not observe a set being edited, 
and the tests expect `add` to throw. An update replaces the map entry. An empty 
result is still `Collections.emptySet()`, and a single match is still 
`Collections.singleton`, which is already immutable.
   
   I assume you mean 
src/test/groovy/org/apache/groovy/vmplugin/internal/DefaultImportPackagesTest.groovy.
 This test does not establish multi threading behaviour. It tests an API and 
can maybe be used as regression test to ensure the ordering logic is kept. But 
it does not explain why this is needed. What is the goal? To catch the mistake 
during development? It most likely is not to enable multithreading behaviour. 
Also unmodifiable is not the same as immutable. A unmodifiableSet may still 
have publication issues based on the underlying collection. Again... I am not 
saying this is wrong. Just want a more clear picture why here exactly like this 
and not different. Just pointing to the test with the argument of thread safety 
and then having a test that does not test that if not convincing. 
   



-- 
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]

Reply via email to