org.deeplearning4j.berkeley.Pair Maven / Gradle / Ivy
package org.deeplearning4j.berkeley;
import java.io.*;
import java.util.*;
/**
* A generic-typed pair of objects.
* @author Dan Klein
*/
public class Pair implements Serializable {
static final long serialVersionUID = 42;
F first;
S second;
public F getFirst() {
return first;
}
public S getSecond() {
return second;
}
public void setFirst(F pFirst) {
first = pFirst;
}
public void setSecond(S pSecond) {
second = pSecond;
}
public Pair reverse() {
return new Pair(second, first);
}
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof Pair))
return false;
final Pair pair = (Pair) o;
if (first != null ? !first.equals(pair.first) : pair.first != null)
return false;
if (second != null ? !second.equals(pair.second) : pair.second != null)
return false;
return true;
}
public int hashCode() {
int result;
result = (first != null ? first.hashCode() : 0);
result = 29 * result + (second != null ? second.hashCode() : 0);
return result;
}
public String toString() {
return "(" + getFirst() + ", " + getSecond() + ")";
}
public Pair(F first, S second) {
this.first = first;
this.second = second;
}
// Compares only first values
public static class FirstComparator, T>
implements Comparator> {
public int compare(Pair p1, Pair p2) {
return p1.getFirst().compareTo(p2.getFirst());
}
}
public static class ReverseFirstComparator, T>
implements Comparator> {
public int compare(Pair p1, Pair p2) {
return p2.getFirst().compareTo(p1.getFirst());
}
}
// Compares only second values
public static class SecondComparator>
implements Comparator> {
public int compare(Pair p1, Pair p2) {
return p1.getSecond().compareTo(p2.getSecond());
}
}
public static class ReverseSecondComparator>
implements Comparator> {
public int compare(Pair p1, Pair p2) {
return p2.getSecond().compareTo(p1.getSecond());
}
}
public static Pair newPair(S first, T second) {
return new Pair(first, second);
}
// Duplicate method to faccilitate backwards compatibility
// - aria42
public static Pair makePair(S first, T second) {
return new Pair(first, second);
}
public static class LexicographicPairComparator implements Comparator> {
Comparator firstComparator;
Comparator secondComparator;
public int compare(Pair pair1, Pair pair2) {
int firstCompare = firstComparator.compare(pair1.getFirst(), pair2.getFirst());
if (firstCompare != 0)
return firstCompare;
return secondComparator.compare(pair1.getSecond(), pair2.getSecond());
}
public LexicographicPairComparator(Comparator firstComparator, Comparator secondComparator) {
this.firstComparator = firstComparator;
this.secondComparator = secondComparator;
}
}
public static class DefaultLexicographicPairComparator,S extends Comparable>
implements Comparator> {
public int compare(Pair o1, Pair o2) {
int firstCompare = o1.getFirst().compareTo(o2.getFirst());
if (firstCompare != 0) {
return firstCompare;
}
return o2.getSecond().compareTo(o2.getSecond());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy