下面列出了com.google.common.collect.Ordering#nullsLast ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private OrderingByPosition (int position, boolean reverse, @Nullable Boolean nullFirst) {
this.position = position;
// note, that we are reverse for the queue so this conditional is by intent
Ordering<Comparable> ordering;
nullFirst = nullFirst != null ? !nullFirst : null; // swap because queue is reverse
if (reverse) {
ordering = Ordering.natural();
if (nullFirst == null || !nullFirst) {
ordering = ordering.nullsLast();
} else {
ordering = ordering.nullsFirst();
}
} else {
ordering = Ordering.natural().reverse();
if (nullFirst == null || nullFirst) {
ordering = ordering.nullsFirst();
} else {
ordering = ordering.nullsLast();
}
}
this.ordering = ordering;
}
/**
* Builds a comparator from the list of columns in ORDER BY clause.
* @param orderByExpressions the columns in ORDER BY clause.
* @return the comparator built from the list of columns in ORDER BY clause.
*/
// ImmutableBytesWritable.Comparator doesn't implement generics
@SuppressWarnings("unchecked")
private static Comparator<ResultEntry> buildComparator(List<OrderByExpression> orderByExpressions) {
Ordering<ResultEntry> ordering = null;
int pos = 0;
for (OrderByExpression col : orderByExpressions) {
Expression e = col.getExpression();
Comparator<ImmutableBytesWritable> comparator =
e.getSortOrder() == SortOrder.DESC && !e.getDataType().isFixedWidth()
? buildDescVarLengthComparator()
: new ImmutableBytesWritable.Comparator();
Ordering<ImmutableBytesWritable> o = Ordering.from(comparator);
if(!col.isAscending()) o = o.reverse();
o = col.isNullsLast() ? o.nullsLast() : o.nullsFirst();
Ordering<ResultEntry> entryOrdering = o.onResultOf(new NthKey(pos++));
ordering = ordering == null ? entryOrdering : ordering.compound(entryOrdering);
}
return ordering;
}
private int getCompareValue(WorldTableRow row1, WorldTableRow row2, Function<WorldTableRow, Comparable> compareByFn)
{
Ordering<Comparable> ordering = Ordering.natural();
if (!ascendingOrder)
{
ordering = ordering.reverse();
}
ordering = ordering.nullsLast();
return ordering.compare(compareByFn.apply(row1), compareByFn.apply(row2));
}
private int getCompareValue(WorldTableRow row1, WorldTableRow row2, Function<WorldTableRow, Comparable> compareByFn)
{
Ordering<Comparable> ordering = Ordering.natural();
if (!ascendingOrder)
{
ordering = ordering.reverse();
}
ordering = ordering.nullsLast();
return ordering.compare(compareByFn.apply(row1), compareByFn.apply(row2));
}
/**
* Builds a comparator from the list of columns in ORDER BY clause.
* @param orderByExpressions the columns in ORDER BY clause.
* @return the comparator built from the list of columns in ORDER BY clause.
*/
// ImmutableBytesWritable.Comparator doesn't implement generics
@SuppressWarnings("unchecked")
private static Comparator<ResultEntry> buildComparator(List<OrderByExpression> orderByExpressions) {
Ordering<ResultEntry> ordering = null;
int pos = 0;
for (OrderByExpression col : orderByExpressions) {
Ordering<ImmutableBytesWritable> o = Ordering.from(new ImmutableBytesWritable.Comparator());
if(!col.isAscending()) o = o.reverse();
o = col.isNullsLast() ? o.nullsLast() : o.nullsFirst();
Ordering<ResultEntry> entryOrdering = o.onResultOf(new NthKey(pos++));
ordering = ordering == null ? entryOrdering : ordering.compound(entryOrdering);
}
return ordering;
}
private void helpTestOrderByNullColumns(boolean nullsFirst) throws Exception {
long ts = nextTimestamp();
String tenantId = getOrganizationId();
String query = "SELECT region " +
"FROM PRODUCT_METRICS " +
"WHERE organization_id=? " +
"GROUP BY region " +
"ORDER BY region nulls " + (nullsFirst ? "first" : "last");
String url = getUrl() + ";" + PhoenixRuntime.CURRENT_SCN_ATTRIB + "=" + (ts + 5); // Run query at timestamp 5
Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
Connection conn = DriverManager.getConnection(url, props);
try {
initTableValues(tenantId, getSplits(tenantId), ts);
PreparedStatement statement = conn.prepareStatement(query);
statement.setString(1, tenantId);
ResultSet rs = statement.executeQuery();
List<String> expected = Lists.newArrayList(null, R1, R2);
Ordering<String> regionOrdering = Ordering.natural();
regionOrdering = nullsFirst ? regionOrdering.nullsFirst() : regionOrdering.nullsLast();
Collections.sort(expected, regionOrdering);
for (String region : expected) {
assertTrue(rs.next());
assertEquals(region, rs.getString(1));
}
assertFalse(rs.next());
} finally {
conn.close();
}
}
private void helpTestOrderByNullColumns(boolean nullsFirst) throws Exception {
String tablename=generateUniqueName();
String tenantId = getOrganizationId();
String query = "SELECT region " +
"FROM " +tablename+
" WHERE organization_id=? " +
"GROUP BY region " +
"ORDER BY region nulls " + (nullsFirst ? "first" : "last");
Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
Connection conn = DriverManager.getConnection(getUrl(), props);
try {
initTableValues(tablename, tenantId, getSplits(tenantId));
PreparedStatement statement = conn.prepareStatement(query);
statement.setString(1, tenantId);
ResultSet rs = statement.executeQuery();
List<String> expected = Lists.newArrayList(null, R1, R2);
Ordering<String> regionOrdering = Ordering.natural();
regionOrdering = nullsFirst ? regionOrdering.nullsFirst() : regionOrdering.nullsLast();
Collections.sort(expected, regionOrdering);
for (String region : expected) {
assertTrue(rs.next());
assertEquals(region, rs.getString(1));
}
assertFalse(rs.next());
} finally {
conn.close();
}
}
/**
* Builds a comparator from the list of columns in ORDER BY clause.
* @param orderByExpressions the columns in ORDER BY clause.
* @return the comparator built from the list of columns in ORDER BY clause.
*/
// ImmutableBytesWritable.Comparator doesn't implement generics
@SuppressWarnings("unchecked")
private static Comparator<ResultEntry> buildComparator(List<OrderByExpression> orderByExpressions) {
Ordering<ResultEntry> ordering = null;
int pos = 0;
for (OrderByExpression col : orderByExpressions) {
Ordering<ImmutableBytesWritable> o = Ordering.from(new ImmutableBytesWritable.Comparator());
if(!col.isAscending()) o = o.reverse();
o = col.isNullsLast() ? o.nullsLast() : o.nullsFirst();
Ordering<ResultEntry> entryOrdering = o.onResultOf(new NthKey(pos++));
ordering = ordering == null ? entryOrdering : ordering.compound(entryOrdering);
}
return ordering;
}
private void helpTestOrderByNullColumns(boolean nullsFirst) throws Exception {
long ts = nextTimestamp();
String tenantId = getOrganizationId();
String query = "SELECT region " +
"FROM PRODUCT_METRICS " +
"WHERE organization_id=? " +
"GROUP BY region " +
"ORDER BY region nulls " + (nullsFirst ? "first" : "last");
String url = PHOENIX_JDBC_URL + ";" + PhoenixRuntime.CURRENT_SCN_ATTRIB + "=" + (ts + 5); // Run query at timestamp 5
Properties props = new Properties(TEST_PROPERTIES);
Connection conn = DriverManager.getConnection(url, props);
try {
initTableValues(tenantId, getSplits(tenantId), ts);
PreparedStatement statement = conn.prepareStatement(query);
statement.setString(1, tenantId);
ResultSet rs = statement.executeQuery();
List<String> expected = Lists.newArrayList(null, R1, R2);
Ordering<String> regionOrdering = Ordering.natural();
regionOrdering = nullsFirst ? regionOrdering.nullsFirst() : regionOrdering.nullsLast();
Collections.sort(expected, regionOrdering);
for (String region : expected) {
assertTrue(rs.next());
assertEquals(region, rs.getString(1));
}
assertFalse(rs.next());
} finally {
conn.close();
}
}
public static <T, K extends Comparable> Matcher<Iterable<? extends T>> isSortedBy(final Function<T, K> extractSortingKeyFunction,
final boolean descending,
@Nullable final Boolean nullsFirst) {
Ordering<K> ordering = Ordering.natural();
if (descending) {
ordering = ordering.reverse();
}
if (nullsFirst != null && nullsFirst) {
ordering = ordering.nullsFirst();
} else {
ordering = ordering.nullsLast();
}
final Ordering<K> ord = ordering;
return new TypeSafeDiagnosingMatcher<Iterable<? extends T>>() {
@Override
protected boolean matchesSafely(Iterable<? extends T> item, Description mismatchDescription) {
K previous = null;
int i = 0;
for (T elem : item) {
K current = extractSortingKeyFunction.apply(elem);
if (previous != null) {
if (ord.compare(previous, current) > 0) {
mismatchDescription
.appendText("element ").appendValue(current)
.appendText(" at position ").appendValue(i)
.appendText(" is ")
.appendText(descending ? "bigger" : "smaller")
.appendText(" than previous element ")
.appendValue(previous);
return false;
}
}
i++;
previous = current;
}
return true;
}
@Override
public void describeTo(Description description) {
description.appendText("expected iterable to be sorted ");
if (descending) {
description.appendText("in DESCENDING order");
} else {
description.appendText("in ASCENDING order");
}
}
};
}