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

com.liferay.message.boards.model.impl.MBTreeWalkerImpl Maven / Gradle / Ivy

There is a newer version: 5.0.110
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.message.boards.model.impl;

import com.liferay.message.boards.model.MBMessage;
import com.liferay.message.boards.model.MBTreeWalker;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Brian Wing Shun Chan
 */
public class MBTreeWalkerImpl implements MBTreeWalker {

	public MBTreeWalkerImpl(List messages) {
		MBMessage rootMessage = null;

		try {
			for (int i = 0; i < messages.size(); i++) {
				MBMessage curMessage = messages.get(i);

				if (curMessage.isRoot()) {
					rootMessage = curMessage;
				}

				long parentMessageId = curMessage.getParentMessageId();

				if (!curMessage.isRoot() &&
					!_messageIdsMap.containsKey(parentMessageId)) {

					_messageIdsMap.put(parentMessageId, i);
				}
			}
		}
		catch (Exception exception) {
			_log.error("Unable to initialize tree walker", exception);
		}

		_messages = messages;

		_rootMessage = rootMessage;
	}

	@Override
	public List getChildren(MBMessage message) {
		List children = new ArrayList<>();

		int[] range = getChildrenRange(message);

		for (int i = range[0]; i < range[1]; i++) {
			children.add(_messages.get(i));
		}

		return children;
	}

	@Override
	public int[] getChildrenRange(MBMessage message) {
		long messageId = message.getMessageId();

		Integer pos = _messageIdsMap.get(messageId);

		if (pos == null) {
			return new int[] {0, 0};
		}

		int[] range = new int[2];

		range[0] = pos.intValue();

		for (int i = range[0]; i < _messages.size(); i++) {
			MBMessage curMessage = _messages.get(i);

			if (curMessage.getParentMessageId() == messageId) {
				range[1] = i + 1;
			}
			else {
				break;
			}
		}

		return range;
	}

	@Override
	public List getMessages() {
		return _messages;
	}

	@Override
	public MBMessage getRoot() {
		return _rootMessage;
	}

	@Override
	public boolean isLeaf(MBMessage message) {
		Long messageIdObj = Long.valueOf(message.getMessageId());

		if (_messageIdsMap.containsKey(messageIdObj)) {
			return false;
		}

		return true;
	}

	@Override
	public boolean isOdd() {
		_odd = !_odd;

		return _odd;
	}

	private static final Log _log = LogFactoryUtil.getLog(
		MBTreeWalkerImpl.class);

	private final Map _messageIdsMap = new HashMap<>();
	private final List _messages;
	private boolean _odd;
	private final MBMessage _rootMessage;

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy