org.canova.api.berkeley.Pair Maven / Gradle / Ivy
/*
*
* *
* * * Copyright 2015 Skymind,Inc.
* * *
* * * Licensed under the Apache License, Version 2.0 (the "License");
* * * you may not use this file except in compliance with the License.
* * * You may obtain a copy of the License at
* * *
* * * http://www.apache.org/licenses/LICENSE-2.0
* * *
* * * Unless required by applicable law or agreed to in writing, software
* * * distributed under the License is distributed on an "AS IS" BASIS,
* * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * * See the License for the specific language governing permissions and
* * * limitations under the License.
* *
*
*/
package org.canova.api.berkeley;
import java.io.Serializable;
import java.util.Comparator;
/**
* A generic-typed pair of objects.
* @author Dan Klein
*/
public class Pair implements Serializable,Comparable> {
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 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.
*
* The implementor must ensure sgn(x.compareTo(y)) ==
* -sgn(y.compareTo(x)) for all x and y. (This
* implies that x.compareTo(y) must throw an exception iff
* y.compareTo(x) throws an exception.)
*
* The implementor must also ensure that the relation is transitive:
* (x.compareTo(y)>0 && y.compareTo(z)>0) implies
* x.compareTo(z)>0.
*
* Finally, the implementor must ensure that x.compareTo(y)==0
* implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for
* all z.
*
* It is strongly recommended, but not strictly required that
* (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any
* class that implements the Comparable interface and violates
* this condition should clearly indicate this fact. The recommended
* language is "Note: this class has a natural ordering that is
* inconsistent with equals."
*
* In the foregoing description, the notation
* sgn(expression) designates the mathematical
* signum function, which is defined to return one of -1,
* 0, or 1 according to whether the value of
* expression is negative, zero or positive.
*
* @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 NullPointerException if the specified object is null
* @throws ClassCastException if the specified object's type prevents it
* from being compared to this object.
*/
@Override
public int compareTo(Pair o) {
return new DefaultLexicographicPairComparator().compare(this,o);
}
// 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());
}
}
}