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

com.liferay.fragment.util.comparator.FragmentEntryNameComparator Maven / Gradle / Ivy

There is a newer version: 50.0.0
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.fragment.util.comparator;

import com.liferay.fragment.model.FragmentEntry;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringUtil;

/**
 * @author Jürgen Kappler
 */
public class FragmentEntryNameComparator
	extends OrderByComparator {

	public static final String ORDER_BY_ASC = "FragmentEntry.name ASC";

	public static final String ORDER_BY_DESC = "FragmentEntry.name DESC";

	public static final String[] ORDER_BY_FIELDS = {"name"};

	public static FragmentEntryNameComparator getInstance(boolean ascending) {
		if (ascending) {
			return _INSTANCE_ASCENDING;
		}

		return _INSTANCE_DESCENDING;
	}

	@Override
	public int compare(
		FragmentEntry fragmentEntry1, FragmentEntry fragmentEntry2) {

		String name1 = StringUtil.toLowerCase(fragmentEntry1.getName());
		String name2 = StringUtil.toLowerCase(fragmentEntry2.getName());

		int value = name1.compareTo(name2);

		if (_ascending) {
			return value;
		}

		return -value;
	}

	@Override
	public String getOrderBy() {
		if (_ascending) {
			return ORDER_BY_ASC;
		}

		return ORDER_BY_DESC;
	}

	@Override
	public String[] getOrderByFields() {
		return ORDER_BY_FIELDS;
	}

	@Override
	public boolean isAscending() {
		return _ascending;
	}

	private FragmentEntryNameComparator(boolean ascending) {
		_ascending = ascending;
	}

	private static final FragmentEntryNameComparator _INSTANCE_ASCENDING =
		new FragmentEntryNameComparator(true);

	private static final FragmentEntryNameComparator _INSTANCE_DESCENDING =
		new FragmentEntryNameComparator(false);

	private final boolean _ascending;

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy