Created
February 4, 2011 09:10
-
-
Save noiano/810905 to your computer and use it in GitHub Desktop.
PairOfStringInt.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.noiano.hXMLInvIndex; | |
import java.io.DataInput; | |
import java.io.DataOutput; | |
import java.io.IOException; | |
import org.apache.hadoop.io.Text; | |
import org.apache.hadoop.io.WritableComparable; | |
import org.apache.hadoop.io.WritableComparator; | |
import org.apache.hadoop.io.WritableUtils; | |
import edu.umd.cloud9.io.WritableComparatorUtils; | |
/** | |
* WritableComparable representing a pair consisting of a {@link String } and an int. | |
* The elements in the pair are referred to as the left and right elements. The | |
* natural sort order is: first by the left element, and then by the right | |
* element. | |
* | |
* @author MD | |
*/ | |
public class PairOfStringInt implements WritableComparable<PairOfStringInt> { | |
private String leftElement; | |
private int rightElement; | |
/** | |
* Creates a pair. | |
*/ | |
public PairOfStringInt() {} | |
/** | |
* Creates a pair. | |
* | |
* @param left the left element | |
* @param right the right element | |
*/ | |
public PairOfStringInt(String left, int right) { | |
set(left, right); | |
} | |
/** | |
* Deserializes the pair. | |
* | |
* @param in source for raw byte representation | |
*/ | |
public void readFields(DataInput in) throws IOException { | |
leftElement = in.readUTF(); | |
rightElement = in.readInt(); | |
} | |
/** | |
* Serializes this pair. | |
* | |
* @param out where to write the raw byte representation | |
*/ | |
public void write(DataOutput out) throws IOException { | |
out.writeUTF(leftElement); | |
out.writeInt(rightElement); | |
} | |
/** | |
* Returns the left element. | |
* | |
* @return the left element | |
*/ | |
public String getLeftElement() { | |
return leftElement; | |
} | |
/** | |
* Returns the right element. | |
* | |
* @return the right element | |
*/ | |
public int getRightElement() { | |
return rightElement; | |
} | |
/** | |
* Returns the key (left element). | |
* | |
* @return the key | |
*/ | |
public String getKey() { | |
return leftElement; | |
} | |
/** | |
* Returns the value (right element). | |
* | |
* @return the value | |
*/ | |
public int getValue() { | |
return rightElement; | |
} | |
/** | |
* Sets the right and left elements of this pair. | |
* | |
* @param left the left element | |
* @param right the right element | |
*/ | |
public void set(String left, int right) { | |
leftElement = left; | |
rightElement = right; | |
} | |
/** | |
* Checks two pairs for equality. | |
* | |
* @param obj object for comparison | |
* @return <code>true</code> if <code>obj</code> is equal to this object, <code>false</code> otherwise | |
*/ | |
public boolean equals(Object obj) { | |
PairOfStringInt pair = (PairOfStringInt) obj; | |
return leftElement.equals(pair.getLeftElement()) && rightElement == pair.getRightElement(); | |
} | |
/** | |
* Defines a natural sort order for pairs. Pairs are sorted first by the | |
* left element, and then by the right element. | |
* | |
* @return a value less than zero, a value greater than zero, or zero if | |
* this pair should be sorted before, sorted after, or is equal to | |
* <code>obj</code>. | |
*/ | |
public int compareTo(PairOfStringInt obj) { | |
PairOfStringInt pair = (PairOfStringInt) obj; | |
int cmp = leftElement.compareTo(pair.leftElement); | |
if (cmp != 0) { | |
return cmp; | |
} | |
if (rightElement == pair.getRightElement()) | |
return 0; | |
else | |
return rightElement < pair.getRightElement() ? -1 : 1; | |
} | |
/** | |
* Returns a hash code value for the pair. | |
* | |
* @return hash code for the pair | |
*/ | |
public int hashCode() { | |
return leftElement.hashCode() + rightElement; | |
} | |
/** | |
* Generates human-readable String representation of this pair. | |
* | |
* @return human-readable String representation of this pair | |
*/ | |
public String toString() { | |
return "(" + leftElement + ", " + rightElement + ")"; | |
} | |
/** | |
* Clones this object. | |
* | |
* @return clone of this object | |
*/ | |
public PairOfStringInt clone() { | |
return new PairOfStringInt(this.leftElement, this.rightElement); | |
} | |
/** Comparator optimized for <code>PairOfIntString</code>. */ | |
public static class Comparator extends WritableComparator { | |
/** | |
* Creates a new Comparator optimized for <code>PairOfIntString</code>. | |
*/ | |
public Comparator() { | |
super(PairOfStringInt.class); | |
} | |
private static final Text.Comparator TEXT_COMPARATOR = new Text.Comparator(); | |
/** | |
* Optimization hook. | |
*/ | |
@Override | |
public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { | |
int firstL1; | |
int firstL2; | |
try{ | |
firstL1 = WritableUtils.decodeVIntSize(b1[s1]) + readVInt(b1, s1); | |
firstL2 = WritableUtils.decodeVIntSize(b2[s2]) + readVInt(b2, s2); | |
int cmp = TEXT_COMPARATOR.compare(b1, s1, firstL1, b2, s2, firstL2); | |
if (cmp != 0) | |
return cmp; | |
} | |
catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
int thisRightValue = readInt(b1, firstL1); | |
int thatRightValue = readInt(b2, firstL2); | |
if (thisRightValue == thatRightValue) | |
return 0; | |
else | |
return thisRightValue < thatRightValue ? -1 : 1; | |
} | |
} | |
static { // register this comparator | |
WritableComparator.define(PairOfStringInt.class, new Comparator()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment