Skip to content

Commit

Permalink
corrected after review
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Smirnov-Exactpro committed Jun 18, 2024
1 parent 9078577 commit 3766f0e
Showing 1 changed file with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
Expand All @@ -61,18 +60,19 @@
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.exactpro.th2.common.schema.util.ArchiveUtils.getGzipBase64StringDecoder;
import static java.util.Collections.emptyMap;
import static java.util.Objects.requireNonNull;
import static java.util.Objects.requireNonNullElseGet;
import static org.apache.commons.io.FilenameUtils.removeExtension;
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;

/**
Expand Down Expand Up @@ -466,23 +466,21 @@ public Set<String> getDictionaryAliases() {
}

try (Stream<Path> files = Files.list(dictionaryFolder)) {
List<Path> fileList = files
.filter(Files::isRegularFile)
.collect(Collectors.toList());

Set<String> aliasSet = new HashSet<>();
Map<String, Set<String>> duplicates = new HashMap<>();
for (Path path : fileList) {
String alias = FilenameUtils.removeExtension(path.getFileName().toString()).toLowerCase();
if (!aliasSet.add(alias)) {
duplicates.computeIfAbsent(alias, (ignore) -> new HashSet<>())
.add(path.getFileName().toString());
}
}
Function<Path, String> getAlias = path -> removeExtension(path.getFileName().toString()).toLowerCase();

Map<String, Set<Path>> filesByAlias = files.filter(Files::isRegularFile)
.collect(Collectors.groupingBy(getAlias, Collectors.toSet()));
Map<String, Set<Path>> duplicates = filesByAlias.entrySet().stream()
.filter(entry -> entry.getValue().size() > 1)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

if (!duplicates.isEmpty()) {
throw new IllegalStateException("Dictionary directory contains files with the same name in different cases, files: " + duplicates + ", path: " + dictionaryFolder.toAbsolutePath());
throw new IllegalStateException(
"Dictionary directory contains files with the same name in different cases, " +
"files by dictionary alias: " + duplicates + ", " +
"path: " + dictionaryFolder.toAbsolutePath());
}
return aliasSet;
return Set.copyOf(filesByAlias.keySet());
}
} catch (IOException e) {
throw new IllegalStateException("Can not get dictionaries aliases from path: " + dictionaryFolder.toAbsolutePath(), e);
Expand All @@ -500,7 +498,7 @@ public InputStream loadDictionary(String alias) {
try (Stream<Path> files = Files.list(dictionaryFolder)) {
dictionaries = files
.filter(Files::isRegularFile)
.filter(path -> FilenameUtils.removeExtension(path.getFileName().toString()).equalsIgnoreCase(alias))
.filter(path -> removeExtension(path.getFileName().toString()).equalsIgnoreCase(alias))
.collect(Collectors.toList());
}
}
Expand Down Expand Up @@ -546,7 +544,7 @@ public InputStream readDictionary(DictionaryType dictionaryType) {
Path dictionaryAliasFolder = getPathToDictionaryAliasesDir();
if ((dictionaries == null || dictionaries.isEmpty()) && Files.isDirectory(dictionaryAliasFolder)) {
try (Stream<Path> files = Files.list(dictionaryAliasFolder)) {
dictionaries = files.filter(Files::isRegularFile).filter(path -> FilenameUtils.removeExtension(path.getFileName().toString()).equalsIgnoreCase(dictionaryType.name())).collect(Collectors.toList());
dictionaries = files.filter(Files::isRegularFile).filter(path -> removeExtension(path.getFileName().toString()).equalsIgnoreCase(dictionaryType.name())).collect(Collectors.toList());
}
}

Expand Down

0 comments on commit 3766f0e

Please sign in to comment.