com.liferay.change.tracking.internal.closure.CTClosureImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.liferay.change.tracking.service
Show all versions of com.liferay.change.tracking.service
Liferay Change Tracking Service
/**
* SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/
package com.liferay.change.tracking.internal.closure;
import com.liferay.change.tracking.closure.CTClosure;
import com.liferay.petra.string.CharPool;
import com.liferay.petra.string.StringBundler;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
/**
* @author Preston Crary
*/
public class CTClosureImpl implements CTClosure {
public CTClosureImpl(
long ctCollectionId, Map> closureMap) {
_ctCollectionId = ctCollectionId;
_closureMap = closureMap;
}
@Override
public Map> getChildPKsMap(
long classNameId, long classPK) {
Collection nodes = _closureMap.get(
new Node(classNameId, classPK));
if (nodes == null) {
return Collections.emptyMap();
}
Set excludedNodes = new HashSet<>();
Queue queue = new LinkedList<>(nodes);
while (queue.size() > 0) {
Collection childNodes = _closureMap.get(queue.poll());
if (childNodes != null) {
for (Node childNode : childNodes) {
if (excludedNodes.add(childNode)) {
queue.add(childNode);
}
}
}
}
return _getPrimaryKeysMap(nodes, excludedNodes);
}
@Override
public long getCTCollectionId() {
return _ctCollectionId;
}
@Override
public Map> getRootPKsMap() {
return _getPrimaryKeysMap(
_closureMap.get(Node.ROOT_NODE), Collections.emptySet());
}
@Override
public String toString() {
StringBundler sb1 = new StringBundler();
sb1.append("{\n");
Map> pksMap = getRootPKsMap();
Deque>, Integer>>
queue = new LinkedList<>();
for (Map.Entry> entry :
pksMap.entrySet()) {
queue.add(new AbstractMap.SimpleImmutableEntry<>(entry, 1));
}
Map.Entry>, Integer>
indentEntry = null;
while ((indentEntry = queue.poll()) != null) {
Map.Entry> entry =
indentEntry.getKey();
long classNameId = entry.getKey();
int indent = indentEntry.getValue();
StringBundler sb2 = new StringBundler(indent);
for (int i = 0; i < indent; i++) {
sb2.append(CharPool.TAB);
}
String indentString = sb2.toString();
for (long classPK : entry.getValue()) {
sb1.append(indentString);
sb1.append("(classNameId=");
sb1.append(classNameId);
sb1.append(", classPK=");
sb1.append(classPK);
sb1.append(")\n");
Map> childPKsMap =
getChildPKsMap(classNameId, classPK);
for (Map.Entry> childEntry :
childPKsMap.entrySet()) {
queue.addFirst(
new AbstractMap.SimpleImmutableEntry<>(
childEntry, indent + 1));
}
}
}
sb1.append("}");
return sb1.toString();
}
private Map> _getPrimaryKeysMap(
Collection nodes, Set excludedNodes) {
Map> primaryKeysMap = new HashMap<>();
for (Node node : nodes) {
if (excludedNodes.contains(node)) {
continue;
}
List primaryKeys = primaryKeysMap.computeIfAbsent(
node.getClassNameId(), key -> new ArrayList<>());
primaryKeys.add(node.getPrimaryKey());
}
return primaryKeysMap;
}
private final Map> _closureMap;
private final long _ctCollectionId;
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy