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

com.softicar.platform.common.container.map.SortedMaps Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.container.map;

import com.softicar.platform.common.core.utils.CastUtils;
import java.util.Comparator;
import java.util.Map;
import java.util.SortedMap;

public class SortedMaps {

	/**
	 * Returns the first entry of the specified sorted map.
	 * 
	 * @param map
	 *            the sorted map
	 * @return the first entry or null if and only if the map is empty
	 */
	public static  Map.Entry getFirstEntry(SortedMap map) {

		if (map.isEmpty()) {
			return null;
		}

		return map.entrySet().iterator().next();
	}

	/**
	 * Returns the last map entry whose key is less than the specified key. If
	 * there is no such entry, this returns null.
	 * 
	 * @param map
	 *            the sorted map
	 * @param key
	 *            the key (the map does not need contain this key)
	 * @return the preceding entry or null
	 */
	public static  Map.Entry getPrecedingEntry(SortedMap map, K key) {

		return SortedMaps.getLastEntry(map.headMap(key));
	}

	/**
	 * Returns the entry with the specified key of the specified sorted map.
	 * 
	 * @param map
	 *            the sorted map
	 * @return the entry or null if there is no matching entry for the key
	 */
	public static  Map.Entry getEntry(SortedMap map, K key) {

		Map.Entry entry = SortedMaps.getFirstEntry(map.tailMap(key));

		return entry != null && compareKeys(map, key, entry.getKey()) == 0? entry : null;
	}

	/**
	 * Returns the first map entry whose key is greater than the specified key.
	 * If there is no such entry, this returns null.
	 * 
	 * @param map
	 *            the sorted map
	 * @param key
	 *            the key (the map does not need contain this key)
	 * @return the succeeding entry or null
	 */
	public static  Map.Entry getSucceedingEntry(SortedMap map, K key) {

		SortedMap tailMap = map.tailMap(key);
		for (Map.Entry entry: tailMap.entrySet()) {
			if (compareKeys(map, entry.getKey(), key) > 0) {
				return entry;
			}
		}
		return null;
	}

	/**
	 * Returns the last entry of the specified sorted map.
	 * 
	 * @param map
	 *            the sorted map
	 * @return the last entry or null if and only if the map is empty
	 */
	public static  Map.Entry getLastEntry(SortedMap map) {

		if (map.isEmpty()) {
			return null;
		}

		return SortedMaps.getEntry(map, map.lastKey());
	}

	private static  int compareKeys(SortedMap map, K a, K b) {

		Comparator comparator = map.comparator();
		if (comparator != null) {
			return comparator.compare(a, b);
		} else {
			Comparable comparableA = CastUtils.cast(a);
			return comparableA.compareTo(b);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy