📄 pair.java
字号:
package edu.stanford.nlp.util;import java.io.Serializable;import java.io.DataInputStream;import java.io.DataOutputStream;/** * Pair: A Class for holding a pair of objects. * * @author Dan Klein * @author Christopher Manning (added stuff from Kristina's, rounded out) * @version 2002/08/25 */public class Pair implements Comparable, Serializable { /** Direct access is deprecated. Use first(). * @serial */ public Object first; /** Direct access is deprecated. Use second(). * @serial */ public Object second; public Pair() { // first = null; second = null; -- default initialization } public Pair(Object first, Object second) { this.first = first; this.second = second; } public Object first() { return first; } public Object second() { return second; } public String toString() { return "("+first+","+second+")"; } public boolean equals(Object o) { if (o instanceof Pair) { Pair p = (Pair) o; return (first == null ? p.first == null : first.equals(p.first)) && (second == null ? p.second == null : second.equals(p.second)); } else { return false; } } public int hashCode() { return (((first == null) ? 0 : first.hashCode()) << 16) ^ ((second == null) ? 0: second.hashCode()); } /** Read a string representation of a Pair from a DataStream. * This might not work correctly unless the pair of objects are of type * <code>String</code>. */ public void read(DataInputStream in) { try { first = in.readUTF(); second = in.readUTF(); } catch(Exception e) { e.printStackTrace(); } } /** Write a string representation of a Pair from a DataStream. * The <code>toString()</code> method is called on each of the pair * of objects and a <code>String</code> representation is written. * This might not allow one to recover the pair of objects unless they * are of type <code>String</code>. */ public void save(DataOutputStream out) { try { out.writeUTF(first.toString()); out.writeUTF(second.toString()); } catch(Exception e) { e.printStackTrace(); } } /** * Compares this <code>Pair</code> to another object. * If the object is a <code>Pair</code>, this function will work providing * the elements of the <code>Pair</code> are themselves comparable. * It will then return a value based on the pair of objects, where * <code>p > q iff p.first() > q.first() || * (p.first().equals(q.first()) && p.second() > q.second())</code>. * If the other object is not a <code>Pair</code>, it throws a * <code>ClassCastException</code>. * * @param o the <code>Object</code> to be compared. * @return the value <code>0</code> if the argument is a * <code>Pair</code> equal to this <code>Pair</code>; a value less than * <code>0</code> if the argument is a <code>Pair</code> * greater than this <code>Pair</code>; and a value * greater than <code>0</code> if the argument is a * <code>Pair</code> less than this <code>Pair</code>. * @exception <code>ClassCastException</code> if the argument is not a * <code>Pair</code>. * @see java.lang.Comparable */ public int compareTo(Object o) { Pair another = (Pair) o; int comp = ((Comparable) first()).compareTo(another.first()); if (comp != 0) { return comp; } else { return ((Comparable) second()).compareTo(another.second()); } } /** use serialVersionUID for cross version serialization compatibility */ private static final long serialVersionUID = 1360822168806852920L;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -