org.jopendocument.util.CompareUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jOpenDocument Show documentation
Show all versions of jOpenDocument Show documentation
jOpenDocument is a free library for developers looking to use
Open Document files without OpenOffice.org.
The newest version!
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2008-2013 jOpenDocument, by ILM Informatique. All rights reserved.
*
* The contents of this file are subject to the terms of the GNU
* General Public License Version 3 only ("GPL").
* You may not use this file except in compliance with the License.
* You can obtain a copy of the License at http://www.gnu.org/licenses/gpl-3.0.html
* See the License for the specific language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each file.
*
*/
/*
* Créé le 14 avr. 2005
*/
package org.jopendocument.util;
import org.jopendocument.util.cc.ITransformer;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
/**
* @author Sylvain CUAZ
*/
public class CompareUtils {
/**
* Compare 2 nombres entier avec longValue().
*
* @param n1 le premier nombre.
* @param n2 le deuxième nombre.
* @return 0 si ==, >0 si n1>2.
*/
public static final int compareIntNumbers(Number n1, Number n2) {
return compareLong(n1.longValue(), n2.longValue());
}
static public final int compareInt(int int1, int int2) {
if (int1 < int2)
return -1;
else if (int1 == int2)
return 0;
else
return +1;
}
static public final int compareLong(long int1, long int2) {
if (int1 < int2)
return -1;
else if (int1 == int2)
return 0;
else
return +1;
}
/**
* Compare two objects if they're numbers or comparable.
*
* @param o1 first object.
* @param o2 second object.
* @return a negative integer, zero, or a positive integer as o1 is less than, equal to, or
* greater than o2.
* @throws ClassCastException if o1 is neither a {@link Number} nor a {@link Comparable}, or if
* o2's type prevents it from being compared to o1.
* @throws NullPointerException if o1 or o2 is null
.
* @see Comparable#compareTo(Object)
* @see NumberUtils#compare(Number, Number)
*/
static public final int compare(final Object o1, final Object o2) throws ClassCastException {
if (o1 == null || o2 == null)
throw new NullPointerException();
if (o1 instanceof Number && o2 instanceof Number) {
return NumberUtils.compare((Number) o1, (Number) o2);
} else {
// see Arrays.mergeSort()
@SuppressWarnings({ "rawtypes", "unchecked" })
final int res = ((Comparable) o1).compareTo(o2);
return res;
}
}
/**
* Renvoie un comparateur qui utilise successivement la liste passée tant que les objets sont
* égaux.
*
* @param comparators une liste de Comparator.
* @return le Comparator demandé.
* @param type of comparator
*/
static public final Comparator createComparator(final List> comparators) {
return new Comparator() {
public String toString() {
return "CompareUtils comparator with " + comparators;
}
public int compare(T o1, T o2) {
int result = 0;
int i = 0;
while (i < comparators.size() && result == 0) {
final Comparator transf = comparators.get(i);
result = transf.compare(o1, o2);
i++;
}
return result;
}
};
}
/**
* Compare 2 objets pouvant être null
.
*
* @param o1 the first object, can be null
.
* @param o2 the second object, can be null
.
* @return true
if both are null
or if o1.equals(o2).
* @see Object#equals(Object)
*/
static public final boolean equals(Object o1, Object o2) {
if (o1 == null && o2 == null)
return true;
if (o1 == null || o2 == null)
return false;
return o1.equals(o2);
}
/**
* Compare 2 objets pouvant être null
avec compareTo(). Useful since for some
* classes equals() is more specific than compareTo()==0, e.g. {@link BigDecimal#equals(Object)}
* doesn't compare the numeric value but instance variables (1E2 is not equal to 100 or 100.00).
*
* @param o1 the first object, can be null
.
* @param o2 the second object, can be null
.
* @return true
if both are null
or if o1.compareTo(o2) == 0.
* @see Comparable#compareTo(Object)
*/
static public final boolean equalsWithCompareTo(Comparable o1, T o2) {
if (o1 == null && o2 == null)
return true;
if (o1 == null || o2 == null)
return false;
return o1.compareTo(o2) == 0;
}
static public interface Equalizer {
public boolean equals(T o1, T o2);
}
static public final Equalizer