Skip to content

Commit

Permalink
#217 refactor sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Jul 1, 2024
1 parent aa34d2a commit 47e80c2
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
*/
public class FileUtils {

private FileUtils () {}

public static void deleteFolder(Path pathToBeDeleted) throws IOException {
Files.walk(pathToBeDeleted)
.sorted(Comparator.reverseOrder())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public List<ContentNode> listDirectories(final Path base, final String start) {
List<ContentNode> nodes = new ArrayList<>();

if ("".equals(folder)) {
metaData.tree().values()
metaData.getTree().values()
.stream()
.filter(node -> node.isDirectory())
.forEach((node) -> {
Expand All @@ -156,7 +156,7 @@ public List<ContentNode> listDirectories(final Path base, final String start) {
var parts = folder.split("\\/");
for (var part : parts) {
if (node == null) {
node = metaData.tree().get(part);
node = metaData.getTree().get(part);
} else {
node = node.children().get(part);
}
Expand All @@ -170,7 +170,7 @@ public List<ContentNode> listDirectories(final Path base, final String start) {
});
}
} else {
metaData.tree().get(folder).children().values()
metaData.getTree().get(folder).children().values()
.stream()
.filter(node -> node.isDirectory())
.forEach((node) -> {
Expand Down Expand Up @@ -205,7 +205,7 @@ public List<ContentNode> listSections(final Path contentFile) {
final Pattern isOrderedSectionOf = Constants.SECTION_ORDERED_OF_PATTERN.apply(filename);

if ("".equals(folder)) {
metaData.tree().values()
metaData.getTree().values()
.stream()
.filter(node -> !node.isHidden())
.filter(node -> node.isPublished())
Expand All @@ -232,7 +232,6 @@ public List<ContentNode> listSections(final Path contentFile) {
nodes.add(node);
});
}

}

return nodes;
Expand Down Expand Up @@ -268,10 +267,10 @@ public void init(MetaData.Type metaDataType) throws IOException {

if (MetaData.Type.PERSISTENT.equals(metaDataType)) {
this.metaData = new PersistentMetaData(this.hostBaseDirectory);
this.metaData.open();
} else {
this.metaData = new MemoryMetaData();
}
this.metaData.open();

this.contentBase = resolve("content/");
var templateBase = resolve("templates/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public enum Type {

void clear ();

Map<String, ContentNode> nodes();
Map<String, ContentNode> getNodes();

Map<String, ContentNode> tree();
Map<String, ContentNode> getTree();

<T> ContentQuery<T> query(final BiFunction<ContentNode, Integer, T> nodeMapper);
<T> ContentQuery<T> query(final String startURI, final BiFunction<ContentNode, Integer, T> nodeMapper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.github.thmarx.cms.api.Constants;
import com.github.thmarx.cms.api.db.ContentNode;
import com.github.thmarx.cms.filesystem.MetaData;
import com.github.thmarx.cms.filesystem.metadata.memory.MemoryMetaData;
import com.google.common.base.Strings;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -35,7 +34,6 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
Expand All @@ -50,17 +48,25 @@ public abstract class AbstractMetaData implements MetaData {

@Override
public void clear() {
nodes.clear();
tree.clear();
if (nodes != null) {
nodes.clear();
tree.clear();
}
}

@Override
public ConcurrentMap<String, ContentNode> nodes() {
public ConcurrentMap<String, ContentNode> getNodes() {
if (nodes == null) {
return new ConcurrentHashMap<>();
}
return new ConcurrentHashMap<>(nodes);
}

@Override
public ConcurrentMap<String, ContentNode> tree() {
public ConcurrentMap<String, ContentNode> getTree() {
if (tree == null) {
return new ConcurrentHashMap<>();
}
return new ConcurrentHashMap<>(tree);
}

Expand All @@ -75,10 +81,10 @@ public static boolean isVisible (ContentNode node) {

@Override
public Optional<ContentNode> byUri(String uri) {
if (!nodes().containsKey(uri)) {
if (!nodes.containsKey(uri)) {
return Optional.empty();
}
return Optional.of(nodes().get(uri));
return Optional.of(nodes.get(uri));
}

@Override
Expand All @@ -95,7 +101,7 @@ protected Optional<ContentNode> getFolder(String uri) {
return;
}
if (folder.get() == null) {
folder.set(tree().get(part));
folder.set(getTree().get(part));
} else {
folder.set(folder.get().children().get(part));
}
Expand Down Expand Up @@ -123,19 +129,19 @@ public void createDirectory(String uri) {
if (parentFolder.isPresent()) {
parentFolder.get().children().put(n.name(), n);
} else {
tree().put(n.name(), n);
tree.put(n.name(), n);
}
}

@Override
public List<ContentNode> listChildren(String uri) {
if ("".equals(uri)) {
return tree().values().stream()
return getTree().values().stream()
.filter(node -> !node.isHidden())
.map(this::mapToIndex)
.filter(node -> node != null)
.filter(MemoryMetaData::isVisible)
.collect(Collectors.toList());
.filter(AbstractMetaData::isVisible)
.toList();

} else {
Optional<ContentNode> findFolder = findFolder(uri);
Expand All @@ -145,8 +151,8 @@ public List<ContentNode> listChildren(String uri) {
.filter(node -> !node.isHidden())
.map(this::mapToIndex)
.filter(node -> node != null)
.filter(MemoryMetaData::isVisible)
.collect(Collectors.toList());
.filter(AbstractMetaData::isVisible)
.toList();
}
}
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,13 @@
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.BiFunction;

/**
*
* @author t.marx
*/
public class MemoryMetaData extends AbstractMetaData {

public MemoryMetaData () {
nodes = new ConcurrentHashMap<>();
tree = new ConcurrentHashMap<>();
}

@Override
public void addFile(final String uri, final Map<String, Object> data, final LocalDate lastModified) {
Expand All @@ -62,7 +56,7 @@ public void addFile(final String uri, final Map<String, Object> data, final Loca
}

void remove(String uri) {
var node = nodes.remove(uri);
nodes.remove(uri);

var folder = getFolder(uri);
var parts = uri.split(Constants.SPLIT_PATH_PATTERN);
Expand All @@ -76,10 +70,16 @@ void remove(String uri) {

@Override
public void open() throws IOException {
if (nodes == null) {
nodes = new ConcurrentHashMap<>();
tree = new ConcurrentHashMap<>();
}
}

@Override
public void close() throws IOException {
nodes = null;
tree = null;
}

@Override
Expand All @@ -97,7 +97,7 @@ public <T> ContentQuery<T> query(final String startURI, final BiFunction<Content
uri = startURI;
}

var filtered = nodes().values().stream().filter(node -> node.uri().startsWith(uri)).toList();
var filtered = getNodes().values().stream().filter(node -> node.uri().startsWith(uri)).toList();

return new MemoryQuery(filtered, nodeMapper);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public Page<T> page(final long page, final long pageSize) {
var filteredNodes = context.getNodes()
.filter(NodeUtil.contentTypeFiler(context.getContentType()))
.filter(node -> !node.isDirectory())
.filter(MemoryMetaData::isVisible)
.filter(AbstractMetaData::isVisible)
.toList();

var totalItems = filteredNodes.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import com.github.thmarx.cms.api.markdown.MarkdownRenderer;
import com.github.thmarx.cms.api.request.RequestContext;
import com.github.thmarx.cms.content.DefaultContentParser;
import com.github.thmarx.cms.content.views.ViewParser;
import com.github.thmarx.cms.content.views.ViewParser;
import com.github.thmarx.cms.eventbus.DefaultEventBus;
import com.github.thmarx.cms.filesystem.FileDB;
import java.io.IOException;
Expand Down

0 comments on commit 47e80c2

Please sign in to comment.