Skip to content

Commit 3dd4375

Browse files
Alex-J-Wchaubold
authored andcommitted
AP-23761: Use LinkedHashSet to hold python paths
1 parent f2bcb07 commit 3dd4375

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

org.knime.python3/src/main/java/org/knime/python3/PythonPath.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,12 @@
5252
import java.nio.file.Path;
5353
import java.util.ArrayList;
5454
import java.util.Collections;
55+
import java.util.LinkedHashSet;
5556
import java.util.List;
5657
import java.util.Objects;
58+
import java.util.Set;
59+
60+
import org.knime.core.node.NodeLogger;
5761

5862
/**
5963
* A representation of a PYTHONPATH with Python modules on it. The {@link PythonPath} contains a list of strings which
@@ -65,26 +69,25 @@
6569
*/
6670
public final class PythonPath {
6771

68-
private final List<String> m_paths;
72+
private final Set<String> m_paths = new LinkedHashSet<>();
6973

7074
private PythonPath(final List<String> paths) {
71-
m_paths = paths;
75+
m_paths.addAll(paths);
7276
}
7377

7478
/**
7579
* @return the list of absolute paths to the folders containing the Python modules
7680
*/
7781
public List<String> getPaths() {
78-
return Collections.unmodifiableList(m_paths);
82+
return Collections.unmodifiableList(new ArrayList<>(m_paths));
7983
}
8084

8185
/**
8286
* @return a string which can be used as the PYTHONPATH environment variable. The paths are separated by the
8387
* platform specific separator.
8488
*/
8589
public String getPythonPath() {
86-
final String seperator = File.pathSeparator;
87-
return String.join(seperator, m_paths);
90+
return String.join(File.pathSeparator, m_paths);
8891
}
8992

9093
@Override
@@ -120,11 +123,13 @@ public static PythonPathBuilder builder() {
120123
/** A builder for {@link PythonPath}. */
121124
public static final class PythonPathBuilder {
122125

123-
private final List<String> m_paths;
126+
private final Set<String> m_paths;
127+
128+
private static final NodeLogger LOGGER = NodeLogger.getLogger(PythonPathBuilder.class);
124129

125130
/** Create a new builder for {@link PythonPath}. */
126131
public PythonPathBuilder() {
127-
m_paths = new ArrayList<>();
132+
m_paths = new LinkedHashSet<>();
128133
}
129134

130135
/**
@@ -144,7 +149,10 @@ public PythonPathBuilder(final PythonPath path) {
144149
* @return the builder
145150
*/
146151
public PythonPathBuilder add(final Path path) {
147-
m_paths.add(path.toAbsolutePath().toString());
152+
if (!m_paths.add(path.toAbsolutePath().toString())) {
153+
LOGGER.coding(String.format("The path '%s' is already present.",
154+
path.toAbsolutePath()));
155+
}
148156
return this;
149157
}
150158

0 commit comments

Comments
 (0)