All pastes #185956 Raw Edit

Something

public java v1 · immutable
#185956 ·published 2006-09-29 12:59 UTC
rendered paste body
package de.dfki.lt.ontarget.tools;/** * Represents a pair which can be compared to other pairs. Thus, both components * of the pair must be comparable, e.g. numbers, strings, etc. *  * @author Daniel Beck */public class ComparablePair<K extends Comparable<K>, V extends Comparable<V>>		extends Pair<K, V> implements Comparable<ComparablePair<K, V>> {	/** First Component of the pair */	private K first;	/** Second component of the pair */	private V second;	/**	 * Constructor.	 * <p>	 * Initializes the first and the second component of the pair to null	 */	public ComparablePair() {		super();	}	/**	 * constructor	 * 	 * @param first	 *            Value to which the first component of the pair is set	 * @param second	 *            Value to which the second component of the pair is set	 */	public ComparablePair(K first, V second) {		this.first = first;		this.second = second;	}	/**	 * returns the type	 * 	 * @return a model	 */	protected K first() {		return first;	}	/**	 * returns the model	 * 	 * @return a model	 */	protected V second() {		return second;	}	/**	 * sets the type	 * 	 * @param first	 *            Type to set	 */	protected void setFirst(K first) {		this.first = first;	}	/**	 * sets the model of the pair	 * 	 * @param secondComponent	 *            The model component of te pair will be set to this model	 */	protected void setSecond(V secondComponent) {		this.second = secondComponent;	}	/**	 * Compares this object with the specified object for order. Returns a	 * negative integer, zero, or a positive integer as this object is less	 * than, equal to, or greater than the specified object.	 * <p> *	 * 	 * @param o	 *            the Object to be compared.	 * @return a negative integer, zero, or a positive integer as this object is	 *         less than, equal to, or greater than the specified object.	 * 	 * @throws ClassCastException	 *             if the specified object's type prevents it from being	 *             compared to this Object.	 */	public int compareTo(ComparablePair<K, V> pair) {		int firstComparaison = first.compareTo(pair.first());		if (firstComparaison == 0)			return second.compareTo(pair.second());		return firstComparaison;	}	@Override	public boolean equals(Object obj) {		if (this == obj)			return true;		if (obj == null)			return false;		if (getClass() != obj.getClass())			return false;		final Pair other = (Pair) obj;		if (first == null) {			if (other.first() != null)				return false;		} else if (!first.equals(other.first()))			return false;		if (second == null) {			if (other.second() != null)				return false;		} else if (!second.equals(other.second()))			return false;		return true;	}	/*	 * (non-Javadoc)	 * 	 * @see java.lang.Object#hashCode()	 */	@Override	public int hashCode() {		final int PRIME = 31;		int result = 1;		result = PRIME * result + ((first == null) ? 0 : first.hashCode());		result = PRIME * result + ((second == null) ? 0 : second.hashCode());		return result;	}}==================package de.dfki.lt.ontarget.tools;/** * Represents a pair, i.e. a 2 components tuple. It contains method to get and * set each of the components. *  * @author Daniel Beck */public class Pair<K, V> {	/** First Component of the pair */	private K first;	/** Second component of the pair */	private V second;	/**	 * Constructor.	 * <p>	 * Initializes the first and the second component of the pair to null	 */	public Pair() {		first = null;		second = null;	}	/**	 * Class Constructor	 * 	 * @param first	 *            Value to which the first component of the pair is set	 * @param second	 *            Value to which the second component of the pair is set	 */	public Pair(K first, V second) {		this.first = first;		this.second = second;	}	/**	 * Copy constructor	 * 	 * @param pair	 *            pair to be copied	 */	public Pair(Pair<K, V> pair) {		this.first = (K) pair.first();		this.second = (V) pair.second();	}	/**	 * Returns the second component	 * 	 * @return a model	 */	protected V second() {		return second;	}	/**	 * Sets the second component	 * 	 * @param second	 *            The model component of te pair will be set to this model	 */	protected void setSecond(V second) {		this.second = second;	}	/**	 * Returns the first component	 * 	 * @return a model	 */	protected K first() {		return first;	}	/**	 * Sets the first component	 * 	 * @param first	 *            Type to set	 */	protected void setFirst(K first) {		this.first = first;	}	/*	 * (non-Javadoc)	 * 	 * @see java.lang.Object#equals(java.lang.Object)	 */	@Override	public boolean equals(Object obj) {		if (this == obj)			return true;		if (obj == null)			return false;		if (getClass() != obj.getClass())			return false;		final Pair other = (Pair) obj;		if (first == null) {			if (other.first() != null)				return false;		} else if (!first.equals(other.first()))			return false;		if (second == null) {			if (other.second() != null)				return false;		} else if (!second.equals(other.second()))			return false;		return true;	}	/*	 * (non-Javadoc)	 * 	 * @see java.lang.Object#hashCode()	 */	@Override	public int hashCode() {		final int PRIME = 31;		int result = 1;		result = PRIME * result + ((first == null) ? 0 : first.hashCode());		result = PRIME * result + ((second == null) ? 0 : second.hashCode());		return result;	}}