All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.change.tracking.internal.closure.CTClosureImpl Maven / Gradle / Ivy

There is a newer version: 3.0.108
Show newest version
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

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 - 2025 Weber Informatics LLC | Privacy Policy