diff --git a/src/java/com/samskivert/util/ComparableTuple.java b/src/java/com/samskivert/util/ComparableTuple.java new file mode 100644 index 00000000..a3009dab --- /dev/null +++ b/src/java/com/samskivert/util/ComparableTuple.java @@ -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, R extends Comparable> + extends Tuple + implements Comparable> +{ + /** + * 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 other) + { + int rv = ObjectUtil.compareTo(left, other.left); + return (rv != 0) ? rv : ObjectUtil.compareTo(right, other.right); + } +} diff --git a/src/java/com/samskivert/util/ObjectUtil.java b/src/java/com/samskivert/util/ObjectUtil.java index 6df4ea43..c3390330 100644 --- a/src/java/com/samskivert/util/ObjectUtil.java +++ b/src/java/com/samskivert/util/ObjectUtil.java @@ -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 > 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 toString() data for each