Added ObjectUtil.compare(Comparable,Comparable) which sorts nulls before
non-nulls; added ComparableTuple which sorts first on its left element (nulls sorting before non-nulls) and then on its right. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2265 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// samskivert library - useful routines for java programs
|
||||
// Copyright (C) 2001-2007 Michael Bayne
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it
|
||||
// under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package com.samskivert.util;
|
||||
|
||||
/**
|
||||
* A pair of {@link Comparable} objects that is itself {@link Comparable}.
|
||||
*/
|
||||
public class ComparableTuple<L extends Comparable<? super L>, R extends Comparable<? super R>>
|
||||
extends Tuple<L,R>
|
||||
implements Comparable<ComparableTuple<L,R>>
|
||||
{
|
||||
/**
|
||||
* Constructs a tuple with the supplied contents.
|
||||
*/
|
||||
public ComparableTuple (L left, R right)
|
||||
{
|
||||
super(left, right);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a blank tuple.
|
||||
*/
|
||||
public ComparableTuple ()
|
||||
{
|
||||
}
|
||||
|
||||
// from interface Comparable
|
||||
public int compareTo (ComparableTuple<L, R> other)
|
||||
{
|
||||
int rv = ObjectUtil.compareTo(left, other.left);
|
||||
return (rv != 0) ? rv : ObjectUtil.compareTo(right, other.right);
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,23 @@ public class ObjectUtil
|
||||
Arrays.equals(t1.getStackTrace(), t2.getStackTrace()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two objects, returning -1 if left is null and right is not and 1 if left is
|
||||
* non-null and right is null.
|
||||
*/
|
||||
public static <T extends Comparable<? super T>> int compareTo (T left, T right)
|
||||
{
|
||||
if (left == null && right == null) {
|
||||
return 0;
|
||||
} else if (left == null) {
|
||||
return -1;
|
||||
} else if (right == null) {
|
||||
return 1;
|
||||
} else {
|
||||
return left.compareTo(right);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dumps the contents of the supplied object instance, listing the
|
||||
* class name, hash code and <code>toString()</code> data for each
|
||||
|
||||
Reference in New Issue
Block a user