Skip to content

Commit 3ab95ba

Browse files
committed
Replace a list with O(N²) performance by O(N) at least during iteration.
1 parent 6aa86c5 commit 3ab95ba

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

impl/maven-core/src/main/java/org/apache/maven/project/MavenProject.java

+22-31
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@
2222
import java.io.IOException;
2323
import java.io.Writer;
2424
import java.nio.file.Path;
25-
import java.util.AbstractList;
25+
import java.util.AbstractSequentialList;
2626
import java.util.ArrayList;
2727
import java.util.Collection;
2828
import java.util.Collections;
2929
import java.util.HashMap;
3030
import java.util.LinkedHashMap;
3131
import java.util.LinkedHashSet;
3232
import java.util.List;
33+
import java.util.ListIterator;
3334
import java.util.Map;
3435
import java.util.Objects;
3536
import java.util.Properties;
@@ -738,56 +739,42 @@ public Build getBuild() {
738739
*/
739740
@Deprecated(since = "4.0.0")
740741
public List<Resource> getResources() {
741-
return new AbstractList<>() {
742-
@Override
743-
public Resource get(int index) {
744-
return toResource(getEnabledSourceRoots(ProjectScope.MAIN, Language.RESOURCES)
745-
.toList()
746-
.get(index));
747-
}
748-
749-
@Override
750-
public int size() {
751-
return (int) getEnabledSourceRoots(ProjectScope.MAIN, Language.RESOURCES)
752-
.count();
753-
}
754-
755-
@Override
756-
public boolean add(Resource resource) {
757-
addResource(resource);
758-
return true;
759-
}
760-
};
742+
return getResources(ProjectScope.MAIN);
761743
}
762744

763745
/**
764746
* @deprecated Replaced by {@code getEnabledSourceRoots(ProjectScope.TEST, Language.RESOURCES)}.
765747
*/
766748
@Deprecated(since = "4.0.0")
767749
public List<Resource> getTestResources() {
768-
return new AbstractList<>() {
750+
return getResources(ProjectScope.TEST);
751+
}
752+
753+
private List<Resource> getResources(final ProjectScope scope) {
754+
return new AbstractSequentialList<>() {
769755
@Override
770-
public Resource get(int index) {
771-
return toResource(getEnabledSourceRoots(ProjectScope.TEST, Language.RESOURCES)
756+
public ListIterator<Resource> listIterator(int index) {
757+
return getEnabledSourceRoots(scope, Language.RESOURCES)
758+
.map(MavenProject::toResource)
772759
.toList()
773-
.get(index));
760+
.listIterator(index);
774761
}
775762

776763
@Override
777764
public int size() {
778-
return (int) getEnabledSourceRoots(ProjectScope.TEST, Language.RESOURCES)
779-
.count();
765+
return Math.toIntExact(
766+
getEnabledSourceRoots(scope, Language.RESOURCES).count());
780767
}
781768

782769
@Override
783770
public boolean add(Resource resource) {
784-
addTestResource(resource);
771+
addResource(resource);
785772
return true;
786773
}
787774
};
788775
}
789776

790-
private Resource toResource(SourceRoot sourceRoot) {
777+
private static Resource toResource(SourceRoot sourceRoot) {
791778
return new Resource(org.apache.maven.api.model.Resource.newBuilder()
792779
.directory(sourceRoot.directory().toString())
793780
.includes(sourceRoot.includes().stream().map(Object::toString).toList())
@@ -796,20 +783,24 @@ private Resource toResource(SourceRoot sourceRoot) {
796783
.build());
797784
}
798785

786+
private void addResource(ProjectScope scope, Resource resource) {
787+
addSourceRoot(new DefaultSourceRoot(getBaseDirectory(), scope, resource.getDelegate()));
788+
}
789+
799790
/**
800791
* @deprecated {@link Resource} is replaced by {@link SourceRoot}.
801792
*/
802793
@Deprecated(since = "4.0.0")
803794
public void addResource(Resource resource) {
804-
addSourceRoot(new DefaultSourceRoot(getBaseDirectory(), ProjectScope.MAIN, resource.getDelegate()));
795+
addResource(ProjectScope.MAIN, resource);
805796
}
806797

807798
/**
808799
* @deprecated {@link Resource} is replaced by {@link SourceRoot}.
809800
*/
810801
@Deprecated(since = "4.0.0")
811802
public void addTestResource(Resource testResource) {
812-
addSourceRoot(new DefaultSourceRoot(getBaseDirectory(), ProjectScope.TEST, testResource.getDelegate()));
803+
addResource(ProjectScope.TEST, testResource);
813804
}
814805

815806
public void setLicenses(List<License> licenses) {

0 commit comments

Comments
 (0)