ayush-builds-stuff commented on code in PR #14133:
URL: https://github.com/apache/maven/pull/14133#discussion_r4728483409


##########
impl/maven-impl/src/main/java/org/apache/maven/impl/MutableProject.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.maven.impl;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CopyOnWriteArraySet;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Predicate;
+
+import org.apache.maven.api.Project;
+import org.apache.maven.api.SourceRoot;
+import org.apache.maven.api.annotations.Nonnull;
+import org.apache.maven.api.annotations.Nullable;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Internal stable identity for a project whose state is represented by 
immutable snapshots.
+ *
+ * @param <A> the legacy attached-artifact representation
+ */
+public final class MutableProject<A> {
+    private final AtomicReference<Project> project = new AtomicReference<>();
+    private final CopyOnWriteArraySet<SourceRoot> sourceRoots = new 
CopyOnWriteArraySet<>();
+    private final CopyOnWriteArrayList<A> attachedArtifacts = new 
CopyOnWriteArrayList<>();
+    private volatile Properties properties = new Properties();
+
+    public void initialize(@Nonnull Project project) {
+        Project value = requireNonNull(project, "project cannot be null");
+        if (!this.project.compareAndSet(null, value) && this.project.get() != 
value) {
+            throw new IllegalStateException("project has already been 
initialized");
+        }
+    }
+
+    @Nonnull
+    public Project project() {
+        return requireNonNull(project.get(), "project has not been 
initialized");
+    }
+
+    @Nullable
+    public Project projectOrNull() {
+        return project.get();
+    }
+
+    public void replace(@Nonnull Project current, @Nonnull Project 
replacement) {
+        requireNonNull(current, "current project cannot be null");
+        requireNonNull(replacement, "replacement project cannot be null");
+        if (!project.compareAndSet(current, replacement)) {
+            throw new IllegalStateException("project changed while it was 
being replaced");
+        }
+    }
+
+    public synchronized boolean addSourceRoot(@Nonnull SourceRoot sourceRoot) {
+        return sourceRoots.add(requireNonNull(sourceRoot, "sourceRoot cannot 
be null"));
+    }
+
+    public synchronized boolean removeSourceRoot(@Nonnull SourceRoot 
sourceRoot) {
+        return sourceRoots.remove(requireNonNull(sourceRoot, "sourceRoot 
cannot be null"));
+    }
+
+    public synchronized boolean removeSourceRoots(@Nonnull 
Predicate<SourceRoot> filter) {
+        return sourceRoots.removeIf(requireNonNull(filter, "filter cannot be 
null"));
+    }
+
+    public synchronized boolean replaceSourceRoot(
+            @Nonnull Predicate<SourceRoot> filter, @Nonnull SourceRoot 
replacement) {
+        requireNonNull(filter, "filter cannot be null");
+        requireNonNull(replacement, "replacement cannot be null");
+        List<SourceRoot> roots = List.copyOf(sourceRoots);
+        for (int i = 0; i < roots.size(); i++) {
+            if (filter.test(roots.get(i))) {
+                sourceRoots.clear();
+                sourceRoots.addAll(roots.subList(0, i));
+                sourceRoots.add(replacement);
+                sourceRoots.addAll(roots.subList(i + 1, roots.size()));
+                return true;
+            }
+        }
+        return false;
+    }
+
+    public synchronized void setSourceRoots(@Nonnull Collection<SourceRoot> 
sourceRoots) {
+        this.sourceRoots.clear();
+        this.sourceRoots.addAll(requireNonNull(sourceRoots, "sourceRoots 
cannot be null"));
+    }
+
+    @Nonnull
+    public Collection<SourceRoot> sourceRoots() {
+        return Collections.unmodifiableSet(sourceRoots);
+    }

Review Comment:
   Fixed in 46842f6a9b. sourceRoots() now synchronizes with the mutators and 
returns an ordered snapshot captured while holding the monitor. This avoids 
exposing both the intermediate clear() state and a live view after the lock is 
released. Regression coverage was added.
   



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