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

javolution.util.internal.map.EntryComparatorImpl Maven / Gradle / Ivy

Go to download

Only the Java Core part of Javolution library, with slight modifications for use in MSFTBX.

The newest version!
/*
 * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
 * Copyright (C) 2012 - Javolution (http://javolution.org/)
 * All rights reserved.
 * 
 * Permission to use, copy, modify, and distribute this software is
 * freely granted, provided that this notice is preserved.
 */
package javolution.util.internal.map;

import java.io.Serializable;
import java.util.Map;

import javolution.util.function.Equality;

/**
 * Entry comparator implementation consistent with Map.Entry equals/hashcode
 * contract for standard key and value comparators.
 */
public final class EntryComparatorImpl implements
		Equality>, Serializable {

	private static final long serialVersionUID = 0x600L; // Version.
	public final Equality keyComparator;
	public final Equality valueComparator;

	public EntryComparatorImpl(Equality keyComparator,
			Equality valueComparator) {
		this.keyComparator = keyComparator;
		this.valueComparator = valueComparator;
	}

	@Override
	public boolean equal(Map.Entry left, Map.Entry right) {
		return keyComparator.equal(left.getKey(), right.getKey())
				&& valueComparator.equal(left.getValue(), right.getValue());
	}

	@Override
	public int compare(Map.Entry left, Map.Entry right) {
		int cmp = keyComparator.compare(left.getKey(), right.getKey());
		if (cmp != 0)
			return cmp;
		return valueComparator.compare(left.getValue(), right.getValue());
	}

	@Override
	public int hashOf(Map.Entry e) {
		// Consistent with Map.Entry hashcode if the comparators are standard.
		return keyComparator.hashOf(e.getKey())
				^ valueComparator.hashOf(e.getValue());
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy