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

com.liferay.jenkins.results.parser.BalancedListSplitter Maven / Gradle / Ivy

There is a newer version: 1.0.1492
Show newest version
/**
 * 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.jenkins.results.parser;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Peter Yoo
 */
public abstract class BalancedListSplitter {

	public BalancedListSplitter(long maxListWeight) {
		_maxListWeight = maxListWeight;
	}

	public long getWeight(ListItemList listItemList) {
		long weight = 0;

		for (ListItem listItem : listItemList) {
			weight += listItem.getWeight(null);
		}

		return weight;
	}

	public abstract long getWeight(T item);

	public List> split(List list) {
		ListItemList listItems = new ListItemList(this);

		for (T item : list) {
			listItems.add(new ListItem(this, item));
		}

		Collections.sort(listItems);

		long totalWeight = listItems.getWeight();

		int minNumberOfLists = (int)(totalWeight / _maxListWeight);

		if ((totalWeight % _maxListWeight) >= 0) {
			minNumberOfLists++;
		}

		List listItemLists = _createListItemSortedSetList(
			minNumberOfLists);

		for (ListItem listItem : listItems) {
			Collections.sort(listItemLists);

			ListItemList emptiestListItemList = listItemLists.get(0);

			if (emptiestListItemList.isEmpty() ||
				(emptiestListItemList.getAvailableWeight() >=
					listItem.getWeight(emptiestListItemList))) {

				emptiestListItemList.add(listItem);

				continue;
			}

			ListItemList newListItemList = new ListItemList(
				this, _maxListWeight);

			newListItemList.add(listItem);

			listItemLists.add(newListItemList);
		}

		List> lists = new ArrayList<>(listItemLists.size());

		for (ListItemList listItemList : listItemLists) {
			List newList = listItemList.toList();

			if ((newList == null) || newList.isEmpty()) {
				continue;
			}

			lists.add(newList);
		}

		return lists;
	}

	protected class ListItem implements Comparable {

		public ListItem(BalancedListSplitter balancedListSplitter, T item) {
			_balancedListSplitter = balancedListSplitter;
			_item = item;
		}

		@Override
		public int compareTo(ListItem otherListItem) {
			Long weight = getWeight(null);

			return -1 * weight.compareTo(otherListItem.getWeight(null));
		}

		public T getItem() {
			return _item;
		}

		public long getWeight(ListItemList listItemList) {
			if (listItemList == null) {
				return _getWeight();
			}

			ListItemList tempListItemList = new ListItemList(
				_balancedListSplitter);

			tempListItemList.addAll(listItemList);

			long originalWeight = tempListItemList.getWeight();

			tempListItemList.add(this);

			return tempListItemList.getWeight() - originalWeight;
		}

		private long _getWeight() {
			return _balancedListSplitter.getWeight(_item);
		}

		private BalancedListSplitter _balancedListSplitter;
		private final T _item;

	}

	protected class ListItemList
		extends ArrayList implements Comparable {

		public ListItemList(BalancedListSplitter balancedListSplitter) {
			_balancedListSplitter = balancedListSplitter;
		}

		public ListItemList(
			BalancedListSplitter balancedListSplitter, Long targetWeight) {

			this(balancedListSplitter);

			_targetWeight = targetWeight;
		}

		@Override
		public int compareTo(ListItemList otherListItemSortedSet) {
			Long availableWeight = getAvailableWeight();
			Long otherAvailableWeight =
				otherListItemSortedSet.getAvailableWeight();

			if ((availableWeight == null) && (otherAvailableWeight == null)) {
				return 0;
			}

			if (availableWeight == null) {
				return 1;
			}

			if (otherAvailableWeight == null) {
				return -1;
			}

			return -1 * availableWeight.compareTo(otherAvailableWeight);
		}

		public Long getAvailableWeight() {
			if (_targetWeight == null) {
				return null;
			}

			long availableWeight = _targetWeight - getWeight();

			if (availableWeight <= 0) {
				return 0L;
			}

			return availableWeight;
		}

		public long getWeight() {
			return _balancedListSplitter.getWeight(this);
		}

		public List toList() {
			List list = new ArrayList<>(size());

			for (ListItem listItem : this) {
				list.add(listItem._item);
			}

			return list;
		}

		private BalancedListSplitter _balancedListSplitter;
		private Long _targetWeight;

	}

	private List _createListItemSortedSetList(int size) {
		List listItemSortedSetList = new ArrayList<>();

		for (int i = 0; i < size; i++) {
			listItemSortedSetList.add(new ListItemList(this, _maxListWeight));
		}

		return listItemSortedSetList;
	}

	private final long _maxListWeight;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy