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

com.liferay.osgi.service.tracker.collections.map.PropertyServiceReferenceComparator Maven / Gradle / Ivy

There is a newer version: 8.1.0
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.osgi.service.tracker.collections.map;

import java.io.Serializable;

import java.util.Comparator;

import org.osgi.framework.ServiceReference;

/**
 * @author Carlos Sierra Andrés
 */
public class PropertyServiceReferenceComparator
	implements Comparator>, Serializable {

	public PropertyServiceReferenceComparator(String propertyKey) {
		_propertyKey = propertyKey;
	}

	@Override
	public int compare(
		ServiceReference serviceReference1,
		ServiceReference serviceReference2) {

		if (serviceReference1 == null) {
			if (serviceReference2 == null) {
				return 0;
			}

			return 1;
		}
		else if (serviceReference2 == null) {
			return -1;
		}

		Object propertyValue1 = serviceReference1.getProperty(_propertyKey);
		Object propertyValue2 = serviceReference2.getProperty(_propertyKey);

		if (propertyValue1 == null) {
			if (propertyValue2 == null) {
				return 0;
			}

			return 1;
		}
		else if (propertyValue2 == null) {
			return -1;
		}

		if (!(propertyValue2 instanceof Comparable)) {
			return serviceReference2.compareTo(serviceReference1);
		}

		Comparable propertyValueComparable2 =
			(Comparable)propertyValue2;

		return propertyValueComparable2.compareTo(propertyValue1);
	}

	private final String _propertyKey;

}