com.ibm.icu.impl.IterableComparator Maven / Gradle / Ivy
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html#License
/*
************************************************************************************
* Copyright (C) 2007-2015, Google Inc, International Business Machines Corporation
* and others. All Rights Reserved.
************************************************************************************
*/
package com.ibm.icu.impl;
import java.util.Comparator;
import java.util.Iterator;
/**
* TODO: Move to com.ibm.icu.dev.somewhere.
* 2015-sep-03: Not used in ICU but used in CLDR and in UnicodeTools.
*/
public class IterableComparator implements Comparator> {
private final Comparator comparator;
private final int shorterFirst; // = 1 for shorter first, -1 otherwise
public IterableComparator() {
this(null, true);
}
public IterableComparator(Comparator comparator) {
this(comparator, true);
}
public IterableComparator(Comparator comparator, boolean shorterFirst) {
this.comparator = comparator;
this.shorterFirst = shorterFirst ? 1 : -1;
}
@Override
public int compare(Iterable a, Iterable b) {
if (a == null) {
return b == null ? 0 : -shorterFirst;
} else if (b == null) {
return shorterFirst;
}
Iterator ai = a.iterator();
Iterator bi = b.iterator();
while (true) {
if (!ai.hasNext()) {
return bi.hasNext() ? -shorterFirst : 0;
}
if (!bi.hasNext()) {
return shorterFirst;
}
T aItem = ai.next();
T bItem = bi.next();
@SuppressWarnings("unchecked")
int result = comparator != null ? comparator.compare(aItem, bItem) : ((Comparable)aItem).compareTo(bItem);
if (result != 0) {
return result;
}
}
}
@SuppressWarnings("unchecked")
public static int compareIterables(Iterable a, Iterable b) {
return NOCOMPARATOR.compare(a, b);
}
@SuppressWarnings("rawtypes")
private static final IterableComparator NOCOMPARATOR = new IterableComparator();
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy