com.liferay.commerce.util.comparator.CommerceAddressNameComparator Maven / Gradle / Ivy
/**
* 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.commerce.util.comparator;
import com.liferay.commerce.model.CommerceAddress;
import com.liferay.portal.kernel.util.OrderByComparator;
/**
* @author Luca Pellizzon
*/
public class CommerceAddressNameComparator
extends OrderByComparator {
public static final String ORDER_BY_ASC = "name ASC";
public static final String ORDER_BY_DESC = "name DESC";
public static final String[] ORDER_BY_FIELDS = {"name"};
public static CommerceAddressNameComparator getInstance(boolean ascending) {
if (ascending) {
return _INSTANCE_ASCENDING;
}
return _INSTANCE_DESCENDING;
}
@Override
public int compare(
CommerceAddress commerceAddress1, CommerceAddress commerceAddress2) {
String name1 = commerceAddress1.getName();
String name2 = commerceAddress2.getName();
int value = name1.compareTo(name2);
if (_ascending) {
return value;
}
return Math.negateExact(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 CommerceAddressNameComparator(boolean ascending) {
_ascending = ascending;
}
private static final CommerceAddressNameComparator _INSTANCE_ASCENDING =
new CommerceAddressNameComparator(true);
private static final CommerceAddressNameComparator _INSTANCE_DESCENDING =
new CommerceAddressNameComparator(false);
private final boolean _ascending;
}