alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (Pair.java)

This example Java source code file (Pair.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

comparable, comparator, firstcomparator, lexicographicpaircomparator, override, pair, reversefirstcomparator, secondcomparator, serializable,comparable, string, util

The Pair.java Java example source code

/*
 *
 *  * 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.deeplearning4j.berkeley;

import java.io.*;
import java.util.*;


/**
 * A generic-typed pair of objects.
 * @author Dan Klein
 */
public class Pair<F, S> 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<S, F> reverse() {
		return new Pair<S, F>(second, first);
	}

	public boolean equals(Object o) {
    if (this == o)
      return true;
    if (!(o instanceof Pair))
      return false;

    final Pair pair = (Pair) o;

    return !(first != null ? !first.equals(pair.first) : pair.first != null) && !(second != null ? !second.equals(pair.second) : pair.second != null);

  }

	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.
     * <p/>
     * <p>The implementor must ensure sgn(x.compareTo(y)) ==
     * -sgn(y.compareTo(x))</tt> for all x and y.  (This
     * implies that <tt>x.compareTo(y) must throw an exception iff
     * <tt>y.compareTo(x) throws an exception.)
     * <p/>
     * <p>The implementor must also ensure that the relation is transitive:
     * <tt>(x.compareTo(y)>0 && y.compareTo(z)>0) implies
     * <tt>x.compareTo(z)>0.
     * <p/>
     * <p>Finally, the implementor must ensure that x.compareTo(y)==0
     * implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for
     * all <tt>z.
     * <p/>
     * <p>It is strongly recommended, but not strictly required that
     * <tt>(x.compareTo(y)==0) == (x.equals(y)).  Generally speaking, any
     * class that implements the <tt>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."
     * <p/>
     * <p>In the foregoing description, the notation
     * <tt>sgn(expression) designates the mathematical
     * <i>signum function, which is defined to return one of -1,
     * <tt>0, or 1 according to whether the value of
     * <i>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<F,S> o) {
        return new DefaultLexicographicPairComparator().compare(this,o);
    }

    // Compares only first values
	public static class FirstComparator<S extends Comparable
	implements Comparator<Pair {
		public int compare(Pair<S, T> p1, Pair p2) {
			return p1.getFirst().compareTo(p2.getFirst());
		}
	}

	public static class ReverseFirstComparator<S extends Comparable
	implements Comparator<Pair {
		public int compare(Pair<S, T> p1, Pair p2) {
			return p2.getFirst().compareTo(p1.getFirst());
		}
	}

	// Compares only second values
	public static class SecondComparator<S, T extends Comparable
	implements Comparator<Pair {
		public int compare(Pair<S, T> p1, Pair p2) {
			return p1.getSecond().compareTo(p2.getSecond());
		}
	}

	public static class ReverseSecondComparator<S, T extends Comparable
	implements Comparator<Pair {
		public int compare(Pair<S, T> p1, Pair p2) {
			return p2.getSecond().compareTo(p1.getSecond());
		}
	}

	public static <S, T> Pair newPair(S first, T second) {
		return new Pair<S, T>(first, second);
	}
	// Duplicate method to faccilitate backwards compatibility
	// - aria42
	public static <S, T> Pair makePair(S first, T second) {
		return new Pair<S, T>(first, second);
	}

	public static class LexicographicPairComparator<F,S>  implements Comparator> {
		Comparator<F> firstComparator;
		Comparator<S> secondComparator;

		public int compare(Pair<F, S> 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<F> firstComparator, Comparator secondComparator) {
			this.firstComparator = firstComparator;
			this.secondComparator = secondComparator;
		}
	}

	public static class DefaultLexicographicPairComparator<F extends Comparable>  
	implements Comparator<Pair {

		public int compare(Pair<F, S> o1, Pair o2) {
			int firstCompare = o1.getFirst().compareTo(o2.getFirst());
			if (firstCompare != 0) {
				return firstCompare;
			}
			return o1.getSecond().compareTo(o2.getSecond());
		}

	}


}

Other Java examples (source code examples)

Here is a short list of links related to this Java Pair.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.