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

org.opentripplanner.common.MinMap Maven / Gradle / Ivy

package org.opentripplanner.common;

import java.util.HashMap;

/**
 * A HashMap that has been extended to track the greatest or smallest value for each key.
 * Note that this does not change the meaning of the 'put' method. It adds two new methods that add the min/max
 * behavior. This class used to be inside SimpleIsochrone.
 */
public class MinMap> extends HashMap {
    private static final long serialVersionUID = -23L;

    /**
     * Put the given key-value pair in the map if the map does not yet contain the key, or if
     * the value is less than the existing value for the same key.
     * @return whether the key-value pair was inserted in the map.
     */
    public boolean putMin(K key, V value) {
        V oldValue = this.get(key);
        if (oldValue == null || value.compareTo(oldValue) < 0) {
            this.put(key, value);
            return true;
        }
        return false;
    }

    /**
     * Put the given key-value pair in the map if the map does not yet contain the key, or if
     * the value is greater than the existing value for the same key.
     * @return whether the key-value pair was inserted in the map.
     */
    public boolean putMax(K key, V value) {
        V oldValue = this.get(key);
        if (oldValue == null || value.compareTo(oldValue) > 0) {
            this.put(key, value);
            return true;
        }
        return false;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy