skipList / src / KVPair.java
KVPair.java
Raw
/**
 * 
 * @author nickgrifasi
 * @version 9/20/21
 * @param <T> generic used for handling key 
 * @param <N> generic used for handling nodeData
 * 
 * generic KVPair class that stores keys and values. 
 * Inspiration from OpenDSA textbook:
 * https://opendsa-server.cs.vt.edu/ODSA/Books/Everything/html/Comparison.html
 */
public class KVPair<T extends Comparable<T>, N> implements
    Comparable<KVPair<T, N>> {

    /*
     * Generic key to be used
     */
    private T key;

    /**
     * Generic data within the pair
     */
    private N nodeData;

    /**
     * Constructor of KVPair that stores key, nodeData
     * @param keyT key of KVPair
     * @param data nodeData of KVPair
     */
    public KVPair(T keyT, N data) {
        key = keyT;
        nodeData = data;
    }

    /**
     * Generic comparison method used to compare generic keys
     * @return int compares keys, returns 0,1,-1 based on comparison
     * @param exKey generic object to compare
     */
    public int compareTo(T exKey) {
        return key.compareTo(exKey);
    }

    /**
     * Un-generic comparison used to compare KVPair object keys 
     * @return int compares KVPairs, returns 0,1,-1 based on comparison
     * @param kvpair KVPair to compare with 
     */
    public int compareTo(KVPair<T, N> kvpair) {
        return key.compareTo(kvpair.getKey());
    }

    /**
     * Getter methods that returns key in KVpair
     * 
     * @return key in KVPair
     */
    public T getKey() {
        return key;
    }

    /**
     * Getter method that returns the data within KVPair
     * 
     * @return nodeData in KVPair
     */
    public N getNodeData() {
        return nodeData;
    }

    /**
     * converts K,V in the KVPair into a string.
     * @return string being returned of K,V in KVPair
     */
    public String toString() {
        return key.toString() + ", " + nodeData.toString();
    }

}